KickJava   Java API By Example, From Geeks To Geeks.

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


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 IntRange} class.
23  *
24  * @author Stephen Colebourne
25  * @author Janek Bogucki
26  * @author Phil Steitz
27  * @version $Id: IntRangeTest.java 155423 2005-02-26 13:08:30Z dirkv $
28  */

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

56     public void testConstructor1a() {
57         IntRange nr = new IntRange(5);
58         assertEquals(five, nr.getMinimumNumber());
59         assertEquals(five, nr.getMaximumNumber());
60     }
61     
62     public void testConstructor1b() {
63         IntRange nr = new IntRange(five);
64         assertSame(five, nr.getMinimumNumber());
65         assertSame(five, nr.getMaximumNumber());
66         
67         Range r = new IntRange(nonComparable);
68         
69         try {
70             new IntRange(null);
71             fail();
72         } catch (IllegalArgumentException JavaDoc ex) {}
73     }
74     
75     public void testConstructor2a() {
76         IntRange nr = new IntRange(5, 10);
77         assertEquals(five, nr.getMinimumNumber());
78         assertEquals(ten, nr.getMaximumNumber());
79         
80         nr = new IntRange(5, 10);
81         assertEquals(five, nr.getMinimumNumber());
82         assertEquals(ten, nr.getMaximumNumber());
83     }
84
85     public void testConstructor2b() {
86         IntRange nr = new IntRange(five, ten);
87         assertSame(five, nr.getMinimumNumber());
88         assertSame(ten, nr.getMaximumNumber());
89         
90         nr = new IntRange(ten, five);
91         assertSame(five, nr.getMinimumNumber());
92         assertSame(ten, nr.getMaximumNumber());
93         
94         nr = new IntRange(five, long10);
95         assertSame(five, nr.getMinimumNumber());
96         assertEquals(ten, nr.getMaximumNumber());
97         
98         // test non Integer, for full coverage
99
Long JavaDoc fiveL = new Long JavaDoc(5L);
100         Long JavaDoc tenL = new Long JavaDoc(10L);
101         nr = new IntRange(fiveL, tenL);
102         assertEquals(five, nr.getMinimumNumber());
103         assertEquals(ten, nr.getMaximumNumber());
104         nr = new IntRange(tenL, fiveL);
105         assertEquals(five, nr.getMinimumNumber());
106         assertEquals(ten, nr.getMaximumNumber());
107         
108         // not null
109
try {
110             new IntRange(five, null);
111             fail();
112         } catch (IllegalArgumentException JavaDoc ex) {}
113         try {
114             new IntRange(null, five);
115             fail();
116         } catch (IllegalArgumentException JavaDoc ex) {}
117         try {
118             new IntRange(null, null);
119             fail();
120         } catch (IllegalArgumentException JavaDoc ex) {}
121     }
122
123     //--------------------------------------------------------------------------
124

125     public void testContainsNumber() {
126         assertEquals(false, tenToTwenty.containsNumber(null));
127         assertEquals(true, tenToTwenty.containsNumber(nonComparable));
128         
129         assertEquals(false, tenToTwenty.containsNumber(five));
130         assertEquals(true, tenToTwenty.containsNumber(ten));
131         assertEquals(true, tenToTwenty.containsNumber(fifteen));
132         assertEquals(true, tenToTwenty.containsNumber(twenty));
133         assertEquals(false, tenToTwenty.containsNumber(twentyFive));
134         
135         assertEquals(false, tenToTwenty.containsNumber(long8));
136         assertEquals(true, tenToTwenty.containsNumber(long10));
137         assertEquals(true, tenToTwenty.containsNumber(long12));
138         assertEquals(true, tenToTwenty.containsNumber(long20));
139         assertEquals(false, tenToTwenty.containsNumber(long21));
140         
141         assertEquals(false, tenToTwenty.containsNumber(double8));
142         assertEquals(true, tenToTwenty.containsNumber(double10));
143         assertEquals(true, tenToTwenty.containsNumber(double12));
144         assertEquals(true, tenToTwenty.containsNumber(double20));
145         assertEquals(false, tenToTwenty.containsNumber(double21));
146         
147         assertEquals(false, tenToTwenty.containsNumber(float8));
148         assertEquals(true, tenToTwenty.containsNumber(float10));
149         assertEquals(true, tenToTwenty.containsNumber(float12));
150         assertEquals(true, tenToTwenty.containsNumber(float20));
151         assertEquals(false, tenToTwenty.containsNumber(float21));
152     }
153
154     public void testContainsIntegerBig() {
155         IntRange big = new IntRange(Integer.MAX_VALUE, Integer.MAX_VALUE- 2);
156         assertEquals(true, big.containsInteger(Integer.MAX_VALUE - 1));
157         assertEquals(false, big.containsInteger(Integer.MAX_VALUE - 3));
158     }
159
160     //--------------------------------------------------------------------------
161

162 }
163
Popular Tags