samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
changeset 184 6b2cd8df14c0
child 185 ebce91ecae84
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java	Sat Jun 14 10:01:18 2008 +0200
     1.3 @@ -0,0 +1,168 @@
     1.4 +package api;
     1.5 +
     1.6 +import junit.framework.*;
     1.7 +import java.util.*;
     1.8 +
     1.9 +public class ArithmeticaCompatibilityTest extends TestCase {
    1.10 +    public ArithmeticaCompatibilityTest(String name) {
    1.11 +        super(name);
    1.12 +    }
    1.13 +
    1.14 +    private static final class CountingSubclass extends Arithmetica {
    1.15 +        int countSumTwo;
    1.16 +        int countSumAll;
    1.17 +        int countSumRange;
    1.18 +
    1.19 +        @Override
    1.20 +        public int sumAll(int... numbers) {
    1.21 +            countSumAll++;
    1.22 +            return super.sumAll(numbers);
    1.23 +        }
    1.24 +
    1.25 +        @Override
    1.26 +        public int sumRange(int from, int to) {
    1.27 +            countSumRange++;
    1.28 +            return super.sumRange(from, to);
    1.29 +        }
    1.30 +
    1.31 +        @Override
    1.32 +        public int sumTwo(int one, int second) {
    1.33 +            countSumTwo++;
    1.34 +            return super.sumTwo(one, second);
    1.35 +        }
    1.36 +    } // end of CountingSubclass
    1.37 +    
    1.38 +    private static final class CountingOldSubclass extends OldArithmetica1 {
    1.39 +        int countSumTwo;
    1.40 +        int countSumAll;
    1.41 +        int countSumRange;
    1.42 +
    1.43 +        @Override
    1.44 +        public int sumAll(int... numbers) {
    1.45 +            countSumAll++;
    1.46 +            return super.sumAll(numbers);
    1.47 +        }
    1.48 +
    1.49 +        @Override
    1.50 +        public int sumRange(int from, int to) {
    1.51 +            countSumRange++;
    1.52 +            return super.sumRange(from, to);
    1.53 +        }
    1.54 +
    1.55 +        @Override
    1.56 +        public int sumTwo(int one, int second) {
    1.57 +            countSumTwo++;
    1.58 +            return super.sumTwo(one, second);
    1.59 +        }
    1.60 +    } // end of CountingSubclass
    1.61 +    
    1.62 +    public void testRandomCheck () throws Exception {
    1.63 +        long seed = System.currentTimeMillis();
    1.64 +        try {
    1.65 +            CountingSubclass now = new CountingSubclass();
    1.66 +            CountingOldSubclass old = new CountingOldSubclass();
    1.67 +            
    1.68 +            compare(now, old, seed);
    1.69 +        
    1.70 +            assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
    1.71 +            assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
    1.72 +            assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
    1.73 +        } catch (AssertionFailedError ex) {
    1.74 +            IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
    1.75 +            n.initCause(ex);
    1.76 +            throw n;
    1.77 +        } catch (Exception ex) {
    1.78 +            IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
    1.79 +            n.initCause(ex);
    1.80 +            throw n;
    1.81 +        }
    1.82 +    }
    1.83 +    
    1.84 +    public void testSimulateOKRunOn1208120436947() throws Exception {
    1.85 +        CountingSubclass now = new CountingSubclass();
    1.86 +        CountingOldSubclass old = new CountingOldSubclass();
    1.87 +
    1.88 +        compare(now, old, 1208120436947L);
    1.89 +
    1.90 +        assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
    1.91 +        assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
    1.92 +        assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
    1.93 +    }
    1.94 +
    1.95 +    public void testSimulateFailureOn1208120628821() throws Exception {
    1.96 +        CountingSubclass now = new CountingSubclass();
    1.97 +        CountingOldSubclass old = new CountingOldSubclass();
    1.98 +
    1.99 +        compare(now, old, 1208120628821L);
   1.100 +
   1.101 +        assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
   1.102 +        assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
   1.103 +        assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
   1.104 +    }
   1.105 +    
   1.106 +    private void compare (CountingSubclass now, CountingOldSubclass old, long seed) throws Exception {
   1.107 +        java.util.Random r = new java.util.Random (seed);
   1.108 +        
   1.109 +        for (int loop = 0; loop < r.nextInt(5); loop++) {
   1.110 +            int operation = r.nextInt(3);
   1.111 +            switch (operation) {
   1.112 +                case 0: { // sumTwo
   1.113 +                    int a1 = r.nextInt(100);
   1.114 +                    int a2 = r.nextInt(100);
   1.115 +                    int resNow = now.sumTwo(a1, a2);
   1.116 +                    int resOld = old.sumTwo(a1, a2);
   1.117 +                    assertEquals("Results of sumTwo are equal", resNow, resOld);
   1.118 +                    break;
   1.119 +                }
   1.120 +                case 1: { // sumArray
   1.121 +                    int[] arr = new int[r.nextInt(100)];
   1.122 +                    for (int i = 0; i < arr.length; i++) {
   1.123 +                        arr[i] = r.nextInt(100);
   1.124 +                    }
   1.125 +                    int resNow = now.sumAll(arr);
   1.126 +                    int resOld = old.sumAll(arr);
   1.127 +                    assertEquals("Results of sumArray are equal", resNow, resOld);
   1.128 +                    break;
   1.129 +                }
   1.130 +                case 2: { // sumRange
   1.131 +                    int a1 = r.nextInt(100);
   1.132 +                    int a2 = r.nextInt(100);
   1.133 +                    int resNow = now.sumRange(a1, a1 + a2);
   1.134 +                    int resOld = old.sumRange(a1, a1 + a2);
   1.135 +                    assertEquals("Results of sumRange are equal", resNow, resOld);
   1.136 +                    break;
   1.137 +                }
   1.138 +            }
   1.139 +        }
   1.140 +    }
   1.141 +
   1.142 +    
   1.143 +    
   1.144 +    /** This is a copy of the implementation of Arithmetica from version 1.0 */
   1.145 +    static class OldArithmetica1 {
   1.146 +        public int sumTwo(int one, int second) {
   1.147 +            return one + second;
   1.148 +        }
   1.149 +
   1.150 +        public int sumAll(int... numbers) {
   1.151 +            if (numbers.length == 0) {
   1.152 +                return 0;
   1.153 +            }
   1.154 +            int sum = numbers[0];
   1.155 +            for (int i = 1; i < numbers.length; i++) {
   1.156 +                sum = sumTwo(sum, numbers[i]);
   1.157 +            }
   1.158 +            return sum;
   1.159 +        }
   1.160 +
   1.161 +        public int sumRange(int from, int to) {
   1.162 +            int len = to - from;
   1.163 +            int[] array = new int[len + 1];
   1.164 +            for (int i = 0; i <= len; i++) {
   1.165 +                array[i] = from + i;
   1.166 +            }
   1.167 +            return sumAll(array);
   1.168 +        }
   1.169 +    } // end of OldArithmetica1
   1.170 +    
   1.171 +}