3 import junit.framework.*;
5 public class ArithmeticaCompatibilityTest extends TestCase {
6 public ArithmeticaCompatibilityTest(String name) {
10 private static final class CountingSubclass extends Arithmetica {
16 public int sumAll(int... numbers) {
18 return super.sumAll(numbers);
22 public int sumRange(int from, int to) {
24 return super.sumRange(from, to);
28 public int sumTwo(int one, int second) {
30 return super.sumTwo(one, second);
32 } // end of CountingSubclass
34 private static final class CountingOldSubclass extends OldArithmetica1 {
40 public int sumAll(int... numbers) {
42 return super.sumAll(numbers);
46 public int sumRange(int from, int to) {
48 return super.sumRange(from, to);
52 public int sumTwo(int one, int second) {
54 return super.sumTwo(one, second);
56 } // end of CountingSubclass
58 // BEGIN: total.rewrite.tests
59 public void testRandomCheck () throws Exception {
60 if (Boolean.getBoolean("no.failures")) return;
61 long seed = System.currentTimeMillis();
63 CountingSubclass now = new CountingSubclass();
64 CountingOldSubclass old = new CountingOldSubclass();
66 compare(now, old, seed);
69 "Verify amount calls to of sumRange is the same",
70 now.countSumRange, old.countSumRange
73 "Verify amount calls to of sumAll is the same",
74 now.countSumAll, old.countSumAll
77 "Verify amount calls to of sumTwo is the same",
78 now.countSumTwo, old.countSumTwo
80 } catch (AssertionFailedError ex) {
81 IllegalStateException n = new IllegalStateException (
82 "Seed: " + seed + "\n" + ex.getMessage ()
86 } catch (Exception ex) {
87 IllegalStateException n = new IllegalStateException (
88 "Seed: " + seed + "\n" + ex.getMessage ()
95 public void testSimulateOKRunOn1208120436947() throws Exception {
96 CountingSubclass now = new CountingSubclass();
97 CountingOldSubclass old = new CountingOldSubclass();
99 compare(now, old, 1208120436947L);
102 "Verify amount of calls to sumRange is the same",
103 now.countSumRange, old.countSumRange
106 "Verify amount of calls to sumAll is the same",
107 now.countSumAll, old.countSumAll
110 "Verify amount of calls to sumTwo is the same",
111 now.countSumTwo, old.countSumTwo
115 public void testSimulateFailureOn1208120628821() throws Exception {
116 if (Boolean.getBoolean("no.failures")) return;
117 CountingSubclass now = new CountingSubclass();
118 CountingOldSubclass old = new CountingOldSubclass();
120 compare(now, old, 1208120628821L);
123 "Verify amount of calls to sumRange is the same",
124 now.countSumRange, old.countSumRange
127 "Verify amount of calls to sumAll is the same",
128 now.countSumAll, old.countSumAll
131 "Verify amount of calls to sumTwo is the same",
132 now.countSumTwo, old.countSumTwo
135 // END: total.rewrite.tests
137 // BEGIN: total.rewrite.compare
138 private void compare (Arithmetica now, OldArithmetica1 old, long seed)
140 java.util.Random r = new java.util.Random (seed);
142 for (int loop = 0; loop < r.nextInt(5); loop++) {
143 int operation = r.nextInt(3);
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);
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);
158 int resNow = now.sumAll(arr);
159 int resOld = old.sumAll(arr);
160 assertEquals("sumArray results are equal", resNow, resOld);
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);
174 // END: total.rewrite.compare
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) {
184 public int sumAll(int... numbers) {
185 if (numbers.length == 0) {
188 int sum = numbers[0];
189 for (int i = 1; i < numbers.length; i++) {
190 sum = sumTwo(sum, numbers[i]);
195 public int sumRange(int from, int to) {
197 int[] array = new int[len + 1];
198 for (int i = 0; i <= len; i++) {
201 return sumAll(array);
204 // END: total.rewrite.oldimpl