jtulach@184: package api; jtulach@184: jtulach@184: import junit.framework.*; jtulach@184: import java.util.*; jtulach@184: jtulach@184: public class ArithmeticaCompatibilityTest extends TestCase { jtulach@184: public ArithmeticaCompatibilityTest(String name) { jtulach@184: super(name); jtulach@184: } jtulach@184: jtulach@184: private static final class CountingSubclass extends Arithmetica { jtulach@184: int countSumTwo; jtulach@184: int countSumAll; jtulach@184: int countSumRange; jtulach@184: jtulach@184: @Override jtulach@184: public int sumAll(int... numbers) { jtulach@184: countSumAll++; jtulach@184: return super.sumAll(numbers); jtulach@184: } jtulach@184: jtulach@184: @Override jtulach@184: public int sumRange(int from, int to) { jtulach@184: countSumRange++; jtulach@184: return super.sumRange(from, to); jtulach@184: } jtulach@184: jtulach@184: @Override jtulach@184: public int sumTwo(int one, int second) { jtulach@184: countSumTwo++; jtulach@184: return super.sumTwo(one, second); jtulach@184: } jtulach@184: } // end of CountingSubclass jtulach@184: jtulach@184: private static final class CountingOldSubclass extends OldArithmetica1 { jtulach@184: int countSumTwo; jtulach@184: int countSumAll; jtulach@184: int countSumRange; jtulach@184: jtulach@184: @Override jtulach@184: public int sumAll(int... numbers) { jtulach@184: countSumAll++; jtulach@184: return super.sumAll(numbers); jtulach@184: } jtulach@184: jtulach@184: @Override jtulach@184: public int sumRange(int from, int to) { jtulach@184: countSumRange++; jtulach@184: return super.sumRange(from, to); jtulach@184: } jtulach@184: jtulach@184: @Override jtulach@184: public int sumTwo(int one, int second) { jtulach@184: countSumTwo++; jtulach@184: return super.sumTwo(one, second); jtulach@184: } jtulach@184: } // end of CountingSubclass jtulach@184: jtulach@185: // BEGIN: total.rewrite.tests jtulach@184: public void testRandomCheck () throws Exception { jtulach@184: long seed = System.currentTimeMillis(); jtulach@184: try { jtulach@184: CountingSubclass now = new CountingSubclass(); jtulach@184: CountingOldSubclass old = new CountingOldSubclass(); jtulach@184: jtulach@184: compare(now, old, seed); jtulach@184: jtulach@188: assertEquals( jtulach@210: "Verify amount calls to of sumRange is the same", jtulach@188: now.countSumRange, old.countSumRange jtulach@188: ); jtulach@188: assertEquals( jtulach@210: "Verify amount calls to of sumAll is the same", jtulach@188: now.countSumAll, old.countSumAll jtulach@188: ); jtulach@188: assertEquals( jtulach@210: "Verify amount calls to of sumTwo is the same", jtulach@188: now.countSumTwo, old.countSumTwo jtulach@188: ); jtulach@184: } catch (AssertionFailedError ex) { jtulach@188: IllegalStateException n = new IllegalStateException ( jtulach@188: "Seed: " + seed + "\n" + ex.getMessage () jtulach@188: ); jtulach@184: n.initCause(ex); jtulach@184: throw n; jtulach@184: } catch (Exception ex) { jtulach@188: IllegalStateException n = new IllegalStateException ( jtulach@188: "Seed: " + seed + "\n" + ex.getMessage () jtulach@188: ); jtulach@184: n.initCause(ex); jtulach@184: throw n; jtulach@184: } jtulach@184: } jtulach@184: jtulach@184: public void testSimulateOKRunOn1208120436947() throws Exception { jtulach@184: CountingSubclass now = new CountingSubclass(); jtulach@184: CountingOldSubclass old = new CountingOldSubclass(); jtulach@184: jtulach@184: compare(now, old, 1208120436947L); jtulach@184: jtulach@188: assertEquals( jtulach@210: "Verify amount of calls to sumRange is the same", jtulach@188: now.countSumRange, old.countSumRange jtulach@188: ); jtulach@188: assertEquals( jtulach@210: "Verify amount of calls to sumAll is the same", jtulach@188: now.countSumAll, old.countSumAll jtulach@188: ); jtulach@188: assertEquals( jtulach@210: "Verify amount of calls to sumTwo is the same", jtulach@188: now.countSumTwo, old.countSumTwo jtulach@188: ); jtulach@184: } jtulach@184: jtulach@184: public void testSimulateFailureOn1208120628821() throws Exception { jtulach@184: CountingSubclass now = new CountingSubclass(); jtulach@184: CountingOldSubclass old = new CountingOldSubclass(); jtulach@184: jtulach@184: compare(now, old, 1208120628821L); jtulach@184: jtulach@188: assertEquals( jtulach@210: "Verify amount of calls to sumRange is the same", jtulach@188: now.countSumRange, old.countSumRange jtulach@188: ); jtulach@188: assertEquals( jtulach@210: "Verify amount of calls to sumAll is the same", jtulach@188: now.countSumAll, old.countSumAll jtulach@188: ); jtulach@188: assertEquals( jtulach@210: "Verify amount of calls to sumTwo is the same", jtulach@188: now.countSumTwo, old.countSumTwo jtulach@188: ); jtulach@184: } jtulach@185: // END: total.rewrite.tests jtulach@184: jtulach@185: // BEGIN: total.rewrite.compare jtulach@188: private void compare (Arithmetica now, OldArithmetica1 old, long seed) jtulach@188: throws Exception { jtulach@184: java.util.Random r = new java.util.Random (seed); jtulach@184: jtulach@184: for (int loop = 0; loop < r.nextInt(5); loop++) { jtulach@184: int operation = r.nextInt(3); jtulach@184: switch (operation) { jtulach@184: case 0: { // sumTwo jtulach@184: int a1 = r.nextInt(100); jtulach@184: int a2 = r.nextInt(100); jtulach@184: int resNow = now.sumTwo(a1, a2); jtulach@184: int resOld = old.sumTwo(a1, a2); jtulach@188: assertEquals("sumTwo results are equal", resNow, resOld); jtulach@184: break; jtulach@184: } jtulach@184: case 1: { // sumArray jtulach@184: int[] arr = new int[r.nextInt(100)]; jtulach@184: for (int i = 0; i < arr.length; i++) { jtulach@184: arr[i] = r.nextInt(100); jtulach@184: } jtulach@184: int resNow = now.sumAll(arr); jtulach@184: int resOld = old.sumAll(arr); jtulach@188: assertEquals("sumArray results are equal", resNow, resOld); jtulach@184: break; jtulach@184: } jtulach@184: case 2: { // sumRange jtulach@184: int a1 = r.nextInt(100); jtulach@184: int a2 = r.nextInt(100); jtulach@184: int resNow = now.sumRange(a1, a1 + a2); jtulach@184: int resOld = old.sumRange(a1, a1 + a2); jtulach@188: assertEquals("sumRange results are equal", resNow, resOld); jtulach@184: break; jtulach@184: } jtulach@184: } jtulach@184: } jtulach@184: } jtulach@185: // END: total.rewrite.compare jtulach@184: jtulach@184: jtulach@185: // BEGIN: total.rewrite.oldimpl jtulach@184: /** This is a copy of the implementation of Arithmetica from version 1.0 */ jtulach@184: static class OldArithmetica1 { jtulach@184: public int sumTwo(int one, int second) { jtulach@184: return one + second; jtulach@184: } jtulach@184: jtulach@184: public int sumAll(int... numbers) { jtulach@184: if (numbers.length == 0) { jtulach@184: return 0; jtulach@184: } jtulach@184: int sum = numbers[0]; jtulach@184: for (int i = 1; i < numbers.length; i++) { jtulach@184: sum = sumTwo(sum, numbers[i]); jtulach@184: } jtulach@184: return sum; jtulach@184: } jtulach@184: jtulach@184: public int sumRange(int from, int to) { jtulach@184: int len = to - from; jtulach@184: int[] array = new int[len + 1]; jtulach@184: for (int i = 0; i <= len; i++) { jtulach@184: array[i] = from + i; jtulach@184: } jtulach@184: return sumAll(array); jtulach@184: } jtulach@185: } jtulach@185: // END: total.rewrite.oldimpl jtulach@184: jtulach@184: }