review31: Providing randomized Arithmetica test to verify inconsistencies with version 1.0
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);
68 assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
69 assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
70 assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
71 } catch (AssertionFailedError ex) {
72 IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
75 } catch (Exception ex) {
76 IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
82 public void testSimulateOKRunOn1208120436947() throws Exception {
83 CountingSubclass now = new CountingSubclass();
84 CountingOldSubclass old = new CountingOldSubclass();
86 compare(now, old, 1208120436947L);
88 assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
89 assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
90 assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
93 public void testSimulateFailureOn1208120628821() throws Exception {
94 CountingSubclass now = new CountingSubclass();
95 CountingOldSubclass old = new CountingOldSubclass();
97 compare(now, old, 1208120628821L);
99 assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
100 assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
101 assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
103 // END: total.rewrite.tests
105 // BEGIN: total.rewrite.compare
106 private void compare (Arithmetica now, OldArithmetica1 old, long seed) throws Exception {
107 java.util.Random r = new java.util.Random (seed);
109 for (int loop = 0; loop < r.nextInt(5); loop++) {
110 int operation = r.nextInt(3);
113 int a1 = r.nextInt(100);
114 int a2 = r.nextInt(100);
115 int resNow = now.sumTwo(a1, a2);
116 int resOld = old.sumTwo(a1, a2);
117 assertEquals("Results of sumTwo are equal", resNow, resOld);
120 case 1: { // sumArray
121 int[] arr = new int[r.nextInt(100)];
122 for (int i = 0; i < arr.length; i++) {
123 arr[i] = r.nextInt(100);
125 int resNow = now.sumAll(arr);
126 int resOld = old.sumAll(arr);
127 assertEquals("Results of sumArray are equal", resNow, resOld);
130 case 2: { // sumRange
131 int a1 = r.nextInt(100);
132 int a2 = r.nextInt(100);
133 int resNow = now.sumRange(a1, a1 + a2);
134 int resOld = old.sumRange(a1, a1 + a2);
135 assertEquals("Results of sumRange are equal", resNow, resOld);
141 // END: total.rewrite.compare
144 // BEGIN: total.rewrite.oldimpl
145 /** This is a copy of the implementation of Arithmetica from version 1.0 */
146 static class OldArithmetica1 {
147 public int sumTwo(int one, int second) {
151 public int sumAll(int... numbers) {
152 if (numbers.length == 0) {
155 int sum = numbers[0];
156 for (int i = 1; i < numbers.length; i++) {
157 sum = sumTwo(sum, numbers[i]);
162 public int sumRange(int from, int to) {
164 int[] array = new int[len + 1];
165 for (int i = 0; i <= len; i++) {
168 return sumAll(array);
171 // END: total.rewrite.oldimpl