1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.lang.math;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  
24  /**
25   * Test cases for the {@link FloatRange} class.
26   *
27   * @author Stephen Colebourne
28   * @version $Id: FloatRangeTest.java 437554 2006-08-28 06:21:41Z bayard $
29   */
30  public final class FloatRangeTest extends AbstractRangeTest {
31  
32      public FloatRangeTest(String name) {
33          super(name);
34      }
35  
36      public static Test suite() {
37          TestSuite suite = new TestSuite(FloatRangeTest.class);
38          suite.setName("FloatRange Tests");
39          return suite;
40      }
41      
42      public void setUp() {
43          super.setUp();
44          tenToTwenty = new FloatRange(float10, float20);
45          otherRange = new NumberRange(ten, twenty);
46      }
47  
48      protected Range createRange(Integer integer1, Integer integer2) {
49          return new FloatRange(integer1, integer2);
50      }
51      protected Range createRange(Integer integer) {
52          return new NumberRange(integer);
53      }
54      
55      //--------------------------------------------------------------------------
56  
57      public void testConstructor1a() {
58          FloatRange nr = new FloatRange(8f);
59          assertEquals(float8, nr.getMinimumNumber());
60          assertEquals(float8, nr.getMaximumNumber());
61          
62          try {
63              new FloatRange(Float.NaN);
64              fail();
65          } catch (IllegalArgumentException ex) {}
66      }
67      
68      public void testConstructor1b() {
69          FloatRange nr = new FloatRange(float8);
70          assertSame(float8, nr.getMinimumNumber());
71          assertSame(float8, nr.getMaximumNumber());
72          
73          Range r = new FloatRange(nonComparableNumber);
74          
75          try {
76              new FloatRange(null);
77              fail();
78          } catch (IllegalArgumentException ex) {}
79          try {
80              new FloatRange(new Double(Double.NaN));
81              fail();
82          } catch (IllegalArgumentException ex) {}
83      }
84      
85      public void testConstructor2a() {
86          FloatRange nr = new FloatRange(8f, 10f);
87          assertEquals(float8, nr.getMinimumNumber());
88          assertEquals(float10, nr.getMaximumNumber());
89          
90          nr = new FloatRange(10f, 8f);
91          assertEquals(float8, nr.getMinimumNumber());
92          assertEquals(float10, nr.getMaximumNumber());
93          
94          try {
95              new FloatRange(Float.NaN, 8f);
96              fail();
97          } catch (IllegalArgumentException ex) {}
98      }
99  
100     public void testConstructor2b() {
101         FloatRange nr = new FloatRange(float8, float10);
102         assertSame(float8, nr.getMinimumNumber());
103         assertSame(float10, nr.getMaximumNumber());
104         
105         nr = new FloatRange(float10, float8);
106         assertSame(float8, nr.getMinimumNumber());
107         assertSame(float10, nr.getMaximumNumber());
108         
109         nr = new FloatRange(float8, float10);
110         assertSame(float8, nr.getMinimumNumber());
111         assertEquals(float10, nr.getMaximumNumber());
112         
113         // not null
114         try {
115             new FloatRange(float8, null);
116             fail();
117         } catch (IllegalArgumentException ex) {}
118         try {
119             new FloatRange(null, float8);
120             fail();
121         } catch (IllegalArgumentException ex) {}
122         try {
123             new FloatRange(null, null);
124             fail();
125         } catch (IllegalArgumentException ex) {}
126         
127         try {
128             new FloatRange(new Double(Double.NaN), float10);
129             fail();
130         } catch (IllegalArgumentException ex) {}
131     }
132 
133     //--------------------------------------------------------------------------
134 
135     public void testContainsNumber() {
136         assertEquals(false, tenToTwenty.containsNumber(null));
137         assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
138         
139         assertEquals(false, tenToTwenty.containsNumber(five));
140         assertEquals(true, tenToTwenty.containsNumber(ten));
141         assertEquals(true, tenToTwenty.containsNumber(fifteen));
142         assertEquals(true, tenToTwenty.containsNumber(twenty));
143         assertEquals(false, tenToTwenty.containsNumber(twentyFive));
144         
145         assertEquals(false, tenToTwenty.containsNumber(long8));
146         assertEquals(true, tenToTwenty.containsNumber(long10));
147         assertEquals(true, tenToTwenty.containsNumber(long12));
148         assertEquals(true, tenToTwenty.containsNumber(long20));
149         assertEquals(false, tenToTwenty.containsNumber(long21));
150         
151         assertEquals(false, tenToTwenty.containsNumber(double8));
152         assertEquals(true, tenToTwenty.containsNumber(double10));
153         assertEquals(true, tenToTwenty.containsNumber(double12));
154         assertEquals(true, tenToTwenty.containsNumber(double20));
155         assertEquals(false, tenToTwenty.containsNumber(double21));
156         
157         assertEquals(false, tenToTwenty.containsNumber(float8));
158         assertEquals(true, tenToTwenty.containsNumber(float10));
159         assertEquals(true, tenToTwenty.containsNumber(float12));
160         assertEquals(true, tenToTwenty.containsNumber(float20));
161         assertEquals(false, tenToTwenty.containsNumber(float21));
162     }
163 
164     public void testToString() {
165         String str = tenToTwenty.toString();
166         assertEquals("Range[10.0,20.0]", str);
167         assertSame(str, tenToTwenty.toString());
168         assertEquals("Range[-20.0,-10.0]", createRange(new Integer(-20), new Integer(-10)).toString());
169     }
170     
171     //--------------------------------------------------------------------------
172     
173 }