001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.commons.lang.math;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: /**
026: * Test cases for the {@link NumberRange} class.
027: *
028: * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
029: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
030: * @author Stephen Colebourne
031: * @version $Id: NumberRangeTest.java 437554 2006-08-28 06:21:41Z bayard $
032: */
033: public final class NumberRangeTest extends AbstractRangeTest {
034:
035: public static Test suite() {
036: TestSuite suite = new TestSuite(NumberRangeTest.class);
037: suite.setName("NumberRange Tests");
038: return suite;
039: }
040:
041: public NumberRangeTest(String name) {
042: super (name);
043: }
044:
045: void checkConstructorException(Number num) {
046: try {
047: new NumberRange(num);
048: fail("Expected an IllegalArgumentException");
049: } catch (IllegalArgumentException e) {
050: // Expected.
051: }
052: }
053:
054: void checkConstructorException(Number num1, Number num2) {
055: try {
056: new NumberRange(num1, num2);
057: fail("Expected an IllegalArgumentException");
058: } catch (IllegalArgumentException e) {
059: // Expected.
060: }
061: }
062:
063: protected Range createRange(Integer integer) {
064: return new NumberRange(integer);
065: }
066:
067: // --------------------------------------------------------------------------
068:
069: protected Range createRange(Integer integer1, Integer integer2) {
070: return new NumberRange(integer1, integer2);
071: }
072:
073: public void setUp() {
074: super .setUp();
075: tenToTwenty = new NumberRange(ten, twenty);
076: otherRange = new IntRange(ten, twenty);
077: }
078:
079: /**
080: * Tests non-exceptional conditions for the one argument constructor.
081: */
082: public void testConstructor1() {
083: NumberRange nr = new NumberRange(five);
084: assertSame(five, nr.getMinimumNumber());
085: assertSame(five, nr.getMaximumNumber());
086: }
087:
088: /**
089: * Tests exceptional conditions for the one argument constructor.
090: */
091: public void testConstructor1Exceptions() {
092: this .checkConstructorException(null);
093: this .checkConstructorException(nonComparableNumber);
094: this .checkConstructorException(new Float(Float.NaN));
095: this .checkConstructorException(new Double(Double.NaN));
096: }
097:
098: /**
099: * Tests non-exceptional conditions for the two argument constructor.
100: */
101: public void testConstructor2() {
102: NumberRange nr = new NumberRange(five, ten);
103: assertSame(five, nr.getMinimumNumber());
104: assertSame(ten, nr.getMaximumNumber());
105:
106: nr = new NumberRange(ten, five);
107: assertSame(five, nr.getMinimumNumber());
108: assertSame(ten, nr.getMaximumNumber());
109: }
110:
111: /**
112: * Tests exceptional conditions for the two argument constructor.
113: */
114: public void testConstructor2Exceptions() {
115: this .checkConstructorException(null, null);
116:
117: this .checkConstructorException(new Float(12.2f), new Double(
118: 12.2));
119: this .checkConstructorException(new Float(Float.NaN),
120: new Double(12.2));
121: this .checkConstructorException(new Double(Double.NaN),
122: new Double(12.2));
123: this .checkConstructorException(new Double(12.2), new Double(
124: Double.NaN));
125: this .checkConstructorException(new Double(Double.NaN),
126: new Double(Double.NaN));
127: this .checkConstructorException(null, new Double(12.2));
128: this .checkConstructorException(new Double(12.2), null);
129:
130: this .checkConstructorException(new Double(12.2f), new Float(
131: 12.2));
132: this .checkConstructorException(new Double(Double.NaN),
133: new Float(12.2));
134: this .checkConstructorException(new Float(Float.NaN), new Float(
135: 12.2));
136: this .checkConstructorException(new Float(12.2), new Float(
137: Float.NaN));
138: this .checkConstructorException(new Float(Float.NaN), new Float(
139: Float.NaN));
140: this .checkConstructorException(null, new Float(12.2));
141: this .checkConstructorException(new Float(12.2), null);
142:
143: this .checkConstructorException(nonComparableNumber,
144: nonComparableNumber);
145: this .checkConstructorException(null, nonComparableNumber);
146: this .checkConstructorException(nonComparableNumber, null);
147: this .checkConstructorException(new Float(12.2),
148: nonComparableNumber);
149: this .checkConstructorException(nonComparableNumber, new Float(
150: 12.2));
151: }
152:
153: // --------------------------------------------------------------------------
154:
155: public void testContainsLongBig() {
156: // original NumberRange class failed this test
157: NumberRange big = new NumberRange(new Long(Long.MAX_VALUE),
158: new Long(Long.MAX_VALUE - 2));
159: assertEquals(true, big.containsLong(Long.MAX_VALUE - 1));
160: assertEquals(false, big.containsLong(Long.MAX_VALUE - 3));
161: }
162:
163: public void testContainsNumber() {
164: assertEquals(false, tenToTwenty.containsNumber(null));
165: assertEquals(false, tenToTwenty.containsNumber(five));
166: assertEquals(true, tenToTwenty.containsNumber(ten));
167: assertEquals(true, tenToTwenty.containsNumber(fifteen));
168: assertEquals(true, tenToTwenty.containsNumber(twenty));
169: assertEquals(false, tenToTwenty.containsNumber(twentyFive));
170:
171: try {
172: tenToTwenty.containsNumber(long21);
173: fail();
174: } catch (IllegalArgumentException ex) {
175: }
176: }
177:
178: // --------------------------------------------------------------------------
179:
180: }
|