samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:01:18 +0200
changeset 184 6b2cd8df14c0
child 185 ebce91ecae84
permissions -rw-r--r--
Enhancing the example to contain randomized that to verify consistency with previous implementation
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@184
    59
    public void testRandomCheck () throws Exception {
jtulach@184
    60
        long seed = System.currentTimeMillis();
jtulach@184
    61
        try {
jtulach@184
    62
            CountingSubclass now = new CountingSubclass();
jtulach@184
    63
            CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
    64
            
jtulach@184
    65
            compare(now, old, seed);
jtulach@184
    66
        
jtulach@184
    67
            assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
jtulach@184
    68
            assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
jtulach@184
    69
            assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
jtulach@184
    70
        } catch (AssertionFailedError ex) {
jtulach@184
    71
            IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
jtulach@184
    72
            n.initCause(ex);
jtulach@184
    73
            throw n;
jtulach@184
    74
        } catch (Exception ex) {
jtulach@184
    75
            IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
jtulach@184
    76
            n.initCause(ex);
jtulach@184
    77
            throw n;
jtulach@184
    78
        }
jtulach@184
    79
    }
jtulach@184
    80
    
jtulach@184
    81
    public void testSimulateOKRunOn1208120436947() throws Exception {
jtulach@184
    82
        CountingSubclass now = new CountingSubclass();
jtulach@184
    83
        CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
    84
jtulach@184
    85
        compare(now, old, 1208120436947L);
jtulach@184
    86
jtulach@184
    87
        assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
jtulach@184
    88
        assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
jtulach@184
    89
        assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
jtulach@184
    90
    }
jtulach@184
    91
jtulach@184
    92
    public void testSimulateFailureOn1208120628821() throws Exception {
jtulach@184
    93
        CountingSubclass now = new CountingSubclass();
jtulach@184
    94
        CountingOldSubclass old = new CountingOldSubclass();
jtulach@184
    95
jtulach@184
    96
        compare(now, old, 1208120628821L);
jtulach@184
    97
jtulach@184
    98
        assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
jtulach@184
    99
        assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
jtulach@184
   100
        assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
jtulach@184
   101
    }
jtulach@184
   102
    
jtulach@184
   103
    private void compare (CountingSubclass now, CountingOldSubclass old, long seed) throws Exception {
jtulach@184
   104
        java.util.Random r = new java.util.Random (seed);
jtulach@184
   105
        
jtulach@184
   106
        for (int loop = 0; loop < r.nextInt(5); loop++) {
jtulach@184
   107
            int operation = r.nextInt(3);
jtulach@184
   108
            switch (operation) {
jtulach@184
   109
                case 0: { // sumTwo
jtulach@184
   110
                    int a1 = r.nextInt(100);
jtulach@184
   111
                    int a2 = r.nextInt(100);
jtulach@184
   112
                    int resNow = now.sumTwo(a1, a2);
jtulach@184
   113
                    int resOld = old.sumTwo(a1, a2);
jtulach@184
   114
                    assertEquals("Results of sumTwo are equal", resNow, resOld);
jtulach@184
   115
                    break;
jtulach@184
   116
                }
jtulach@184
   117
                case 1: { // sumArray
jtulach@184
   118
                    int[] arr = new int[r.nextInt(100)];
jtulach@184
   119
                    for (int i = 0; i < arr.length; i++) {
jtulach@184
   120
                        arr[i] = r.nextInt(100);
jtulach@184
   121
                    }
jtulach@184
   122
                    int resNow = now.sumAll(arr);
jtulach@184
   123
                    int resOld = old.sumAll(arr);
jtulach@184
   124
                    assertEquals("Results of sumArray are equal", resNow, resOld);
jtulach@184
   125
                    break;
jtulach@184
   126
                }
jtulach@184
   127
                case 2: { // sumRange
jtulach@184
   128
                    int a1 = r.nextInt(100);
jtulach@184
   129
                    int a2 = r.nextInt(100);
jtulach@184
   130
                    int resNow = now.sumRange(a1, a1 + a2);
jtulach@184
   131
                    int resOld = old.sumRange(a1, a1 + a2);
jtulach@184
   132
                    assertEquals("Results of sumRange are equal", resNow, resOld);
jtulach@184
   133
                    break;
jtulach@184
   134
                }
jtulach@184
   135
            }
jtulach@184
   136
        }
jtulach@184
   137
    }
jtulach@184
   138
jtulach@184
   139
    
jtulach@184
   140
    
jtulach@184
   141
    /** This is a copy of the implementation of Arithmetica from version 1.0 */
jtulach@184
   142
    static class OldArithmetica1 {
jtulach@184
   143
        public int sumTwo(int one, int second) {
jtulach@184
   144
            return one + second;
jtulach@184
   145
        }
jtulach@184
   146
jtulach@184
   147
        public int sumAll(int... numbers) {
jtulach@184
   148
            if (numbers.length == 0) {
jtulach@184
   149
                return 0;
jtulach@184
   150
            }
jtulach@184
   151
            int sum = numbers[0];
jtulach@184
   152
            for (int i = 1; i < numbers.length; i++) {
jtulach@184
   153
                sum = sumTwo(sum, numbers[i]);
jtulach@184
   154
            }
jtulach@184
   155
            return sum;
jtulach@184
   156
        }
jtulach@184
   157
jtulach@184
   158
        public int sumRange(int from, int to) {
jtulach@184
   159
            int len = to - from;
jtulach@184
   160
            int[] array = new int[len + 1];
jtulach@184
   161
            for (int i = 0; i <= len; i++) {
jtulach@184
   162
                array[i] = from + i;
jtulach@184
   163
            }
jtulach@184
   164
            return sumAll(array);
jtulach@184
   165
        }
jtulach@184
   166
    } // end of OldArithmetica1
jtulach@184
   167
    
jtulach@184
   168
}