commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/math Fraction.java
Date Mon, 04 Aug 2003 02:01:53 GMT
scolebourne    2003/08/03 19:01:53

  Modified:    lang/src/test/org/apache/commons/lang/math IntRangeTest.java
                        FloatRangeTest.java FractionTest.java
                        DoubleRangeTest.java AbstractRangeTest.java
                        NumberUtilsTest.java
               lang/src/java/org/apache/commons/lang/math Fraction.java
  Log:
  Extra tests suggested by Clover
  
  Revision  Changes    Path
  1.4       +7 -1      jakarta-commons/lang/src/test/org/apache/commons/lang/math/IntRangeTest.java
  
  Index: IntRangeTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/IntRangeTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- IntRangeTest.java	4 Aug 2003 01:14:02 -0000	1.3
  +++ IntRangeTest.java	4 Aug 2003 02:01:53 -0000	1.4
  @@ -131,6 +131,12 @@
           assertSame(five, nr.getMinimumNumber());
           assertEquals(ten, nr.getMaximumNumber());
           
  +        // test non Integer, for full coverage
  +        Long fiveL = new Long(5L);
  +        Long tenL = new Long(10L);
  +        assertEquals(five, nr.getMinimumNumber());
  +        assertEquals(ten, nr.getMaximumNumber());
  +        
           // not null
           try {
               new IntRange(five, null);
  
  
  
  1.4       +4 -2      jakarta-commons/lang/src/test/org/apache/commons/lang/math/FloatRangeTest.java
  
  Index: FloatRangeTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/FloatRangeTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FloatRangeTest.java	4 Aug 2003 01:14:02 -0000	1.3
  +++ FloatRangeTest.java	4 Aug 2003 02:01:53 -0000	1.4
  @@ -197,7 +197,9 @@
       }
   
       public void testToString() {
  -        assertEquals("Range[10.0,20.0]", tenToTwenty.toString());
  +        String str = tenToTwenty.toString();
  +        assertEquals("Range[10.0,20.0]", str);
  +        assertSame(str, tenToTwenty.toString());
           assertEquals("Range[-20.0,-10.0]", createRange(new Integer(-20), new Integer(-10)).toString());
       }
       
  
  
  
  1.3       +19 -3     jakarta-commons/lang/src/test/org/apache/commons/lang/math/FractionTest.java
  
  Index: FractionTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/FractionTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FractionTest.java	22 Dec 2002 21:18:51 -0000	1.2
  +++ FractionTest.java	4 Aug 2003 02:01:53 -0000	1.3
  @@ -703,6 +703,18 @@
           assertEquals(7, f.getNumerator());
           assertEquals(5, f.getDenominator());
           
  +        f1 = Fraction.getFraction(0, 5);
  +        f2 = Fraction.getFraction(4, 5);
  +        f = f1.subtract(f2);
  +        assertEquals(-4, f.getNumerator());
  +        assertEquals(5, f.getDenominator());
  +        
  +        f1 = Fraction.getFraction(0, 5);
  +        f2 = Fraction.getFraction(-4, 5);
  +        f = f1.subtract(f2);
  +        assertEquals(4, f.getNumerator());
  +        assertEquals(5, f.getDenominator());
  +        
           f1 = Fraction.getFraction(3, 5);
           f2 = Fraction.getFraction(1, 2);
           f = f1.subtract(f2);
  @@ -845,7 +857,9 @@
           Fraction f = null;
   
           f = Fraction.getFraction(3, 5);
  -        assertEquals("3/5", f.toString());
  +        String str = f.toString();
  +        assertEquals("3/5", str);
  +        assertSame(str, f.toString());
           
           f = Fraction.getFraction(7, 5);
           assertEquals("7/5", f.toString());        
  @@ -864,7 +878,9 @@
           Fraction f = null;
   
           f = Fraction.getFraction(3, 5);
  -        assertEquals("3/5", f.toProperString());        
  +        String str = f.toProperString();
  +        assertEquals("3/5", str);
  +        assertSame(str, f.toProperString());
           
           f = Fraction.getFraction(7, 5);
           assertEquals("1 2/5", f.toProperString());        
  
  
  
  1.4       +4 -2      jakarta-commons/lang/src/test/org/apache/commons/lang/math/DoubleRangeTest.java
  
  Index: DoubleRangeTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/DoubleRangeTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DoubleRangeTest.java	4 Aug 2003 01:14:02 -0000	1.3
  +++ DoubleRangeTest.java	4 Aug 2003 02:01:53 -0000	1.4
  @@ -197,7 +197,9 @@
       }
   
       public void testToString() {
  -        assertEquals("Range[10.0,20.0]", tenToTwenty.toString());
  +        String str = tenToTwenty.toString();
  +        assertEquals("Range[10.0,20.0]", str);
  +        assertSame(str, tenToTwenty.toString());
           assertEquals("Range[-20.0,-10.0]", createRange(new Integer(-20), new Integer(-10)).toString());
       }
       
  
  
  
  1.4       +7 -2      jakarta-commons/lang/src/test/org/apache/commons/lang/math/AbstractRangeTest.java
  
  Index: AbstractRangeTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/AbstractRangeTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractRangeTest.java	4 Aug 2003 01:14:02 -0000	1.3
  +++ AbstractRangeTest.java	4 Aug 2003 02:01:53 -0000	1.4
  @@ -296,6 +296,7 @@
       //--------------------------------------------------------------------------
   
       public void testContainsRange() {
  +        assertEquals(false, tenToTwenty.containsRange(null));
           assertEquals(false, tenToTwenty.containsRange(createRange(five, five)));
           assertEquals(false, tenToTwenty.containsRange(createRange(five, ten)));
           assertEquals(false, tenToTwenty.containsRange(createRange(five, twelve)));
  @@ -325,6 +326,7 @@
       }
   
       public void testOverlapsRange() {
  +        assertEquals(false, tenToTwenty.overlapsRange(null));
           assertEquals(false, tenToTwenty.overlapsRange(createRange(five, five)));
           assertEquals(true, tenToTwenty.overlapsRange(createRange(five, ten)));
           assertEquals(true, tenToTwenty.overlapsRange(createRange(five, twelve)));
  @@ -365,6 +367,7 @@
           assertEquals(false, tenToTwenty.equals(createRange(five, ten)));
           assertEquals(false, tenToTwenty.equals(createRange(ten)));
   
  +        assertEquals(true, tenToTwenty.equals(tenToTwenty));
           assertEquals(true, tenToTwenty.equals(createRange(ten, twenty)));
           assertEquals(true, tenToTwenty.equals(createRange(twenty, ten)));
           
  @@ -379,7 +382,9 @@
       }
       
       public void testToString() {
  -        assertEquals("Range[10,20]", tenToTwenty.toString());
  +        String str = tenToTwenty.toString();
  +        assertEquals("Range[10,20]", str);
  +        assertSame(str, tenToTwenty.toString());
           assertEquals("Range[-20,-10]", createRange(new Integer(-20), new Integer(-10)).toString());
       }
   
  
  
  
  1.6       +93 -29    jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
  
  Index: NumberUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- NumberUtilsTest.java	26 Jul 2003 19:12:30 -0000	1.5
  +++ NumberUtilsTest.java	4 Aug 2003 02:01:53 -0000	1.6
  @@ -53,6 +53,8 @@
    */
   package org.apache.commons.lang.math;
   
  +import java.lang.reflect.Constructor;
  +import java.lang.reflect.Modifier;
   import java.math.BigDecimal;
   import java.math.BigInteger;
   
  @@ -91,6 +93,16 @@
           return suite;
       }
   
  +    //-----------------------------------------------------------------------
  +    public void testConstructor() {
  +        assertNotNull(new NumberUtils());
  +        Constructor[] cons = NumberUtils.class.getDeclaredConstructors();
  +        assertEquals(1, cons.length);
  +        assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
  +        assertEquals(true, Modifier.isPublic(NumberUtils.class.getModifiers()));
  +        assertEquals(false, Modifier.isFinal(NumberUtils.class.getModifiers()));
  +    }
  +    
       //---------------------------------------------------------------------
   
       /**
  @@ -286,10 +298,8 @@
               6,
               NumberUtils.min(new long[] { 6, 9 }));
   
  -        assertEquals(
  -            "min(long[]) failed for array length 5",
  -            -10,
  -            NumberUtils.min(new long[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(-10, NumberUtils.min(new long[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(-10, NumberUtils.min(new long[] { -5, 0, -10, 5, 10 }));
       }
   
       public void testMinInt() {
  @@ -314,10 +324,8 @@
               6,
               NumberUtils.min(new int[] { 6, 9 }));
   
  -        assertEquals(
  -            "min(int[]) failed for array length 5",
  -            -10,
  -            NumberUtils.min(new int[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(-10, NumberUtils.min(new int[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(-10, NumberUtils.min(new int[] { -5, 0, -10, 5, 10 }));
       }
   
       public void testMinShort() {
  @@ -342,10 +350,8 @@
               6,
               NumberUtils.min(new short[] { 6, 9 }));
   
  -        assertEquals(
  -            "min(short[]) failed for array length 5",
  -            -10,
  -            NumberUtils.min(new short[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(-10, NumberUtils.min(new short[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(-10, NumberUtils.min(new short[] { -5, 0, -10, 5, 10 }));
       }
   
       public void testMinDouble() {
  @@ -377,6 +383,8 @@
               -10.45,
               NumberUtils.min(new double[] { -10.45, -5.56, 0, 5.67, 10.78 }),
               0);
  +        assertEquals(-10, NumberUtils.min(new double[] { -10, -5, 0, 5, 10 }), 0.0001);
  +        assertEquals(-10, NumberUtils.min(new double[] { -5, 0, -10, 5, 10 }), 0.0001);
       }
   
       public void testMinFloat() {
  @@ -408,6 +416,8 @@
               -10.6f,
               NumberUtils.min(new float[] { -10.6f, -5.5f, 0, 5.4f, 10.3f }),
               0);
  +        assertEquals(-10, NumberUtils.min(new float[] { -10, -5, 0, 5, 10 }), 0.0001f);
  +        assertEquals(-10, NumberUtils.min(new float[] { -5, 0, -10, 5, 10 }), 0.0001f);
       }
   
       public void testMaxLong() {
  @@ -436,6 +446,8 @@
               "max(long[]) failed for array length 5",
               10,
               NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(10, NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(10, NumberUtils.max(new long[] { -5, 0, 10, 5, -10 }));
       }
   
       public void testMaxInt() {
  @@ -464,6 +476,8 @@
               "max(int[]) failed for array length 5",
               10,
               NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(10, NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(10, NumberUtils.max(new int[] { -5, 0, 10, 5, -10 }));
       }
   
       public void testMaxShort() {
  @@ -492,6 +506,8 @@
               "max(short[]) failed for array length 5",
               10,
               NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(10, NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
  +        assertEquals(10, NumberUtils.max(new short[] { -5, 0, 10, 5, -10 }));
       }
   
       public void testMaxDouble() {
  @@ -523,6 +539,8 @@
               10.4f,
               NumberUtils.max(new double[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
               0);
  +        assertEquals(10, NumberUtils.max(new double[] { -10, -5, 0, 5, 10 }), 0.0001);
  +        assertEquals(10, NumberUtils.max(new double[] { -5, 0, 10, 5, -10 }), 0.0001);
       }
    
       public void testMaxFloat() {
  @@ -554,6 +572,8 @@
               10.4f,
               NumberUtils.max(new float[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
               0);
  +        assertEquals(10, NumberUtils.max(new float[] { -10, -5, 0, 5, 10 }), 0.0001f);
  +        assertEquals(10, NumberUtils.max(new float[] { -5, 0, 10, 5, -10 }), 0.0001f);
       }
   
       public void testMinimumLong() {
  @@ -576,20 +596,42 @@
           short low = 1234;
           short mid = 1234 + 1;
           short high = 1234 + 2;
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, high));
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, low, high));
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, high, low));
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, low));
  +        assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(low, mid,
high));
  +        assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(mid, low,
high));
  +        assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(mid, high,
low));
  +        assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(low, mid,
low));
       }
   
       public void testMinimumByte() {
           byte low = 123;
           byte mid = 123 + 1;
           byte high = 123 + 2;
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, high));
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, low, high));
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, high, low));
  -        assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, low));
  +        assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(low, mid,
high));
  +        assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(mid, low,
high));
  +        assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(mid, high,
low));
  +        assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(low, mid,
low));
  +    }
  +
  +    public void testMinimumDouble() {
  +        double low = 12.3;
  +        double mid = 12.3 + 1;
  +        double high = 12.3 + 2;
  +        assertEquals(low, NumberUtils.min(low, mid, high), 0.0001);
  +        assertEquals(low, NumberUtils.min(mid, low, high), 0.0001);
  +        assertEquals(low, NumberUtils.min(mid, high, low), 0.0001);
  +        assertEquals(low, NumberUtils.min(low, mid, low), 0.0001);
  +        assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001);
  +    }
  +
  +    public void testMinimumFloat() {
  +        float low = 12.3f;
  +        float mid = 12.3f + 1;
  +        float high = 12.3f + 2;
  +        assertEquals(low, NumberUtils.min(low, mid, high), 0.0001f);
  +        assertEquals(low, NumberUtils.min(mid, low, high), 0.0001f);
  +        assertEquals(low, NumberUtils.min(mid, high, low), 0.0001f);
  +        assertEquals(low, NumberUtils.min(low, mid, low), 0.0001f);
  +        assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001f);
       }
   
       public void testMaximumLong() {
  @@ -612,20 +654,42 @@
           short low = 1234;
           short mid = 1234 + 1;
           short high = 1234 + 2;
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(low, mid, high));
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, low, high));
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, high,
low));
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(high, mid,
high));
  +        assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(low,
mid, high));
  +        assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(mid,
low, high));
  +        assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(mid,
high, low));
  +        assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(high,
mid, high));
       }
   
       public void testMaximumByte() {
           byte low = 123;
           byte mid = 123 + 1;
           byte high = 123 + 2;
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(low, mid, high));
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, low, high));
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, high,
low));
  -        assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(high, mid,
high));
  +        assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(low, mid,
high));
  +        assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(mid, low,
high));
  +        assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(mid, high,
low));
  +        assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(high, mid,
high));
  +    }
  +
  +    public void testMaximumDouble() {
  +        double low = 12.3;
  +        double mid = 12.3 + 1;
  +        double high = 12.3 + 2;
  +        assertEquals(high, NumberUtils.max(low, mid, high), 0.0001);
  +        assertEquals(high, NumberUtils.max(mid, low, high), 0.0001);
  +        assertEquals(high, NumberUtils.max(mid, high, low), 0.0001);
  +        assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001);
  +        assertEquals(high, NumberUtils.max(high, mid, high), 0.0001);
  +    }
  +
  +    public void testMaximumFloat() {
  +        float low = 12.3f;
  +        float mid = 12.3f + 1;
  +        float high = 12.3f + 2;
  +        assertEquals(high, NumberUtils.max(low, mid, high), 0.0001f);
  +        assertEquals(high, NumberUtils.max(mid, low, high), 0.0001f);
  +        assertEquals(high, NumberUtils.max(mid, high, low), 0.0001f);
  +        assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001f);
  +        assertEquals(high, NumberUtils.max(high, mid, high), 0.0001f);
       }
   
       public void testCompareDouble() {
  
  
  
  1.8       +2 -8      jakarta-commons/lang/src/java/org/apache/commons/lang/math/Fraction.java
  
  Index: Fraction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/Fraction.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Fraction.java	4 Aug 2003 01:20:47 -0000	1.7
  +++ Fraction.java	4 Aug 2003 02:01:53 -0000	1.8
  @@ -200,9 +200,6 @@
               denominator = -denominator;
           }
           int gcd = greatestCommonDenominator(Math.abs(numerator), denominator);
  -        if (gcd == 0) {
  -            return new Fraction(numerator, denominator);
  -        }
           return new Fraction(numerator / gcd, denominator / gcd);
       }
   
  @@ -434,9 +431,6 @@
        */
       public Fraction reduce() {
           int gcd = greatestCommonDenominator(Math.abs(numerator), denominator);
  -        if (gcd == 0) {
  -            return this;
  -        }
           return Fraction.getFraction(numerator / gcd, denominator / gcd);
       }
   
  @@ -507,7 +501,7 @@
        *
        * @param number1  a positive number
        * @param number2  a positive number
  -     * @return the greatest common denominator
  +     * @return the greatest common denominator, never zero
        */
       private static int greatestCommonDenominator(int number1, int number2) {
           int remainder = number1 % number2;
  
  
  

Mime
View raw message