samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
child 263 7e8e995065c5
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
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@188
    68
            assertEquals(
jtulach@210
    69
                "Verify amount calls to of sumRange is the same", 
jtulach@188
    70
                now.countSumRange, old.countSumRange
jtulach@188
    71
            );
jtulach@188
    72
            assertEquals(
jtulach@210
    73
                "Verify amount calls to of sumAll is the same", 
jtulach@188
    74
                now.countSumAll, old.countSumAll
jtulach@188
    75
            );
jtulach@188
    76
            assertEquals(
jtulach@210
    77
                "Verify amount calls to of sumTwo is the same", 
jtulach@188
    78
                now.countSumTwo, old.countSumTwo
jtulach@188
    79
            );
jtulach@184
    80
        } catch (AssertionFailedError ex) {
jtulach@188
    81
            IllegalStateException n = new IllegalStateException (
jtulach@188
    82
                "Seed: " + seed + "\n" + ex.getMessage ()
jtulach@188
    83
            );
jtulach@184
    84
            n.initCause(ex);
jtulach@184
    85
            throw n;
jtulach@184
    86
        } catch (Exception ex) {
jtulach@188
    87
            IllegalStateException n = new IllegalStateException (
jtulach@188
    88
                "Seed: " + seed + "\n" + ex.getMessage ()
jtulach@188
    89
            );
jtulach@184
    90
            n.initCause(ex);
jtulach@184
    91
            throw n;
jtulach@184
    92
        }
jtulach@184
    93
    }
jtulach@184
    94
    
jtulach@184
    95
    public void testSimulateOKRunOn1208120436947() throws Exception {
jtulach@184
    96
        CountingSubclass now = new CountingSubclass();
jtulach@184
    97
        CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
    98
jtulach@184
    99
        compare(now, old, 1208120436947L);
jtulach@184
   100
jtulach@188
   101
        assertEquals(
jtulach@210
   102
            "Verify amount of calls to sumRange is the same", 
jtulach@188
   103
            now.countSumRange, old.countSumRange
jtulach@188
   104
        );
jtulach@188
   105
        assertEquals(
jtulach@210
   106
            "Verify amount of calls to sumAll is the same", 
jtulach@188
   107
            now.countSumAll, old.countSumAll
jtulach@188
   108
        );
jtulach@188
   109
        assertEquals(
jtulach@210
   110
            "Verify amount of calls to sumTwo is the same", 
jtulach@188
   111
            now.countSumTwo, old.countSumTwo
jtulach@188
   112
        );
jtulach@184
   113
    }
jtulach@184
   114
jtulach@184
   115
    public void testSimulateFailureOn1208120628821() throws Exception {
jtulach@184
   116
        CountingSubclass now = new CountingSubclass();
jtulach@184
   117
        CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
   118
jtulach@184
   119
        compare(now, old, 1208120628821L);
jtulach@184
   120
jtulach@188
   121
        assertEquals(
jtulach@210
   122
            "Verify amount of calls to sumRange is the same", 
jtulach@188
   123
            now.countSumRange, old.countSumRange
jtulach@188
   124
        );
jtulach@188
   125
        assertEquals(
jtulach@210
   126
            "Verify amount of calls to sumAll is the same", 
jtulach@188
   127
            now.countSumAll, old.countSumAll
jtulach@188
   128
        );
jtulach@188
   129
        assertEquals(
jtulach@210
   130
            "Verify amount of calls to sumTwo is the same", 
jtulach@188
   131
            now.countSumTwo, old.countSumTwo
jtulach@188
   132
        );
jtulach@184
   133
    }
jtulach@185
   134
    // END: total.rewrite.tests
jtulach@184
   135
    
jtulach@185
   136
    // BEGIN: total.rewrite.compare
jtulach@188
   137
    private void compare (Arithmetica now, OldArithmetica1 old, long seed) 
jtulach@188
   138
    throws Exception {
jtulach@184
   139
        java.util.Random r = new java.util.Random (seed);
jtulach@184
   140
        
jtulach@184
   141
        for (int loop = 0; loop < r.nextInt(5); loop++) {
jtulach@184
   142
            int operation = r.nextInt(3);
jtulach@184
   143
            switch (operation) {
jtulach@184
   144
                case 0: { // sumTwo
jtulach@184
   145
                    int a1 = r.nextInt(100);
jtulach@184
   146
                    int a2 = r.nextInt(100);
jtulach@184
   147
                    int resNow = now.sumTwo(a1, a2);
jtulach@184
   148
                    int resOld = old.sumTwo(a1, a2);
jtulach@188
   149
                    assertEquals("sumTwo results are equal", resNow, resOld);
jtulach@184
   150
                    break;
jtulach@184
   151
                }
jtulach@184
   152
                case 1: { // sumArray
jtulach@184
   153
                    int[] arr = new int[r.nextInt(100)];
jtulach@184
   154
                    for (int i = 0; i < arr.length; i++) {
jtulach@184
   155
                        arr[i] = r.nextInt(100);
jtulach@184
   156
                    }
jtulach@184
   157
                    int resNow = now.sumAll(arr);
jtulach@184
   158
                    int resOld = old.sumAll(arr);
jtulach@188
   159
                    assertEquals("sumArray results are equal", resNow, resOld);
jtulach@184
   160
                    break;
jtulach@184
   161
                }
jtulach@184
   162
                case 2: { // sumRange
jtulach@184
   163
                    int a1 = r.nextInt(100);
jtulach@184
   164
                    int a2 = r.nextInt(100);
jtulach@184
   165
                    int resNow = now.sumRange(a1, a1 + a2);
jtulach@184
   166
                    int resOld = old.sumRange(a1, a1 + a2);
jtulach@188
   167
                    assertEquals("sumRange results are equal", resNow, resOld);
jtulach@184
   168
                    break;
jtulach@184
   169
                }
jtulach@184
   170
            }
jtulach@184
   171
        }
jtulach@184
   172
    }
jtulach@185
   173
    // END: total.rewrite.compare
jtulach@184
   174
jtulach@184
   175
    
jtulach@185
   176
    // BEGIN: total.rewrite.oldimpl
jtulach@184
   177
    /** This is a copy of the implementation of Arithmetica from version 1.0 */
jtulach@184
   178
    static class OldArithmetica1 {
jtulach@184
   179
        public int sumTwo(int one, int second) {
jtulach@184
   180
            return one + second;
jtulach@184
   181
        }
jtulach@184
   182
jtulach@184
   183
        public int sumAll(int... numbers) {
jtulach@184
   184
            if (numbers.length == 0) {
jtulach@184
   185
                return 0;
jtulach@184
   186
            }
jtulach@184
   187
            int sum = numbers[0];
jtulach@184
   188
            for (int i = 1; i < numbers.length; i++) {
jtulach@184
   189
                sum = sumTwo(sum, numbers[i]);
jtulach@184
   190
            }
jtulach@184
   191
            return sum;
jtulach@184
   192
        }
jtulach@184
   193
jtulach@184
   194
        public int sumRange(int from, int to) {
jtulach@184
   195
            int len = to - from;
jtulach@184
   196
            int[] array = new int[len + 1];
jtulach@184
   197
            for (int i = 0; i <= len; i++) {
jtulach@184
   198
                array[i] = from + i;
jtulach@184
   199
            }
jtulach@184
   200
            return sumAll(array);
jtulach@184
   201
        }
jtulach@185
   202
    } 
jtulach@185
   203
    // END: total.rewrite.oldimpl
jtulach@184
   204
    
jtulach@184
   205
}