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