Enhancing the example to contain randomized that to verify consistency with previous implementation
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 public void testRandomCheck () throws Exception {
60 long seed = System.currentTimeMillis();
62 CountingSubclass now = new CountingSubclass();
63 CountingOldSubclass old = new CountingOldSubclass();
65 compare(now, old, seed);
67 assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
68 assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
69 assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
70 } catch (AssertionFailedError ex) {
71 IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
74 } catch (Exception ex) {
75 IllegalStateException n = new IllegalStateException ("Seed: " + seed + "\n" + ex.getMessage ());
81 public void testSimulateOKRunOn1208120436947() throws Exception {
82 CountingSubclass now = new CountingSubclass();
83 CountingOldSubclass old = new CountingOldSubclass();
85 compare(now, old, 1208120436947L);
87 assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
88 assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
89 assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
92 public void testSimulateFailureOn1208120628821() throws Exception {
93 CountingSubclass now = new CountingSubclass();
94 CountingOldSubclass old = new CountingOldSubclass();
96 compare(now, old, 1208120628821L);
98 assertEquals("Verify amount of sumRange is the same", now.countSumRange, old.countSumRange);
99 assertEquals("Verify amount of sumAll is the same", now.countSumAll, old.countSumAll);
100 assertEquals("Verify amount of sumTwo is the same", now.countSumTwo, old.countSumTwo);
103 private void compare (CountingSubclass now, CountingOldSubclass old, long seed) throws Exception {
104 java.util.Random r = new java.util.Random (seed);
106 for (int loop = 0; loop < r.nextInt(5); loop++) {
107 int operation = r.nextInt(3);
110 int a1 = r.nextInt(100);
111 int a2 = r.nextInt(100);
112 int resNow = now.sumTwo(a1, a2);
113 int resOld = old.sumTwo(a1, a2);
114 assertEquals("Results of sumTwo are equal", resNow, resOld);
117 case 1: { // sumArray
118 int[] arr = new int[r.nextInt(100)];
119 for (int i = 0; i < arr.length; i++) {
120 arr[i] = r.nextInt(100);
122 int resNow = now.sumAll(arr);
123 int resOld = old.sumAll(arr);
124 assertEquals("Results of sumArray are equal", resNow, resOld);
127 case 2: { // sumRange
128 int a1 = r.nextInt(100);
129 int a2 = r.nextInt(100);
130 int resNow = now.sumRange(a1, a1 + a2);
131 int resOld = old.sumRange(a1, a1 + a2);
132 assertEquals("Results of sumRange are equal", resNow, resOld);
141 /** This is a copy of the implementation of Arithmetica from version 1.0 */
142 static class OldArithmetica1 {
143 public int sumTwo(int one, int second) {
147 public int sumAll(int... numbers) {
148 if (numbers.length == 0) {
151 int sum = numbers[0];
152 for (int i = 1; i < numbers.length; i++) {
153 sum = sumTwo(sum, numbers[i]);
158 public int sumRange(int from, int to) {
160 int[] array = new int[len + 1];
161 for (int i = 0; i <= len; i++) {
164 return sumAll(array);
166 } // end of OldArithmetica1