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 IntRange} class.
026: *
027: * @author Stephen Colebourne
028: * @author Janek Bogucki
029: * @author Phil Steitz
030: * @version $Id: IntRangeTest.java 437554 2006-08-28 06:21:41Z bayard $
031: */
032: public final class IntRangeTest extends AbstractRangeTest {
033:
034: public IntRangeTest(String name) {
035: super (name);
036: }
037:
038: public static Test suite() {
039: TestSuite suite = new TestSuite(IntRangeTest.class);
040: suite.setName("IntRange Tests");
041: return suite;
042: }
043:
044: public void setUp() {
045: super .setUp();
046: tenToTwenty = new IntRange(ten, twenty);
047: otherRange = new NumberRange(ten, twenty);
048: }
049:
050: protected Range createRange(Integer integer1, Integer integer2) {
051: return new IntRange(integer1, integer2);
052: }
053:
054: protected Range createRange(Integer integer) {
055: return new NumberRange(integer);
056: }
057:
058: //--------------------------------------------------------------------------
059:
060: public void testConstructor1a() {
061: IntRange nr = new IntRange(5);
062: assertEquals(five, nr.getMinimumNumber());
063: assertEquals(five, nr.getMaximumNumber());
064: }
065:
066: public void testConstructor1b() {
067: IntRange nr = new IntRange(five);
068: assertSame(five, nr.getMinimumNumber());
069: assertSame(five, nr.getMaximumNumber());
070:
071: Range r = new IntRange(nonComparableNumber);
072:
073: try {
074: new IntRange(null);
075: fail();
076: } catch (IllegalArgumentException ex) {
077: }
078: }
079:
080: public void testConstructor2a() {
081: IntRange nr = new IntRange(5, 10);
082: assertEquals(five, nr.getMinimumNumber());
083: assertEquals(ten, nr.getMaximumNumber());
084:
085: nr = new IntRange(5, 10);
086: assertEquals(five, nr.getMinimumNumber());
087: assertEquals(ten, nr.getMaximumNumber());
088: }
089:
090: public void testConstructor2b() {
091: IntRange nr = new IntRange(five, ten);
092: assertSame(five, nr.getMinimumNumber());
093: assertSame(ten, nr.getMaximumNumber());
094:
095: nr = new IntRange(ten, five);
096: assertSame(five, nr.getMinimumNumber());
097: assertSame(ten, nr.getMaximumNumber());
098:
099: nr = new IntRange(five, long10);
100: assertSame(five, nr.getMinimumNumber());
101: assertEquals(ten, nr.getMaximumNumber());
102:
103: // test non Integer, for full coverage
104: Long fiveL = new Long(5L);
105: Long tenL = new Long(10L);
106: nr = new IntRange(fiveL, tenL);
107: assertEquals(five, nr.getMinimumNumber());
108: assertEquals(ten, nr.getMaximumNumber());
109: nr = new IntRange(tenL, fiveL);
110: assertEquals(five, nr.getMinimumNumber());
111: assertEquals(ten, nr.getMaximumNumber());
112:
113: // not null
114: try {
115: new IntRange(five, null);
116: fail();
117: } catch (IllegalArgumentException ex) {
118: }
119: try {
120: new IntRange(null, five);
121: fail();
122: } catch (IllegalArgumentException ex) {
123: }
124: try {
125: new IntRange(null, null);
126: fail();
127: } catch (IllegalArgumentException ex) {
128: }
129: }
130:
131: //--------------------------------------------------------------------------
132:
133: public void testContainsNumber() {
134: assertEquals(false, tenToTwenty.containsNumber(null));
135: assertEquals(true, tenToTwenty
136: .containsNumber(nonComparableNumber));
137:
138: assertEquals(false, tenToTwenty.containsNumber(five));
139: assertEquals(true, tenToTwenty.containsNumber(ten));
140: assertEquals(true, tenToTwenty.containsNumber(fifteen));
141: assertEquals(true, tenToTwenty.containsNumber(twenty));
142: assertEquals(false, tenToTwenty.containsNumber(twentyFive));
143:
144: assertEquals(false, tenToTwenty.containsNumber(long8));
145: assertEquals(true, tenToTwenty.containsNumber(long10));
146: assertEquals(true, tenToTwenty.containsNumber(long12));
147: assertEquals(true, tenToTwenty.containsNumber(long20));
148: assertEquals(false, tenToTwenty.containsNumber(long21));
149:
150: assertEquals(false, tenToTwenty.containsNumber(double8));
151: assertEquals(true, tenToTwenty.containsNumber(double10));
152: assertEquals(true, tenToTwenty.containsNumber(double12));
153: assertEquals(true, tenToTwenty.containsNumber(double20));
154: assertEquals(false, tenToTwenty.containsNumber(double21));
155:
156: assertEquals(false, tenToTwenty.containsNumber(float8));
157: assertEquals(true, tenToTwenty.containsNumber(float10));
158: assertEquals(true, tenToTwenty.containsNumber(float12));
159: assertEquals(true, tenToTwenty.containsNumber(float20));
160: assertEquals(false, tenToTwenty.containsNumber(float21));
161: }
162:
163: public void testContainsIntegerBig() {
164: IntRange big = new IntRange(Integer.MAX_VALUE,
165: Integer.MAX_VALUE - 2);
166: assertEquals(true, big.containsInteger(Integer.MAX_VALUE - 1));
167: assertEquals(false, big.containsInteger(Integer.MAX_VALUE - 3));
168: }
169:
170: //--------------------------------------------------------------------------
171:
172: }
|