samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
changeset 321 06bf3a32eaa0
parent 320 9ad0cc253aa9
child 322 c12a563d36a4
     1.1 --- a/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java	Thu Feb 12 11:00:41 2009 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,206 +0,0 @@
     1.4 -package api;
     1.5 -
     1.6 -import junit.framework.*;
     1.7 -
     1.8 -public class ArithmeticaCompatibilityTest extends TestCase {
     1.9 -    public ArithmeticaCompatibilityTest(String name) {
    1.10 -        super(name);
    1.11 -    }
    1.12 -
    1.13 -    private static final class CountingSubclass extends Arithmetica {
    1.14 -        int countSumTwo;
    1.15 -        int countSumAll;
    1.16 -        int countSumRange;
    1.17 -
    1.18 -        @Override
    1.19 -        public int sumAll(int... numbers) {
    1.20 -            countSumAll++;
    1.21 -            return super.sumAll(numbers);
    1.22 -        }
    1.23 -
    1.24 -        @Override
    1.25 -        public int sumRange(int from, int to) {
    1.26 -            countSumRange++;
    1.27 -            return super.sumRange(from, to);
    1.28 -        }
    1.29 -
    1.30 -        @Override
    1.31 -        public int sumTwo(int one, int second) {
    1.32 -            countSumTwo++;
    1.33 -            return super.sumTwo(one, second);
    1.34 -        }
    1.35 -    } // end of CountingSubclass
    1.36 -    
    1.37 -    private static final class CountingOldSubclass extends OldArithmetica1 {
    1.38 -        int countSumTwo;
    1.39 -        int countSumAll;
    1.40 -        int countSumRange;
    1.41 -
    1.42 -        @Override
    1.43 -        public int sumAll(int... numbers) {
    1.44 -            countSumAll++;
    1.45 -            return super.sumAll(numbers);
    1.46 -        }
    1.47 -
    1.48 -        @Override
    1.49 -        public int sumRange(int from, int to) {
    1.50 -            countSumRange++;
    1.51 -            return super.sumRange(from, to);
    1.52 -        }
    1.53 -
    1.54 -        @Override
    1.55 -        public int sumTwo(int one, int second) {
    1.56 -            countSumTwo++;
    1.57 -            return super.sumTwo(one, second);
    1.58 -        }
    1.59 -    } // end of CountingSubclass
    1.60 -    
    1.61 -    // BEGIN: total.rewrite.tests
    1.62 -    public void testRandomCheck () throws Exception {
    1.63 -        if (Boolean.getBoolean("no.failures")) return;
    1.64 -        long seed = System.currentTimeMillis();
    1.65 -        try {
    1.66 -            CountingSubclass now = new CountingSubclass();
    1.67 -            CountingOldSubclass old = new CountingOldSubclass();
    1.68 -            
    1.69 -            compare(now, old, seed);
    1.70 -        
    1.71 -            assertEquals(
    1.72 -                "Verify amount calls to of sumRange is the same", 
    1.73 -                now.countSumRange, old.countSumRange
    1.74 -            );
    1.75 -            assertEquals(
    1.76 -                "Verify amount calls to of sumAll is the same", 
    1.77 -                now.countSumAll, old.countSumAll
    1.78 -            );
    1.79 -            assertEquals(
    1.80 -                "Verify amount calls to of sumTwo is the same", 
    1.81 -                now.countSumTwo, old.countSumTwo
    1.82 -            );
    1.83 -        } catch (AssertionFailedError ex) {
    1.84 -            IllegalStateException n = new IllegalStateException (
    1.85 -                "Seed: " + seed + "\n" + ex.getMessage ()
    1.86 -            );
    1.87 -            n.initCause(ex);
    1.88 -            throw n;
    1.89 -        } catch (Exception ex) {
    1.90 -            IllegalStateException n = new IllegalStateException (
    1.91 -                "Seed: " + seed + "\n" + ex.getMessage ()
    1.92 -            );
    1.93 -            n.initCause(ex);
    1.94 -            throw n;
    1.95 -        }
    1.96 -    }
    1.97 -    
    1.98 -    public void testSimulateOKRunOn1208120436947() throws Exception {
    1.99 -        CountingSubclass now = new CountingSubclass();
   1.100 -        CountingOldSubclass old = new CountingOldSubclass();
   1.101 -
   1.102 -        compare(now, old, 1208120436947L);
   1.103 -
   1.104 -        assertEquals(
   1.105 -            "Verify amount of calls to sumRange is the same", 
   1.106 -            now.countSumRange, old.countSumRange
   1.107 -        );
   1.108 -        assertEquals(
   1.109 -            "Verify amount of calls to sumAll is the same", 
   1.110 -            now.countSumAll, old.countSumAll
   1.111 -        );
   1.112 -        assertEquals(
   1.113 -            "Verify amount of calls to sumTwo is the same", 
   1.114 -            now.countSumTwo, old.countSumTwo
   1.115 -        );
   1.116 -    }
   1.117 -
   1.118 -    public void testSimulateFailureOn1208120628821() throws Exception {
   1.119 -        if (Boolean.getBoolean("no.failures")) return;
   1.120 -        CountingSubclass now = new CountingSubclass();
   1.121 -        CountingOldSubclass old = new CountingOldSubclass();
   1.122 -
   1.123 -        compare(now, old, 1208120628821L);
   1.124 -
   1.125 -        assertEquals(
   1.126 -            "Verify amount of calls to sumRange is the same", 
   1.127 -            now.countSumRange, old.countSumRange
   1.128 -        );
   1.129 -        assertEquals(
   1.130 -            "Verify amount of calls to sumAll is the same", 
   1.131 -            now.countSumAll, old.countSumAll
   1.132 -        );
   1.133 -        assertEquals(
   1.134 -            "Verify amount of calls to sumTwo is the same", 
   1.135 -            now.countSumTwo, old.countSumTwo
   1.136 -        );
   1.137 -    }
   1.138 -    // END: total.rewrite.tests
   1.139 -    
   1.140 -    // BEGIN: total.rewrite.compare
   1.141 -    private void compare (Arithmetica now, OldArithmetica1 old, long seed) 
   1.142 -    throws Exception {
   1.143 -        java.util.Random r = new java.util.Random (seed);
   1.144 -        
   1.145 -        for (int loop = 0; loop < r.nextInt(5); loop++) {
   1.146 -            int operation = r.nextInt(3);
   1.147 -            switch (operation) {
   1.148 -                case 0: { // sumTwo
   1.149 -                    int a1 = r.nextInt(100);
   1.150 -                    int a2 = r.nextInt(100);
   1.151 -                    int resNow = now.sumTwo(a1, a2);
   1.152 -                    int resOld = old.sumTwo(a1, a2);
   1.153 -                    assertEquals("sumTwo results are equal", resNow, resOld);
   1.154 -                    break;
   1.155 -                }
   1.156 -                case 1: { // sumArray
   1.157 -                    int[] arr = new int[r.nextInt(100)];
   1.158 -                    for (int i = 0; i < arr.length; i++) {
   1.159 -                        arr[i] = r.nextInt(100);
   1.160 -                    }
   1.161 -                    int resNow = now.sumAll(arr);
   1.162 -                    int resOld = old.sumAll(arr);
   1.163 -                    assertEquals("sumArray results are equal", resNow, resOld);
   1.164 -                    break;
   1.165 -                }
   1.166 -                case 2: { // sumRange
   1.167 -                    int a1 = r.nextInt(100);
   1.168 -                    int a2 = r.nextInt(100);
   1.169 -                    int resNow = now.sumRange(a1, a1 + a2);
   1.170 -                    int resOld = old.sumRange(a1, a1 + a2);
   1.171 -                    assertEquals("sumRange results are equal", resNow, resOld);
   1.172 -                    break;
   1.173 -                }
   1.174 -            }
   1.175 -        }
   1.176 -    }
   1.177 -    // END: total.rewrite.compare
   1.178 -
   1.179 -    
   1.180 -    // BEGIN: total.rewrite.oldimpl
   1.181 -    /** This is a copy of the implementation of Arithmetica from version 1.0 */
   1.182 -    static class OldArithmetica1 {
   1.183 -        public int sumTwo(int one, int second) {
   1.184 -            return one + second;
   1.185 -        }
   1.186 -
   1.187 -        public int sumAll(int... numbers) {
   1.188 -            if (numbers.length == 0) {
   1.189 -                return 0;
   1.190 -            }
   1.191 -            int sum = numbers[0];
   1.192 -            for (int i = 1; i < numbers.length; i++) {
   1.193 -                sum = sumTwo(sum, numbers[i]);
   1.194 -            }
   1.195 -            return sum;
   1.196 -        }
   1.197 -
   1.198 -        public int sumRange(int from, int to) {
   1.199 -            int len = to - from;
   1.200 -            int[] array = new int[len + 1];
   1.201 -            for (int i = 0; i <= len; i++) {
   1.202 -                array[i] = from + i;
   1.203 -            }
   1.204 -            return sumAll(array);
   1.205 -        }
   1.206 -    } 
   1.207 -    // END: total.rewrite.oldimpl
   1.208 -    
   1.209 -}