KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > math > DoubleRangeTest


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.math;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 /**
22  * Test cases for the {@link DoubleRange} class.
23  *
24  * @author Stephen Colebourne
25  * @version $Id: DoubleRangeTest.java 155423 2005-02-26 13:08:30Z dirkv $
26  */

27 public final class DoubleRangeTest extends AbstractRangeTest {
28
29     public DoubleRangeTest(String JavaDoc name) {
30         super(name);
31     }
32
33     public static Test suite() {
34         TestSuite suite = new TestSuite(DoubleRangeTest.class);
35         suite.setName("DoubleRange Tests");
36         return suite;
37     }
38     
39     public void setUp() {
40         super.setUp();
41         tenToTwenty = new DoubleRange(double10, double20);
42         otherRange = new NumberRange(ten, twenty);
43     }
44
45     protected Range createRange(Integer JavaDoc integer1, Integer JavaDoc integer2) {
46         return new DoubleRange(integer1, integer2);
47     }
48     protected Range createRange(Integer JavaDoc integer) {
49         return new NumberRange(integer);
50     }
51     
52     //--------------------------------------------------------------------------
53

54     public void testConstructor1a() {
55         DoubleRange nr = new DoubleRange(8d);
56         assertEquals(double8, nr.getMinimumNumber());
57         assertEquals(double8, nr.getMaximumNumber());
58         
59         try {
60             new DoubleRange(Double.NaN);
61             fail();
62         } catch (IllegalArgumentException JavaDoc ex) {}
63     }
64     
65     public void testConstructor1b() {
66         DoubleRange nr = new DoubleRange(double8);
67         assertSame(double8, nr.getMinimumNumber());
68         assertSame(double8, nr.getMaximumNumber());
69         
70         Range r = new DoubleRange(nonComparable);
71         
72         try {
73             new DoubleRange(null);
74             fail();
75         } catch (IllegalArgumentException JavaDoc ex) {}
76         try {
77             new DoubleRange(new Double JavaDoc(Double.NaN));
78             fail();
79         } catch (IllegalArgumentException JavaDoc ex) {}
80     }
81     
82     public void testConstructor2a() {
83         DoubleRange nr = new DoubleRange(8d, 10d);
84         assertEquals(double8, nr.getMinimumNumber());
85         assertEquals(double10, nr.getMaximumNumber());
86         
87         nr = new DoubleRange(10d, 8d);
88         assertEquals(double8, nr.getMinimumNumber());
89         assertEquals(double10, nr.getMaximumNumber());
90         
91         try {
92             new DoubleRange(Double.NaN, 8d);
93             fail();
94         } catch (IllegalArgumentException JavaDoc ex) {}
95     }
96
97     public void testConstructor2b() {
98         DoubleRange nr = new DoubleRange(double8, double10);
99         assertSame(double8, nr.getMinimumNumber());
100         assertSame(double10, nr.getMaximumNumber());
101         
102         nr = new DoubleRange(double10, double8);
103         assertSame(double8, nr.getMinimumNumber());
104         assertSame(double10, nr.getMaximumNumber());
105         
106         nr = new DoubleRange(double8, double10);
107         assertSame(double8, nr.getMinimumNumber());
108         assertEquals(double10, nr.getMaximumNumber());
109         
110         // not null
111
try {
112             new DoubleRange(double8, null);
113             fail();
114         } catch (IllegalArgumentException JavaDoc ex) {}
115         try {
116             new DoubleRange(null, double8);
117             fail();
118         } catch (IllegalArgumentException JavaDoc ex) {}
119         try {
120             new DoubleRange(null, null);
121             fail();
122         } catch (IllegalArgumentException JavaDoc ex) {}
123         
124         try {
125             new DoubleRange(new Double JavaDoc(Double.NaN), double10);
126             fail();
127         } catch (IllegalArgumentException JavaDoc ex) {}
128     }
129
130     //--------------------------------------------------------------------------
131

132     public void testContainsNumber() {
133         assertEquals(false, tenToTwenty.containsNumber(null));
134         assertEquals(true, tenToTwenty.containsNumber(nonComparable));
135         
136         assertEquals(false, tenToTwenty.containsNumber(five));
137         assertEquals(true, tenToTwenty.containsNumber(ten));
138         assertEquals(true, tenToTwenty.containsNumber(fifteen));
139         assertEquals(true, tenToTwenty.containsNumber(twenty));
140         assertEquals(false, tenToTwenty.containsNumber(twentyFive));
141         
142         assertEquals(false, tenToTwenty.containsNumber(long8));
143         assertEquals(true, tenToTwenty.containsNumber(long10));
144         assertEquals(true, tenToTwenty.containsNumber(long12));
145         assertEquals(true, tenToTwenty.containsNumber(long20));
146         assertEquals(false, tenToTwenty.containsNumber(long21));
147         
148         assertEquals(false, tenToTwenty.containsNumber(double8));
149         assertEquals(true, tenToTwenty.containsNumber(double10));
150         assertEquals(true, tenToTwenty.containsNumber(double12));
151         assertEquals(true, tenToTwenty.containsNumber(double20));
152         assertEquals(false, tenToTwenty.containsNumber(double21));
153         
154         assertEquals(false, tenToTwenty.containsNumber(float8));
155         assertEquals(true, tenToTwenty.containsNumber(float10));
156         assertEquals(true, tenToTwenty.containsNumber(float12));
157         assertEquals(true, tenToTwenty.containsNumber(float20));
158         assertEquals(false, tenToTwenty.containsNumber(float21));
159     }
160
161     public void testToString() {
162         String JavaDoc str = tenToTwenty.toString();
163         assertEquals("Range[10.0,20.0]", str);
164         assertSame(str, tenToTwenty.toString());
165         assertEquals("Range[-20.0,-10.0]", createRange(new Integer JavaDoc(-20), new Integer JavaDoc(-10)).toString());
166     }
167     
168     //--------------------------------------------------------------------------
169

170 }
171
Popular Tags