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