# HG changeset patch # User Jaroslav Tulach # Date 1234629006 -3600 # Node ID 06bf3a32eaa0e0c4ec59e3425d981f278c28d0ac # Parent 9ad0cc253aa9f0bdee8729e6189c95172166ad0d Moving code to org.apidesign.math package diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api1.0/api/Arithmetica.java --- a/samples/composition/src-api1.0/api/Arithmetica.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -package api; - -/** Class to simplify arithmetical operations. - * - * @author Jaroslav Tulach - * @version 1.0 - */ -// BEGIN: design.composition.arith1.0 -public class Arithmetica { - public int sumTwo(int one, int second) { - return one + second; - } - - public int sumAll(int... numbers) { - if (numbers.length == 0) { - return 0; - } - int sum = numbers[0]; - for (int i = 1; i < numbers.length; i++) { - sum = sumTwo(sum, numbers[i]); - } - return sum; - } - - public int sumRange(int from, int to) { - int len = to - from; - if (len < 0) { - len = -len; - from = to; - } - int[] array = new int[len + 1]; - for (int i = 0; i <= len; i++) { - array[i] = from + i; - } - return sumAll(array); - } -} -// END: design.composition.arith1.0 diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,38 @@ +package org.apidesign.math; + +/** Class to simplify arithmetical operations. + * + * @author Jaroslav Tulach + * @version 1.0 + */ +// BEGIN: design.composition.arith1.0 +public class Arithmetica { + public int sumTwo(int one, int second) { + return one + second; + } + + public int sumAll(int... numbers) { + if (numbers.length == 0) { + return 0; + } + int sum = numbers[0]; + for (int i = 1; i < numbers.length; i++) { + sum = sumTwo(sum, numbers[i]); + } + return sum; + } + + public int sumRange(int from, int to) { + int len = to - from; + if (len < 0) { + len = -len; + from = to; + } + int[] array = new int[len + 1]; + for (int i = 0; i <= len; i++) { + array[i] = from + i; + } + return sumAll(array); + } +} +// END: design.composition.arith1.0 diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-compat/api/Arithmetica.java --- a/samples/composition/src-api2.0-compat/api/Arithmetica.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -package api; - -/** Class to simplify arithmetical operations, improved version to compute - * the sum for ranges, but only if one uses the new constructor to indicate - * need for new version. - * - * @author Jaroslav Tulach - * @version 2.0 - */ -// BEGIN: design.composition.arith2.0.compat -public class Arithmetica { - private final int version; - - public Arithmetica() { - this(1); - } - public Arithmetica(int version) { - this.version = version; - } - - public int sumTwo(int one, int second) { - return one + second; - } - - public int sumAll(int... numbers) { - if (numbers.length == 0) { - return 0; - } - int sum = numbers[0]; - for (int i = 1; i < numbers.length; i++) { - sum = sumTwo(sum, numbers[i]); - } - return sum; - } - - public int sumRange(int from, int to) { - switch (version) { - case 1: return sumRange1(from, to); - case 2: return sumRange2(from, to); - default: throw new IllegalStateException(); - } - } - - private int sumRange1(int from, int to) { - int len = to - from; - if (len < 0) { - len = -len; - from = to; - } - int[] array = new int[len + 1]; - for (int i = 0; i <= len; i++) { - array[i] = from + i; - } - return sumAll(array); - } - - private int sumRange2(int from, int to) { - return (from + to) * (Math.abs(to - from) + 1) / 2; - } -} -// END: design.composition.arith2.0.compat diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-compat/org/apidesign/math/Arithmetica.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-api2.0-compat/org/apidesign/math/Arithmetica.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,61 @@ +package org.apidesign.math; + +/** Class to simplify arithmetical operations, improved version to compute + * the sum for ranges, but only if one uses the new constructor to indicate + * need for new version. + * + * @author Jaroslav Tulach + * @version 2.0 + */ +// BEGIN: design.composition.arith2.0.compat +public class Arithmetica { + private final int version; + + public Arithmetica() { + this(1); + } + public Arithmetica(int version) { + this.version = version; + } + + public int sumTwo(int one, int second) { + return one + second; + } + + public int sumAll(int... numbers) { + if (numbers.length == 0) { + return 0; + } + int sum = numbers[0]; + for (int i = 1; i < numbers.length; i++) { + sum = sumTwo(sum, numbers[i]); + } + return sum; + } + + public int sumRange(int from, int to) { + switch (version) { + case 1: return sumRange1(from, to); + case 2: return sumRange2(from, to); + default: throw new IllegalStateException(); + } + } + + private int sumRange1(int from, int to) { + int len = to - from; + if (len < 0) { + len = -len; + from = to; + } + int[] array = new int[len + 1]; + for (int i = 0; i <= len; i++) { + array[i] = from + i; + } + return sumAll(array); + } + + private int sumRange2(int from, int to) { + return (from + to) * (Math.abs(to - from) + 1) / 2; + } +} +// END: design.composition.arith2.0.compat diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-enum/api/Arithmetica.java --- a/samples/composition/src-api2.0-enum/api/Arithmetica.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -package api; - -/** Class to simplify arithmetical operations, improved version to compute - * the sum for ranges, but only if one uses the new constructor to indicate - * need for new version. - * - * @author Jaroslav Tulach - * @version 2.0 - */ -// BEGIN: design.composition.arith2.0.enum -public class Arithmetica { - private final Version version; - public enum Version { VERSION_1_0, VERSION_2_0 } - - public Arithmetica() { - this(Version.VERSION_1_0); - } - public Arithmetica(Version version) { - this.version = version; - } - - public int sumRange(int from, int to) { - switch (version) { - case VERSION_1_0: - return sumRange1(from, to); - case VERSION_2_0: - return sumRange2(from, to); - default: - throw new IllegalStateException(); - } - } -// FINISH: design.composition.arith2.0.enum - - public int sumTwo(int one, int second) { - return one + second; - } - - public int sumAll(int... numbers) { - if (numbers.length == 0) { - return 0; - } - int sum = numbers[0]; - for (int i = 1; i < numbers.length; i++) { - sum = sumTwo(sum, numbers[i]); - } - return sum; - } - - - private int sumRange1(int from, int to) { - int len = to - from; - if (len < 0) { - len = -len; - from = to; - } - int[] array = new int[len + 1]; - for (int i = 0; i <= len; i++) { - array[i] = from + i; - } - return sumAll(array); - } - - private int sumRange2(int from, int to) { - return (from + to) * (Math.abs(to - from) + 1) / 2; - } -} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-enum/org/apidesign/math/Arithmetica.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-api2.0-enum/org/apidesign/math/Arithmetica.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,66 @@ +package org.apidesign.math; + +/** Class to simplify arithmetical operations, improved version to compute + * the sum for ranges, but only if one uses the new constructor to indicate + * need for new version. + * + * @author Jaroslav Tulach + * @version 2.0 + */ +// BEGIN: design.composition.arith2.0.enum +public class Arithmetica { + private final Version version; + public enum Version { VERSION_1_0, VERSION_2_0 } + + public Arithmetica() { + this(Version.VERSION_1_0); + } + public Arithmetica(Version version) { + this.version = version; + } + + public int sumRange(int from, int to) { + switch (version) { + case VERSION_1_0: + return sumRange1(from, to); + case VERSION_2_0: + return sumRange2(from, to); + default: + throw new IllegalStateException(); + } + } +// FINISH: design.composition.arith2.0.enum + + public int sumTwo(int one, int second) { + return one + second; + } + + public int sumAll(int... numbers) { + if (numbers.length == 0) { + return 0; + } + int sum = numbers[0]; + for (int i = 1; i < numbers.length; i++) { + sum = sumTwo(sum, numbers[i]); + } + return sum; + } + + + private int sumRange1(int from, int to) { + int len = to - from; + if (len < 0) { + len = -len; + from = to; + } + int[] array = new int[len + 1]; + for (int i = 0; i <= len; i++) { + array[i] = from + i; + } + return sumAll(array); + } + + private int sumRange2(int from, int to) { + return (from + to) * (Math.abs(to - from) + 1) / 2; + } +} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-property/api/Arithmetica.java --- a/samples/composition/src-api2.0-property/api/Arithmetica.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -package api; - -/** Class to simplify arithmetical operations, improved version to compute - * the sum for ranges, but only if the virtual machine is configured to - * run in incompatible mode. - * - * @author Jaroslav Tulach - * @version 2.0 - */ -// BEGIN: design.composition.arith2.0.property -public class Arithmetica { - public int sumTwo(int one, int second) { - return one + second; - } - - public int sumAll(int... numbers) { - if (numbers.length == 0) { - return 0; - } - int sum = numbers[0]; - for (int i = 1; i < numbers.length; i++) { - sum = sumTwo(sum, numbers[i]); - } - return sum; - } - - public int sumRange(int from, int to) { - // BEGIN: design.composition.arith2.0.property.if - if (Boolean.getBoolean("arithmetica.v2")) { - return sumRange2(from, to); - } else { - return sumRange1(from, to); - } - // END: design.composition.arith2.0.property.if - } - - private int sumRange1(int from, int to) { - int len = to - from; - if (len < 0) { - len = -len; - from = to; - } - int[] array = new int[len + 1]; - for (int i = 0; i <= len; i++) { - array[i] = from + i; - } - return sumAll(array); - } - - private int sumRange2(int from, int to) { - return (from + to) * (Math.abs(to - from) + 1) / 2; - } -} -// END: design.composition.arith2.0.property diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-property/org/apidesign/math/Arithmetica.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-api2.0-property/org/apidesign/math/Arithmetica.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,54 @@ +package org.apidesign.math; + +/** Class to simplify arithmetical operations, improved version to compute + * the sum for ranges, but only if the virtual machine is configured to + * run in incompatible mode. + * + * @author Jaroslav Tulach + * @version 2.0 + */ +// BEGIN: design.composition.arith2.0.property +public class Arithmetica { + public int sumTwo(int one, int second) { + return one + second; + } + + public int sumAll(int... numbers) { + if (numbers.length == 0) { + return 0; + } + int sum = numbers[0]; + for (int i = 1; i < numbers.length; i++) { + sum = sumTwo(sum, numbers[i]); + } + return sum; + } + + public int sumRange(int from, int to) { + // BEGIN: design.composition.arith2.0.property.if + if (Boolean.getBoolean("arithmetica.v2")) { + return sumRange2(from, to); + } else { + return sumRange1(from, to); + } + // END: design.composition.arith2.0.property.if + } + + private int sumRange1(int from, int to) { + int len = to - from; + if (len < 0) { + len = -len; + from = to; + } + int[] array = new int[len + 1]; + for (int i = 0; i <= len; i++) { + array[i] = from + i; + } + return sumAll(array); + } + + private int sumRange2(int from, int to) { + return (from + to) * (Math.abs(to - from) + 1) / 2; + } +} +// END: design.composition.arith2.0.property diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-runtime/api/Arithmetica.java --- a/samples/composition/src-api2.0-runtime/api/Arithmetica.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ -package api; - -import org.apidesign.runtime.check.RuntimeCheck; - -/** Class to simplify arithmetical operations, improved version to compute - * the sum for ranges, but only if the virtual machine is configured to - * run in incompatible mode. - * - * @author Jaroslav Tulach - * @version 2.0 - */ -public class Arithmetica { - public int sumTwo(int one, int second) { - return one + second; - } - - public int sumAll(int... numbers) { - if (numbers.length == 0) { - return 0; - } - int sum = numbers[0]; - for (int i = 1; i < numbers.length; i++) { - sum = sumTwo(sum, numbers[i]); - } - return sum; - } - - public int sumRange(int from, int to) { - if (calledByV2AwareLoader()) { - return sumRange2(from, to); - } else { - return sumRange1(from, to); - } - } - - private int sumRange1(int from, int to) { - int len = to - from; - if (len < 0) { - len = -len; - from = to; - } - int[] array = new int[len + 1]; - for (int i = 0; i <= len; i++) { - array[i] = from + i; - } - return sumAll(array); - } - - private int sumRange2(int from, int to) { - return (from + to) * (Math.abs(to - from) + 1) / 2; - } - - private static boolean calledByV2AwareLoader() { - // BEGIN: design.composition.arith2.6.runtime - StackTraceElement[] arr = Thread.currentThread().getStackTrace(); - ClassLoader myLoader = Arithmetica.class.getClassLoader(); - for (int i = 0; i < arr.length; i++) { - ClassLoader caller = arr[i].getClass().getClassLoader(); - if (myLoader == caller) { - continue; - } - if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) { - return true; - } - return false; - } - return true; - // END: design.composition.arith2.6.runtime - } - -} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-runtime/org/apidesign/math/Arithmetica.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-api2.0-runtime/org/apidesign/math/Arithmetica.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,72 @@ +package org.apidesign.math; + +import org.apidesign.math.Arithmetica; +import org.apidesign.runtime.check.RuntimeCheck; + +/** Class to simplify arithmetical operations, improved version to compute + * the sum for ranges, but only if the virtual machine is configured to + * run in incompatible mode. + * + * @author Jaroslav Tulach + * @version 2.0 + */ +public class Arithmetica { + public int sumTwo(int one, int second) { + return one + second; + } + + public int sumAll(int... numbers) { + if (numbers.length == 0) { + return 0; + } + int sum = numbers[0]; + for (int i = 1; i < numbers.length; i++) { + sum = sumTwo(sum, numbers[i]); + } + return sum; + } + + public int sumRange(int from, int to) { + if (calledByV2AwareLoader()) { + return sumRange2(from, to); + } else { + return sumRange1(from, to); + } + } + + private int sumRange1(int from, int to) { + int len = to - from; + if (len < 0) { + len = -len; + from = to; + } + int[] array = new int[len + 1]; + for (int i = 0; i <= len; i++) { + array[i] = from + i; + } + return sumAll(array); + } + + private int sumRange2(int from, int to) { + return (from + to) * (Math.abs(to - from) + 1) / 2; + } + + private static boolean calledByV2AwareLoader() { + // BEGIN: design.composition.arith2.6.runtime + StackTraceElement[] arr = Thread.currentThread().getStackTrace(); + ClassLoader myLoader = Arithmetica.class.getClassLoader(); + for (int i = 0; i < arr.length; i++) { + ClassLoader caller = arr[i].getClass().getClassLoader(); + if (myLoader == caller) { + continue; + } + if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) { + return true; + } + return false; + } + return true; + // END: design.composition.arith2.6.runtime + } + +} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/RuntimeCheck.java --- a/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/RuntimeCheck.java Thu Feb 12 11:00:41 2009 +0100 +++ b/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/RuntimeCheck.java Sat Feb 14 17:30:06 2009 +0100 @@ -1,7 +1,6 @@ package org.apidesign.runtime.check; import java.util.Map; -import java.util.Set; public final class RuntimeCheck { private RuntimeCheck() { diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/VersionAwareClassLoader.java --- a/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/VersionAwareClassLoader.java Thu Feb 12 11:00:41 2009 +0100 +++ b/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/VersionAwareClassLoader.java Sat Feb 14 17:30:06 2009 +0100 @@ -3,9 +3,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.Map; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; final class VersionAwareClassLoader extends ClassLoader implements RuntimeCheck.AwareLoader { diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0/api/Arithmetica.java --- a/samples/composition/src-api2.0/api/Arithmetica.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -package api; - -/** Class to simplify arithmetical operations, improved version to compute - * the sum for ranges. - * - * @author Jaroslav Tulach - * @version 2.0 - */ -public class Arithmetica { - public int sumTwo(int one, int second) { - return one + second; - } - - public int sumAll(int... numbers) { - if (numbers.length == 0) { - return 0; - } - int sum = numbers[0]; - for (int i = 1; i < numbers.length; i++) { - sum = sumTwo(sum, numbers[i]); - } - return sum; - } - -// BEGIN: design.composition.arith2.0 - public int sumRange(int from, int to) { - return (from + to) * (Math.abs(to - from) + 1) / 2; - } -// END: design.composition.arith2.0 -} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,30 @@ +package org.apidesign.math; + +/** Class to simplify arithmetical operations, improved version to compute + * the sum for ranges. + * + * @author Jaroslav Tulach + * @version 2.0 + */ +public class Arithmetica { + public int sumTwo(int one, int second) { + return one + second; + } + + public int sumAll(int... numbers) { + if (numbers.length == 0) { + return 0; + } + int sum = numbers[0]; + for (int i = 1; i < numbers.length; i++) { + sum = sumTwo(sum, numbers[i]); + } + return sum; + } + +// BEGIN: design.composition.arith2.0 + public int sumRange(int from, int to) { + return (from + to) * (Math.abs(to - from) + 1) / 2; + } +// END: design.composition.arith2.0 +} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/api/ArithmeticaCompatibilityTest.java --- a/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,206 +0,0 @@ -package api; - -import junit.framework.*; - -public class ArithmeticaCompatibilityTest extends TestCase { - public ArithmeticaCompatibilityTest(String name) { - super(name); - } - - private static final class CountingSubclass extends Arithmetica { - int countSumTwo; - int countSumAll; - int countSumRange; - - @Override - public int sumAll(int... numbers) { - countSumAll++; - return super.sumAll(numbers); - } - - @Override - public int sumRange(int from, int to) { - countSumRange++; - return super.sumRange(from, to); - } - - @Override - public int sumTwo(int one, int second) { - countSumTwo++; - return super.sumTwo(one, second); - } - } // end of CountingSubclass - - private static final class CountingOldSubclass extends OldArithmetica1 { - int countSumTwo; - int countSumAll; - int countSumRange; - - @Override - public int sumAll(int... numbers) { - countSumAll++; - return super.sumAll(numbers); - } - - @Override - public int sumRange(int from, int to) { - countSumRange++; - return super.sumRange(from, to); - } - - @Override - public int sumTwo(int one, int second) { - countSumTwo++; - return super.sumTwo(one, second); - } - } // end of CountingSubclass - - // BEGIN: total.rewrite.tests - public void testRandomCheck () throws Exception { - if (Boolean.getBoolean("no.failures")) return; - long seed = System.currentTimeMillis(); - try { - CountingSubclass now = new CountingSubclass(); - CountingOldSubclass old = new CountingOldSubclass(); - - compare(now, old, seed); - - assertEquals( - "Verify amount calls to of sumRange is the same", - now.countSumRange, old.countSumRange - ); - assertEquals( - "Verify amount calls to of sumAll is the same", - now.countSumAll, old.countSumAll - ); - assertEquals( - "Verify amount calls to of sumTwo is the same", - now.countSumTwo, old.countSumTwo - ); - } catch (AssertionFailedError ex) { - IllegalStateException n = new IllegalStateException ( - "Seed: " + seed + "\n" + ex.getMessage () - ); - n.initCause(ex); - throw n; - } catch (Exception ex) { - IllegalStateException n = new IllegalStateException ( - "Seed: " + seed + "\n" + ex.getMessage () - ); - n.initCause(ex); - throw n; - } - } - - public void testSimulateOKRunOn1208120436947() throws Exception { - CountingSubclass now = new CountingSubclass(); - CountingOldSubclass old = new CountingOldSubclass(); - - compare(now, old, 1208120436947L); - - assertEquals( - "Verify amount of calls to sumRange is the same", - now.countSumRange, old.countSumRange - ); - assertEquals( - "Verify amount of calls to sumAll is the same", - now.countSumAll, old.countSumAll - ); - assertEquals( - "Verify amount of calls to sumTwo is the same", - now.countSumTwo, old.countSumTwo - ); - } - - public void testSimulateFailureOn1208120628821() throws Exception { - if (Boolean.getBoolean("no.failures")) return; - CountingSubclass now = new CountingSubclass(); - CountingOldSubclass old = new CountingOldSubclass(); - - compare(now, old, 1208120628821L); - - assertEquals( - "Verify amount of calls to sumRange is the same", - now.countSumRange, old.countSumRange - ); - assertEquals( - "Verify amount of calls to sumAll is the same", - now.countSumAll, old.countSumAll - ); - assertEquals( - "Verify amount of calls to sumTwo is the same", - now.countSumTwo, old.countSumTwo - ); - } - // END: total.rewrite.tests - - // BEGIN: total.rewrite.compare - private void compare (Arithmetica now, OldArithmetica1 old, long seed) - throws Exception { - java.util.Random r = new java.util.Random (seed); - - for (int loop = 0; loop < r.nextInt(5); loop++) { - int operation = r.nextInt(3); - switch (operation) { - case 0: { // sumTwo - int a1 = r.nextInt(100); - int a2 = r.nextInt(100); - int resNow = now.sumTwo(a1, a2); - int resOld = old.sumTwo(a1, a2); - assertEquals("sumTwo results are equal", resNow, resOld); - break; - } - case 1: { // sumArray - int[] arr = new int[r.nextInt(100)]; - for (int i = 0; i < arr.length; i++) { - arr[i] = r.nextInt(100); - } - int resNow = now.sumAll(arr); - int resOld = old.sumAll(arr); - assertEquals("sumArray results are equal", resNow, resOld); - break; - } - case 2: { // sumRange - int a1 = r.nextInt(100); - int a2 = r.nextInt(100); - int resNow = now.sumRange(a1, a1 + a2); - int resOld = old.sumRange(a1, a1 + a2); - assertEquals("sumRange results are equal", resNow, resOld); - break; - } - } - } - } - // END: total.rewrite.compare - - - // BEGIN: total.rewrite.oldimpl - /** This is a copy of the implementation of Arithmetica from version 1.0 */ - static class OldArithmetica1 { - public int sumTwo(int one, int second) { - return one + second; - } - - public int sumAll(int... numbers) { - if (numbers.length == 0) { - return 0; - } - int sum = numbers[0]; - for (int i = 1; i < numbers.length; i++) { - sum = sumTwo(sum, numbers[i]); - } - return sum; - } - - public int sumRange(int from, int to) { - int len = to - from; - int[] array = new int[len + 1]; - for (int i = 0; i <= len; i++) { - array[i] = from + i; - } - return sumAll(array); - } - } - // END: total.rewrite.oldimpl - -} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/api/ArithmeticaTest.java --- a/samples/composition/src-test/api/ArithmeticaTest.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -package api; - -import junit.framework.TestCase; - -/** - * - * @author Jaroslav Tulach - */ -public class ArithmeticaTest extends TestCase { - - public ArithmeticaTest(String testName) { - super(testName); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - - // BEGIN: design.composition.arith.test - public void testSumTwo() { - Arithmetica instance = new Arithmetica(); - assertEquals("+", 5, instance.sumTwo(3, 2)); - } - - public void testSumAll() { - Arithmetica instance = new Arithmetica(); - assertEquals("+", 6, instance.sumAll(3, 2, 1)); - } - - public void testSumRange() { - Arithmetica instance = new Arithmetica(); - assertEquals("1+2+3=6", 6, instance.sumRange(1, 3)); - assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10)); - assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1)); - assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1)); - } - // END: design.composition.arith.test - -} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/api/Factorial.java --- a/samples/composition/src-test/api/Factorial.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -package api; - -/** Class showing inventive, non-expected use of - * Arithmetica methods to do multiplication instead of - * addition. - */ -// BEGIN: design.composition.arith.factorial -public final class Factorial extends Arithmetica { - public static int factorial(int n) { - return new Factorial().sumRange(1, n); - } - @Override - public int sumTwo(int one, int second) { - return one * second; - } -} -// END: design.composition.arith.factorial - diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/api/FactorialTest.java --- a/samples/composition/src-test/api/FactorialTest.java Thu Feb 12 11:00:41 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/* - * Žluťoučký kůň je naše hříbátko. - * and open the template in the editor. - */ - -package api; - -import junit.framework.TestCase; - -/** - * - * @author Jaroslav Tulach - */ -public class FactorialTest extends TestCase { - - public FactorialTest(String testName) { - super(testName); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - - public void testFactorial3() { - if (Boolean.getBoolean("no.failures")) return; - assertEquals(6, Factorial.factorial(3)); - } - - public void testFactorial4() { - if (Boolean.getBoolean("no.failures")) return; - assertEquals(24, Factorial.factorial(4)); - } - - public void testFactorial5() { - if (Boolean.getBoolean("no.failures")) return; - assertEquals(120, Factorial.factorial(5)); - } -} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/org/apidesign/math/test/ArithmeticaCompatibilityTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-test/org/apidesign/math/test/ArithmeticaCompatibilityTest.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,208 @@ +package org.apidesign.math.test; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; +import org.apidesign.math.Arithmetica; + +public class ArithmeticaCompatibilityTest extends TestCase { + public ArithmeticaCompatibilityTest(String name) { + super(name); + } + + private static final class CountingSubclass extends Arithmetica { + int countSumTwo; + int countSumAll; + int countSumRange; + + @Override + public int sumAll(int... numbers) { + countSumAll++; + return super.sumAll(numbers); + } + + @Override + public int sumRange(int from, int to) { + countSumRange++; + return super.sumRange(from, to); + } + + @Override + public int sumTwo(int one, int second) { + countSumTwo++; + return super.sumTwo(one, second); + } + } // end of CountingSubclass + + private static final class CountingOldSubclass extends OldArithmetica1 { + int countSumTwo; + int countSumAll; + int countSumRange; + + @Override + public int sumAll(int... numbers) { + countSumAll++; + return super.sumAll(numbers); + } + + @Override + public int sumRange(int from, int to) { + countSumRange++; + return super.sumRange(from, to); + } + + @Override + public int sumTwo(int one, int second) { + countSumTwo++; + return super.sumTwo(one, second); + } + } // end of CountingSubclass + + // BEGIN: total.rewrite.tests + public void testRandomCheck () throws Exception { + if (Boolean.getBoolean("no.failures")) return; + long seed = System.currentTimeMillis(); + try { + CountingSubclass now = new CountingSubclass(); + CountingOldSubclass old = new CountingOldSubclass(); + + compare(now, old, seed); + + assertEquals( + "Verify amount calls to of sumRange is the same", + now.countSumRange, old.countSumRange + ); + assertEquals( + "Verify amount calls to of sumAll is the same", + now.countSumAll, old.countSumAll + ); + assertEquals( + "Verify amount calls to of sumTwo is the same", + now.countSumTwo, old.countSumTwo + ); + } catch (AssertionFailedError ex) { + IllegalStateException n = new IllegalStateException ( + "Seed: " + seed + "\n" + ex.getMessage () + ); + n.initCause(ex); + throw n; + } catch (Exception ex) { + IllegalStateException n = new IllegalStateException ( + "Seed: " + seed + "\n" + ex.getMessage () + ); + n.initCause(ex); + throw n; + } + } + + public void testSimulateOKRunOn1208120436947() throws Exception { + CountingSubclass now = new CountingSubclass(); + CountingOldSubclass old = new CountingOldSubclass(); + + compare(now, old, 1208120436947L); + + assertEquals( + "Verify amount of calls to sumRange is the same", + now.countSumRange, old.countSumRange + ); + assertEquals( + "Verify amount of calls to sumAll is the same", + now.countSumAll, old.countSumAll + ); + assertEquals( + "Verify amount of calls to sumTwo is the same", + now.countSumTwo, old.countSumTwo + ); + } + + public void testSimulateFailureOn1208120628821() throws Exception { + if (Boolean.getBoolean("no.failures")) return; + CountingSubclass now = new CountingSubclass(); + CountingOldSubclass old = new CountingOldSubclass(); + + compare(now, old, 1208120628821L); + + assertEquals( + "Verify amount of calls to sumRange is the same", + now.countSumRange, old.countSumRange + ); + assertEquals( + "Verify amount of calls to sumAll is the same", + now.countSumAll, old.countSumAll + ); + assertEquals( + "Verify amount of calls to sumTwo is the same", + now.countSumTwo, old.countSumTwo + ); + } + // END: total.rewrite.tests + + // BEGIN: total.rewrite.compare + private void compare (Arithmetica now, OldArithmetica1 old, long seed) + throws Exception { + java.util.Random r = new java.util.Random (seed); + + for (int loop = 0; loop < r.nextInt(5); loop++) { + int operation = r.nextInt(3); + switch (operation) { + case 0: { // sumTwo + int a1 = r.nextInt(100); + int a2 = r.nextInt(100); + int resNow = now.sumTwo(a1, a2); + int resOld = old.sumTwo(a1, a2); + assertEquals("sumTwo results are equal", resNow, resOld); + break; + } + case 1: { // sumArray + int[] arr = new int[r.nextInt(100)]; + for (int i = 0; i < arr.length; i++) { + arr[i] = r.nextInt(100); + } + int resNow = now.sumAll(arr); + int resOld = old.sumAll(arr); + assertEquals("sumArray results are equal", resNow, resOld); + break; + } + case 2: { // sumRange + int a1 = r.nextInt(100); + int a2 = r.nextInt(100); + int resNow = now.sumRange(a1, a1 + a2); + int resOld = old.sumRange(a1, a1 + a2); + assertEquals("sumRange results are equal", resNow, resOld); + break; + } + } + } + } + // END: total.rewrite.compare + + + // BEGIN: total.rewrite.oldimpl + /** This is a copy of the implementation of Arithmetica from version 1.0 */ + static class OldArithmetica1 { + public int sumTwo(int one, int second) { + return one + second; + } + + public int sumAll(int... numbers) { + if (numbers.length == 0) { + return 0; + } + int sum = numbers[0]; + for (int i = 1; i < numbers.length; i++) { + sum = sumTwo(sum, numbers[i]); + } + return sum; + } + + public int sumRange(int from, int to) { + int len = to - from; + int[] array = new int[len + 1]; + for (int i = 0; i <= len; i++) { + array[i] = from + i; + } + return sumAll(array); + } + } + // END: total.rewrite.oldimpl + +} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/org/apidesign/math/test/ArithmeticaTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-test/org/apidesign/math/test/ArithmeticaTest.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,46 @@ +package org.apidesign.math.test; + +import org.apidesign.math.Arithmetica; +import junit.framework.TestCase; + +/** + * + * @author Jaroslav Tulach + */ +public class ArithmeticaTest extends TestCase { + + public ArithmeticaTest(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + // BEGIN: design.composition.arith.test + public void testSumTwo() { + Arithmetica instance = new Arithmetica(); + assertEquals("+", 5, instance.sumTwo(3, 2)); + } + + public void testSumAll() { + Arithmetica instance = new Arithmetica(); + assertEquals("+", 6, instance.sumAll(3, 2, 1)); + } + + public void testSumRange() { + Arithmetica instance = new Arithmetica(); + assertEquals("1+2+3=6", 6, instance.sumRange(1, 3)); + assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10)); + assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1)); + assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1)); + } + // END: design.composition.arith.test + +} diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/org/apidesign/math/test/Factorial.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-test/org/apidesign/math/test/Factorial.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,20 @@ +package org.apidesign.math.test; + +import org.apidesign.math.Arithmetica; + +/** Class showing inventive, non-expected use of + * Arithmetica methods to do multiplication instead of + * addition. + */ +// BEGIN: design.composition.arith.factorial +public final class Factorial extends Arithmetica { + public static int factorial(int n) { + return new Factorial().sumRange(1, n); + } + @Override + public int sumTwo(int one, int second) { + return one * second; + } +} +// END: design.composition.arith.factorial + diff -r 9ad0cc253aa9 -r 06bf3a32eaa0 samples/composition/src-test/org/apidesign/math/test/FactorialTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/composition/src-test/org/apidesign/math/test/FactorialTest.java Sat Feb 14 17:30:06 2009 +0100 @@ -0,0 +1,44 @@ +/* + * Žluťoučký kůň je naše hříbátko. + * and open the template in the editor. + */ + +package org.apidesign.math.test; + +import junit.framework.TestCase; + +/** + * + * @author Jaroslav Tulach + */ +public class FactorialTest extends TestCase { + + public FactorialTest(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testFactorial3() { + if (Boolean.getBoolean("no.failures")) return; + assertEquals(6, Factorial.factorial(3)); + } + + public void testFactorial4() { + if (Boolean.getBoolean("no.failures")) return; + assertEquals(24, Factorial.factorial(4)); + } + + public void testFactorial5() { + if (Boolean.getBoolean("no.failures")) return; + assertEquals(120, Factorial.factorial(5)); + } +}