samples/composition/src-test/org/apidesign/math/test/ArithmeticaCompatibilityTest.java
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
1 package org.apidesign.math.test;
3 import junit.framework.AssertionFailedError;
4 import junit.framework.TestCase;
5 import org.apidesign.math.Arithmetica;
7 public class ArithmeticaCompatibilityTest extends TestCase {
8 public ArithmeticaCompatibilityTest(String name) {
12 private static final class CountingSubclass extends Arithmetica {
18 public int sumAll(int... numbers) {
20 return super.sumAll(numbers);
24 public int sumRange(int from, int to) {
26 return super.sumRange(from, to);
30 public int sumTwo(int one, int second) {
32 return super.sumTwo(one, second);
34 } // end of CountingSubclass
36 private static final class CountingOldSubclass extends OldArithmetica1 {
42 public int sumAll(int... numbers) {
44 return super.sumAll(numbers);
48 public int sumRange(int from, int to) {
50 return super.sumRange(from, to);
54 public int sumTwo(int one, int second) {
56 return super.sumTwo(one, second);
58 } // end of CountingSubclass
60 // BEGIN: total.rewrite.tests
61 public void testRandomCheck () throws Exception {
62 if (Boolean.getBoolean("no.failures")) return;
63 long seed = System.currentTimeMillis();
65 CountingSubclass now = new CountingSubclass();
66 CountingOldSubclass old = new CountingOldSubclass();
68 compare(now, old, seed);
71 "Verify amount calls to of sumRange is the same",
72 now.countSumRange, old.countSumRange
75 "Verify amount calls to of sumAll is the same",
76 now.countSumAll, old.countSumAll
79 "Verify amount calls to of sumTwo is the same",
80 now.countSumTwo, old.countSumTwo
82 } catch (AssertionFailedError ex) {
83 IllegalStateException n = new IllegalStateException (
84 "Seed: " + seed + "\n" + ex.getMessage ()
88 } catch (Exception ex) {
89 IllegalStateException n = new IllegalStateException (
90 "Seed: " + seed + "\n" + ex.getMessage ()
97 public void testSimulateOKRunOn1208120436947() throws Exception {
98 CountingSubclass now = new CountingSubclass();
99 CountingOldSubclass old = new CountingOldSubclass();
101 compare(now, old, 1208120436947L);
104 "Verify amount of calls to sumRange is the same",
105 now.countSumRange, old.countSumRange
108 "Verify amount of calls to sumAll is the same",
109 now.countSumAll, old.countSumAll
112 "Verify amount of calls to sumTwo is the same",
113 now.countSumTwo, old.countSumTwo
117 public void testSimulateFailureOn1208120628821() throws Exception {
118 if (Boolean.getBoolean("no.failures")) return;
119 CountingSubclass now = new CountingSubclass();
120 CountingOldSubclass old = new CountingOldSubclass();
122 compare(now, old, 1208120628821L);
125 "Verify amount of calls to sumRange is the same",
126 now.countSumRange, old.countSumRange
129 "Verify amount of calls to sumAll is the same",
130 now.countSumAll, old.countSumAll
133 "Verify amount of calls to sumTwo is the same",
134 now.countSumTwo, old.countSumTwo
137 // END: total.rewrite.tests
139 // BEGIN: total.rewrite.compare
140 private void compare (Arithmetica now, OldArithmetica1 old, long seed)
142 java.util.Random r = new java.util.Random (seed);
144 for (int loop = 0; loop < r.nextInt(5); loop++) {
145 int operation = r.nextInt(3);
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);
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);
160 int resNow = now.sumAll(arr);
161 int resOld = old.sumAll(arr);
162 assertEquals("sumArray results are equal", resNow, resOld);
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);
176 // END: total.rewrite.compare
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) {
186 public int sumAll(int... numbers) {
187 if (numbers.length == 0) {
190 int sum = numbers[0];
191 for (int i = 1; i < numbers.length; i++) {
192 sum = sumTwo(sum, numbers[i]);
197 public int sumRange(int from, int to) {
199 int[] array = new int[len + 1];
200 for (int i = 0; i <= len; i++) {
203 return sumAll(array);
206 // END: total.rewrite.oldimpl