diff --git a/src/test/org/apache/commons/lang/math/MathTestSuite.java b/src/test/org/apache/commons/lang/math/MathTestSuite.java index dd3c0fbf7..a84d8de1c 100644 --- a/src/test/org/apache/commons/lang/math/MathTestSuite.java +++ b/src/test/org/apache/commons/lang/math/MathTestSuite.java @@ -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; } } diff --git a/src/test/org/apache/commons/lang/math/NumberRangeTest.java b/src/test/org/apache/commons/lang/math/NumberRangeTest.java index bab668446..ec767f5c0 100644 --- a/src/test/org/apache/commons/lang/math/NumberRangeTest.java +++ b/src/test/org/apache/commons/lang/math/NumberRangeTest.java @@ -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()); diff --git a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java index b2c05d9a4..16dbe444a 100644 --- a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java +++ b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java @@ -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()); diff --git a/src/test/org/apache/commons/lang/math/RandomUtilsTest.java b/src/test/org/apache/commons/lang/math/RandomUtilsTest.java index 0e9c8cd15..3c1d41b2f 100644 --- a/src/test/org/apache/commons/lang/math/RandomUtilsTest.java +++ b/src/test/org/apache/commons/lang/math/RandomUtilsTest.java @@ -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) */ diff --git a/src/test/org/apache/commons/lang/math/RangeTest.java b/src/test/org/apache/commons/lang/math/RangeTest.java new file mode 100644 index 000000000..3ffd0e955 --- /dev/null +++ b/src/test/org/apache/commons/lang/math/RangeTest.java @@ -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; + +/** + *

+ * Tests the base methods in the {@link org.apache.commons.lang.math.Range} class. + *

+ * + * @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()); + } + +}