Better unit test code coverage from Nathan Beyer [nbeyer@kc.rr.com].
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@226702 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2a760cb9d5
commit
b8c069723e
|
@ -55,6 +55,7 @@ public class MathTestSuite extends TestCase {
|
|||
suite.addTest(NumberRangeTest.suite());
|
||||
suite.addTest(NumberUtilsTest.suite());
|
||||
suite.addTest(RandomUtilsTest.suite());
|
||||
suite.addTest(RangeTest.suite());
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,6 +53,44 @@ public final class NumberRangeTest extends AbstractRangeTest {
|
|||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
public void testConstructorExceptions() {
|
||||
try {
|
||||
new NumberRange (new Double (Double.NaN));
|
||||
fail("no illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new NumberRange (new Double (Double.NaN), new Double (12.2));
|
||||
fail("no illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new NumberRange (new Double (12.2), new Double (Double.NaN));
|
||||
fail("no illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new NumberRange (new Float (Float.NaN));
|
||||
fail("no illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new NumberRange (new Float (Float.NaN), new Float(12.2));
|
||||
fail("no illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new NumberRange (new Float(12.2), new Float (Float.NaN));
|
||||
fail("no illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testConstructor1() {
|
||||
NumberRange nr = new NumberRange(five);
|
||||
assertSame(five, nr.getMinimumNumber());
|
||||
|
|
|
@ -171,9 +171,12 @@ public class NumberUtilsTest extends TestCase {
|
|||
assertEquals("createNumber(String) 1 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5"));
|
||||
assertEquals("createNumber(String) 2 failed", new Integer("12345"), NumberUtils.createNumber("12345"));
|
||||
assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5D"));
|
||||
assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5d"));
|
||||
assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5F"));
|
||||
assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5f"));
|
||||
assertEquals("createNumber(String) 5 failed", new Long(Integer.MAX_VALUE + 1L), NumberUtils.createNumber("" + (Integer.MAX_VALUE + 1L)));
|
||||
assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345L"));
|
||||
assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345l"));
|
||||
assertEquals("createNumber(String) 7 failed", new Float("-1234.5"), NumberUtils.createNumber("-1234.5"));
|
||||
assertEquals("createNumber(String) 8 failed", new Integer("-12345"), NumberUtils.createNumber("-12345"));
|
||||
assertTrue("createNumber(String) 9 failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
|
||||
|
|
|
@ -46,6 +46,8 @@ public final class RandomUtilsTest extends TestCase {
|
|||
/** test distribution of nextInt() */
|
||||
public void testNextInt() {
|
||||
tstNextInt(null);
|
||||
|
||||
assertTrue (RandomUtils.nextInt() >= 0);
|
||||
}
|
||||
|
||||
/** test distribution of nextInt(Random) */
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* Copyright 2002-2005 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.lang.math;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Tests the base methods in the {@link org.apache.commons.lang.math.Range} class.
|
||||
* </p>
|
||||
*
|
||||
* @author Nathan Beyer
|
||||
* @version $Id$
|
||||
*/
|
||||
public class RangeTest extends TestCase {
|
||||
|
||||
private static class RangeTestFixture extends Range {
|
||||
private byte max;
|
||||
|
||||
private byte min;
|
||||
|
||||
RangeTestFixture(byte min, byte max) {
|
||||
super();
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public boolean containsNumber(Number number) {
|
||||
if (number.byteValue() >= min && number.byteValue() <= max) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Number getMaximumNumber() {
|
||||
return new Byte(max);
|
||||
}
|
||||
|
||||
public Number getMinimumNumber() {
|
||||
return new Byte(min);
|
||||
}
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(RangeTest.class);
|
||||
suite.setName("Range Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
public RangeTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for 'org.apache.commons.lang.math.Range.equals(Object)'
|
||||
*/
|
||||
public void testEqualsObject() {
|
||||
RangeTestFixture r1 = new RangeTestFixture((byte) 0, (byte) 5);
|
||||
RangeTestFixture r2 = new RangeTestFixture((byte) 0, (byte) 5);
|
||||
RangeTestFixture r3 = new RangeTestFixture((byte) 0, (byte) 10);
|
||||
|
||||
assertEquals(r1, r2);
|
||||
assertFalse(r2.equals(r3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for 'org.apache.commons.lang.math.Range.hashCode()'
|
||||
*/
|
||||
public void testHashCode() {
|
||||
RangeTestFixture r1 = new RangeTestFixture((byte) 0, (byte) 5);
|
||||
RangeTestFixture r2 = new RangeTestFixture((byte) 0, (byte) 5);
|
||||
RangeTestFixture r3 = new RangeTestFixture((byte) 0, (byte) 10);
|
||||
|
||||
assertEquals(r1.hashCode(), r2.hashCode());
|
||||
assertFalse(r1.hashCode() == r3.hashCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for 'org.apache.commons.lang.math.Range.toString()'
|
||||
*/
|
||||
public void testToString() {
|
||||
RangeTestFixture r1 = new RangeTestFixture((byte) 0, (byte) 5);
|
||||
assertNotNull(r1.toString());
|
||||
assertNotNull(r1.toString());
|
||||
RangeTestFixture r2 = new RangeTestFixture((byte) 0, (byte) 5);
|
||||
assertNotNull(r2.toString());
|
||||
assertNotNull(r2.toString());
|
||||
RangeTestFixture r3 = new RangeTestFixture((byte) 0, (byte) 10);
|
||||
assertNotNull(r3.toString());
|
||||
assertNotNull(r3.toString());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue