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