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 DoubleRange} class.
026: *
027: * @author Stephen Colebourne
028: * @version $Id: DoubleRangeTest.java 437554 2006-08-28 06:21:41Z bayard $
029: */
030: public final class DoubleRangeTest extends AbstractRangeTest {
031:
032: public DoubleRangeTest(String name) {
033: super (name);
034: }
035:
036: public static Test suite() {
037: TestSuite suite = new TestSuite(DoubleRangeTest.class);
038: suite.setName("DoubleRange Tests");
039: return suite;
040: }
041:
042: public void setUp() {
043: super .setUp();
044: tenToTwenty = new DoubleRange(double10, double20);
045: otherRange = new NumberRange(ten, twenty);
046: }
047:
048: protected Range createRange(Integer integer1, Integer integer2) {
049: return new DoubleRange(integer1, integer2);
050: }
051:
052: protected Range createRange(Integer integer) {
053: return new NumberRange(integer);
054: }
055:
056: //--------------------------------------------------------------------------
057:
058: public void testConstructor1a() {
059: DoubleRange nr = new DoubleRange(8d);
060: assertEquals(double8, nr.getMinimumNumber());
061: assertEquals(double8, nr.getMaximumNumber());
062:
063: try {
064: new DoubleRange(Double.NaN);
065: fail();
066: } catch (IllegalArgumentException ex) {
067: }
068: }
069:
070: public void testConstructor1b() {
071: DoubleRange nr = new DoubleRange(double8);
072: assertSame(double8, nr.getMinimumNumber());
073: assertSame(double8, nr.getMaximumNumber());
074:
075: Range r = new DoubleRange(nonComparableNumber);
076:
077: try {
078: new DoubleRange(null);
079: fail();
080: } catch (IllegalArgumentException ex) {
081: }
082: try {
083: new DoubleRange(new Double(Double.NaN));
084: fail();
085: } catch (IllegalArgumentException ex) {
086: }
087: }
088:
089: public void testConstructor2a() {
090: DoubleRange nr = new DoubleRange(8d, 10d);
091: assertEquals(double8, nr.getMinimumNumber());
092: assertEquals(double10, nr.getMaximumNumber());
093:
094: nr = new DoubleRange(10d, 8d);
095: assertEquals(double8, nr.getMinimumNumber());
096: assertEquals(double10, nr.getMaximumNumber());
097:
098: try {
099: new DoubleRange(Double.NaN, 8d);
100: fail();
101: } catch (IllegalArgumentException ex) {
102: }
103: }
104:
105: public void testConstructor2b() {
106: DoubleRange nr = new DoubleRange(double8, double10);
107: assertSame(double8, nr.getMinimumNumber());
108: assertSame(double10, nr.getMaximumNumber());
109:
110: nr = new DoubleRange(double10, double8);
111: assertSame(double8, nr.getMinimumNumber());
112: assertSame(double10, nr.getMaximumNumber());
113:
114: nr = new DoubleRange(double8, double10);
115: assertSame(double8, nr.getMinimumNumber());
116: assertEquals(double10, nr.getMaximumNumber());
117:
118: // not null
119: try {
120: new DoubleRange(double8, null);
121: fail();
122: } catch (IllegalArgumentException ex) {
123: }
124: try {
125: new DoubleRange(null, double8);
126: fail();
127: } catch (IllegalArgumentException ex) {
128: }
129: try {
130: new DoubleRange(null, null);
131: fail();
132: } catch (IllegalArgumentException ex) {
133: }
134:
135: try {
136: new DoubleRange(new Double(Double.NaN), double10);
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: }
|