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.TestCase;
022:
023: /**
024: * Test cases for the {@link Range} classes.
025: *
026: * @author Stephen Colebourne
027: * @version $Id: AbstractRangeTest.java 437554 2006-08-28 06:21:41Z bayard $
028: */
029: public abstract class AbstractRangeTest extends TestCase {
030:
031: protected Range tenToTwenty;
032: protected Range otherRange;
033:
034: protected Integer five;
035: protected Integer ten;
036: protected Integer twelve;
037: protected Integer fifteen;
038: protected Integer twenty;
039: protected Integer twentyFive;
040: protected Long long8;
041: protected Long long10;
042: protected Long long12;
043: protected Long long20;
044: protected Long long21;
045: protected Double double8;
046: protected Double double10;
047: protected Double double12;
048: protected Double double20;
049: protected Double double21;
050: protected Float float8;
051: protected Float float10;
052: protected Float float12;
053: protected Float float20;
054: protected Float float21;
055:
056: private static class InnerNumber extends Number {
057: public double doubleValue() {
058: return 12d;
059: }
060:
061: public float floatValue() {
062: return 12f;
063: }
064:
065: public int intValue() {
066: return 12;
067: }
068:
069: public long longValue() {
070: return 12L;
071: }
072: }
073:
074: protected InnerNumber nonComparableNumber = new InnerNumber();
075:
076: public AbstractRangeTest(String name) {
077: super (name);
078: }
079:
080: public void setUp() {
081: five = new Integer(5);
082: ten = new Integer(10);
083: twelve = new Integer(12);
084: fifteen = new Integer(15);
085: twenty = new Integer(20);
086: twentyFive = new Integer(25);
087: long8 = new Long(8);
088: long10 = new Long(10);
089: long12 = new Long(12);
090: long20 = new Long(20);
091: long21 = new Long(21);
092: double8 = new Double(8);
093: double10 = new Double(10);
094: double12 = new Double(12);
095: double20 = new Double(20);
096: double21 = new Double(21);
097: float8 = new Float(8);
098: float10 = new Float(10);
099: float12 = new Float(12);
100: float20 = new Float(20);
101: float21 = new Float(21);
102: }
103:
104: // --------------------------------------------------------------------------
105:
106: public void testGetMinimum() {
107: assertEquals(10L, tenToTwenty.getMinimumLong());
108: assertEquals(10, tenToTwenty.getMinimumInteger());
109: assertEquals(10d, tenToTwenty.getMinimumDouble(), 0.00001d);
110: assertEquals(10f, tenToTwenty.getMinimumFloat(), 0.00001f);
111: }
112:
113: public void testGetMaximum() {
114: assertEquals(20L, tenToTwenty.getMaximumLong());
115: assertEquals(20, tenToTwenty.getMaximumInteger());
116: assertEquals(20d, tenToTwenty.getMaximumDouble(), 0.00001d);
117: assertEquals(20f, tenToTwenty.getMaximumFloat(), 0.00001f);
118: }
119:
120: //--------------------------------------------------------------------------
121:
122: public void testContainsLong() {
123: assertEquals(false, tenToTwenty.containsLong(null));
124: assertEquals(true, tenToTwenty
125: .containsLong(nonComparableNumber));
126:
127: assertEquals(false, tenToTwenty.containsLong(five));
128: assertEquals(true, tenToTwenty.containsLong(ten));
129: assertEquals(true, tenToTwenty.containsLong(fifteen));
130: assertEquals(true, tenToTwenty.containsLong(twenty));
131: assertEquals(false, tenToTwenty.containsLong(twentyFive));
132:
133: assertEquals(false, tenToTwenty.containsLong(long8));
134: assertEquals(true, tenToTwenty.containsLong(long10));
135: assertEquals(true, tenToTwenty.containsLong(long12));
136: assertEquals(true, tenToTwenty.containsLong(long20));
137: assertEquals(false, tenToTwenty.containsLong(long21));
138:
139: assertEquals(false, tenToTwenty.containsLong(double8));
140: assertEquals(true, tenToTwenty.containsLong(double10));
141: assertEquals(true, tenToTwenty.containsLong(double12));
142: assertEquals(true, tenToTwenty.containsLong(double20));
143: assertEquals(false, tenToTwenty.containsLong(double21));
144:
145: assertEquals(false, tenToTwenty.containsLong(float8));
146: assertEquals(true, tenToTwenty.containsLong(float10));
147: assertEquals(true, tenToTwenty.containsLong(float12));
148: assertEquals(true, tenToTwenty.containsLong(float20));
149: assertEquals(false, tenToTwenty.containsLong(float21));
150:
151: assertEquals(false, tenToTwenty.containsLong(9L));
152: assertEquals(true, tenToTwenty.containsLong(10L));
153: assertEquals(true, tenToTwenty.containsLong(15L));
154: assertEquals(true, tenToTwenty.containsLong(20L));
155: assertEquals(false, tenToTwenty.containsLong(21L));
156: }
157:
158: public void testContainsInteger() {
159: assertEquals(false, tenToTwenty.containsInteger(null));
160: assertEquals(true, tenToTwenty
161: .containsInteger(nonComparableNumber));
162:
163: assertEquals(false, tenToTwenty.containsInteger(five));
164: assertEquals(true, tenToTwenty.containsInteger(ten));
165: assertEquals(true, tenToTwenty.containsInteger(fifteen));
166: assertEquals(true, tenToTwenty.containsInteger(twenty));
167: assertEquals(false, tenToTwenty.containsInteger(twentyFive));
168:
169: assertEquals(false, tenToTwenty.containsInteger(long8));
170: assertEquals(true, tenToTwenty.containsInteger(long10));
171: assertEquals(true, tenToTwenty.containsInteger(long12));
172: assertEquals(true, tenToTwenty.containsInteger(long20));
173: assertEquals(false, tenToTwenty.containsInteger(long21));
174:
175: assertEquals(false, tenToTwenty.containsInteger(double8));
176: assertEquals(true, tenToTwenty.containsInteger(double10));
177: assertEquals(true, tenToTwenty.containsInteger(double12));
178: assertEquals(true, tenToTwenty.containsInteger(double20));
179: assertEquals(false, tenToTwenty.containsInteger(double21));
180:
181: assertEquals(false, tenToTwenty.containsInteger(float8));
182: assertEquals(true, tenToTwenty.containsInteger(float10));
183: assertEquals(true, tenToTwenty.containsInteger(float12));
184: assertEquals(true, tenToTwenty.containsInteger(float20));
185: assertEquals(false, tenToTwenty.containsInteger(float21));
186:
187: assertEquals(false, tenToTwenty.containsInteger(9));
188: assertEquals(true, tenToTwenty.containsInteger(10));
189: assertEquals(true, tenToTwenty.containsInteger(15));
190: assertEquals(true, tenToTwenty.containsInteger(20));
191: assertEquals(false, tenToTwenty.containsInteger(21));
192: }
193:
194: public void testContainsDouble() {
195: assertEquals(false, tenToTwenty.containsDouble(null));
196: assertEquals(true, tenToTwenty
197: .containsDouble(nonComparableNumber));
198:
199: assertEquals(false, tenToTwenty.containsDouble(five));
200: assertEquals(true, tenToTwenty.containsDouble(ten));
201: assertEquals(true, tenToTwenty.containsDouble(fifteen));
202: assertEquals(true, tenToTwenty.containsDouble(twenty));
203: assertEquals(false, tenToTwenty.containsDouble(twentyFive));
204:
205: assertEquals(false, tenToTwenty.containsDouble(long8));
206: assertEquals(true, tenToTwenty.containsDouble(long10));
207: assertEquals(true, tenToTwenty.containsDouble(long12));
208: assertEquals(true, tenToTwenty.containsDouble(long20));
209: assertEquals(false, tenToTwenty.containsDouble(long21));
210:
211: assertEquals(false, tenToTwenty.containsDouble(double8));
212: assertEquals(true, tenToTwenty.containsDouble(double10));
213: assertEquals(true, tenToTwenty.containsDouble(double12));
214: assertEquals(true, tenToTwenty.containsDouble(double20));
215: assertEquals(false, tenToTwenty.containsDouble(double21));
216:
217: assertEquals(false, tenToTwenty.containsDouble(float8));
218: assertEquals(true, tenToTwenty.containsDouble(float10));
219: assertEquals(true, tenToTwenty.containsDouble(float12));
220: assertEquals(true, tenToTwenty.containsDouble(float20));
221: assertEquals(false, tenToTwenty.containsDouble(float21));
222:
223: assertEquals(false, tenToTwenty.containsDouble(9d));
224: assertEquals(true, tenToTwenty.containsDouble(10d));
225: assertEquals(true, tenToTwenty.containsDouble(15d));
226: assertEquals(true, tenToTwenty.containsDouble(20d));
227: assertEquals(false, tenToTwenty.containsDouble(21d));
228: }
229:
230: public void testContainsFloat() {
231: assertEquals(false, tenToTwenty.containsFloat(null));
232: assertEquals(true, tenToTwenty
233: .containsFloat(nonComparableNumber));
234:
235: assertEquals(false, tenToTwenty.containsFloat(five));
236: assertEquals(true, tenToTwenty.containsFloat(ten));
237: assertEquals(true, tenToTwenty.containsFloat(fifteen));
238: assertEquals(true, tenToTwenty.containsFloat(twenty));
239: assertEquals(false, tenToTwenty.containsFloat(twentyFive));
240:
241: assertEquals(false, tenToTwenty.containsFloat(long8));
242: assertEquals(true, tenToTwenty.containsFloat(long10));
243: assertEquals(true, tenToTwenty.containsFloat(long12));
244: assertEquals(true, tenToTwenty.containsFloat(long20));
245: assertEquals(false, tenToTwenty.containsFloat(long21));
246:
247: assertEquals(false, tenToTwenty.containsFloat(double8));
248: assertEquals(true, tenToTwenty.containsFloat(double10));
249: assertEquals(true, tenToTwenty.containsFloat(double12));
250: assertEquals(true, tenToTwenty.containsFloat(double20));
251: assertEquals(false, tenToTwenty.containsFloat(double21));
252:
253: assertEquals(false, tenToTwenty.containsFloat(float8));
254: assertEquals(true, tenToTwenty.containsFloat(float10));
255: assertEquals(true, tenToTwenty.containsFloat(float12));
256: assertEquals(true, tenToTwenty.containsFloat(float20));
257: assertEquals(false, tenToTwenty.containsFloat(float21));
258:
259: assertEquals(false, tenToTwenty.containsFloat(9f));
260: assertEquals(true, tenToTwenty.containsFloat(10f));
261: assertEquals(true, tenToTwenty.containsFloat(15f));
262: assertEquals(true, tenToTwenty.containsFloat(20f));
263: assertEquals(false, tenToTwenty.containsFloat(21f));
264: }
265:
266: //--------------------------------------------------------------------------
267:
268: public void testContainsRange() {
269: assertEquals(false, tenToTwenty.containsRange(null));
270: assertEquals(false, tenToTwenty.containsRange(createRange(five,
271: five)));
272: assertEquals(false, tenToTwenty.containsRange(createRange(five,
273: ten)));
274: assertEquals(false, tenToTwenty.containsRange(createRange(five,
275: twelve)));
276: assertEquals(false, tenToTwenty.containsRange(createRange(five,
277: fifteen)));
278: assertEquals(false, tenToTwenty.containsRange(createRange(five,
279: twenty)));
280: assertEquals(false, tenToTwenty.containsRange(createRange(five,
281: twentyFive)));
282:
283: assertEquals(true, tenToTwenty.containsRange(createRange(ten,
284: ten)));
285: assertEquals(true, tenToTwenty.containsRange(createRange(ten,
286: twelve)));
287: assertEquals(true, tenToTwenty.containsRange(createRange(ten,
288: fifteen)));
289: assertEquals(true, tenToTwenty.containsRange(createRange(ten,
290: twenty)));
291: assertEquals(false, tenToTwenty.containsRange(createRange(ten,
292: twentyFive)));
293:
294: assertEquals(true, tenToTwenty.containsRange(createRange(
295: twelve, twelve)));
296: assertEquals(true, tenToTwenty.containsRange(createRange(
297: twelve, fifteen)));
298: assertEquals(true, tenToTwenty.containsRange(createRange(
299: twelve, twenty)));
300: assertEquals(false, tenToTwenty.containsRange(createRange(
301: twelve, twentyFive)));
302:
303: assertEquals(true, tenToTwenty.containsRange(createRange(
304: fifteen, fifteen)));
305: assertEquals(true, tenToTwenty.containsRange(createRange(
306: fifteen, twenty)));
307: assertEquals(false, tenToTwenty.containsRange(createRange(
308: fifteen, twentyFive)));
309:
310: assertEquals(true, tenToTwenty.containsRange(createRange(
311: twenty, twenty)));
312: assertEquals(false, tenToTwenty.containsRange(createRange(
313: twenty, twentyFive)));
314:
315: assertEquals(false, tenToTwenty.containsRange(createRange(
316: twentyFive, twentyFive)));
317: }
318:
319: public void testOverlapsRange() {
320: assertEquals(false, tenToTwenty.overlapsRange(null));
321: assertEquals(false, tenToTwenty.overlapsRange(createRange(five,
322: five)));
323: assertEquals(true, tenToTwenty.overlapsRange(createRange(five,
324: ten)));
325: assertEquals(true, tenToTwenty.overlapsRange(createRange(five,
326: twelve)));
327: assertEquals(true, tenToTwenty.overlapsRange(createRange(five,
328: fifteen)));
329: assertEquals(true, tenToTwenty.overlapsRange(createRange(five,
330: twenty)));
331: assertEquals(true, tenToTwenty.overlapsRange(createRange(five,
332: twentyFive)));
333:
334: assertEquals(true, tenToTwenty.overlapsRange(createRange(ten,
335: ten)));
336: assertEquals(true, tenToTwenty.overlapsRange(createRange(ten,
337: twelve)));
338: assertEquals(true, tenToTwenty.overlapsRange(createRange(ten,
339: fifteen)));
340: assertEquals(true, tenToTwenty.overlapsRange(createRange(ten,
341: twenty)));
342: assertEquals(true, tenToTwenty.overlapsRange(createRange(ten,
343: twentyFive)));
344:
345: assertEquals(true, tenToTwenty.overlapsRange(createRange(
346: twelve, twelve)));
347: assertEquals(true, tenToTwenty.overlapsRange(createRange(
348: twelve, fifteen)));
349: assertEquals(true, tenToTwenty.overlapsRange(createRange(
350: twelve, twenty)));
351: assertEquals(true, tenToTwenty.overlapsRange(createRange(
352: twelve, twentyFive)));
353:
354: assertEquals(true, tenToTwenty.overlapsRange(createRange(
355: fifteen, fifteen)));
356: assertEquals(true, tenToTwenty.overlapsRange(createRange(
357: fifteen, twenty)));
358: assertEquals(true, tenToTwenty.overlapsRange(createRange(
359: fifteen, twentyFive)));
360:
361: assertEquals(true, tenToTwenty.overlapsRange(createRange(
362: twenty, twenty)));
363: assertEquals(true, tenToTwenty.overlapsRange(createRange(
364: twenty, twentyFive)));
365:
366: assertEquals(false, tenToTwenty.overlapsRange(createRange(
367: twentyFive, twentyFive)));
368: }
369:
370: //--------------------------------------------------------------------------
371:
372: public void testEquals() {
373: assertEquals(false, tenToTwenty
374: .equals(createRange(ten, fifteen)));
375: assertEquals(false, tenToTwenty.equals(createRange(ten,
376: twentyFive)));
377:
378: assertEquals(false, tenToTwenty.equals(createRange(fifteen,
379: twenty)));
380: assertEquals(false, tenToTwenty
381: .equals(createRange(five, twenty)));
382:
383: assertEquals(false, tenToTwenty.equals(createRange(five, ten)));
384: assertEquals(false, tenToTwenty.equals(createRange(ten)));
385:
386: assertEquals(true, tenToTwenty.equals(tenToTwenty));
387: assertEquals(true, tenToTwenty.equals(createRange(ten, twenty)));
388: assertEquals(true, tenToTwenty.equals(createRange(twenty, ten)));
389:
390: assertEquals(false, tenToTwenty.equals(null));
391: assertEquals(false, tenToTwenty.equals(new Object()));
392: assertEquals(false, tenToTwenty.equals(otherRange));
393: }
394:
395: public void testHashCode() {
396: assertEquals(tenToTwenty.hashCode(), tenToTwenty.hashCode());
397: assertTrue(tenToTwenty.hashCode() != 0);
398: }
399:
400: public void testToString() {
401: String str = tenToTwenty.toString();
402: assertEquals("Range[10,20]", str);
403: assertSame(str, tenToTwenty.toString());
404: assertEquals("Range[-20,-10]", createRange(new Integer(-20),
405: new Integer(-10)).toString());
406: }
407:
408: protected abstract Range createRange(Integer integer);
409:
410: protected abstract Range createRange(Integer integer1,
411: Integer integer2);
412:
413: }
|