KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 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 java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20 import java.math.BigDecimal JavaDoc;
21 import java.math.BigInteger JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27 import org.apache.commons.lang.SystemUtils;
28
29 /**
30  * Unit tests {@link org.apache.commons.lang.math.NumberUtils}.
31  *
32  * @author <a HREF="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
33  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
34  * @author Eric Pugh
35  * @author Phil Steitz
36  * @author Stephen Colebourne
37  * @author Matthew Hawthorne
38  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
39  * @version $Id: NumberUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
40  */

41 public class NumberUtilsTest extends TestCase {
42
43     public NumberUtilsTest(String JavaDoc name) {
44         super(name);
45     }
46
47     public static void main(String JavaDoc[] args) {
48         TestRunner.run(suite());
49     }
50     
51     public static Test suite() {
52         TestSuite suite = new TestSuite(NumberUtilsTest.class);
53         suite.setName("NumberUtils Tests");
54         return suite;
55     }
56
57     //-----------------------------------------------------------------------
58
public void testConstructor() {
59         assertNotNull(new NumberUtils());
60         Constructor JavaDoc[] cons = NumberUtils.class.getDeclaredConstructors();
61         assertEquals(1, cons.length);
62         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
63         assertEquals(true, Modifier.isPublic(NumberUtils.class.getModifiers()));
64         assertEquals(false, Modifier.isFinal(NumberUtils.class.getModifiers()));
65     }
66     
67     //---------------------------------------------------------------------
68

69     /**
70      * Test for {@link NumberUtils#stringToInt(String)}.
71      */

72     public void testStringToIntString() {
73         assertTrue("stringToInt(String) 1 failed", NumberUtils.stringToInt("12345") == 12345);
74         assertTrue("stringToInt(String) 2 failed", NumberUtils.stringToInt("abc") == 0);
75         assertTrue("stringToInt(empty) failed", NumberUtils.stringToInt("") == 0);
76         assertTrue("stringToInt(null) failed", NumberUtils.stringToInt(null) == 0);
77     }
78
79     /**
80      * Test for {@link NumberUtils#toInt(String)}.
81      */

82     public void testToIntString() {
83         assertTrue("toInt(String) 1 failed", NumberUtils.toInt("12345") == 12345);
84         assertTrue("toInt(String) 2 failed", NumberUtils.toInt("abc") == 0);
85         assertTrue("toInt(empty) failed", NumberUtils.toInt("") == 0);
86         assertTrue("toInt(null) failed", NumberUtils.toInt(null) == 0);
87     }
88
89     /**
90      * Test for {@link NumberUtils#stringToInt(String, int)}.
91      */

92     public void testStringToIntStringI() {
93         assertTrue("stringToInt(String,int) 1 failed", NumberUtils.stringToInt("12345", 5) == 12345);
94         assertTrue("stringToInt(String,int) 2 failed", NumberUtils.stringToInt("1234.5", 5) == 5);
95     }
96
97     /**
98      * Test for {@link NumberUtils#toInt(String, int)}.
99      */

100     public void testToIntStringI() {
101         assertTrue("toInt(String,int) 1 failed", NumberUtils.toInt("12345", 5) == 12345);
102         assertTrue("toInt(String,int) 2 failed", NumberUtils.toInt("1234.5", 5) == 5);
103     }
104
105     /**
106      * Test for {@link NumberUtils#toLong(String)}.
107      */

108     public void testToLongString() {
109         assertTrue("toLong(String) 1 failed", NumberUtils.toLong("12345") == 12345l);
110         assertTrue("toLong(String) 2 failed", NumberUtils.toLong("abc") == 0l);
111         assertTrue("toLong(String) 3 failed", NumberUtils.toLong("1L") == 0l);
112         assertTrue("toLong(String) 4 failed", NumberUtils.toLong("1l") == 0l);
113         assertTrue("toLong(Long.MAX_VALUE) failed", NumberUtils.toLong(Long.MAX_VALUE+"") == Long.MAX_VALUE);
114         assertTrue("toLong(Long.MIN_VALUE) failed", NumberUtils.toLong(Long.MIN_VALUE+"") == Long.MIN_VALUE);
115         assertTrue("toLong(empty) failed", NumberUtils.toLong("") == 0l);
116         assertTrue("toLong(null) failed", NumberUtils.toLong(null) == 0l);
117     }
118
119     /**
120      * Test for {@link NumberUtils#toLong(String, long)}.
121      */

122     public void testToLongStringL() {
123         assertTrue("toLong(String,long) 1 failed", NumberUtils.toLong("12345", 5l) == 12345l);
124         assertTrue("toLong(String,long) 2 failed", NumberUtils.toLong("1234.5", 5l) == 5l);
125     }
126
127     /**
128      * Test for {@link NumberUtils#toFloat(String)}.
129      */

130     public void testToFloatString() {
131         assertTrue("toFloat(String) 1 failed", NumberUtils.toFloat("-1.2345") == -1.2345f);
132         assertTrue("toFloat(String) 2 failed", NumberUtils.toFloat("1.2345") == 1.2345f);
133         assertTrue("toFloat(String) 3 failed", NumberUtils.toFloat("abc") == 0.0f);
134         assertTrue("toFloat(Float.MAX_VALUE) failed", NumberUtils.toFloat(Float.MAX_VALUE+"") == Float.MAX_VALUE);
135         assertTrue("toFloat(Float.MIN_VALUE) failed", NumberUtils.toFloat(Float.MIN_VALUE+"") == Float.MIN_VALUE);
136         assertTrue("toFloat(empty) failed", NumberUtils.toFloat("") == 0.0f);
137         assertTrue("toFloat(null) failed", NumberUtils.toFloat(null) == 0.0f);
138     }
139
140     /**
141      * Test for {@link NumberUtils#toFloat(String, float)}.
142      */

143     public void testToFloatStringF() {
144         assertTrue("toFloat(String,int) 1 failed", NumberUtils.toFloat("1.2345", 5.1f) == 1.2345f);
145         assertTrue("toFloat(String,int) 2 failed", NumberUtils.toFloat("a", 5.0f) == 5.0f);
146     }
147
148     /**
149      * Test for {@link NumberUtils#toDouble(String)}.
150      */

151     public void testStringToDoubleString() {
152         assertTrue("toDouble(String) 1 failed", NumberUtils.toDouble("-1.2345") == -1.2345d);
153         assertTrue("toDouble(String) 2 failed", NumberUtils.toDouble("1.2345") == 1.2345d);
154         assertTrue("toDouble(String) 3 failed", NumberUtils.toDouble("abc") == 0.0d);
155         assertTrue("toDouble(Double.MAX_VALUE) failed", NumberUtils.toDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE);
156         assertTrue("toDouble(Double.MIN_VALUE) failed", NumberUtils.toDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE);
157         assertTrue("toDouble(empty) failed", NumberUtils.toDouble("") == 0.0d);
158         assertTrue("toDouble(null) failed", NumberUtils.toDouble(null) == 0.0d);
159     }
160
161     /**
162      * Test for {@link NumberUtils#toDouble(String, double)}.
163      */

164     public void testStringToDoubleStringD() {
165         assertTrue("toDouble(String,int) 1 failed", NumberUtils.toDouble("1.2345", 5.1d) == 1.2345d);
166         assertTrue("toDouble(String,int) 2 failed", NumberUtils.toDouble("a", 5.0d) == 5.0d);
167     }
168
169     public void testCreateNumber() {
170         //a lot of things can go wrong
171
assertEquals("createNumber(String) 1 failed", new Float JavaDoc("1234.5"), NumberUtils.createNumber("1234.5"));
172         assertEquals("createNumber(String) 2 failed", new Integer JavaDoc("12345"), NumberUtils.createNumber("12345"));
173         assertEquals("createNumber(String) 3 failed", new Double JavaDoc("1234.5"), NumberUtils.createNumber("1234.5D"));
174         assertEquals("createNumber(String) 4 failed", new Float JavaDoc("1234.5"), NumberUtils.createNumber("1234.5F"));
175         assertEquals("createNumber(String) 5 failed", new Long JavaDoc(Integer.MAX_VALUE + 1L), NumberUtils.createNumber("" + (Integer.MAX_VALUE + 1L)));
176         assertEquals("createNumber(String) 6 failed", new Long JavaDoc(12345), NumberUtils.createNumber("12345L"));
177         assertEquals("createNumber(String) 7 failed", new Float JavaDoc("-1234.5"), NumberUtils.createNumber("-1234.5"));
178         assertEquals("createNumber(String) 8 failed", new Integer JavaDoc("-12345"), NumberUtils.createNumber("-12345"));
179         assertTrue("createNumber(String) 9 failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
180         assertTrue("createNumber(String) 10 failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue());
181         assertEquals("createNumber(String) 11 failed", new Double JavaDoc("1.1E200"), NumberUtils.createNumber("1.1E200"));
182         assertEquals("createNumber(String) 12 failed", new Float JavaDoc("1.1E20"), NumberUtils.createNumber("1.1E20"));
183         assertEquals("createNumber(String) 13 failed", new Double JavaDoc("-1.1E200"), NumberUtils.createNumber("-1.1E200"));
184         assertEquals("createNumber(String) 14 failed", new Double JavaDoc("1.1E-200"), NumberUtils.createNumber("1.1E-200"));
185         assertEquals("createNumber(null) failed", null, NumberUtils.createNumber(null));
186         
187         // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
188
if(SystemUtils.isJavaVersionAtLeast(1.3f)) {
189             assertEquals("createNumber(String) 15 failed", new BigDecimal JavaDoc("1.1E-700"), NumberUtils.createNumber("1.1E-700F"));
190         }
191         assertEquals(
192             "createNumber(String) 16 failed",
193             new Long JavaDoc("10" + Integer.MAX_VALUE),
194             NumberUtils.createNumber("10" + Integer.MAX_VALUE + "L"));
195         assertEquals(
196             "createNumber(String) 17 failed",
197             new Long JavaDoc("10" + Integer.MAX_VALUE),
198             NumberUtils.createNumber("10" + Integer.MAX_VALUE));
199         assertEquals(
200             "createNumber(String) 18 failed",
201             new BigInteger JavaDoc("10" + Long.MAX_VALUE),
202             NumberUtils.createNumber("10" + Long.MAX_VALUE));
203
204     }
205
206     public void testCreateFloat() {
207         assertEquals("createFloat(String) failed", new Float JavaDoc("1234.5"), NumberUtils.createFloat("1234.5"));
208         assertEquals("createFloat(null) failed", null, NumberUtils.createFloat(null));
209         this.testCreateFloatFailure("");
210         this.testCreateFloatFailure(" ");
211         this.testCreateFloatFailure("\b\t\n\f\r");
212         // Funky whitespaces
213
this.testCreateFloatFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
214     }
215
216     protected void testCreateFloatFailure(String JavaDoc str) {
217         try {
218             Float JavaDoc value = NumberUtils.createFloat(str);
219             fail("createFloat(blank) failed: " + value);
220         } catch (NumberFormatException JavaDoc ex) {
221             // empty
222
}
223     }
224
225     public void testCreateDouble() {
226         assertEquals("createDouble(String) failed", new Double JavaDoc("1234.5"), NumberUtils.createDouble("1234.5"));
227         assertEquals("createDouble(null) failed", null, NumberUtils.createDouble(null));
228         this.testCreateDoubleFailure("");
229         this.testCreateDoubleFailure(" ");
230         this.testCreateDoubleFailure("\b\t\n\f\r");
231         // Funky whitespaces
232
this.testCreateDoubleFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
233     }
234
235     protected void testCreateDoubleFailure(String JavaDoc str) {
236         try {
237             Double JavaDoc value = NumberUtils.createDouble(str);
238             fail("createDouble(blank) failed: " + value);
239         } catch (NumberFormatException JavaDoc ex) {
240             // empty
241
}
242     }
243
244     public void testCreateInteger() {
245         assertEquals("createInteger(String) failed", new Integer JavaDoc("12345"), NumberUtils.createInteger("12345"));
246         assertEquals("createInteger(null) failed", null, NumberUtils.createInteger(null));
247         this.testCreateIntegerFailure("");
248         this.testCreateIntegerFailure(" ");
249         this.testCreateIntegerFailure("\b\t\n\f\r");
250         // Funky whitespaces
251
this.testCreateIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
252     }
253
254     protected void testCreateIntegerFailure(String JavaDoc str) {
255         try {
256             Integer JavaDoc value = NumberUtils.createInteger(str);
257             fail("createInteger(blank) failed: " + value);
258         } catch (NumberFormatException JavaDoc ex) {
259             // empty
260
}
261     }
262
263     public void testCreateLong() {
264         assertEquals("createLong(String) failed", new Long JavaDoc("12345"), NumberUtils.createLong("12345"));
265         assertEquals("createLong(null) failed", null, NumberUtils.createLong(null));
266         this.testCreateLongFailure("");
267         this.testCreateLongFailure(" ");
268         this.testCreateLongFailure("\b\t\n\f\r");
269         // Funky whitespaces
270
this.testCreateLongFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
271     }
272
273     protected void testCreateLongFailure(String JavaDoc str) {
274         try {
275             Long JavaDoc value = NumberUtils.createLong(str);
276             fail("createLong(blank) failed: " + value);
277         } catch (NumberFormatException JavaDoc ex) {
278             // empty
279
}
280     }
281
282     public void testCreateBigInteger() {
283         assertEquals("createBigInteger(String) failed", new BigInteger JavaDoc("12345"), NumberUtils.createBigInteger("12345"));
284         assertEquals("createBigInteger(null) failed", null, NumberUtils.createBigInteger(null));
285         this.testCreateBigIntegerFailure("");
286         this.testCreateBigIntegerFailure(" ");
287         this.testCreateBigIntegerFailure("\b\t\n\f\r");
288         // Funky whitespaces
289
this.testCreateBigIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
290     }
291
292     protected void testCreateBigIntegerFailure(String JavaDoc str) {
293         try {
294             BigInteger JavaDoc value = NumberUtils.createBigInteger(str);
295             fail("createBigInteger(blank) failed: " + value);
296         } catch (NumberFormatException JavaDoc ex) {
297             // empty
298
}
299     }
300
301     public void testCreateBigDecimal() {
302         assertEquals("createBigDecimal(String) failed", new BigDecimal JavaDoc("1234.5"), NumberUtils.createBigDecimal("1234.5"));
303         assertEquals("createBigDecimal(null) failed", null, NumberUtils.createBigDecimal(null));
304         this.testCreateBigDecimalFailure("");
305         this.testCreateBigDecimalFailure(" ");
306         this.testCreateBigDecimalFailure("\b\t\n\f\r");
307         // Funky whitespaces
308
this.testCreateBigDecimalFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
309     }
310
311     protected void testCreateBigDecimalFailure(String JavaDoc str) {
312         try {
313             BigDecimal JavaDoc value = NumberUtils.createBigDecimal(str);
314             fail("createBigDecimal(blank) failed: " + value);
315         } catch (NumberFormatException JavaDoc ex) {
316             // empty
317
}
318     }
319
320     // min/max tests
321
// ----------------------------------------------------------------------
322
public void testMinLong() {
323         final long[] l = null;
324         try {
325             NumberUtils.min(l);
326             fail("No exception was thrown for null input.");
327         } catch (IllegalArgumentException JavaDoc ex) {}
328
329         try {
330             NumberUtils.min(new long[0]);
331             fail("No exception was thrown for empty input.");
332         } catch (IllegalArgumentException JavaDoc ex) {}
333
334         assertEquals(
335             "min(long[]) failed for array length 1",
336             5,
337             NumberUtils.min(new long[] { 5 }));
338
339         assertEquals(
340             "min(long[]) failed for array length 2",
341             6,
342             NumberUtils.min(new long[] { 6, 9 }));
343
344         assertEquals(-10, NumberUtils.min(new long[] { -10, -5, 0, 5, 10 }));
345         assertEquals(-10, NumberUtils.min(new long[] { -5, 0, -10, 5, 10 }));
346     }
347
348     public void testMinInt() {
349         final int[] i = null;
350         try {
351             NumberUtils.min(i);
352             fail("No exception was thrown for null input.");
353         } catch (IllegalArgumentException JavaDoc ex) {}
354
355         try {
356             NumberUtils.min(new int[0]);
357             fail("No exception was thrown for empty input.");
358         } catch (IllegalArgumentException JavaDoc ex) {}
359
360         assertEquals(
361             "min(int[]) failed for array length 1",
362             5,
363             NumberUtils.min(new int[] { 5 }));
364
365         assertEquals(
366             "min(int[]) failed for array length 2",
367             6,
368             NumberUtils.min(new int[] { 6, 9 }));
369
370         assertEquals(-10, NumberUtils.min(new int[] { -10, -5, 0, 5, 10 }));
371         assertEquals(-10, NumberUtils.min(new int[] { -5, 0, -10, 5, 10 }));
372     }
373
374     public void testMinShort() {
375         final short[] s = null;
376         try {
377             NumberUtils.min(s);
378             fail("No exception was thrown for null input.");
379         } catch (IllegalArgumentException JavaDoc ex) {}
380
381         try {
382             NumberUtils.min(new short[0]);
383             fail("No exception was thrown for empty input.");
384         } catch (IllegalArgumentException JavaDoc ex) {}
385
386         assertEquals(
387             "min(short[]) failed for array length 1",
388             5,
389             NumberUtils.min(new short[] { 5 }));
390
391         assertEquals(
392             "min(short[]) failed for array length 2",
393             6,
394             NumberUtils.min(new short[] { 6, 9 }));
395
396         assertEquals(-10, NumberUtils.min(new short[] { -10, -5, 0, 5, 10 }));
397         assertEquals(-10, NumberUtils.min(new short[] { -5, 0, -10, 5, 10 }));
398     }
399
400     public void testMinDouble() {
401         final double[] d = null;
402         try {
403             NumberUtils.min(d);
404             fail("No exception was thrown for null input.");
405         } catch (IllegalArgumentException JavaDoc ex) {}
406
407         try {
408             NumberUtils.min(new double[0]);
409             fail("No exception was thrown for empty input.");
410         } catch (IllegalArgumentException JavaDoc ex) {}
411
412         assertEquals(
413             "min(double[]) failed for array length 1",
414             5.12,
415             NumberUtils.min(new double[] { 5.12 }),
416             0);
417
418         assertEquals(
419             "min(double[]) failed for array length 2",
420             6.23,
421             NumberUtils.min(new double[] { 6.23, 9.34 }),
422             0);
423
424         assertEquals(
425             "min(double[]) failed for array length 5",
426             -10.45,
427             NumberUtils.min(new double[] { -10.45, -5.56, 0, 5.67, 10.78 }),
428             0);
429         assertEquals(-10, NumberUtils.min(new double[] { -10, -5, 0, 5, 10 }), 0.0001);
430         assertEquals(-10, NumberUtils.min(new double[] { -5, 0, -10, 5, 10 }), 0.0001);
431     }
432
433     public void testMinFloat() {
434         final float[] f = null;
435         try {
436             NumberUtils.min(f);
437             fail("No exception was thrown for null input.");
438         } catch (IllegalArgumentException JavaDoc ex) {}
439
440         try {
441             NumberUtils.min(new float[0]);
442             fail("No exception was thrown for empty input.");
443         } catch (IllegalArgumentException JavaDoc ex) {}
444
445         assertEquals(
446             "min(float[]) failed for array length 1",
447             5.9f,
448             NumberUtils.min(new float[] { 5.9f }),
449             0);
450
451         assertEquals(
452             "min(float[]) failed for array length 2",
453             6.8f,
454             NumberUtils.min(new float[] { 6.8f, 9.7f }),
455             0);
456
457         assertEquals(
458             "min(float[]) failed for array length 5",
459             -10.6f,
460             NumberUtils.min(new float[] { -10.6f, -5.5f, 0, 5.4f, 10.3f }),
461             0);
462         assertEquals(-10, NumberUtils.min(new float[] { -10, -5, 0, 5, 10 }), 0.0001f);
463         assertEquals(-10, NumberUtils.min(new float[] { -5, 0, -10, 5, 10 }), 0.0001f);
464     }
465
466     public void testMaxLong() {
467         final long[] l = null;
468         try {
469             NumberUtils.max(l);
470             fail("No exception was thrown for null input.");
471         } catch (IllegalArgumentException JavaDoc ex) {}
472
473         try {
474             NumberUtils.max(new long[0]);
475             fail("No exception was thrown for empty input.");
476         } catch (IllegalArgumentException JavaDoc ex) {}
477
478         assertEquals(
479             "max(long[]) failed for array length 1",
480             5,
481             NumberUtils.max(new long[] { 5 }));
482
483         assertEquals(
484             "max(long[]) failed for array length 2",
485             9,
486             NumberUtils.max(new long[] { 6, 9 }));
487
488         assertEquals(
489             "max(long[]) failed for array length 5",
490             10,
491             NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
492         assertEquals(10, NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
493         assertEquals(10, NumberUtils.max(new long[] { -5, 0, 10, 5, -10 }));
494     }
495
496     public void testMaxInt() {
497         final int[] i = null;
498         try {
499             NumberUtils.max(i);
500             fail("No exception was thrown for null input.");
501         } catch (IllegalArgumentException JavaDoc ex) {}
502
503         try {
504             NumberUtils.max(new int[0]);
505             fail("No exception was thrown for empty input.");
506         } catch (IllegalArgumentException JavaDoc ex) {}
507
508         assertEquals(
509             "max(int[]) failed for array length 1",
510             5,
511             NumberUtils.max(new int[] { 5 }));
512
513         assertEquals(
514             "max(int[]) failed for array length 2",
515             9,
516             NumberUtils.max(new int[] { 6, 9 }));
517
518         assertEquals(
519             "max(int[]) failed for array length 5",
520             10,
521             NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
522         assertEquals(10, NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
523         assertEquals(10, NumberUtils.max(new int[] { -5, 0, 10, 5, -10 }));
524     }
525
526     public void testMaxShort() {
527         final short[] s = null;
528         try {
529             NumberUtils.max(s);
530             fail("No exception was thrown for null input.");
531         } catch (IllegalArgumentException JavaDoc ex) {}
532
533         try {
534             NumberUtils.max(new short[0]);
535             fail("No exception was thrown for empty input.");
536         } catch (IllegalArgumentException JavaDoc ex) {}
537
538         assertEquals(
539             "max(short[]) failed for array length 1",
540             5,
541             NumberUtils.max(new short[] { 5 }));
542
543         assertEquals(
544             "max(short[]) failed for array length 2",
545             9,
546             NumberUtils.max(new short[] { 6, 9 }));
547
548         assertEquals(
549             "max(short[]) failed for array length 5",
550             10,
551             NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
552         assertEquals(10, NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
553         assertEquals(10, NumberUtils.max(new short[] { -5, 0, 10, 5, -10 }));
554     }
555
556     public void testMaxDouble() {
557         final double[] d = null;
558         try {
559             NumberUtils.max(d);
560             fail("No exception was thrown for null input.");
561         } catch (IllegalArgumentException JavaDoc ex) {}
562
563         try {
564             NumberUtils.max(new double[0]);
565             fail("No exception was thrown for empty input.");
566         } catch (IllegalArgumentException JavaDoc ex) {}
567
568         assertEquals(
569             "max(double[]) failed for array length 1",
570             5.1f,
571             NumberUtils.max(new double[] { 5.1f }),
572             0);
573
574         assertEquals(
575             "max(double[]) failed for array length 2",
576             9.2f,
577             NumberUtils.max(new double[] { 6.3f, 9.2f }),
578             0);
579
580         assertEquals(
581             "max(double[]) failed for float length 5",
582             10.4f,
583             NumberUtils.max(new double[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
584             0);
585         assertEquals(10, NumberUtils.max(new double[] { -10, -5, 0, 5, 10 }), 0.0001);
586         assertEquals(10, NumberUtils.max(new double[] { -5, 0, 10, 5, -10 }), 0.0001);
587     }
588  
589     public void testMaxFloat() {
590         final float[] f = null;
591         try {
592             NumberUtils.max(f);
593             fail("No exception was thrown for null input.");
594         } catch (IllegalArgumentException JavaDoc ex) {}
595
596         try {
597             NumberUtils.max(new float[0]);
598             fail("No exception was thrown for empty input.");
599         } catch (IllegalArgumentException JavaDoc ex) {}
600
601         assertEquals(
602             "max(float[]) failed for array length 1",
603             5.1f,
604             NumberUtils.max(new float[] { 5.1f }),
605             0);
606
607         assertEquals(
608             "max(float[]) failed for array length 2",
609             9.2f,
610             NumberUtils.max(new float[] { 6.3f, 9.2f }),
611             0);
612
613         assertEquals(
614             "max(float[]) failed for float length 5",
615             10.4f,
616             NumberUtils.max(new float[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
617             0);
618         assertEquals(10, NumberUtils.max(new float[] { -10, -5, 0, 5, 10 }), 0.0001f);
619         assertEquals(10, NumberUtils.max(new float[] { -5, 0, 10, 5, -10 }), 0.0001f);
620     }
621
622     public void testMinimumLong() {
623         assertEquals("minimum(long,long,long) 1 failed", 12345L, NumberUtils.min(12345L, 12345L + 1L, 12345L + 2L));
624         assertEquals("minimum(long,long,long) 2 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L, 12345 + 2L));
625         assertEquals("minimum(long,long,long) 3 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L + 2L, 12345L));
626         assertEquals("minimum(long,long,long) 4 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L, 12345L));
627         assertEquals("minimum(long,long,long) 5 failed", 12345L, NumberUtils.min(12345L, 12345L, 12345L));
628     }
629
630     public void testMinimumInt() {
631         assertEquals("minimum(int,int,int) 1 failed", 12345, NumberUtils.min(12345, 12345 + 1, 12345 + 2));
632         assertEquals("minimum(int,int,int) 2 failed", 12345, NumberUtils.min(12345 + 1, 12345, 12345 + 2));
633         assertEquals("minimum(int,int,int) 3 failed", 12345, NumberUtils.min(12345 + 1, 12345 + 2, 12345));
634         assertEquals("minimum(int,int,int) 4 failed", 12345, NumberUtils.min(12345 + 1, 12345, 12345));
635         assertEquals("minimum(int,int,int) 5 failed", 12345, NumberUtils.min(12345, 12345, 12345));
636     }
637
638     public void testMinimumShort() {
639         short low = 1234;
640         short mid = 1234 + 1;
641         short high = 1234 + 2;
642         assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(low, mid, high));
643         assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(mid, low, high));
644         assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(mid, high, low));
645         assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(low, mid, low));
646     }
647
648     public void testMinimumByte() {
649         byte low = 123;
650         byte mid = 123 + 1;
651         byte high = 123 + 2;
652         assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(low, mid, high));
653         assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(mid, low, high));
654         assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(mid, high, low));
655         assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(low, mid, low));
656     }
657
658     public void testMinimumDouble() {
659         double low = 12.3;
660         double mid = 12.3 + 1;
661         double high = 12.3 + 2;
662         assertEquals(low, NumberUtils.min(low, mid, high), 0.0001);
663         assertEquals(low, NumberUtils.min(mid, low, high), 0.0001);
664         assertEquals(low, NumberUtils.min(mid, high, low), 0.0001);
665         assertEquals(low, NumberUtils.min(low, mid, low), 0.0001);
666         assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001);
667     }
668
669     public void testMinimumFloat() {
670         float low = 12.3f;
671         float mid = 12.3f + 1;
672         float high = 12.3f + 2;
673         assertEquals(low, NumberUtils.min(low, mid, high), 0.0001f);
674         assertEquals(low, NumberUtils.min(mid, low, high), 0.0001f);
675         assertEquals(low, NumberUtils.min(mid, high, low), 0.0001f);
676         assertEquals(low, NumberUtils.min(low, mid, low), 0.0001f);
677         assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001f);
678     }
679
680     public void testMaximumLong() {
681         assertEquals("maximum(long,long,long) 1 failed", 12345L, NumberUtils.max(12345L, 12345L - 1L, 12345L - 2L));
682         assertEquals("maximum(long,long,long) 2 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L - 2L));
683         assertEquals("maximum(long,long,long) 3 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L - 2L, 12345L));
684         assertEquals("maximum(long,long,long) 4 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L));
685         assertEquals("maximum(long,long,long) 5 failed", 12345L, NumberUtils.max(12345L, 12345L, 12345L));
686     }
687
688     public void testMaximumInt() {
689         assertEquals("maximum(int,int,int) 1 failed", 12345, NumberUtils.max(12345, 12345 - 1, 12345 - 2));
690         assertEquals("maximum(int,int,int) 2 failed", 12345, NumberUtils.max(12345 - 1, 12345, 12345 - 2));
691         assertEquals("maximum(int,int,int) 3 failed", 12345, NumberUtils.max(12345 - 1, 12345 - 2, 12345));
692         assertEquals("maximum(int,int,int) 4 failed", 12345, NumberUtils.max(12345 - 1, 12345, 12345));
693         assertEquals("maximum(int,int,int) 5 failed", 12345, NumberUtils.max(12345, 12345, 12345));
694     }
695
696     public void testMaximumShort() {
697         short low = 1234;
698         short mid = 1234 + 1;
699         short high = 1234 + 2;
700         assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(low, mid, high));
701         assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(mid, low, high));
702         assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(mid, high, low));
703         assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(high, mid, high));
704     }
705
706     public void testMaximumByte() {
707         byte low = 123;
708         byte mid = 123 + 1;
709         byte high = 123 + 2;
710         assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(low, mid, high));
711         assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(mid, low, high));
712         assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(mid, high, low));
713         assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(high, mid, high));
714     }
715
716     public void testMaximumDouble() {
717         double low = 12.3;
718         double mid = 12.3 + 1;
719         double high = 12.3 + 2;
720         assertEquals(high, NumberUtils.max(low, mid, high), 0.0001);
721         assertEquals(high, NumberUtils.max(mid, low, high), 0.0001);
722         assertEquals(high, NumberUtils.max(mid, high, low), 0.0001);
723         assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001);
724         assertEquals(high, NumberUtils.max(high, mid, high), 0.0001);
725     }
726
727     public void testMaximumFloat() {
728         float low = 12.3f;
729         float mid = 12.3f + 1;
730         float high = 12.3f + 2;
731         assertEquals(high, NumberUtils.max(low, mid, high), 0.0001f);
732         assertEquals(high, NumberUtils.max(mid, low, high), 0.0001f);
733         assertEquals(high, NumberUtils.max(mid, high, low), 0.0001f);
734         assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001f);
735         assertEquals(high, NumberUtils.max(high, mid, high), 0.0001f);
736     }
737
738     public void testCompareDouble() {
739         assertTrue(NumberUtils.compare(Double.NaN, Double.NaN) == 0);
740         assertTrue(NumberUtils.compare(Double.NaN, Double.POSITIVE_INFINITY) == +1);
741         assertTrue(NumberUtils.compare(Double.NaN, Double.MAX_VALUE) == +1);
742         assertTrue(NumberUtils.compare(Double.NaN, 1.2d) == +1);
743         assertTrue(NumberUtils.compare(Double.NaN, 0.0d) == +1);
744         assertTrue(NumberUtils.compare(Double.NaN, -0.0d) == +1);
745         assertTrue(NumberUtils.compare(Double.NaN, -1.2d) == +1);
746         assertTrue(NumberUtils.compare(Double.NaN, -Double.MAX_VALUE) == +1);
747         assertTrue(NumberUtils.compare(Double.NaN, Double.NEGATIVE_INFINITY) == +1);
748         
749         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.NaN) == -1);
750         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) == 0);
751         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.MAX_VALUE) == +1);
752         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 1.2d) == +1);
753         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 0.0d) == +1);
754         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -0.0d) == +1);
755         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -1.2d) == +1);
756         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -Double.MAX_VALUE) == +1);
757         assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY) == +1);
758         
759         assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NaN) == -1);
760         assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1);
761         assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.MAX_VALUE) == 0);
762         assertTrue(NumberUtils.compare(Double.MAX_VALUE, 1.2d) == +1);
763         assertTrue(NumberUtils.compare(Double.MAX_VALUE, 0.0d) == +1);
764         assertTrue(NumberUtils.compare(Double.MAX_VALUE, -0.0d) == +1);
765         assertTrue(NumberUtils.compare(Double.MAX_VALUE, -1.2d) == +1);
766         assertTrue(NumberUtils.compare(Double.MAX_VALUE, -Double.MAX_VALUE) == +1);
767         assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1);
768         
769         assertTrue(NumberUtils.compare(1.2d, Double.NaN) == -1);
770         assertTrue(NumberUtils.compare(1.2d, Double.POSITIVE_INFINITY) == -1);
771         assertTrue(NumberUtils.compare(1.2d, Double.MAX_VALUE) == -1);
772         assertTrue(NumberUtils.compare(1.2d, 1.2d) == 0);
773         assertTrue(NumberUtils.compare(1.2d, 0.0d) == +1);
774         assertTrue(NumberUtils.compare(1.2d, -0.0d) == +1);
775         assertTrue(NumberUtils.compare(1.2d, -1.2d) == +1);
776         assertTrue(NumberUtils.compare(1.2d, -Double.MAX_VALUE) == +1);
777         assertTrue(NumberUtils.compare(1.2d, Double.NEGATIVE_INFINITY) == +1);
778         
779         assertTrue(NumberUtils.compare(0.0d, Double.NaN) == -1);
780         assertTrue(NumberUtils.compare(0.0d, Double.POSITIVE_INFINITY) == -1);
781         assertTrue(NumberUtils.compare(0.0d, Double.MAX_VALUE) == -1);
782         assertTrue(NumberUtils.compare(0.0d, 1.2d) == -1);
783         assertTrue(NumberUtils.compare(0.0d, 0.0d) == 0);
784         assertTrue(NumberUtils.compare(0.0d, -0.0d) == +1);
785         assertTrue(NumberUtils.compare(0.0d, -1.2d) == +1);
786         assertTrue(NumberUtils.compare(0.0d, -Double.MAX_VALUE) == +1);
787         assertTrue(NumberUtils.compare(0.0d, Double.NEGATIVE_INFINITY) == +1);
788         
789         assertTrue(NumberUtils.compare(-0.0d, Double.NaN) == -1);
790         assertTrue(NumberUtils.compare(-0.0d, Double.POSITIVE_INFINITY) == -1);
791         assertTrue(NumberUtils.compare(-0.0d, Double.MAX_VALUE) == -1);
792         assertTrue(NumberUtils.compare(-0.0d, 1.2d) == -1);
793         assertTrue(NumberUtils.compare(-0.0d, 0.0d) == -1);
794         assertTrue(NumberUtils.compare(-0.0d, -0.0d) == 0);
795         assertTrue(NumberUtils.compare(-0.0d, -1.2d) == +1);
796         assertTrue(NumberUtils.compare(-0.0d, -Double.MAX_VALUE) == +1);
797         assertTrue(NumberUtils.compare(-0.0d, Double.NEGATIVE_INFINITY) == +1);
798         
799         assertTrue(NumberUtils.compare(-1.2d, Double.NaN) == -1);
800         assertTrue(NumberUtils.compare(-1.2d, Double.POSITIVE_INFINITY) == -1);
801         assertTrue(NumberUtils.compare(-1.2d, Double.MAX_VALUE) == -1);
802         assertTrue(NumberUtils.compare(-1.2d, 1.2d) == -1);
803         assertTrue(NumberUtils.compare(-1.2d, 0.0d) == -1);
804         assertTrue(NumberUtils.compare(-1.2d, -0.0d) == -1);
805         assertTrue(NumberUtils.compare(-1.2d, -1.2d) == 0);
806         assertTrue(NumberUtils.compare(-1.2d, -Double.MAX_VALUE) == +1);
807         assertTrue(NumberUtils.compare(-1.2d, Double.NEGATIVE_INFINITY) == +1);
808         
809         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NaN) == -1);
810         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1);
811         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.MAX_VALUE) == -1);
812         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 1.2d) == -1);
813         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 0.0d) == -1);
814         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -0.0d) == -1);
815         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -1.2d) == -1);
816         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -Double.MAX_VALUE) == 0);
817         assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1);
818         
819         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.NaN) == -1);
820         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY) == -1);
821         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.MAX_VALUE) == -1);
822         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 1.2d) == -1);
823         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 0.0d) == -1);
824         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -0.0d) == -1);
825         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -1.2d) == -1);
826         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE) == -1);
827         assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) == 0);
828     }
829
830     public void testCompareFloat() {
831         assertTrue(NumberUtils.compare(Float.NaN, Float.NaN) == 0);
832         assertTrue(NumberUtils.compare(Float.NaN, Float.POSITIVE_INFINITY) == +1);
833         assertTrue(NumberUtils.compare(Float.NaN, Float.MAX_VALUE) == +1);
834         assertTrue(NumberUtils.compare(Float.NaN, 1.2f) == +1);
835         assertTrue(NumberUtils.compare(Float.NaN, 0.0f) == +1);
836         assertTrue(NumberUtils.compare(Float.NaN, -0.0f) == +1);
837         assertTrue(NumberUtils.compare(Float.NaN, -1.2f) == +1);
838         assertTrue(NumberUtils.compare(Float.NaN, -Float.MAX_VALUE) == +1);
839         assertTrue(NumberUtils.compare(Float.NaN, Float.NEGATIVE_INFINITY) == +1);
840         
841         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.NaN) == -1);
842         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) == 0);
843         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.MAX_VALUE) == +1);
844         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 1.2f) == +1);
845         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 0.0f) == +1);
846         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -0.0f) == +1);
847         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -1.2f) == +1);
848         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -Float.MAX_VALUE) == +1);
849         assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY) == +1);
850         
851         assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NaN) == -1);
852         assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1);
853         assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.MAX_VALUE) == 0);
854         assertTrue(NumberUtils.compare(Float.MAX_VALUE, 1.2f) == +1);
855         assertTrue(NumberUtils.compare(Float.MAX_VALUE, 0.0f) == +1);
856         assertTrue(NumberUtils.compare(Float.MAX_VALUE, -0.0f) == +1);
857         assertTrue(NumberUtils.compare(Float.MAX_VALUE, -1.2f) == +1);
858         assertTrue(NumberUtils.compare(Float.MAX_VALUE, -Float.MAX_VALUE) == +1);
859         assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1);
860         
861         assertTrue(NumberUtils.compare(1.2f, Float.NaN) == -1);
862         assertTrue(NumberUtils.compare(1.2f, Float.POSITIVE_INFINITY) == -1);
863         assertTrue(NumberUtils.compare(1.2f, Float.MAX_VALUE) == -1);
864         assertTrue(NumberUtils.compare(1.2f, 1.2f) == 0);
865         assertTrue(NumberUtils.compare(1.2f, 0.0f) == +1);
866         assertTrue(NumberUtils.compare(1.2f, -0.0f) == +1);
867         assertTrue(NumberUtils.compare(1.2f, -1.2f) == +1);
868         assertTrue(NumberUtils.compare(1.2f, -Float.MAX_VALUE) == +1);
869         assertTrue(NumberUtils.compare(1.2f, Float.NEGATIVE_INFINITY) == +1);
870         
871         assertTrue(NumberUtils.compare(0.0f, Float.NaN) == -1);
872         assertTrue(NumberUtils.compare(0.0f, Float.POSITIVE_INFINITY) == -1);
873         assertTrue(NumberUtils.compare(0.0f, Float.MAX_VALUE) == -1);
874         assertTrue(NumberUtils.compare(0.0f, 1.2f) == -1);
875         assertTrue(NumberUtils.compare(0.0f, 0.0f) == 0);
876         assertTrue(NumberUtils.compare(0.0f, -0.0f) == +1);
877         assertTrue(NumberUtils.compare(0.0f, -1.2f) == +1);
878         assertTrue(NumberUtils.compare(0.0f, -Float.MAX_VALUE) == +1);
879         assertTrue(NumberUtils.compare(0.0f, Float.NEGATIVE_INFINITY) == +1);
880         
881         assertTrue(NumberUtils.compare(-0.0f, Float.NaN) == -1);
882         assertTrue(NumberUtils.compare(-0.0f, Float.POSITIVE_INFINITY) == -1);
883         assertTrue(NumberUtils.compare(-0.0f, Float.MAX_VALUE) == -1);
884         assertTrue(NumberUtils.compare(-0.0f, 1.2f) == -1);
885         assertTrue(NumberUtils.compare(-0.0f, 0.0f) == -1);
886         assertTrue(NumberUtils.compare(-0.0f, -0.0f) == 0);
887         assertTrue(NumberUtils.compare(-0.0f, -1.2f) == +1);
888         assertTrue(NumberUtils.compare(-0.0f, -Float.MAX_VALUE) == +1);
889         assertTrue(NumberUtils.compare(-0.0f, Float.NEGATIVE_INFINITY) == +1);
890         
891         assertTrue(NumberUtils.compare(-1.2f, Float.NaN) == -1);
892         assertTrue(NumberUtils.compare(-1.2f, Float.POSITIVE_INFINITY) == -1);
893         assertTrue(NumberUtils.compare(-1.2f, Float.MAX_VALUE) == -1);
894         assertTrue(NumberUtils.compare(-1.2f, 1.2f) == -1);
895         assertTrue(NumberUtils.compare(-1.2f, 0.0f) == -1);
896         assertTrue(NumberUtils.compare(-1.2f, -0.0f) == -1);
897         assertTrue(NumberUtils.compare(-1.2f, -1.2f) == 0);
898         assertTrue(NumberUtils.compare(-1.2f, -Float.MAX_VALUE) == +1);
899         assertTrue(NumberUtils.compare(-1.2f, Float.NEGATIVE_INFINITY) == +1);
900         
901         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NaN) == -1);
902         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1);
903         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.MAX_VALUE) == -1);
904         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 1.2f) == -1);
905         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 0.0f) == -1);
906         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -0.0f) == -1);
907         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -1.2f) == -1);
908         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -Float.MAX_VALUE) == 0);
909         assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1);
910         
911         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.NaN) == -1);
912         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY) == -1);
913         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.MAX_VALUE) == -1);
914         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 1.2f) == -1);
915         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 0.0f) == -1);
916         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -0.0f) == -1);
917         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -1.2f) == -1);
918         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -Float.MAX_VALUE) == -1);
919         assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) == 0);
920     }
921
922     public void testIsDigits() {
923         assertEquals("isDigits(null) failed", false, NumberUtils.isDigits(null));
924         assertEquals("isDigits('') failed", false, NumberUtils.isDigits(""));
925         assertEquals("isDigits(String) failed", true, NumberUtils.isDigits("12345"));
926         assertEquals("isDigits(String) neg 1 failed", false, NumberUtils.isDigits("1234.5"));
927         assertEquals("isDigits(String) neg 3 failed", false, NumberUtils.isDigits("1ab"));
928         assertEquals("isDigits(String) neg 4 failed", false, NumberUtils.isDigits("abc"));
929     }
930     
931     /**
932      * Tests isNumber(String) and tests that createNumber(String) returns
933      * a valid number iff isNumber(String) returns false.
934      */

935     public void testIsNumber() {
936         String JavaDoc val = "12345";
937         assertTrue("isNumber(String) 1 failed", NumberUtils.isNumber(val));
938         assertTrue("isNumber(String)/createNumber(String) 1 failed", checkCreateNumber(val));
939         val = "1234.5";
940         assertTrue("isNumber(String) 2 failed", NumberUtils.isNumber(val));
941         assertTrue("isNumber(String)/createNumber(String) 2 failed", checkCreateNumber(val));
942         val = ".12345";
943         assertTrue("isNumber(String) 3 failed", NumberUtils.isNumber(val));
944         assertTrue("isNumber(String)/createNumber(String) 3 failed", checkCreateNumber(val));
945         val = "1234E5";
946         assertTrue("isNumber(String) 4 failed", NumberUtils.isNumber(val));
947         assertTrue("isNumber(String)/createNumber(String) 4 failed", checkCreateNumber(val));
948         val = "1234E+5";
949         assertTrue("isNumber(String) 5 failed", NumberUtils.isNumber(val));
950         assertTrue("isNumber(String)/createNumber(String) 5 failed", checkCreateNumber(val));
951         val = "1234E-5";
952         assertTrue("isNumber(String) 6 failed", NumberUtils.isNumber(val));
953         assertTrue("isNumber(String)/createNumber(String) 6 failed", checkCreateNumber(val));
954         val = "123.4E5";
955         assertTrue("isNumber(String) 7 failed", NumberUtils.isNumber(val));
956         assertTrue("isNumber(String)/createNumber(String) 7 failed", checkCreateNumber(val));
957         val = "-1234";
958         assertTrue("isNumber(String) 8 failed", NumberUtils.isNumber(val));
959         assertTrue("isNumber(String)/createNumber(String) 8 failed", checkCreateNumber(val));
960         val = "-1234.5";
961         assertTrue("isNumber(String) 9 failed", NumberUtils.isNumber(val));
962         assertTrue("isNumber(String)/createNumber(String) 9 failed", checkCreateNumber(val));
963         val = "-.12345";
964         assertTrue("isNumber(String) 10 failed", NumberUtils.isNumber(val));
965         assertTrue("isNumber(String)/createNumber(String) 10 failed", checkCreateNumber(val));
966         val = "-1234E5";
967         assertTrue("isNumber(String) 11 failed", NumberUtils.isNumber(val));
968         assertTrue("isNumber(String)/createNumber(String) 11 failed", checkCreateNumber(val));
969         val = "0";
970         assertTrue("isNumber(String) 12 failed", NumberUtils.isNumber(val));
971         assertTrue("isNumber(String)/createNumber(String) 12 failed", checkCreateNumber(val));
972         val = "-0";
973         assertTrue("isNumber(String) 13 failed", NumberUtils.isNumber(val));
974         assertTrue("isNumber(String)/createNumber(String) 13 failed", checkCreateNumber(val));
975         val = "01234";
976         assertTrue("isNumber(String) 14 failed", NumberUtils.isNumber(val));
977         assertTrue("isNumber(String)/createNumber(String) 14 failed", checkCreateNumber(val));
978         val = "-01234";
979         assertTrue("isNumber(String) 15 failed", NumberUtils.isNumber(val));
980         assertTrue("isNumber(String)/createNumber(String) 15 failed", checkCreateNumber(val));
981         val = "0xABC123";
982         assertTrue("isNumber(String) 16 failed", NumberUtils.isNumber(val));
983         assertTrue("isNumber(String)/createNumber(String) 16 failed", checkCreateNumber(val));
984         val = "0x0";
985         assertTrue("isNumber(String) 17 failed", NumberUtils.isNumber(val));
986         assertTrue("isNumber(String)/createNumber(String) 17 failed", checkCreateNumber(val));
987         val = "123.4E21D";
988         assertTrue("isNumber(String) 19 failed", NumberUtils.isNumber(val));
989         assertTrue("isNumber(String)/createNumber(String) 19 failed", checkCreateNumber(val));
990         val = "-221.23F";
991         assertTrue("isNumber(String) 20 failed", NumberUtils.isNumber(val));
992         assertTrue("isNumber(String)/createNumber(String) 20 failed", checkCreateNumber(val));
993         val = "22338L";
994         assertTrue("isNumber(String) 21 failed", NumberUtils.isNumber(val));
995         assertTrue("isNumber(String)/createNumber(String) 21 failed", checkCreateNumber(val));
996         val = null;
997         assertTrue("isNumber(String) 1 Neg failed", !NumberUtils.isNumber(val));
998         assertTrue("isNumber(String)/createNumber(String) 1 Neg failed", !checkCreateNumber(val));
999         val = "";
1000        assertTrue("isNumber(String) 2 Neg failed", !NumberUtils.isNumber(val));
1001        assertTrue("isNumber(String)/createNumber(String) 2 Neg failed", !checkCreateNumber(val));
1002        val = "--2.3";
1003        assertTrue("isNumber(String) 3 Neg failed", !NumberUtils.isNumber(val));
1004        assertTrue("isNumber(String)/createNumber(String) 3 Neg failed", !checkCreateNumber(val));
1005        val = ".12.3";
1006        assertTrue("isNumber(String) 4 Neg failed", !NumberUtils.isNumber(val));
1007        assertTrue("isNumber(String)/createNumber(String) 4 Neg failed", !checkCreateNumber(val));
1008        val = "-123E";
1009        assertTrue("isNumber(String) 5 Neg failed", !NumberUtils.isNumber(val));
1010        assertTrue("isNumber(String)/createNumber(String) 5 Neg failed", !checkCreateNumber(val));
1011        val = "-123E+-212";
1012        assertTrue("isNumber(String) 6 Neg failed", !NumberUtils.isNumber(val));
1013        assertTrue("isNumber(String)/createNumber(String) 6 Neg failed", !checkCreateNumber(val));
1014        val = "-123E2.12";
1015        assertTrue("isNumber(String) 7 Neg failed", !NumberUtils.isNumber(val));
1016        assertTrue("isNumber(String)/createNumber(String) 7 Neg failed", !checkCreateNumber(val));
1017        val = "0xGF";
1018        assertTrue("isNumber(String) 8 Neg failed", !NumberUtils.isNumber(val));
1019        assertTrue("isNumber(String)/createNumber(String) 8 Neg failed", !checkCreateNumber(val));
1020        val = "0xFAE-1";
1021        assertTrue("isNumber(String) 9 Neg failed", !NumberUtils.isNumber(val));
1022        assertTrue("isNumber(String)/createNumber(String) 9 Neg failed", !checkCreateNumber(val));
1023        val = ".";
1024        assertTrue("isNumber(String) 10 Neg failed", !NumberUtils.isNumber(val));
1025        assertTrue("isNumber(String)/createNumber(String) 10 Neg failed", !checkCreateNumber(val));
1026        val = "-0ABC123";
1027        assertTrue("isNumber(String) 11 Neg failed", !NumberUtils.isNumber(val));
1028        assertTrue("isNumber(String)/createNumber(String) 11 Neg failed", !checkCreateNumber(val));
1029        val = "123.4E-D";
1030        assertTrue("isNumber(String) 12 Neg failed", !NumberUtils.isNumber(val));
1031        assertTrue("isNumber(String)/createNumber(String) 12 Neg failed", !checkCreateNumber(val));
1032        val = "123.4ED";
1033        assertTrue("isNumber(String) 13 Neg failed", !NumberUtils.isNumber(val));
1034        assertTrue("isNumber(String)/createNumber(String) 13 Neg failed", !checkCreateNumber(val));
1035        val = "1234E5l";
1036        assertTrue("isNumber(String) 14 Neg failed", !NumberUtils.isNumber(val));
1037        assertTrue("isNumber(String)/createNumber(String) 14 Neg failed", !checkCreateNumber(val));
1038        val = "11a";
1039        assertTrue("isNumber(String) 15 Neg failed", !NumberUtils.isNumber(val));
1040        assertTrue("isNumber(String)/createNumber(String) 15 Neg failed", !checkCreateNumber(val));
1041        val = "1a";
1042        assertTrue("isNumber(String) 16 Neg failed", !NumberUtils.isNumber(val));
1043        assertTrue("isNumber(String)/createNumber(String) 16 Neg failed", !checkCreateNumber(val));
1044        val = "a";
1045        assertTrue("isNumber(String) 17 Neg failed", !NumberUtils.isNumber(val));
1046        assertTrue("isNumber(String)/createNumber(String) 17 Neg failed", !checkCreateNumber(val));
1047        val = "11g";
1048        assertTrue("isNumber(String) 18 Neg failed", !NumberUtils.isNumber(val));
1049        assertTrue("isNumber(String)/createNumber(String) 18 Neg failed", !checkCreateNumber(val));
1050        val = "11z";
1051        assertTrue("isNumber(String) 19 Neg failed", !NumberUtils.isNumber(val));
1052        assertTrue("isNumber(String)/createNumber(String) 19 Neg failed", !checkCreateNumber(val));
1053        val = "11def";
1054        assertTrue("isNumber(String) 20 Neg failed", !NumberUtils.isNumber(val));
1055        assertTrue("isNumber(String)/createNumber(String) 20 Neg failed", !checkCreateNumber(val));
1056        val = "11d11";
1057        assertTrue("isNumber(String) 21 Neg failed", !NumberUtils.isNumber(val));
1058        assertTrue("isNumber(String)/createNumber(String) 21 Neg failed", !checkCreateNumber(val));
1059        val = "11 11";
1060        assertTrue("isNumber(String) 22 Neg failed", !NumberUtils.isNumber(val));
1061        assertTrue("isNumber(String)/createNumber(String) 22 Neg failed", !checkCreateNumber(val));
1062        val = " 1111";
1063        assertTrue("isNumber(String) 23 Neg failed", !NumberUtils.isNumber(val));
1064        assertTrue("isNumber(String)/createNumber(String) 23 Neg failed", !checkCreateNumber(val));
1065        val = "1111 ";
1066        assertTrue("isNumber(String) 24 Neg failed", !NumberUtils.isNumber(val));
1067        assertTrue("isNumber(String)/createNumber(String) 24 Neg failed", !checkCreateNumber(val));
1068
1069    }
1070
1071    private boolean checkCreateNumber(String JavaDoc val) {
1072        try {
1073            Object JavaDoc obj = NumberUtils.createNumber(val);
1074            if (obj == null) {
1075                return false;
1076            }
1077            return true;
1078        } catch (NumberFormatException JavaDoc e) {
1079            return false;
1080       }
1081    }
1082
1083    public void testConstants() {
1084        assertTrue(NumberUtils.LONG_ZERO instanceof Long JavaDoc);
1085        assertTrue(NumberUtils.LONG_ONE instanceof Long JavaDoc);
1086        assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long JavaDoc);
1087        assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer JavaDoc);
1088        assertTrue(NumberUtils.INTEGER_ONE instanceof Integer JavaDoc);
1089        assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer JavaDoc);
1090        assertTrue(NumberUtils.SHORT_ZERO instanceof Short JavaDoc);
1091        assertTrue(NumberUtils.SHORT_ONE instanceof Short JavaDoc);
1092        assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short JavaDoc);
1093        assertTrue(NumberUtils.BYTE_ZERO instanceof Byte JavaDoc);
1094        assertTrue(NumberUtils.BYTE_ONE instanceof Byte JavaDoc);
1095        assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte JavaDoc);
1096        assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double JavaDoc);
1097        assertTrue(NumberUtils.DOUBLE_ONE instanceof Double JavaDoc);
1098        assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double JavaDoc);
1099        assertTrue(NumberUtils.FLOAT_ZERO instanceof Float JavaDoc);
1100        assertTrue(NumberUtils.FLOAT_ONE instanceof Float JavaDoc);
1101        assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float JavaDoc);
1102        
1103        assertTrue(NumberUtils.LONG_ZERO.longValue() == 0);
1104        assertTrue(NumberUtils.LONG_ONE.longValue() == 1);
1105        assertTrue(NumberUtils.LONG_MINUS_ONE.longValue() == -1);
1106        assertTrue(NumberUtils.INTEGER_ZERO.intValue() == 0);
1107        assertTrue(NumberUtils.INTEGER_ONE.intValue() == 1);
1108        assertTrue(NumberUtils.INTEGER_MINUS_ONE.intValue() == -1);
1109        assertTrue(NumberUtils.SHORT_ZERO.shortValue() == 0);
1110        assertTrue(NumberUtils.SHORT_ONE.shortValue() == 1);
1111        assertTrue(NumberUtils.SHORT_MINUS_ONE.shortValue() == -1);
1112        assertTrue(NumberUtils.BYTE_ZERO.byteValue() == 0);
1113        assertTrue(NumberUtils.BYTE_ONE.byteValue() == 1);
1114        assertTrue(NumberUtils.BYTE_MINUS_ONE.byteValue() == -1);
1115        assertTrue(NumberUtils.DOUBLE_ZERO.doubleValue() == 0.0d);
1116        assertTrue(NumberUtils.DOUBLE_ONE.doubleValue() == 1.0d);
1117        assertTrue(NumberUtils.DOUBLE_MINUS_ONE.doubleValue() == -1.0d);
1118        assertTrue(NumberUtils.FLOAT_ZERO.floatValue() == 0.0f);
1119        assertTrue(NumberUtils.FLOAT_ONE.floatValue() == 1.0f);
1120        assertTrue(NumberUtils.FLOAT_MINUS_ONE.floatValue() == -1.0f);
1121    }
1122    
1123}
1124
Popular Tags