Making sure the code survives negative inputs
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:41 +0200
changeset 206b1f279a7ec46
parent 205 6ad6f4162691
child 207 675393d7093a
Making sure the code survives negative inputs
samples/composition/src-api1.0/api/Arithmetica.java
samples/composition/src-api2.0-compat/api/Arithmetica.java
samples/composition/src-api2.0-enum/api/Arithmetica.java
samples/composition/src-api2.0-property/api/Arithmetica.java
samples/composition/src-api2.0-runtime/api/Arithmetica.java
samples/composition/src-api2.0/api/Arithmetica.java
samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
samples/composition/src-test/api/ArithmeticaTest.java
     1.1 --- a/samples/composition/src-api1.0/api/Arithmetica.java	Sat Jun 14 10:04:36 2008 +0200
     1.2 +++ b/samples/composition/src-api1.0/api/Arithmetica.java	Sat Jun 14 10:04:41 2008 +0200
     1.3 @@ -24,6 +24,10 @@
     1.4      
     1.5      public int sumRange(int from, int to) {
     1.6          int len = to - from;
     1.7 +        if (len < 0) {
     1.8 +            len = -len;
     1.9 +            from = to;
    1.10 +        }
    1.11          int[] array = new int[len + 1];
    1.12          for (int i = 0; i <= len; i++) {
    1.13              array[i] = from + i;
     2.1 --- a/samples/composition/src-api2.0-compat/api/Arithmetica.java	Sat Jun 14 10:04:36 2008 +0200
     2.2 +++ b/samples/composition/src-api2.0-compat/api/Arithmetica.java	Sat Jun 14 10:04:41 2008 +0200
     2.3 @@ -43,6 +43,10 @@
     2.4  
     2.5      private int sumRange1(int from, int to) {
     2.6          int len = to - from;
     2.7 +        if (len < 0) {
     2.8 +            len = -len;
     2.9 +            from = to;
    2.10 +        }
    2.11          int[] array = new int[len + 1];
    2.12          for (int i = 0; i <= len; i++) {
    2.13              array[i] = from + i;
    2.14 @@ -51,7 +55,7 @@
    2.15      }
    2.16      
    2.17      private int sumRange2(int from, int to) {
    2.18 -        return (from + to) * (to - from + 1) / 2;
    2.19 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    2.20      }
    2.21  }
    2.22  // END: design.composition.arith2.0.compat
     3.1 --- a/samples/composition/src-api2.0-enum/api/Arithmetica.java	Sat Jun 14 10:04:36 2008 +0200
     3.2 +++ b/samples/composition/src-api2.0-enum/api/Arithmetica.java	Sat Jun 14 10:04:41 2008 +0200
     3.3 @@ -49,6 +49,10 @@
     3.4  
     3.5      private int sumRange1(int from, int to) {
     3.6          int len = to - from;
     3.7 +        if (len < 0) {
     3.8 +            len = -len;
     3.9 +            from = to;
    3.10 +        }
    3.11          int[] array = new int[len + 1];
    3.12          for (int i = 0; i <= len; i++) {
    3.13              array[i] = from + i;
    3.14 @@ -57,6 +61,6 @@
    3.15      }
    3.16      
    3.17      private int sumRange2(int from, int to) {
    3.18 -        return (from + to) * (to - from + 1) / 2;
    3.19 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    3.20      }
    3.21  }
     4.1 --- a/samples/composition/src-api2.0-property/api/Arithmetica.java	Sat Jun 14 10:04:36 2008 +0200
     4.2 +++ b/samples/composition/src-api2.0-property/api/Arithmetica.java	Sat Jun 14 10:04:41 2008 +0200
     4.3 @@ -36,6 +36,10 @@
     4.4  
     4.5      private int sumRange1(int from, int to) {
     4.6          int len = to - from;
     4.7 +        if (len < 0) {
     4.8 +            len = -len;
     4.9 +            from = to;
    4.10 +        }
    4.11          int[] array = new int[len + 1];
    4.12          for (int i = 0; i <= len; i++) {
    4.13              array[i] = from + i;
    4.14 @@ -44,7 +48,7 @@
    4.15      }
    4.16      
    4.17      private int sumRange2(int from, int to) {
    4.18 -        return (from + to) * (to - from + 1) / 2;
    4.19 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    4.20      }
    4.21  }
    4.22  // END: design.composition.arith2.0.property
     5.1 --- a/samples/composition/src-api2.0-runtime/api/Arithmetica.java	Sat Jun 14 10:04:36 2008 +0200
     5.2 +++ b/samples/composition/src-api2.0-runtime/api/Arithmetica.java	Sat Jun 14 10:04:41 2008 +0200
     5.3 @@ -35,6 +35,10 @@
     5.4  
     5.5      private int sumRange1(int from, int to) {
     5.6          int len = to - from;
     5.7 +        if (len < 0) {
     5.8 +            len = -len;
     5.9 +            from = to;
    5.10 +        }
    5.11          int[] array = new int[len + 1];
    5.12          for (int i = 0; i <= len; i++) {
    5.13              array[i] = from + i;
    5.14 @@ -43,7 +47,7 @@
    5.15      }
    5.16      
    5.17      private int sumRange2(int from, int to) {
    5.18 -        return (from + to) * (to - from + 1) / 2;
    5.19 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    5.20      }
    5.21  
    5.22      private static boolean calledByV2AwareLoader() {
     6.1 --- a/samples/composition/src-api2.0/api/Arithmetica.java	Sat Jun 14 10:04:36 2008 +0200
     6.2 +++ b/samples/composition/src-api2.0/api/Arithmetica.java	Sat Jun 14 10:04:41 2008 +0200
     6.3 @@ -24,7 +24,7 @@
     6.4      
     6.5  // BEGIN: design.composition.arith2.0
     6.6      public int sumRange(int from, int to) {
     6.7 -        return (from + to) * (to - from + 1) / 2;
     6.8 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
     6.9      }
    6.10  // END: design.composition.arith2.0
    6.11  }
     7.1 --- a/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java	Sat Jun 14 10:04:36 2008 +0200
     7.2 +++ b/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java	Sat Jun 14 10:04:41 2008 +0200
     7.3 @@ -66,15 +66,15 @@
     7.4              compare(now, old, seed);
     7.5          
     7.6              assertEquals(
     7.7 -                "Verify amount of sumRange is the same", 
     7.8 +                "Verify amount calls to of sumRange is the same", 
     7.9                  now.countSumRange, old.countSumRange
    7.10              );
    7.11              assertEquals(
    7.12 -                "Verify amount of sumAll is the same", 
    7.13 +                "Verify amount calls to of sumAll is the same", 
    7.14                  now.countSumAll, old.countSumAll
    7.15              );
    7.16              assertEquals(
    7.17 -                "Verify amount of sumTwo is the same", 
    7.18 +                "Verify amount calls to of sumTwo is the same", 
    7.19                  now.countSumTwo, old.countSumTwo
    7.20              );
    7.21          } catch (AssertionFailedError ex) {
    7.22 @@ -99,15 +99,15 @@
    7.23          compare(now, old, 1208120436947L);
    7.24  
    7.25          assertEquals(
    7.26 -            "Verify amount of sumRange is the same", 
    7.27 +            "Verify amount of calls to sumRange is the same", 
    7.28              now.countSumRange, old.countSumRange
    7.29          );
    7.30          assertEquals(
    7.31 -            "Verify amount of sumAll is the same", 
    7.32 +            "Verify amount of calls to sumAll is the same", 
    7.33              now.countSumAll, old.countSumAll
    7.34          );
    7.35          assertEquals(
    7.36 -            "Verify amount of sumTwo is the same", 
    7.37 +            "Verify amount of calls to sumTwo is the same", 
    7.38              now.countSumTwo, old.countSumTwo
    7.39          );
    7.40      }
    7.41 @@ -119,15 +119,15 @@
    7.42          compare(now, old, 1208120628821L);
    7.43  
    7.44          assertEquals(
    7.45 -            "Verify amount of sumRange is the same", 
    7.46 +            "Verify amount of calls to sumRange is the same", 
    7.47              now.countSumRange, old.countSumRange
    7.48          );
    7.49          assertEquals(
    7.50 -            "Verify amount of sumAll is the same", 
    7.51 +            "Verify amount of calls to sumAll is the same", 
    7.52              now.countSumAll, old.countSumAll
    7.53          );
    7.54          assertEquals(
    7.55 -            "Verify amount of sumTwo is the same", 
    7.56 +            "Verify amount of calls to sumTwo is the same", 
    7.57              now.countSumTwo, old.countSumTwo
    7.58          );
    7.59      }
     8.1 --- a/samples/composition/src-test/api/ArithmeticaTest.java	Sat Jun 14 10:04:36 2008 +0200
     8.2 +++ b/samples/composition/src-test/api/ArithmeticaTest.java	Sat Jun 14 10:04:41 2008 +0200
     8.3 @@ -35,8 +35,10 @@
     8.4  
     8.5      public void testSumRange() {
     8.6          Arithmetica instance = new Arithmetica();
     8.7 -        assertEquals("+", 6, instance.sumRange(1, 3));
     8.8 -        assertEquals("10", 55, instance.sumRange(1, 10));
     8.9 +        assertEquals("1+2+3=6", 6, instance.sumRange(1, 3));
    8.10 +        assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10));
    8.11 +        assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1));
    8.12 +        assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1));
    8.13      }
    8.14      //END: design.composition.arith.test
    8.15