samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 18:51:38 +0200
changeset 263 7e8e995065c5
parent 210 acf2c31e22d4
permissions -rw-r--r--
Tests of all modules are executed and can fail the build
     1 package api;
     2 
     3 import junit.framework.*;
     4 
     5 public class ArithmeticaCompatibilityTest extends TestCase {
     6     public ArithmeticaCompatibilityTest(String name) {
     7         super(name);
     8     }
     9 
    10     private static final class CountingSubclass extends Arithmetica {
    11         int countSumTwo;
    12         int countSumAll;
    13         int countSumRange;
    14 
    15         @Override
    16         public int sumAll(int... numbers) {
    17             countSumAll++;
    18             return super.sumAll(numbers);
    19         }
    20 
    21         @Override
    22         public int sumRange(int from, int to) {
    23             countSumRange++;
    24             return super.sumRange(from, to);
    25         }
    26 
    27         @Override
    28         public int sumTwo(int one, int second) {
    29             countSumTwo++;
    30             return super.sumTwo(one, second);
    31         }
    32     } // end of CountingSubclass
    33     
    34     private static final class CountingOldSubclass extends OldArithmetica1 {
    35         int countSumTwo;
    36         int countSumAll;
    37         int countSumRange;
    38 
    39         @Override
    40         public int sumAll(int... numbers) {
    41             countSumAll++;
    42             return super.sumAll(numbers);
    43         }
    44 
    45         @Override
    46         public int sumRange(int from, int to) {
    47             countSumRange++;
    48             return super.sumRange(from, to);
    49         }
    50 
    51         @Override
    52         public int sumTwo(int one, int second) {
    53             countSumTwo++;
    54             return super.sumTwo(one, second);
    55         }
    56     } // end of CountingSubclass
    57     
    58     // BEGIN: total.rewrite.tests
    59     public void testRandomCheck () throws Exception {
    60         if (Boolean.getBoolean("no.failures")) return;
    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         if (Boolean.getBoolean("no.failures")) return;
   117         CountingSubclass now = new CountingSubclass();
   118         CountingOldSubclass old = new CountingOldSubclass();
   119 
   120         compare(now, old, 1208120628821L);
   121 
   122         assertEquals(
   123             "Verify amount of calls to sumRange is the same", 
   124             now.countSumRange, old.countSumRange
   125         );
   126         assertEquals(
   127             "Verify amount of calls to sumAll is the same", 
   128             now.countSumAll, old.countSumAll
   129         );
   130         assertEquals(
   131             "Verify amount of calls to sumTwo is the same", 
   132             now.countSumTwo, old.countSumTwo
   133         );
   134     }
   135     // END: total.rewrite.tests
   136     
   137     // BEGIN: total.rewrite.compare
   138     private void compare (Arithmetica now, OldArithmetica1 old, long seed) 
   139     throws Exception {
   140         java.util.Random r = new java.util.Random (seed);
   141         
   142         for (int loop = 0; loop < r.nextInt(5); loop++) {
   143             int operation = r.nextInt(3);
   144             switch (operation) {
   145                 case 0: { // sumTwo
   146                     int a1 = r.nextInt(100);
   147                     int a2 = r.nextInt(100);
   148                     int resNow = now.sumTwo(a1, a2);
   149                     int resOld = old.sumTwo(a1, a2);
   150                     assertEquals("sumTwo results are equal", resNow, resOld);
   151                     break;
   152                 }
   153                 case 1: { // sumArray
   154                     int[] arr = new int[r.nextInt(100)];
   155                     for (int i = 0; i < arr.length; i++) {
   156                         arr[i] = r.nextInt(100);
   157                     }
   158                     int resNow = now.sumAll(arr);
   159                     int resOld = old.sumAll(arr);
   160                     assertEquals("sumArray results are equal", resNow, resOld);
   161                     break;
   162                 }
   163                 case 2: { // sumRange
   164                     int a1 = r.nextInt(100);
   165                     int a2 = r.nextInt(100);
   166                     int resNow = now.sumRange(a1, a1 + a2);
   167                     int resOld = old.sumRange(a1, a1 + a2);
   168                     assertEquals("sumRange results are equal", resNow, resOld);
   169                     break;
   170                 }
   171             }
   172         }
   173     }
   174     // END: total.rewrite.compare
   175 
   176     
   177     // BEGIN: total.rewrite.oldimpl
   178     /** This is a copy of the implementation of Arithmetica from version 1.0 */
   179     static class OldArithmetica1 {
   180         public int sumTwo(int one, int second) {
   181             return one + second;
   182         }
   183 
   184         public int sumAll(int... numbers) {
   185             if (numbers.length == 0) {
   186                 return 0;
   187             }
   188             int sum = numbers[0];
   189             for (int i = 1; i < numbers.length; i++) {
   190                 sum = sumTwo(sum, numbers[i]);
   191             }
   192             return sum;
   193         }
   194 
   195         public int sumRange(int from, int to) {
   196             int len = to - from;
   197             int[] array = new int[len + 1];
   198             for (int i = 0; i <= len; i++) {
   199                 array[i] = from + i;
   200             }
   201             return sumAll(array);
   202         }
   203     } 
   204     // END: total.rewrite.oldimpl
   205     
   206 }