KickJava   Java API By Example, From Geeks To Geeks.

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


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.TestCase;
19
20 /**
21  * Test cases for the {@link Range} classes.
22  *
23  * @author Stephen Colebourne
24  * @version $Id: AbstractRangeTest.java 155423 2005-02-26 13:08:30Z dirkv $
25  */

26 public abstract class AbstractRangeTest extends TestCase {
27
28     protected Range tenToTwenty;
29     protected Range otherRange;
30     
31     protected Integer JavaDoc five;
32     protected Integer JavaDoc ten;
33     protected Integer JavaDoc twelve;
34     protected Integer JavaDoc fifteen;
35     protected Integer JavaDoc twenty;
36     protected Integer JavaDoc twentyFive;
37     protected Long JavaDoc long8;
38     protected Long JavaDoc long10;
39     protected Long JavaDoc long12;
40     protected Long JavaDoc long20;
41     protected Long JavaDoc long21;
42     protected Double JavaDoc double8;
43     protected Double JavaDoc double10;
44     protected Double JavaDoc double12;
45     protected Double JavaDoc double20;
46     protected Double JavaDoc double21;
47     protected Float JavaDoc float8;
48     protected Float JavaDoc float10;
49     protected Float JavaDoc float12;
50     protected Float JavaDoc float20;
51     protected Float JavaDoc float21;
52     
53     private static class InnerNumber extends Number JavaDoc {
54         public double doubleValue() {
55             return 12d;
56         }
57         public float floatValue() {
58             return 12f;
59         }
60         public int intValue() {
61             return 12;
62         }
63         public long longValue() {
64             return 12L;
65         }
66
67     }
68     
69     protected InnerNumber nonComparable = new InnerNumber();
70
71
72     public AbstractRangeTest(String JavaDoc name) {
73         super(name);
74     }
75
76     public void setUp() {
77         five = new Integer JavaDoc(5);
78         ten = new Integer JavaDoc(10);
79         twelve = new Integer JavaDoc(12);
80         fifteen = new Integer JavaDoc(15);
81         twenty = new Integer JavaDoc(20);
82         twentyFive = new Integer JavaDoc(25);
83         long8 = new Long JavaDoc(8);
84         long10 = new Long JavaDoc(10);
85         long12 = new Long JavaDoc(12);
86         long20 = new Long JavaDoc(20);
87         long21 = new Long JavaDoc(21);
88         double8 = new Double JavaDoc(8);
89         double10 = new Double JavaDoc(10);
90         double12 = new Double JavaDoc(12);
91         double20 = new Double JavaDoc(20);
92         double21 = new Double JavaDoc(21);
93         float8 = new Float JavaDoc(8);
94         float10 = new Float JavaDoc(10);
95         float12 = new Float JavaDoc(12);
96         float20 = new Float JavaDoc(20);
97         float21 = new Float JavaDoc(21);
98     }
99
100     //--------------------------------------------------------------------------
101

102     public void testGetMinimum() {
103         assertEquals(10L, tenToTwenty.getMinimumLong());
104         assertEquals(10, tenToTwenty.getMinimumInteger());
105         assertEquals(10d, tenToTwenty.getMinimumDouble(), 0.00001d);
106         assertEquals(10f, tenToTwenty.getMinimumFloat(), 0.00001f);
107     }
108     
109     public void testGetMaximum() {
110         assertEquals(20L, tenToTwenty.getMaximumLong());
111         assertEquals(20, tenToTwenty.getMaximumInteger());
112         assertEquals(20d, tenToTwenty.getMaximumDouble(), 0.00001d);
113         assertEquals(20f, tenToTwenty.getMaximumFloat(), 0.00001f);
114     }
115
116     //--------------------------------------------------------------------------
117

118     public void testContainsLong() {
119         assertEquals(false, tenToTwenty.containsLong(null));
120         assertEquals(true, tenToTwenty.containsLong(nonComparable));
121         
122         assertEquals(false, tenToTwenty.containsLong(five));
123         assertEquals(true, tenToTwenty.containsLong(ten));
124         assertEquals(true, tenToTwenty.containsLong(fifteen));
125         assertEquals(true, tenToTwenty.containsLong(twenty));
126         assertEquals(false, tenToTwenty.containsLong(twentyFive));
127         
128         assertEquals(false, tenToTwenty.containsLong(long8));
129         assertEquals(true, tenToTwenty.containsLong(long10));
130         assertEquals(true, tenToTwenty.containsLong(long12));
131         assertEquals(true, tenToTwenty.containsLong(long20));
132         assertEquals(false, tenToTwenty.containsLong(long21));
133         
134         assertEquals(false, tenToTwenty.containsLong(double8));
135         assertEquals(true, tenToTwenty.containsLong(double10));
136         assertEquals(true, tenToTwenty.containsLong(double12));
137         assertEquals(true, tenToTwenty.containsLong(double20));
138         assertEquals(false, tenToTwenty.containsLong(double21));
139         
140         assertEquals(false, tenToTwenty.containsLong(float8));
141         assertEquals(true, tenToTwenty.containsLong(float10));
142         assertEquals(true, tenToTwenty.containsLong(float12));
143         assertEquals(true, tenToTwenty.containsLong(float20));
144         assertEquals(false, tenToTwenty.containsLong(float21));
145         
146         assertEquals(false, tenToTwenty.containsLong(9L));
147         assertEquals(true, tenToTwenty.containsLong(10L));
148         assertEquals(true, tenToTwenty.containsLong(15L));
149         assertEquals(true, tenToTwenty.containsLong(20L));
150         assertEquals(false, tenToTwenty.containsLong(21L));
151     }
152         
153     public void testContainsInteger() {
154         assertEquals(false, tenToTwenty.containsInteger(null));
155         assertEquals(true, tenToTwenty.containsInteger(nonComparable));
156         
157         assertEquals(false, tenToTwenty.containsInteger(five));
158         assertEquals(true, tenToTwenty.containsInteger(ten));
159         assertEquals(true, tenToTwenty.containsInteger(fifteen));
160         assertEquals(true, tenToTwenty.containsInteger(twenty));
161         assertEquals(false, tenToTwenty.containsInteger(twentyFive));
162         
163         assertEquals(false, tenToTwenty.containsInteger(long8));
164         assertEquals(true, tenToTwenty.containsInteger(long10));
165         assertEquals(true, tenToTwenty.containsInteger(long12));
166         assertEquals(true, tenToTwenty.containsInteger(long20));
167         assertEquals(false, tenToTwenty.containsInteger(long21));
168         
169         assertEquals(false, tenToTwenty.containsInteger(double8));
170         assertEquals(true, tenToTwenty.containsInteger(double10));
171         assertEquals(true, tenToTwenty.containsInteger(double12));
172         assertEquals(true, tenToTwenty.containsInteger(double20));
173         assertEquals(false, tenToTwenty.containsInteger(double21));
174         
175         assertEquals(false, tenToTwenty.containsInteger(float8));
176         assertEquals(true, tenToTwenty.containsInteger(float10));
177         assertEquals(true, tenToTwenty.containsInteger(float12));
178         assertEquals(true, tenToTwenty.containsInteger(float20));
179         assertEquals(false, tenToTwenty.containsInteger(float21));
180         
181         assertEquals(false, tenToTwenty.containsInteger(9));
182         assertEquals(true, tenToTwenty.containsInteger(10));
183         assertEquals(true, tenToTwenty.containsInteger(15));
184         assertEquals(true, tenToTwenty.containsInteger(20));
185         assertEquals(false, tenToTwenty.containsInteger(21));
186     }
187
188     public void testContainsDouble() {
189         assertEquals(false, tenToTwenty.containsDouble(null));
190         assertEquals(true, tenToTwenty.containsDouble(nonComparable));
191         
192         assertEquals(false, tenToTwenty.containsDouble(five));
193         assertEquals(true, tenToTwenty.containsDouble(ten));
194         assertEquals(true, tenToTwenty.containsDouble(fifteen));
195         assertEquals(true, tenToTwenty.containsDouble(twenty));
196         assertEquals(false, tenToTwenty.containsDouble(twentyFive));
197         
198         assertEquals(false, tenToTwenty.containsDouble(long8));
199         assertEquals(true, tenToTwenty.containsDouble(long10));
200         assertEquals(true, tenToTwenty.containsDouble(long12));
201         assertEquals(true, tenToTwenty.containsDouble(long20));
202         assertEquals(false, tenToTwenty.containsDouble(long21));
203         
204         assertEquals(false, tenToTwenty.containsDouble(double8));
205         assertEquals(true, tenToTwenty.containsDouble(double10));
206         assertEquals(true, tenToTwenty.containsDouble(double12));
207         assertEquals(true, tenToTwenty.containsDouble(double20));
208         assertEquals(false, tenToTwenty.containsDouble(double21));
209         
210         assertEquals(false, tenToTwenty.containsDouble(float8));
211         assertEquals(true, tenToTwenty.containsDouble(float10));
212         assertEquals(true, tenToTwenty.containsDouble(float12));
213         assertEquals(true, tenToTwenty.containsDouble(float20));
214         assertEquals(false, tenToTwenty.containsDouble(float21));
215         
216         assertEquals(false, tenToTwenty.containsDouble(9d));
217         assertEquals(true, tenToTwenty.containsDouble(10d));
218         assertEquals(true, tenToTwenty.containsDouble(15d));
219         assertEquals(true, tenToTwenty.containsDouble(20d));
220         assertEquals(false, tenToTwenty.containsDouble(21d));
221     }
222
223     public void testContainsFloat() {
224         assertEquals(false, tenToTwenty.containsFloat(null));
225         assertEquals(true, tenToTwenty.containsFloat(nonComparable));
226         
227         assertEquals(false, tenToTwenty.containsFloat(five));
228         assertEquals(true, tenToTwenty.containsFloat(ten));
229         assertEquals(true, tenToTwenty.containsFloat(fifteen));
230         assertEquals(true, tenToTwenty.containsFloat(twenty));
231         assertEquals(false, tenToTwenty.containsFloat(twentyFive));
232         
233         assertEquals(false, tenToTwenty.containsFloat(long8));
234         assertEquals(true, tenToTwenty.containsFloat(long10));
235         assertEquals(true, tenToTwenty.containsFloat(long12));
236         assertEquals(true, tenToTwenty.containsFloat(long20));
237         assertEquals(false, tenToTwenty.containsFloat(long21));
238         
239         assertEquals(false, tenToTwenty.containsFloat(double8));
240         assertEquals(true, tenToTwenty.containsFloat(double10));
241         assertEquals(true, tenToTwenty.containsFloat(double12));
242         assertEquals(true, tenToTwenty.containsFloat(double20));
243         assertEquals(false, tenToTwenty.containsFloat(double21));
244         
245         assertEquals(false, tenToTwenty.containsFloat(float8));
246         assertEquals(true, tenToTwenty.containsFloat(float10));
247         assertEquals(true, tenToTwenty.containsFloat(float12));
248         assertEquals(true, tenToTwenty.containsFloat(float20));
249         assertEquals(false, tenToTwenty.containsFloat(float21));
250         
251         assertEquals(false, tenToTwenty.containsFloat(9f));
252         assertEquals(true, tenToTwenty.containsFloat(10f));
253         assertEquals(true, tenToTwenty.containsFloat(15f));
254         assertEquals(true, tenToTwenty.containsFloat(20f));
255         assertEquals(false, tenToTwenty.containsFloat(21f));
256     }
257
258     //--------------------------------------------------------------------------
259

260     public void testContainsRange() {
261         assertEquals(false, tenToTwenty.containsRange(null));
262         assertEquals(false, tenToTwenty.containsRange(createRange(five, five)));
263         assertEquals(false, tenToTwenty.containsRange(createRange(five, ten)));
264         assertEquals(false, tenToTwenty.containsRange(createRange(five, twelve)));
265         assertEquals(false, tenToTwenty.containsRange(createRange(five, fifteen)));
266         assertEquals(false, tenToTwenty.containsRange(createRange(five, twenty)));
267         assertEquals(false, tenToTwenty.containsRange(createRange(five, twentyFive)));
268         
269         assertEquals(true, tenToTwenty.containsRange(createRange(ten, ten)));
270         assertEquals(true, tenToTwenty.containsRange(createRange(ten, twelve)));
271         assertEquals(true, tenToTwenty.containsRange(createRange(ten, fifteen)));
272         assertEquals(true, tenToTwenty.containsRange(createRange(ten, twenty)));
273         assertEquals(false, tenToTwenty.containsRange(createRange(ten, twentyFive)));
274         
275         assertEquals(true, tenToTwenty.containsRange(createRange(twelve, twelve)));
276         assertEquals(true, tenToTwenty.containsRange(createRange(twelve, fifteen)));
277         assertEquals(true, tenToTwenty.containsRange(createRange(twelve, twenty)));
278         assertEquals(false, tenToTwenty.containsRange(createRange(twelve, twentyFive)));
279         
280         assertEquals(true, tenToTwenty.containsRange(createRange(fifteen, fifteen)));
281         assertEquals(true, tenToTwenty.containsRange(createRange(fifteen, twenty)));
282         assertEquals(false, tenToTwenty.containsRange(createRange(fifteen, twentyFive)));
283         
284         assertEquals(true, tenToTwenty.containsRange(createRange(twenty, twenty)));
285         assertEquals(false, tenToTwenty.containsRange(createRange(twenty, twentyFive)));
286         
287         assertEquals(false, tenToTwenty.containsRange(createRange(twentyFive, twentyFive)));
288     }
289
290     public void testOverlapsRange() {
291         assertEquals(false, tenToTwenty.overlapsRange(null));
292         assertEquals(false, tenToTwenty.overlapsRange(createRange(five, five)));
293         assertEquals(true, tenToTwenty.overlapsRange(createRange(five, ten)));
294         assertEquals(true, tenToTwenty.overlapsRange(createRange(five, twelve)));
295         assertEquals(true, tenToTwenty.overlapsRange(createRange(five, fifteen)));
296         assertEquals(true, tenToTwenty.overlapsRange(createRange(five, twenty)));
297         assertEquals(true, tenToTwenty.overlapsRange(createRange(five, twentyFive)));
298         
299         assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, ten)));
300         assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, twelve)));
301         assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, fifteen)));
302         assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, twenty)));
303         assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, twentyFive)));
304         
305         assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, twelve)));
306         assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, fifteen)));
307         assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, twenty)));
308         assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, twentyFive)));
309         
310         assertEquals(true, tenToTwenty.overlapsRange(createRange(fifteen, fifteen)));
311         assertEquals(true, tenToTwenty.overlapsRange(createRange(fifteen, twenty)));
312         assertEquals(true, tenToTwenty.overlapsRange(createRange(fifteen, twentyFive)));
313         
314         assertEquals(true, tenToTwenty.overlapsRange(createRange(twenty, twenty)));
315         assertEquals(true, tenToTwenty.overlapsRange(createRange(twenty, twentyFive)));
316         
317         assertEquals(false, tenToTwenty.overlapsRange(createRange(twentyFive, twentyFive)));
318     }
319
320     //--------------------------------------------------------------------------
321

322     public void testEquals() {
323         assertEquals(false, tenToTwenty.equals(createRange(ten, fifteen)));
324         assertEquals(false, tenToTwenty.equals(createRange(ten, twentyFive)));
325         
326         assertEquals(false, tenToTwenty.equals(createRange(fifteen, twenty)));
327         assertEquals(false, tenToTwenty.equals(createRange(five, twenty)));
328         
329         assertEquals(false, tenToTwenty.equals(createRange(five, ten)));
330         assertEquals(false, tenToTwenty.equals(createRange(ten)));
331
332         assertEquals(true, tenToTwenty.equals(tenToTwenty));
333         assertEquals(true, tenToTwenty.equals(createRange(ten, twenty)));
334         assertEquals(true, tenToTwenty.equals(createRange(twenty, ten)));
335         
336         assertEquals(false, tenToTwenty.equals(null));
337         assertEquals(false, tenToTwenty.equals(new Object JavaDoc()));
338         assertEquals(false, tenToTwenty.equals(otherRange));
339     }
340
341     public void testHashCode() {
342         assertEquals(tenToTwenty.hashCode(), tenToTwenty.hashCode());
343         assertTrue(tenToTwenty.hashCode() != 0);
344     }
345     
346     public void testToString() {
347         String JavaDoc str = tenToTwenty.toString();
348         assertEquals("Range[10,20]", str);
349         assertSame(str, tenToTwenty.toString());
350         assertEquals("Range[-20,-10]", createRange(new Integer JavaDoc(-20), new Integer JavaDoc(-10)).toString());
351     }
352
353
354     protected abstract Range createRange(Integer JavaDoc integer);
355     protected abstract Range createRange(Integer JavaDoc integer1, Integer JavaDoc integer2);
356
357 }
358
Popular Tags