# HG changeset patch # User Jaroslav Tulach # Date 1213430681 -7200 # Node ID b1f279a7ec467da829dbad835dfee8bd5bdc07d0 # Parent 6ad6f416269162940f69ca66f8874a4f90d210c8 Making sure the code survives negative inputs diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-api1.0/api/Arithmetica.java --- a/samples/composition/src-api1.0/api/Arithmetica.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-api1.0/api/Arithmetica.java Sat Jun 14 10:04:41 2008 +0200 @@ -24,6 +24,10 @@ public int sumRange(int from, int to) { int len = to - from; + if (len < 0) { + len = -len; + from = to; + } int[] array = new int[len + 1]; for (int i = 0; i <= len; i++) { array[i] = from + i; diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-api2.0-compat/api/Arithmetica.java --- a/samples/composition/src-api2.0-compat/api/Arithmetica.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-api2.0-compat/api/Arithmetica.java Sat Jun 14 10:04:41 2008 +0200 @@ -43,6 +43,10 @@ private int sumRange1(int from, int to) { int len = to - from; + if (len < 0) { + len = -len; + from = to; + } int[] array = new int[len + 1]; for (int i = 0; i <= len; i++) { array[i] = from + i; @@ -51,7 +55,7 @@ } private int sumRange2(int from, int to) { - return (from + to) * (to - from + 1) / 2; + return (from + to) * (Math.abs(to - from) + 1) / 2; } } // END: design.composition.arith2.0.compat diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-api2.0-enum/api/Arithmetica.java --- a/samples/composition/src-api2.0-enum/api/Arithmetica.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-api2.0-enum/api/Arithmetica.java Sat Jun 14 10:04:41 2008 +0200 @@ -49,6 +49,10 @@ private int sumRange1(int from, int to) { int len = to - from; + if (len < 0) { + len = -len; + from = to; + } int[] array = new int[len + 1]; for (int i = 0; i <= len; i++) { array[i] = from + i; @@ -57,6 +61,6 @@ } private int sumRange2(int from, int to) { - return (from + to) * (to - from + 1) / 2; + return (from + to) * (Math.abs(to - from) + 1) / 2; } } diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-api2.0-property/api/Arithmetica.java --- a/samples/composition/src-api2.0-property/api/Arithmetica.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-api2.0-property/api/Arithmetica.java Sat Jun 14 10:04:41 2008 +0200 @@ -36,6 +36,10 @@ private int sumRange1(int from, int to) { int len = to - from; + if (len < 0) { + len = -len; + from = to; + } int[] array = new int[len + 1]; for (int i = 0; i <= len; i++) { array[i] = from + i; @@ -44,7 +48,7 @@ } private int sumRange2(int from, int to) { - return (from + to) * (to - from + 1) / 2; + return (from + to) * (Math.abs(to - from) + 1) / 2; } } // END: design.composition.arith2.0.property diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-api2.0-runtime/api/Arithmetica.java --- a/samples/composition/src-api2.0-runtime/api/Arithmetica.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-api2.0-runtime/api/Arithmetica.java Sat Jun 14 10:04:41 2008 +0200 @@ -35,6 +35,10 @@ private int sumRange1(int from, int to) { int len = to - from; + if (len < 0) { + len = -len; + from = to; + } int[] array = new int[len + 1]; for (int i = 0; i <= len; i++) { array[i] = from + i; @@ -43,7 +47,7 @@ } private int sumRange2(int from, int to) { - return (from + to) * (to - from + 1) / 2; + return (from + to) * (Math.abs(to - from) + 1) / 2; } private static boolean calledByV2AwareLoader() { diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-api2.0/api/Arithmetica.java --- a/samples/composition/src-api2.0/api/Arithmetica.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-api2.0/api/Arithmetica.java Sat Jun 14 10:04:41 2008 +0200 @@ -24,7 +24,7 @@ // BEGIN: design.composition.arith2.0 public int sumRange(int from, int to) { - return (from + to) * (to - from + 1) / 2; + return (from + to) * (Math.abs(to - from) + 1) / 2; } // END: design.composition.arith2.0 } diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-test/api/ArithmeticaCompatibilityTest.java --- a/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java Sat Jun 14 10:04:41 2008 +0200 @@ -66,15 +66,15 @@ compare(now, old, seed); assertEquals( - "Verify amount of sumRange is the same", + "Verify amount calls to of sumRange is the same", now.countSumRange, old.countSumRange ); assertEquals( - "Verify amount of sumAll is the same", + "Verify amount calls to of sumAll is the same", now.countSumAll, old.countSumAll ); assertEquals( - "Verify amount of sumTwo is the same", + "Verify amount calls to of sumTwo is the same", now.countSumTwo, old.countSumTwo ); } catch (AssertionFailedError ex) { @@ -99,15 +99,15 @@ compare(now, old, 1208120436947L); assertEquals( - "Verify amount of sumRange is the same", + "Verify amount of calls to sumRange is the same", now.countSumRange, old.countSumRange ); assertEquals( - "Verify amount of sumAll is the same", + "Verify amount of calls to sumAll is the same", now.countSumAll, old.countSumAll ); assertEquals( - "Verify amount of sumTwo is the same", + "Verify amount of calls to sumTwo is the same", now.countSumTwo, old.countSumTwo ); } @@ -119,15 +119,15 @@ compare(now, old, 1208120628821L); assertEquals( - "Verify amount of sumRange is the same", + "Verify amount of calls to sumRange is the same", now.countSumRange, old.countSumRange ); assertEquals( - "Verify amount of sumAll is the same", + "Verify amount of calls to sumAll is the same", now.countSumAll, old.countSumAll ); assertEquals( - "Verify amount of sumTwo is the same", + "Verify amount of calls to sumTwo is the same", now.countSumTwo, old.countSumTwo ); } diff -r 6ad6f4162691 -r b1f279a7ec46 samples/composition/src-test/api/ArithmeticaTest.java --- a/samples/composition/src-test/api/ArithmeticaTest.java Sat Jun 14 10:04:36 2008 +0200 +++ b/samples/composition/src-test/api/ArithmeticaTest.java Sat Jun 14 10:04:41 2008 +0200 @@ -35,8 +35,10 @@ public void testSumRange() { Arithmetica instance = new Arithmetica(); - assertEquals("+", 6, instance.sumRange(1, 3)); - assertEquals("10", 55, instance.sumRange(1, 10)); + assertEquals("1+2+3=6", 6, instance.sumRange(1, 3)); + assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10)); + assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1)); + assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1)); } //END: design.composition.arith.test