samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:01:21 +0200
changeset 185 ebce91ecae84
parent 184 6b2cd8df14c0
child 188 71ecc458ecd0
permissions -rw-r--r--
review31: Providing randomized Arithmetica test to verify inconsistencies with version 1.0
jtulach@184
     1
package api;
jtulach@184
     2
jtulach@184
     3
import junit.framework.*;
jtulach@184
     4
import java.util.*;
jtulach@184
     5
jtulach@184
     6
public class ArithmeticaCompatibilityTest extends TestCase {
jtulach@184
     7
    public ArithmeticaCompatibilityTest(String name) {
jtulach@184
     8
        super(name);
jtulach@184
     9
    }
jtulach@184
    10
jtulach@184
    11
    private static final class CountingSubclass extends Arithmetica {
jtulach@184
    12
        int countSumTwo;
jtulach@184
    13
        int countSumAll;
jtulach@184
    14
        int countSumRange;
jtulach@184
    15
jtulach@184
    16
        @Override
jtulach@184
    17
        public int sumAll(int... numbers) {
jtulach@184
    18
            countSumAll++;
jtulach@184
    19
            return super.sumAll(numbers);
jtulach@184
    20
        }
jtulach@184
    21
jtulach@184
    22
        @Override
jtulach@184
    23
        public int sumRange(int from, int to) {
jtulach@184
    24
            countSumRange++;
jtulach@184
    25
            return super.sumRange(from, to);
jtulach@184
    26
        }
jtulach@184
    27
jtulach@184
    28
        @Override
jtulach@184
    29
        public int sumTwo(int one, int second) {
jtulach@184
    30
            countSumTwo++;
jtulach@184
    31
            return super.sumTwo(one, second);
jtulach@184
    32
        }
jtulach@184
    33
    } // end of CountingSubclass
jtulach@184
    34
    
jtulach@184
    35
    private static final class CountingOldSubclass extends OldArithmetica1 {
jtulach@184
    36
        int countSumTwo;
jtulach@184
    37
        int countSumAll;
jtulach@184
    38
        int countSumRange;
jtulach@184
    39
jtulach@184
    40
        @Override
jtulach@184
    41
        public int sumAll(int... numbers) {
jtulach@184
    42
            countSumAll++;
jtulach@184
    43
            return super.sumAll(numbers);
jtulach@184
    44
        }
jtulach@184
    45
jtulach@184
    46
        @Override
jtulach@184
    47
        public int sumRange(int from, int to) {
jtulach@184
    48
            countSumRange++;
jtulach@184
    49
            return super.sumRange(from, to);
jtulach@184
    50
        }
jtulach@184
    51
jtulach@184
    52
        @Override
jtulach@184
    53
        public int sumTwo(int one, int second) {
jtulach@184
    54
            countSumTwo++;
jtulach@184
    55
            return super.sumTwo(one, second);
jtulach@184
    56
        }
jtulach@184
    57
    } // end of CountingSubclass
jtulach@184
    58
    
jtulach@185
    59
    // BEGIN: total.rewrite.tests
jtulach@184
    60
    public void testRandomCheck () throws Exception {
jtulach@184
    61
        long seed = System.currentTimeMillis();
jtulach@184
    62
        try {
jtulach@184
    63
            CountingSubclass now = new CountingSubclass();
jtulach@184
    64
            CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
    65
            
jtulach@184
    66
            compare(now, old, seed);
jtulach@184
    67
        
jtulach@184
    68
            assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
jtulach@184
    69
            assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
jtulach@184
    70
            assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
jtulach@184
    71
        } catch (AssertionFailedError ex) {
jtulach@184
    72
            IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
jtulach@184
    73
            n.initCause(ex);
jtulach@184
    74
            throw n;
jtulach@184
    75
        } catch (Exception ex) {
jtulach@184
    76
            IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
jtulach@184
    77
            n.initCause(ex);
jtulach@184
    78
            throw n;
jtulach@184
    79
        }
jtulach@184
    80
    }
jtulach@184
    81
    
jtulach@184
    82
    public void testSimulateOKRunOn1208120436947() throws Exception {
jtulach@184
    83
        CountingSubclass now = new CountingSubclass();
jtulach@184
    84
        CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
    85
jtulach@184
    86
        compare(now, old, 1208120436947L);
jtulach@184
    87
jtulach@184
    88
        assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
jtulach@184
    89
        assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
jtulach@184
    90
        assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
jtulach@184
    91
    }
jtulach@184
    92
jtulach@184
    93
    public void testSimulateFailureOn1208120628821() throws Exception {
jtulach@184
    94
        CountingSubclass now = new CountingSubclass();
jtulach@184
    95
        CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
    96
jtulach@184
    97
        compare(now, old, 1208120628821L);
jtulach@184
    98
jtulach@184
    99
        assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
jtulach@184
   100
        assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
jtulach@184
   101
        assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
jtulach@184
   102
    }
jtulach@185
   103
    // END: total.rewrite.tests
jtulach@184
   104
    
jtulach@185
   105
    // BEGIN: total.rewrite.compare
jtulach@185
   106
    private void compare (Arithmetica now, OldArithmetica1 old, long seed) throws Exception {
jtulach@184
   107
        java.util.Random r = new java.util.Random (seed);
jtulach@184
   108
        
jtulach@184
   109
        for (int loop = 0; loop < r.nextInt(5); loop++) {
jtulach@184
   110
            int operation = r.nextInt(3);
jtulach@184
   111
            switch (operation) {
jtulach@184
   112
                case 0: { // sumTwo
jtulach@184
   113
                    int a1 = r.nextInt(100);
jtulach@184
   114
                    int a2 = r.nextInt(100);
jtulach@184
   115
                    int resNow = now.sumTwo(a1, a2);
jtulach@184
   116
                    int resOld = old.sumTwo(a1, a2);
jtulach@184
   117
                    assertEquals("Results of sumTwo are equal", resNow, resOld);
jtulach@184
   118
                    break;
jtulach@184
   119
                }
jtulach@184
   120
                case 1: { // sumArray
jtulach@184
   121
                    int[] arr = new int[r.nextInt(100)];
jtulach@184
   122
                    for (int i = 0; i < arr.length; i++) {
jtulach@184
   123
                        arr[i] = r.nextInt(100);
jtulach@184
   124
                    }
jtulach@184
   125
                    int resNow = now.sumAll(arr);
jtulach@184
   126
                    int resOld = old.sumAll(arr);
jtulach@184
   127
                    assertEquals("Results of sumArray are equal", resNow, resOld);
jtulach@184
   128
                    break;
jtulach@184
   129
                }
jtulach@184
   130
                case 2: { // sumRange
jtulach@184
   131
                    int a1 = r.nextInt(100);
jtulach@184
   132
                    int a2 = r.nextInt(100);
jtulach@184
   133
                    int resNow = now.sumRange(a1, a1 + a2);
jtulach@184
   134
                    int resOld = old.sumRange(a1, a1 + a2);
jtulach@184
   135
                    assertEquals("Results of sumRange are equal", resNow, resOld);
jtulach@184
   136
                    break;
jtulach@184
   137
                }
jtulach@184
   138
            }
jtulach@184
   139
        }
jtulach@184
   140
    }
jtulach@185
   141
    // END: total.rewrite.compare
jtulach@184
   142
jtulach@184
   143
    
jtulach@185
   144
    // BEGIN: total.rewrite.oldimpl
jtulach@184
   145
    /** This is a copy of the implementation of Arithmetica from version 1.0 */
jtulach@184
   146
    static class OldArithmetica1 {
jtulach@184
   147
        public int sumTwo(int one, int second) {
jtulach@184
   148
            return one + second;
jtulach@184
   149
        }
jtulach@184
   150
jtulach@184
   151
        public int sumAll(int... numbers) {
jtulach@184
   152
            if (numbers.length == 0) {
jtulach@184
   153
                return 0;
jtulach@184
   154
            }
jtulach@184
   155
            int sum = numbers[0];
jtulach@184
   156
            for (int i = 1; i < numbers.length; i++) {
jtulach@184
   157
                sum = sumTwo(sum, numbers[i]);
jtulach@184
   158
            }
jtulach@184
   159
            return sum;
jtulach@184
   160
        }
jtulach@184
   161
jtulach@184
   162
        public int sumRange(int from, int to) {
jtulach@184
   163
            int len = to - from;
jtulach@184
   164
            int[] array = new int[len + 1];
jtulach@184
   165
            for (int i = 0; i <= len; i++) {
jtulach@184
   166
                array[i] = from + i;
jtulach@184
   167
            }
jtulach@184
   168
            return sumAll(array);
jtulach@184
   169
        }
jtulach@185
   170
    } 
jtulach@185
   171
    // END: total.rewrite.oldimpl
jtulach@184
   172
    
jtulach@184
   173
}