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: package org.apache.commons.lang.math;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: /**
025: * Test cases for the {@link FloatRange} class.
026: *
027: * @author Stephen Colebourne
028: * @version $Id: FloatRangeTest.java 437554 2006-08-28 06:21:41Z bayard $
029: */
030: public final class FloatRangeTest extends AbstractRangeTest {
031:
032: public FloatRangeTest(String name) {
033: super (name);
034: }
035:
036: public static Test suite() {
037: TestSuite suite = new TestSuite(FloatRangeTest.class);
038: suite.setName("FloatRange Tests");
039: return suite;
040: }
041:
042: public void setUp() {
043: super .setUp();
044: tenToTwenty = new FloatRange(float10, float20);
045: otherRange = new NumberRange(ten, twenty);
046: }
047:
048: protected Range createRange(Integer integer1, Integer integer2) {
049: return new FloatRange(integer1, integer2);
050: }
051:
052: protected Range createRange(Integer integer) {
053: return new NumberRange(integer);
054: }
055:
056: //--------------------------------------------------------------------------
057:
058: public void testConstructor1a() {
059: FloatRange nr = new FloatRange(8f);
060: assertEquals(float8, nr.getMinimumNumber());
061: assertEquals(float8, nr.getMaximumNumber());
062:
063: try {
064: new FloatRange(Float.NaN);
065: fail();
066: } catch (IllegalArgumentException ex) {
067: }
068: }
069:
070: public void testConstructor1b() {
071: FloatRange nr = new FloatRange(float8);
072: assertSame(float8, nr.getMinimumNumber());
073: assertSame(float8, nr.getMaximumNumber());
074:
075: Range r = new FloatRange(nonComparableNumber);
076:
077: try {
078: new FloatRange(null);
079: fail();
080: } catch (IllegalArgumentException ex) {
081: }
082: try {
083: new FloatRange(new Double(Double.NaN));
084: fail();
085: } catch (IllegalArgumentException ex) {
086: }
087: }
088:
089: public void testConstructor2a() {
090: FloatRange nr = new FloatRange(8f, 10f);
091: assertEquals(float8, nr.getMinimumNumber());
092: assertEquals(float10, nr.getMaximumNumber());
093:
094: nr = new FloatRange(10f, 8f);
095: assertEquals(float8, nr.getMinimumNumber());
096: assertEquals(float10, nr.getMaximumNumber());
097:
098: try {
099: new FloatRange(Float.NaN, 8f);
100: fail();
101: } catch (IllegalArgumentException ex) {
102: }
103: }
104:
105: public void testConstructor2b() {
106: FloatRange nr = new FloatRange(float8, float10);
107: assertSame(float8, nr.getMinimumNumber());
108: assertSame(float10, nr.getMaximumNumber());
109:
110: nr = new FloatRange(float10, float8);
111: assertSame(float8, nr.getMinimumNumber());
112: assertSame(float10, nr.getMaximumNumber());
113:
114: nr = new FloatRange(float8, float10);
115: assertSame(float8, nr.getMinimumNumber());
116: assertEquals(float10, nr.getMaximumNumber());
117:
118: // not null
119: try {
120: new FloatRange(float8, null);
121: fail();
122: } catch (IllegalArgumentException ex) {
123: }
124: try {
125: new FloatRange(null, float8);
126: fail();
127: } catch (IllegalArgumentException ex) {
128: }
129: try {
130: new FloatRange(null, null);
131: fail();
132: } catch (IllegalArgumentException ex) {
133: }
134:
135: try {
136: new FloatRange(new Double(Double.NaN), float10);
137: fail();
138: } catch (IllegalArgumentException ex) {
139: }
140: }
141:
142: //--------------------------------------------------------------------------
143:
144: public void testContainsNumber() {
145: assertEquals(false, tenToTwenty.containsNumber(null));
146: assertEquals(true, tenToTwenty
147: .containsNumber(nonComparableNumber));
148:
149: assertEquals(false, tenToTwenty.containsNumber(five));
150: assertEquals(true, tenToTwenty.containsNumber(ten));
151: assertEquals(true, tenToTwenty.containsNumber(fifteen));
152: assertEquals(true, tenToTwenty.containsNumber(twenty));
153: assertEquals(false, tenToTwenty.containsNumber(twentyFive));
154:
155: assertEquals(false, tenToTwenty.containsNumber(long8));
156: assertEquals(true, tenToTwenty.containsNumber(long10));
157: assertEquals(true, tenToTwenty.containsNumber(long12));
158: assertEquals(true, tenToTwenty.containsNumber(long20));
159: assertEquals(false, tenToTwenty.containsNumber(long21));
160:
161: assertEquals(false, tenToTwenty.containsNumber(double8));
162: assertEquals(true, tenToTwenty.containsNumber(double10));
163: assertEquals(true, tenToTwenty.containsNumber(double12));
164: assertEquals(true, tenToTwenty.containsNumber(double20));
165: assertEquals(false, tenToTwenty.containsNumber(double21));
166:
167: assertEquals(false, tenToTwenty.containsNumber(float8));
168: assertEquals(true, tenToTwenty.containsNumber(float10));
169: assertEquals(true, tenToTwenty.containsNumber(float12));
170: assertEquals(true, tenToTwenty.containsNumber(float20));
171: assertEquals(false, tenToTwenty.containsNumber(float21));
172: }
173:
174: public void testToString() {
175: String str = tenToTwenty.toString();
176: assertEquals("Range[10.0,20.0]", str);
177: assertSame(str, tenToTwenty.toString());
178: assertEquals("Range[-20.0,-10.0]", createRange(
179: new Integer(-20), new Integer(-10)).toString());
180: }
181:
182: //--------------------------------------------------------------------------
183:
184: }
|