Enhancing the example to contain randomized that to verify consistency with previous implementation
1.1 --- a/samples/composition/build.xml Sat Jun 14 09:59:32 2008 +0200
1.2 +++ b/samples/composition/build.xml Sat Jun 14 10:01:18 2008 +0200
1.3 @@ -95,6 +95,7 @@
1.4 destdir="build/${version}/classes"
1.5 source="1.5" target="1.5"
1.6 classpath="${cp}"
1.7 + debug="true"
1.8 />
1.9 </target>
1.10 </project>
2.1 --- a/samples/composition/src-api1.0/api/Arithmetica.java Sat Jun 14 09:59:32 2008 +0200
2.2 +++ b/samples/composition/src-api1.0/api/Arithmetica.java Sat Jun 14 10:01:18 2008 +0200
2.3 @@ -12,6 +12,9 @@
2.4 }
2.5
2.6 public int sumAll(int... numbers) {
2.7 + if (numbers.length == 0) {
2.8 + return 0;
2.9 + }
2.10 int sum = numbers[0];
2.11 for (int i = 1; i < numbers.length; i++) {
2.12 sum = sumTwo(sum, numbers[i]);
3.1 --- a/samples/composition/src-api2.0-compat/api/Arithmetica.java Sat Jun 14 09:59:32 2008 +0200
3.2 +++ b/samples/composition/src-api2.0-compat/api/Arithmetica.java Sat Jun 14 10:01:18 2008 +0200
3.3 @@ -23,6 +23,9 @@
3.4 }
3.5
3.6 public int sumAll(int... numbers) {
3.7 + if (numbers.length == 0) {
3.8 + return 0;
3.9 + }
3.10 int sum = numbers[0];
3.11 for (int i = 1; i < numbers.length; i++) {
3.12 sum = sumTwo(sum, numbers[i]);
4.1 --- a/samples/composition/src-api2.0-enum/api/Arithmetica.java Sat Jun 14 09:59:32 2008 +0200
4.2 +++ b/samples/composition/src-api2.0-enum/api/Arithmetica.java Sat Jun 14 10:01:18 2008 +0200
4.3 @@ -36,6 +36,9 @@
4.4 }
4.5
4.6 public int sumAll(int... numbers) {
4.7 + if (numbers.length == 0) {
4.8 + return 0;
4.9 + }
4.10 int sum = numbers[0];
4.11 for (int i = 1; i < numbers.length; i++) {
4.12 sum = sumTwo(sum, numbers[i]);
5.1 --- a/samples/composition/src-api2.0-property/api/Arithmetica.java Sat Jun 14 09:59:32 2008 +0200
5.2 +++ b/samples/composition/src-api2.0-property/api/Arithmetica.java Sat Jun 14 10:01:18 2008 +0200
5.3 @@ -14,6 +14,9 @@
5.4 }
5.5
5.6 public int sumAll(int... numbers) {
5.7 + if (numbers.length == 0) {
5.8 + return 0;
5.9 + }
5.10 int sum = numbers[0];
5.11 for (int i = 1; i < numbers.length; i++) {
5.12 sum = sumTwo(sum, numbers[i]);
6.1 --- a/samples/composition/src-api2.0-runtime/api/Arithmetica.java Sat Jun 14 09:59:32 2008 +0200
6.2 +++ b/samples/composition/src-api2.0-runtime/api/Arithmetica.java Sat Jun 14 10:01:18 2008 +0200
6.3 @@ -15,6 +15,9 @@
6.4 }
6.5
6.6 public int sumAll(int... numbers) {
6.7 + if (numbers.length == 0) {
6.8 + return 0;
6.9 + }
6.10 int sum = numbers[0];
6.11 for (int i = 1; i < numbers.length; i++) {
6.12 sum = sumTwo(sum, numbers[i]);
7.1 --- a/samples/composition/src-api2.0/api/Arithmetica.java Sat Jun 14 09:59:32 2008 +0200
7.2 +++ b/samples/composition/src-api2.0/api/Arithmetica.java Sat Jun 14 10:01:18 2008 +0200
7.3 @@ -12,6 +12,9 @@
7.4 }
7.5
7.6 public int sumAll(int... numbers) {
7.7 + if (numbers.length == 0) {
7.8 + return 0;
7.9 + }
7.10 int sum = numbers[0];
7.11 for (int i = 1; i < numbers.length; i++) {
7.12 sum = sumTwo(sum, numbers[i]);
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java Sat Jun 14 10:01:18 2008 +0200
8.3 @@ -0,0 +1,168 @@
8.4 +package api;
8.5 +
8.6 +import junit.framework.*;
8.7 +import java.util.*;
8.8 +
8.9 +public class ArithmeticaCompatibilityTest extends TestCase {
8.10 + public ArithmeticaCompatibilityTest(String name) {
8.11 + super(name);
8.12 + }
8.13 +
8.14 + private static final class CountingSubclass extends Arithmetica {
8.15 + int countSumTwo;
8.16 + int countSumAll;
8.17 + int countSumRange;
8.18 +
8.19 + @Override
8.20 + public int sumAll(int... numbers) {
8.21 + countSumAll++;
8.22 + return super.sumAll(numbers);
8.23 + }
8.24 +
8.25 + @Override
8.26 + public int sumRange(int from, int to) {
8.27 + countSumRange++;
8.28 + return super.sumRange(from, to);
8.29 + }
8.30 +
8.31 + @Override
8.32 + public int sumTwo(int one, int second) {
8.33 + countSumTwo++;
8.34 + return super.sumTwo(one, second);
8.35 + }
8.36 + } // end of CountingSubclass
8.37 +
8.38 + private static final class CountingOldSubclass extends OldArithmetica1 {
8.39 + int countSumTwo;
8.40 + int countSumAll;
8.41 + int countSumRange;
8.42 +
8.43 + @Override
8.44 + public int sumAll(int... numbers) {
8.45 + countSumAll++;
8.46 + return super.sumAll(numbers);
8.47 + }
8.48 +
8.49 + @Override
8.50 + public int sumRange(int from, int to) {
8.51 + countSumRange++;
8.52 + return super.sumRange(from, to);
8.53 + }
8.54 +
8.55 + @Override
8.56 + public int sumTwo(int one, int second) {
8.57 + countSumTwo++;
8.58 + return super.sumTwo(one, second);
8.59 + }
8.60 + } // end of CountingSubclass
8.61 +
8.62 + public void testRandomCheck () throws Exception {
8.63 + long seed = System.currentTimeMillis();
8.64 + try {
8.65 + CountingSubclass now = new CountingSubclass();
8.66 + CountingOldSubclass old = new CountingOldSubclass();
8.67 +
8.68 + compare(now, old, seed);
8.69 +
8.70 + assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
8.71 + assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
8.72 + assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
8.73 + } catch (AssertionFailedError ex) {
8.74 + IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
8.75 + n.initCause(ex);
8.76 + throw n;
8.77 + } catch (Exception ex) {
8.78 + IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
8.79 + n.initCause(ex);
8.80 + throw n;
8.81 + }
8.82 + }
8.83 +
8.84 + public void testSimulateOKRunOn1208120436947() throws Exception {
8.85 + CountingSubclass now = new CountingSubclass();
8.86 + CountingOldSubclass old = new CountingOldSubclass();
8.87 +
8.88 + compare(now, old, 1208120436947L);
8.89 +
8.90 + assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
8.91 + assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
8.92 + assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
8.93 + }
8.94 +
8.95 + public void testSimulateFailureOn1208120628821() throws Exception {
8.96 + CountingSubclass now = new CountingSubclass();
8.97 + CountingOldSubclass old = new CountingOldSubclass();
8.98 +
8.99 + compare(now, old, 1208120628821L);
8.100 +
8.101 + assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
8.102 + assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
8.103 + assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
8.104 + }
8.105 +
8.106 + private void compare (CountingSubclass now, CountingOldSubclass old, long seed) throws Exception {
8.107 + java.util.Random r = new java.util.Random (seed);
8.108 +
8.109 + for (int loop = 0; loop < r.nextInt(5); loop++) {
8.110 + int operation = r.nextInt(3);
8.111 + switch (operation) {
8.112 + case 0: { // sumTwo
8.113 + int a1 = r.nextInt(100);
8.114 + int a2 = r.nextInt(100);
8.115 + int resNow = now.sumTwo(a1, a2);
8.116 + int resOld = old.sumTwo(a1, a2);
8.117 + assertEquals("Results of sumTwo are equal", resNow, resOld);
8.118 + break;
8.119 + }
8.120 + case 1: { // sumArray
8.121 + int[] arr = new int[r.nextInt(100)];
8.122 + for (int i = 0; i < arr.length; i++) {
8.123 + arr[i] = r.nextInt(100);
8.124 + }
8.125 + int resNow = now.sumAll(arr);
8.126 + int resOld = old.sumAll(arr);
8.127 + assertEquals("Results of sumArray are equal", resNow, resOld);
8.128 + break;
8.129 + }
8.130 + case 2: { // sumRange
8.131 + int a1 = r.nextInt(100);
8.132 + int a2 = r.nextInt(100);
8.133 + int resNow = now.sumRange(a1, a1 + a2);
8.134 + int resOld = old.sumRange(a1, a1 + a2);
8.135 + assertEquals("Results of sumRange are equal", resNow, resOld);
8.136 + break;
8.137 + }
8.138 + }
8.139 + }
8.140 + }
8.141 +
8.142 +
8.143 +
8.144 + /** This is a copy of the implementation of Arithmetica from version 1.0 */
8.145 + static class OldArithmetica1 {
8.146 + public int sumTwo(int one, int second) {
8.147 + return one + second;
8.148 + }
8.149 +
8.150 + public int sumAll(int... numbers) {
8.151 + if (numbers.length == 0) {
8.152 + return 0;
8.153 + }
8.154 + int sum = numbers[0];
8.155 + for (int i = 1; i < numbers.length; i++) {
8.156 + sum = sumTwo(sum, numbers[i]);
8.157 + }
8.158 + return sum;
8.159 + }
8.160 +
8.161 + public int sumRange(int from, int to) {
8.162 + int len = to - from;
8.163 + int[] array = new int[len + 1];
8.164 + for (int i = 0; i <= len; i++) {
8.165 + array[i] = from + i;
8.166 + }
8.167 + return sumAll(array);
8.168 + }
8.169 + } // end of OldArithmetica1
8.170 +
8.171 +}