samples/composition/src-test/org/apidesign/math/test/ArithmeticaCompatibilityTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 263 7e8e995065c5
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
     1 package org.apidesign.math.test;
     2 
     3 import junit.framework.AssertionFailedError;
     4 import junit.framework.TestCase;
     5 import org.apidesign.math.Arithmetica;
     6 
     7 public class ArithmeticaCompatibilityTest extends TestCase {
     8     public ArithmeticaCompatibilityTest(String name) {
     9         super(name);
    10     }
    11 
    12     private static final class CountingSubclass extends Arithmetica {
    13         int countSumTwo;
    14         int countSumAll;
    15         int countSumRange;
    16 
    17         @Override
    18         public int sumAll(int... numbers) {
    19             countSumAll++;
    20             return super.sumAll(numbers);
    21         }
    22 
    23         @Override
    24         public int sumRange(int from, int to) {
    25             countSumRange++;
    26             return super.sumRange(from, to);
    27         }
    28 
    29         @Override
    30         public int sumTwo(int one, int second) {
    31             countSumTwo++;
    32             return super.sumTwo(one, second);
    33         }
    34     } // end of CountingSubclass
    35     
    36     private static final class CountingOldSubclass extends OldArithmetica1 {
    37         int countSumTwo;
    38         int countSumAll;
    39         int countSumRange;
    40 
    41         @Override
    42         public int sumAll(int... numbers) {
    43             countSumAll++;
    44             return super.sumAll(numbers);
    45         }
    46 
    47         @Override
    48         public int sumRange(int from, int to) {
    49             countSumRange++;
    50             return super.sumRange(from, to);
    51         }
    52 
    53         @Override
    54         public int sumTwo(int one, int second) {
    55             countSumTwo++;
    56             return super.sumTwo(one, second);
    57         }
    58     } // end of CountingSubclass
    59     
    60     // BEGIN: total.rewrite.tests
    61     public void testRandomCheck () throws Exception {
    62         if (Boolean.getBoolean("no.failures")) return;
    63         long seed = System.currentTimeMillis();
    64         try {
    65             CountingSubclass now = new CountingSubclass();
    66             CountingOldSubclass old = new CountingOldSubclass();
    67             
    68             compare(now, old, seed);
    69         
    70             assertEquals(
    71                 "Verify amount calls to of sumRange is the same", 
    72                 now.countSumRange, old.countSumRange
    73             );
    74             assertEquals(
    75                 "Verify amount calls to of sumAll is the same", 
    76                 now.countSumAll, old.countSumAll
    77             );
    78             assertEquals(
    79                 "Verify amount calls to of sumTwo is the same", 
    80                 now.countSumTwo, old.countSumTwo
    81             );
    82         } catch (AssertionFailedError ex) {
    83             IllegalStateException n = new IllegalStateException (
    84                 "Seed: " + seed + "\n" + ex.getMessage ()
    85             );
    86             n.initCause(ex);
    87             throw n;
    88         } catch (Exception ex) {
    89             IllegalStateException n = new IllegalStateException (
    90                 "Seed: " + seed + "\n" + ex.getMessage ()
    91             );
    92             n.initCause(ex);
    93             throw n;
    94         }
    95     }
    96     
    97     public void testSimulateOKRunOn1208120436947() throws Exception {
    98         CountingSubclass now = new CountingSubclass();
    99         CountingOldSubclass old = new CountingOldSubclass();
   100 
   101         compare(now, old, 1208120436947L);
   102 
   103         assertEquals(
   104             "Verify amount of calls to sumRange is the same", 
   105             now.countSumRange, old.countSumRange
   106         );
   107         assertEquals(
   108             "Verify amount of calls to sumAll is the same", 
   109             now.countSumAll, old.countSumAll
   110         );
   111         assertEquals(
   112             "Verify amount of calls to sumTwo is the same", 
   113             now.countSumTwo, old.countSumTwo
   114         );
   115     }
   116 
   117     public void testSimulateFailureOn1208120628821() throws Exception {
   118         if (Boolean.getBoolean("no.failures")) return;
   119         CountingSubclass now = new CountingSubclass();
   120         CountingOldSubclass old = new CountingOldSubclass();
   121 
   122         compare(now, old, 1208120628821L);
   123 
   124         assertEquals(
   125             "Verify amount of calls to sumRange is the same", 
   126             now.countSumRange, old.countSumRange
   127         );
   128         assertEquals(
   129             "Verify amount of calls to sumAll is the same", 
   130             now.countSumAll, old.countSumAll
   131         );
   132         assertEquals(
   133             "Verify amount of calls to sumTwo is the same", 
   134             now.countSumTwo, old.countSumTwo
   135         );
   136     }
   137     // END: total.rewrite.tests
   138     
   139     // BEGIN: total.rewrite.compare
   140     private void compare (Arithmetica now, OldArithmetica1 old, long seed) 
   141     throws Exception {
   142         java.util.Random r = new java.util.Random (seed);
   143         
   144         for (int loop = 0; loop < r.nextInt(5); loop++) {
   145             int operation = r.nextInt(3);
   146             switch (operation) {
   147                 case 0: { // sumTwo
   148                     int a1 = r.nextInt(100);
   149                     int a2 = r.nextInt(100);
   150                     int resNow = now.sumTwo(a1, a2);
   151                     int resOld = old.sumTwo(a1, a2);
   152                     assertEquals("sumTwo results are equal", resNow, resOld);
   153                     break;
   154                 }
   155                 case 1: { // sumArray
   156                     int[] arr = new int[r.nextInt(100)];
   157                     for (int i = 0; i < arr.length; i++) {
   158                         arr[i] = r.nextInt(100);
   159                     }
   160                     int resNow = now.sumAll(arr);
   161                     int resOld = old.sumAll(arr);
   162                     assertEquals("sumArray results are equal", resNow, resOld);
   163                     break;
   164                 }
   165                 case 2: { // sumRange
   166                     int a1 = r.nextInt(100);
   167                     int a2 = r.nextInt(100);
   168                     int resNow = now.sumRange(a1, a1 + a2);
   169                     int resOld = old.sumRange(a1, a1 + a2);
   170                     assertEquals("sumRange results are equal", resNow, resOld);
   171                     break;
   172                 }
   173             }
   174         }
   175     }
   176     // END: total.rewrite.compare
   177 
   178     
   179     // BEGIN: total.rewrite.oldimpl
   180     /** This is a copy of the implementation of Arithmetica from version 1.0 */
   181     static class OldArithmetica1 {
   182         public int sumTwo(int one, int second) {
   183             return one + second;
   184         }
   185 
   186         public int sumAll(int... numbers) {
   187             if (numbers.length == 0) {
   188                 return 0;
   189             }
   190             int sum = numbers[0];
   191             for (int i = 1; i < numbers.length; i++) {
   192                 sum = sumTwo(sum, numbers[i]);
   193             }
   194             return sum;
   195         }
   196 
   197         public int sumRange(int from, int to) {
   198             int len = to - from;
   199             int[] array = new int[len + 1];
   200             for (int i = 0; i <= len; i++) {
   201                 array[i] = from + i;
   202             }
   203             return sumAll(array);
   204         }
   205     } 
   206     // END: total.rewrite.oldimpl
   207     
   208 }