3 import junit.framework.*;
6 public class ArithmeticaCompatibilityTest extends TestCase {
7 public ArithmeticaCompatibilityTest(String name) {
11 private static final class CountingSubclass extends Arithmetica {
17 public int sumAll(int... numbers) {
19 return super.sumAll(numbers);
23 public int sumRange(int from, int to) {
25 return super.sumRange(from, to);
29 public int sumTwo(int one, int second) {
31 return super.sumTwo(one, second);
33 } // end of CountingSubclass
35 private static final class CountingOldSubclass extends OldArithmetica1 {
41 public int sumAll(int... numbers) {
43 return super.sumAll(numbers);
47 public int sumRange(int from, int to) {
49 return super.sumRange(from, to);
53 public int sumTwo(int one, int second) {
55 return super.sumTwo(one, second);
57 } // end of CountingSubclass
59 // BEGIN: total.rewrite.tests
60 public void testRandomCheck () throws Exception {
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 CountingSubclass now = new CountingSubclass();
117 CountingOldSubclass old = new CountingOldSubclass();
119 compare(now, old, 1208120628821L);
122 "Verify amount of calls to sumRange is the same",
123 now.countSumRange, old.countSumRange
126 "Verify amount of calls to sumAll is the same",
127 now.countSumAll, old.countSumAll
130 "Verify amount of calls to sumTwo is the same",
131 now.countSumTwo, old.countSumTwo
134 // END: total.rewrite.tests
136 // BEGIN: total.rewrite.compare
137 private void compare (Arithmetica now, OldArithmetica1 old, long seed)
139 java.util.Random r = new java.util.Random (seed);
141 for (int loop = 0; loop < r.nextInt(5); loop++) {
142 int operation = r.nextInt(3);
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);
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);
157 int resNow = now.sumAll(arr);
158 int resOld = old.sumAll(arr);
159 assertEquals("sumArray results are equal", resNow, resOld);
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);
173 // END: total.rewrite.compare
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) {
183 public int sumAll(int... numbers) {
184 if (numbers.length == 0) {
187 int sum = numbers[0];
188 for (int i = 1; i < numbers.length; i++) {
189 sum = sumTwo(sum, numbers[i]);
194 public int sumRange(int from, int to) {
196 int[] array = new int[len + 1];
197 for (int i = 0; i <= len; i++) {
200 return sumAll(array);
203 // END: total.rewrite.oldimpl