Extra tests suggested by Clover

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137568 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-04 02:01:53 +00:00
parent 8e8b50466b
commit 30426cf21e
7 changed files with 136 additions and 47 deletions

View File

@ -66,7 +66,7 @@
* @author Stephen Colebourne
* @author Tim O'Brien
* @since 2.0
* @version $Id: Fraction.java,v 1.7 2003/08/04 01:20:47 scolebourne Exp $
* @version $Id: Fraction.java,v 1.8 2003/08/04 02:01:53 scolebourne Exp $
*/
public final class Fraction extends Number implements Serializable, Comparable {
@ -200,9 +200,6 @@ public static Fraction getReducedFraction(int numerator, int denominator) {
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 double doubleValue() {
*/
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 @@ public Fraction pow(int power) {
*
* @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;

View File

@ -59,7 +59,7 @@
* Test cases for the {@link Range} classes.
*
* @author Stephen Colebourne
* @version $Id: AbstractRangeTest.java,v 1.3 2003/08/04 01:14:02 scolebourne Exp $
* @version $Id: AbstractRangeTest.java,v 1.4 2003/08/04 02:01:53 scolebourne Exp $
*/
public abstract class AbstractRangeTest extends TestCase {
@ -296,6 +296,7 @@ public void testContainsFloat() {
//--------------------------------------------------------------------------
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 testContainsRange() {
}
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 @@ public void testEquals() {
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 testHashCode() {
}
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());
}

View File

@ -60,7 +60,7 @@
* Test cases for the {@link DoubleRange} class.
*
* @author Stephen Colebourne
* @version $Id: DoubleRangeTest.java,v 1.3 2003/08/04 01:14:02 scolebourne Exp $
* @version $Id: DoubleRangeTest.java,v 1.4 2003/08/04 02:01:53 scolebourne Exp $
*/
public final class DoubleRangeTest extends AbstractRangeTest {
@ -197,7 +197,9 @@ public void testContainsNumber() {
}
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());
}

View File

@ -60,7 +60,7 @@
* Test cases for the {@link FloatRange} class.
*
* @author Stephen Colebourne
* @version $Id: FloatRangeTest.java,v 1.3 2003/08/04 01:14:02 scolebourne Exp $
* @version $Id: FloatRangeTest.java,v 1.4 2003/08/04 02:01:53 scolebourne Exp $
*/
public final class FloatRangeTest extends AbstractRangeTest {
@ -197,7 +197,9 @@ public void testContainsNumber() {
}
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());
}

View File

@ -60,7 +60,7 @@
* Test cases for the {@link Fraction} classes.
*
* @author Stephen Colebourne
* @version $Id: FractionTest.java,v 1.2 2002/12/22 21:18:51 scolebourne Exp $
* @version $Id: FractionTest.java,v 1.3 2003/08/04 02:01:53 scolebourne Exp $
*/
public class FractionTest extends TestCase {
@ -703,6 +703,18 @@ public void testSubtract() {
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 @@ public void testToString() {
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 @@ public void testToProperString() {
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());

View File

@ -60,7 +60,7 @@
* Test cases for the {@link IntRange} class.
*
* @author Stephen Colebourne
* @version $Id: IntRangeTest.java,v 1.3 2003/08/04 01:14:02 scolebourne Exp $
* @version $Id: IntRangeTest.java,v 1.4 2003/08/04 02:01:53 scolebourne Exp $
*/
public final class IntRangeTest extends AbstractRangeTest {
@ -131,6 +131,12 @@ public void testConstructor2b() {
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);

View File

@ -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;
@ -73,7 +75,7 @@
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id: NumberUtilsTest.java,v 1.5 2003/07/26 19:12:30 ggregory Exp $
* @version $Id: NumberUtilsTest.java,v 1.6 2003/08/04 02:01:53 scolebourne Exp $
*/
public class NumberUtilsTest extends TestCase {
@ -91,6 +93,16 @@ public static Test suite() {
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 @@ public void testMinLong() {
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 @@ public void testMinInt() {
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 @@ public void testMinShort() {
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 @@ public void testMinDouble() {
-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 @@ public void testMinFloat() {
-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 @@ public void testMaxLong() {
"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 @@ public void testMaxInt() {
"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 @@ public void testMaxShort() {
"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 @@ public void testMaxDouble() {
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 @@ public void testMaxFloat() {
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 @@ public void testMinimumShort() {
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 @@ public void testMaximumShort() {
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() {