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