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