001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import java.math.BigDecimal;
020: import java.math.BigInteger;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Unit tests {@link org.apache.commons.lang.NumberUtils}.
028: *
029: * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
030: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
031: * @author Eric Pugh
032: * @author Phil Steitz
033: * @author Stephen Colebourne
034: * @version $Id: NumberUtilsTest.java 437554 2006-08-28 06:21:41Z bayard $
035: */
036: public class NumberUtilsTest extends TestCase {
037:
038: public NumberUtilsTest(String name) {
039: super (name);
040: }
041:
042: public static Test suite() {
043: TestSuite suite = new TestSuite(NumberUtilsTest.class);
044: suite.setName("NumberUtils Tests");
045: return suite;
046: }
047:
048: //---------------------------------------------------------------------
049:
050: /**
051: * Test for int stringToInt(String)
052: */
053: public void testStringToIntString() {
054: assertTrue("stringToInt(String) 1 failed", NumberUtils
055: .stringToInt("12345") == 12345);
056: assertTrue("stringToInt(String) 2 failed", NumberUtils
057: .stringToInt("abc") == 0);
058: }
059:
060: /**
061: * Test for int stringToInt(String, int)
062: */
063: public void testStringToIntStringI() {
064: assertTrue("stringToInt(String,int) 1 failed", NumberUtils
065: .stringToInt("12345", 5) == 12345);
066: assertTrue("stringToInt(String,int) 2 failed", NumberUtils
067: .stringToInt("1234.5", 5) == 5);
068: }
069:
070: public void testCreateNumber() {
071: //a lot of things can go wrong
072: assertEquals("createNumber(String) 1 failed", new Float(
073: "1234.5"), NumberUtils.createNumber("1234.5"));
074: assertEquals("createNumber(String) 2 failed", new Integer(
075: "12345"), NumberUtils.createNumber("12345"));
076: assertEquals("createNumber(String) 3 failed", new Double(
077: "1234.5"), NumberUtils.createNumber("1234.5D"));
078: assertEquals("createNumber(String) 4 failed", new Float(
079: "1234.5"), NumberUtils.createNumber("1234.5F"));
080: assertEquals("createNumber(String) 5 failed", new Long(
081: Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
082: + (Integer.MAX_VALUE + 1L)));
083: assertEquals("createNumber(String) 6 failed", new BigInteger(
084: Long.MAX_VALUE + "0"), NumberUtils
085: .createNumber(Long.MAX_VALUE + "0L"));
086: assertEquals("createNumber(String) 7 failed", new Long(12345),
087: NumberUtils.createNumber("12345L"));
088: assertEquals("createNumber(String) 8 failed", new Float(
089: "-1234.5"), NumberUtils.createNumber("-1234.5"));
090: assertEquals("createNumber(String) 9 failed", new Integer(
091: "-12345"), NumberUtils.createNumber("-12345"));
092: assertTrue("createNumber(String) 10 failed",
093: 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
094: assertTrue("createNumber(String) 11 failed",
095: -0xFADE == NumberUtils.createNumber("-0xFADE")
096: .intValue());
097: assertEquals("createNumber(String) 12 failed", new Double(
098: "1.1E200"), NumberUtils.createNumber("1.1E200"));
099: assertEquals("createNumber(String) 13 failed", new Float(
100: "1.1E20"), NumberUtils.createNumber("1.1E20"));
101: assertEquals("createNumber(String) 14 failed", new Double(
102: "-1.1E200"), NumberUtils.createNumber("-1.1E200"));
103: assertEquals("createNumber(String) 15 failed", new Double(
104: "1.1E-200"), NumberUtils.createNumber("1.1E-200"));
105: assertEquals("createNumber(String) 16 failed", new Double(
106: "1.1E-200"), NumberUtils.createNumber("1.1E-200"));
107:
108: // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
109: if (SystemUtils.isJavaVersionAtLeast(1.3f)) {
110: assertEquals("createNumber(String) 15 failed",
111: new BigDecimal("1.1E-700"), NumberUtils
112: .createNumber("1.1E-700F"));
113: }
114: assertEquals("createNumber(String) 16 failed", new Long("10"
115: + Integer.MAX_VALUE), NumberUtils.createNumber("10"
116: + Integer.MAX_VALUE + "L"));
117: assertEquals("createNumber(String) 17 failed", new Long("10"
118: + Integer.MAX_VALUE), NumberUtils.createNumber("10"
119: + Integer.MAX_VALUE));
120: assertEquals("createNumber(String) 18 failed", new BigInteger(
121: "10" + Long.MAX_VALUE), NumberUtils.createNumber("10"
122: + Long.MAX_VALUE));
123:
124: }
125:
126: public void testCreateFloat() {
127: assertEquals("createFloat(String) failed", new Float("1234.5"),
128: NumberUtils.createFloat("1234.5"));
129: }
130:
131: public void testCreateDouble() {
132: assertEquals("createDouble(String) failed",
133: new Double("1234.5"), NumberUtils
134: .createDouble("1234.5"));
135: }
136:
137: public void testCreateInteger() {
138: assertEquals("createInteger(String) failed", new Integer(
139: "12345"), NumberUtils.createInteger("12345"));
140: }
141:
142: public void testCreateLong() {
143: assertEquals("createInteger(String) failed", new Long("12345"),
144: NumberUtils.createLong("12345"));
145: }
146:
147: public void testCreateBigInteger() {
148: assertEquals("createBigInteger(String) failed", new BigInteger(
149: "12345"), NumberUtils.createBigInteger("12345"));
150: }
151:
152: public void testCreateBigDecimal() {
153: assertEquals("createBigDecimal(String) failed", new BigDecimal(
154: "1234.5"), NumberUtils.createBigDecimal("1234.5"));
155: }
156:
157: public void testMinimumLong() {
158: assertEquals("minimum(long,long,long) 1 failed", 12345L,
159: NumberUtils.minimum(12345L, 12345L + 1L, 12345L + 2L));
160: assertEquals("minimum(long,long,long) 2 failed", 12345L,
161: NumberUtils.minimum(12345L + 1L, 12345L, 12345 + 2L));
162: assertEquals("minimum(long,long,long) 3 failed", 12345L,
163: NumberUtils.minimum(12345L + 1L, 12345L + 2L, 12345L));
164: assertEquals("minimum(long,long,long) 4 failed", 12345L,
165: NumberUtils.minimum(12345L + 1L, 12345L, 12345L));
166: assertEquals("minimum(long,long,long) 5 failed", 12345L,
167: NumberUtils.minimum(12345L, 12345L, 12345L));
168:
169: }
170:
171: public void testMinimumInt() {
172: assertEquals("minimum(int,int,int) 1 failed", 12345,
173: NumberUtils.minimum(12345, 12345 + 1, 12345 + 2));
174: assertEquals("minimum(int,int,int) 2 failed", 12345,
175: NumberUtils.minimum(12345 + 1, 12345, 12345 + 2));
176: assertEquals("minimum(int,int,int) 3 failed", 12345,
177: NumberUtils.minimum(12345 + 1, 12345 + 2, 12345));
178: assertEquals("minimum(int,int,int) 4 failed", 12345,
179: NumberUtils.minimum(12345 + 1, 12345, 12345));
180: assertEquals("minimum(int,int,int) 5 failed", 12345,
181: NumberUtils.minimum(12345, 12345, 12345));
182:
183: }
184:
185: public void testMaximumLong() {
186: assertEquals("maximum(long,long,long) 1 failed", 12345L,
187: NumberUtils.maximum(12345L, 12345L - 1L, 12345L - 2L));
188: assertEquals("maximum(long,long,long) 2 failed", 12345L,
189: NumberUtils.maximum(12345L - 1L, 12345L, 12345L - 2L));
190: assertEquals("maximum(long,long,long) 3 failed", 12345L,
191: NumberUtils.maximum(12345L - 1L, 12345L - 2L, 12345L));
192: assertEquals("maximum(long,long,long) 4 failed", 12345L,
193: NumberUtils.maximum(12345L - 1L, 12345L, 12345L));
194: assertEquals("maximum(long,long,long) 5 failed", 12345L,
195: NumberUtils.maximum(12345L, 12345L, 12345L));
196:
197: }
198:
199: public void testMaximumInt() {
200: assertEquals("maximum(int,int,int) 1 failed", 12345,
201: NumberUtils.maximum(12345, 12345 - 1, 12345 - 2));
202: assertEquals("maximum(int,int,int) 2 failed", 12345,
203: NumberUtils.maximum(12345 - 1, 12345, 12345 - 2));
204: assertEquals("maximum(int,int,int) 3 failed", 12345,
205: NumberUtils.maximum(12345 - 1, 12345 - 2, 12345));
206: assertEquals("maximum(int,int,int) 4 failed", 12345,
207: NumberUtils.maximum(12345 - 1, 12345, 12345));
208: assertEquals("maximum(int,int,int) 5 failed", 12345,
209: NumberUtils.maximum(12345, 12345, 12345));
210:
211: }
212:
213: public void testCompareDouble() {
214: assertTrue(NumberUtils.compare(Double.NaN, Double.NaN) == 0);
215: assertTrue(NumberUtils.compare(Double.NaN,
216: Double.POSITIVE_INFINITY) == +1);
217: assertTrue(NumberUtils.compare(Double.NaN, Double.MAX_VALUE) == +1);
218: assertTrue(NumberUtils.compare(Double.NaN, 1.2d) == +1);
219: assertTrue(NumberUtils.compare(Double.NaN, 0.0d) == +1);
220: assertTrue(NumberUtils.compare(Double.NaN, -0.0d) == +1);
221: assertTrue(NumberUtils.compare(Double.NaN, -1.2d) == +1);
222: assertTrue(NumberUtils.compare(Double.NaN, -Double.MAX_VALUE) == +1);
223: assertTrue(NumberUtils.compare(Double.NaN,
224: Double.NEGATIVE_INFINITY) == +1);
225:
226: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
227: Double.NaN) == -1);
228: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
229: Double.POSITIVE_INFINITY) == 0);
230: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
231: Double.MAX_VALUE) == +1);
232: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 1.2d) == +1);
233: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 0.0d) == +1);
234: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -0.0d) == +1);
235: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -1.2d) == +1);
236: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
237: -Double.MAX_VALUE) == +1);
238: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
239: Double.NEGATIVE_INFINITY) == +1);
240:
241: assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NaN) == -1);
242: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
243: Double.POSITIVE_INFINITY) == -1);
244: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
245: Double.MAX_VALUE) == 0);
246: assertTrue(NumberUtils.compare(Double.MAX_VALUE, 1.2d) == +1);
247: assertTrue(NumberUtils.compare(Double.MAX_VALUE, 0.0d) == +1);
248: assertTrue(NumberUtils.compare(Double.MAX_VALUE, -0.0d) == +1);
249: assertTrue(NumberUtils.compare(Double.MAX_VALUE, -1.2d) == +1);
250: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
251: -Double.MAX_VALUE) == +1);
252: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
253: Double.NEGATIVE_INFINITY) == +1);
254:
255: assertTrue(NumberUtils.compare(1.2d, Double.NaN) == -1);
256: assertTrue(NumberUtils.compare(1.2d, Double.POSITIVE_INFINITY) == -1);
257: assertTrue(NumberUtils.compare(1.2d, Double.MAX_VALUE) == -1);
258: assertTrue(NumberUtils.compare(1.2d, 1.2d) == 0);
259: assertTrue(NumberUtils.compare(1.2d, 0.0d) == +1);
260: assertTrue(NumberUtils.compare(1.2d, -0.0d) == +1);
261: assertTrue(NumberUtils.compare(1.2d, -1.2d) == +1);
262: assertTrue(NumberUtils.compare(1.2d, -Double.MAX_VALUE) == +1);
263: assertTrue(NumberUtils.compare(1.2d, Double.NEGATIVE_INFINITY) == +1);
264:
265: assertTrue(NumberUtils.compare(0.0d, Double.NaN) == -1);
266: assertTrue(NumberUtils.compare(0.0d, Double.POSITIVE_INFINITY) == -1);
267: assertTrue(NumberUtils.compare(0.0d, Double.MAX_VALUE) == -1);
268: assertTrue(NumberUtils.compare(0.0d, 1.2d) == -1);
269: assertTrue(NumberUtils.compare(0.0d, 0.0d) == 0);
270: assertTrue(NumberUtils.compare(0.0d, -0.0d) == +1);
271: assertTrue(NumberUtils.compare(0.0d, -1.2d) == +1);
272: assertTrue(NumberUtils.compare(0.0d, -Double.MAX_VALUE) == +1);
273: assertTrue(NumberUtils.compare(0.0d, Double.NEGATIVE_INFINITY) == +1);
274:
275: assertTrue(NumberUtils.compare(-0.0d, Double.NaN) == -1);
276: assertTrue(NumberUtils.compare(-0.0d, Double.POSITIVE_INFINITY) == -1);
277: assertTrue(NumberUtils.compare(-0.0d, Double.MAX_VALUE) == -1);
278: assertTrue(NumberUtils.compare(-0.0d, 1.2d) == -1);
279: assertTrue(NumberUtils.compare(-0.0d, 0.0d) == -1);
280: assertTrue(NumberUtils.compare(-0.0d, -0.0d) == 0);
281: assertTrue(NumberUtils.compare(-0.0d, -1.2d) == +1);
282: assertTrue(NumberUtils.compare(-0.0d, -Double.MAX_VALUE) == +1);
283: assertTrue(NumberUtils.compare(-0.0d, Double.NEGATIVE_INFINITY) == +1);
284:
285: assertTrue(NumberUtils.compare(-1.2d, Double.NaN) == -1);
286: assertTrue(NumberUtils.compare(-1.2d, Double.POSITIVE_INFINITY) == -1);
287: assertTrue(NumberUtils.compare(-1.2d, Double.MAX_VALUE) == -1);
288: assertTrue(NumberUtils.compare(-1.2d, 1.2d) == -1);
289: assertTrue(NumberUtils.compare(-1.2d, 0.0d) == -1);
290: assertTrue(NumberUtils.compare(-1.2d, -0.0d) == -1);
291: assertTrue(NumberUtils.compare(-1.2d, -1.2d) == 0);
292: assertTrue(NumberUtils.compare(-1.2d, -Double.MAX_VALUE) == +1);
293: assertTrue(NumberUtils.compare(-1.2d, Double.NEGATIVE_INFINITY) == +1);
294:
295: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NaN) == -1);
296: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
297: Double.POSITIVE_INFINITY) == -1);
298: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
299: Double.MAX_VALUE) == -1);
300: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 1.2d) == -1);
301: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 0.0d) == -1);
302: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -0.0d) == -1);
303: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -1.2d) == -1);
304: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
305: -Double.MAX_VALUE) == 0);
306: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
307: Double.NEGATIVE_INFINITY) == +1);
308:
309: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
310: Double.NaN) == -1);
311: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
312: Double.POSITIVE_INFINITY) == -1);
313: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
314: Double.MAX_VALUE) == -1);
315: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 1.2d) == -1);
316: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 0.0d) == -1);
317: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -0.0d) == -1);
318: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -1.2d) == -1);
319: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
320: -Double.MAX_VALUE) == -1);
321: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
322: Double.NEGATIVE_INFINITY) == 0);
323: }
324:
325: public void testCompareFloat() {
326: assertTrue(NumberUtils.compare(Float.NaN, Float.NaN) == 0);
327: assertTrue(NumberUtils.compare(Float.NaN,
328: Float.POSITIVE_INFINITY) == +1);
329: assertTrue(NumberUtils.compare(Float.NaN, Float.MAX_VALUE) == +1);
330: assertTrue(NumberUtils.compare(Float.NaN, 1.2f) == +1);
331: assertTrue(NumberUtils.compare(Float.NaN, 0.0f) == +1);
332: assertTrue(NumberUtils.compare(Float.NaN, -0.0f) == +1);
333: assertTrue(NumberUtils.compare(Float.NaN, -1.2f) == +1);
334: assertTrue(NumberUtils.compare(Float.NaN, -Float.MAX_VALUE) == +1);
335: assertTrue(NumberUtils.compare(Float.NaN,
336: Float.NEGATIVE_INFINITY) == +1);
337:
338: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
339: Float.NaN) == -1);
340: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
341: Float.POSITIVE_INFINITY) == 0);
342: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
343: Float.MAX_VALUE) == +1);
344: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 1.2f) == +1);
345: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 0.0f) == +1);
346: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -0.0f) == +1);
347: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -1.2f) == +1);
348: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
349: -Float.MAX_VALUE) == +1);
350: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
351: Float.NEGATIVE_INFINITY) == +1);
352:
353: assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NaN) == -1);
354: assertTrue(NumberUtils.compare(Float.MAX_VALUE,
355: Float.POSITIVE_INFINITY) == -1);
356: assertTrue(NumberUtils
357: .compare(Float.MAX_VALUE, Float.MAX_VALUE) == 0);
358: assertTrue(NumberUtils.compare(Float.MAX_VALUE, 1.2f) == +1);
359: assertTrue(NumberUtils.compare(Float.MAX_VALUE, 0.0f) == +1);
360: assertTrue(NumberUtils.compare(Float.MAX_VALUE, -0.0f) == +1);
361: assertTrue(NumberUtils.compare(Float.MAX_VALUE, -1.2f) == +1);
362: assertTrue(NumberUtils.compare(Float.MAX_VALUE,
363: -Float.MAX_VALUE) == +1);
364: assertTrue(NumberUtils.compare(Float.MAX_VALUE,
365: Float.NEGATIVE_INFINITY) == +1);
366:
367: assertTrue(NumberUtils.compare(1.2f, Float.NaN) == -1);
368: assertTrue(NumberUtils.compare(1.2f, Float.POSITIVE_INFINITY) == -1);
369: assertTrue(NumberUtils.compare(1.2f, Float.MAX_VALUE) == -1);
370: assertTrue(NumberUtils.compare(1.2f, 1.2f) == 0);
371: assertTrue(NumberUtils.compare(1.2f, 0.0f) == +1);
372: assertTrue(NumberUtils.compare(1.2f, -0.0f) == +1);
373: assertTrue(NumberUtils.compare(1.2f, -1.2f) == +1);
374: assertTrue(NumberUtils.compare(1.2f, -Float.MAX_VALUE) == +1);
375: assertTrue(NumberUtils.compare(1.2f, Float.NEGATIVE_INFINITY) == +1);
376:
377: assertTrue(NumberUtils.compare(0.0f, Float.NaN) == -1);
378: assertTrue(NumberUtils.compare(0.0f, Float.POSITIVE_INFINITY) == -1);
379: assertTrue(NumberUtils.compare(0.0f, Float.MAX_VALUE) == -1);
380: assertTrue(NumberUtils.compare(0.0f, 1.2f) == -1);
381: assertTrue(NumberUtils.compare(0.0f, 0.0f) == 0);
382: assertTrue(NumberUtils.compare(0.0f, -0.0f) == +1);
383: assertTrue(NumberUtils.compare(0.0f, -1.2f) == +1);
384: assertTrue(NumberUtils.compare(0.0f, -Float.MAX_VALUE) == +1);
385: assertTrue(NumberUtils.compare(0.0f, Float.NEGATIVE_INFINITY) == +1);
386:
387: assertTrue(NumberUtils.compare(-0.0f, Float.NaN) == -1);
388: assertTrue(NumberUtils.compare(-0.0f, Float.POSITIVE_INFINITY) == -1);
389: assertTrue(NumberUtils.compare(-0.0f, Float.MAX_VALUE) == -1);
390: assertTrue(NumberUtils.compare(-0.0f, 1.2f) == -1);
391: assertTrue(NumberUtils.compare(-0.0f, 0.0f) == -1);
392: assertTrue(NumberUtils.compare(-0.0f, -0.0f) == 0);
393: assertTrue(NumberUtils.compare(-0.0f, -1.2f) == +1);
394: assertTrue(NumberUtils.compare(-0.0f, -Float.MAX_VALUE) == +1);
395: assertTrue(NumberUtils.compare(-0.0f, Float.NEGATIVE_INFINITY) == +1);
396:
397: assertTrue(NumberUtils.compare(-1.2f, Float.NaN) == -1);
398: assertTrue(NumberUtils.compare(-1.2f, Float.POSITIVE_INFINITY) == -1);
399: assertTrue(NumberUtils.compare(-1.2f, Float.MAX_VALUE) == -1);
400: assertTrue(NumberUtils.compare(-1.2f, 1.2f) == -1);
401: assertTrue(NumberUtils.compare(-1.2f, 0.0f) == -1);
402: assertTrue(NumberUtils.compare(-1.2f, -0.0f) == -1);
403: assertTrue(NumberUtils.compare(-1.2f, -1.2f) == 0);
404: assertTrue(NumberUtils.compare(-1.2f, -Float.MAX_VALUE) == +1);
405: assertTrue(NumberUtils.compare(-1.2f, Float.NEGATIVE_INFINITY) == +1);
406:
407: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NaN) == -1);
408: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
409: Float.POSITIVE_INFINITY) == -1);
410: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
411: Float.MAX_VALUE) == -1);
412: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 1.2f) == -1);
413: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 0.0f) == -1);
414: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -0.0f) == -1);
415: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -1.2f) == -1);
416: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
417: -Float.MAX_VALUE) == 0);
418: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
419: Float.NEGATIVE_INFINITY) == +1);
420:
421: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
422: Float.NaN) == -1);
423: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
424: Float.POSITIVE_INFINITY) == -1);
425: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
426: Float.MAX_VALUE) == -1);
427: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 1.2f) == -1);
428: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 0.0f) == -1);
429: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -0.0f) == -1);
430: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -1.2f) == -1);
431: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
432: -Float.MAX_VALUE) == -1);
433: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
434: Float.NEGATIVE_INFINITY) == 0);
435: }
436:
437: public void testIsDigits() {
438: assertEquals("isDigits(null) failed", false, NumberUtils
439: .isDigits(null));
440: assertEquals("isDigits('') failed", false, NumberUtils
441: .isDigits(""));
442: assertEquals("isDigits(String) failed", true, NumberUtils
443: .isDigits("12345"));
444: assertEquals("isDigits(String) neg 1 failed", false,
445: NumberUtils.isDigits("1234.5"));
446: assertEquals("isDigits(String) neg 3 failed", false,
447: NumberUtils.isDigits("1ab"));
448: assertEquals("isDigits(String) neg 4 failed", false,
449: NumberUtils.isDigits("abc"));
450: }
451:
452: /**
453: * Tests isNumber(String) and tests that createNumber(String) returns
454: * a valid number iff isNumber(String) returns false.
455: */
456: public void testIsNumber() {
457: String val = "12345";
458: assertTrue("isNumber(String) 1 failed", NumberUtils
459: .isNumber(val));
460: assertTrue("isNumber(String)/createNumber(String) 1 failed",
461: checkCreateNumber(val));
462: val = "1234.5";
463: assertTrue("isNumber(String) 2 failed", NumberUtils
464: .isNumber(val));
465: assertTrue("isNumber(String)/createNumber(String) 2 failed",
466: checkCreateNumber(val));
467: val = ".12345";
468: assertTrue("isNumber(String) 3 failed", NumberUtils
469: .isNumber(val));
470: assertTrue("isNumber(String)/createNumber(String) 3 failed",
471: checkCreateNumber(val));
472: val = "1234E5";
473: assertTrue("isNumber(String) 4 failed", NumberUtils
474: .isNumber(val));
475: assertTrue("isNumber(String)/createNumber(String) 4 failed",
476: checkCreateNumber(val));
477: val = "1234E+5";
478: assertTrue("isNumber(String) 5 failed", NumberUtils
479: .isNumber(val));
480: assertTrue("isNumber(String)/createNumber(String) 5 failed",
481: checkCreateNumber(val));
482: val = "1234E-5";
483: assertTrue("isNumber(String) 6 failed", NumberUtils
484: .isNumber(val));
485: assertTrue("isNumber(String)/createNumber(String) 6 failed",
486: checkCreateNumber(val));
487: val = "123.4E5";
488: assertTrue("isNumber(String) 7 failed", NumberUtils
489: .isNumber(val));
490: assertTrue("isNumber(String)/createNumber(String) 7 failed",
491: checkCreateNumber(val));
492: val = "-1234";
493: assertTrue("isNumber(String) 8 failed", NumberUtils
494: .isNumber(val));
495: assertTrue("isNumber(String)/createNumber(String) 8 failed",
496: checkCreateNumber(val));
497: val = "-1234.5";
498: assertTrue("isNumber(String) 9 failed", NumberUtils
499: .isNumber(val));
500: assertTrue("isNumber(String)/createNumber(String) 9 failed",
501: checkCreateNumber(val));
502: val = "-.12345";
503: assertTrue("isNumber(String) 10 failed", NumberUtils
504: .isNumber(val));
505: assertTrue("isNumber(String)/createNumber(String) 10 failed",
506: checkCreateNumber(val));
507: val = "-1234E5";
508: assertTrue("isNumber(String) 11 failed", NumberUtils
509: .isNumber(val));
510: assertTrue("isNumber(String)/createNumber(String) 11 failed",
511: checkCreateNumber(val));
512: val = "0";
513: assertTrue("isNumber(String) 12 failed", NumberUtils
514: .isNumber(val));
515: assertTrue("isNumber(String)/createNumber(String) 12 failed",
516: checkCreateNumber(val));
517: val = "-0";
518: assertTrue("isNumber(String) 13 failed", NumberUtils
519: .isNumber(val));
520: assertTrue("isNumber(String)/createNumber(String) 13 failed",
521: checkCreateNumber(val));
522: val = "01234";
523: assertTrue("isNumber(String) 14 failed", NumberUtils
524: .isNumber(val));
525: assertTrue("isNumber(String)/createNumber(String) 14 failed",
526: checkCreateNumber(val));
527: val = "-01234";
528: assertTrue("isNumber(String) 15 failed", NumberUtils
529: .isNumber(val));
530: assertTrue("isNumber(String)/createNumber(String) 15 failed",
531: checkCreateNumber(val));
532: val = "0xABC123";
533: assertTrue("isNumber(String) 16 failed", NumberUtils
534: .isNumber(val));
535: assertTrue("isNumber(String)/createNumber(String) 16 failed",
536: checkCreateNumber(val));
537: val = "0x0";
538: assertTrue("isNumber(String) 17 failed", NumberUtils
539: .isNumber(val));
540: assertTrue("isNumber(String)/createNumber(String) 17 failed",
541: checkCreateNumber(val));
542: val = "123.4E21D";
543: assertTrue("isNumber(String) 19 failed", NumberUtils
544: .isNumber(val));
545: assertTrue("isNumber(String)/createNumber(String) 19 failed",
546: checkCreateNumber(val));
547: val = "-221.23F";
548: assertTrue("isNumber(String) 20 failed", NumberUtils
549: .isNumber(val));
550: assertTrue("isNumber(String)/createNumber(String) 20 failed",
551: checkCreateNumber(val));
552: val = "22338L";
553: assertTrue("isNumber(String) 21 failed", NumberUtils
554: .isNumber(val));
555: assertTrue("isNumber(String)/createNumber(String) 21 failed",
556: checkCreateNumber(val));
557: val = null;
558: assertTrue("isNumber(String) 1 Neg failed", !NumberUtils
559: .isNumber(val));
560: assertTrue(
561: "isNumber(String)/createNumber(String) 1 Neg failed",
562: !checkCreateNumber(val));
563: val = "";
564: assertTrue("isNumber(String) 2 Neg failed", !NumberUtils
565: .isNumber(val));
566: assertTrue(
567: "isNumber(String)/createNumber(String) 2 Neg failed",
568: !checkCreateNumber(val));
569: val = "--2.3";
570: assertTrue("isNumber(String) 3 Neg failed", !NumberUtils
571: .isNumber(val));
572: assertTrue(
573: "isNumber(String)/createNumber(String) 3 Neg failed",
574: !checkCreateNumber(val));
575: val = ".12.3";
576: assertTrue("isNumber(String) 4 Neg failed", !NumberUtils
577: .isNumber(val));
578: assertTrue(
579: "isNumber(String)/createNumber(String) 4 Neg failed",
580: !checkCreateNumber(val));
581: val = "-123E";
582: assertTrue("isNumber(String) 5 Neg failed", !NumberUtils
583: .isNumber(val));
584: assertTrue(
585: "isNumber(String)/createNumber(String) 5 Neg failed",
586: !checkCreateNumber(val));
587: val = "-123E+-212";
588: assertTrue("isNumber(String) 6 Neg failed", !NumberUtils
589: .isNumber(val));
590: assertTrue(
591: "isNumber(String)/createNumber(String) 6 Neg failed",
592: !checkCreateNumber(val));
593: val = "-123E2.12";
594: assertTrue("isNumber(String) 7 Neg failed", !NumberUtils
595: .isNumber(val));
596: assertTrue(
597: "isNumber(String)/createNumber(String) 7 Neg failed",
598: !checkCreateNumber(val));
599: val = "0xGF";
600: assertTrue("isNumber(String) 8 Neg failed", !NumberUtils
601: .isNumber(val));
602: assertTrue(
603: "isNumber(String)/createNumber(String) 8 Neg failed",
604: !checkCreateNumber(val));
605: val = "0xFAE-1";
606: assertTrue("isNumber(String) 9 Neg failed", !NumberUtils
607: .isNumber(val));
608: assertTrue(
609: "isNumber(String)/createNumber(String) 9 Neg failed",
610: !checkCreateNumber(val));
611: val = ".";
612: assertTrue("isNumber(String) 10 Neg failed", !NumberUtils
613: .isNumber(val));
614: assertTrue(
615: "isNumber(String)/createNumber(String) 10 Neg failed",
616: !checkCreateNumber(val));
617: val = "-0ABC123";
618: assertTrue("isNumber(String) 11 Neg failed", !NumberUtils
619: .isNumber(val));
620: assertTrue(
621: "isNumber(String)/createNumber(String) 11 Neg failed",
622: !checkCreateNumber(val));
623: val = "123.4E-D";
624: assertTrue("isNumber(String) 12 Neg failed", !NumberUtils
625: .isNumber(val));
626: assertTrue(
627: "isNumber(String)/createNumber(String) 12 Neg failed",
628: !checkCreateNumber(val));
629: val = "123.4ED";
630: assertTrue("isNumber(String) 13 Neg failed", !NumberUtils
631: .isNumber(val));
632: assertTrue(
633: "isNumber(String)/createNumber(String) 13 Neg failed",
634: !checkCreateNumber(val));
635: val = "1234E5l";
636: assertTrue("isNumber(String) 14 Neg failed", !NumberUtils
637: .isNumber(val));
638: assertTrue(
639: "isNumber(String)/createNumber(String) 14 Neg failed",
640: !checkCreateNumber(val));
641: val = "11a";
642: assertTrue("isNumber(String) 15 Neg failed", !NumberUtils
643: .isNumber(val));
644: assertTrue(
645: "isNumber(String)/createNumber(String) 15 Neg failed",
646: !checkCreateNumber(val));
647: val = "1a";
648: assertTrue("isNumber(String) 16 Neg failed", !NumberUtils
649: .isNumber(val));
650: assertTrue(
651: "isNumber(String)/createNumber(String) 16 Neg failed",
652: !checkCreateNumber(val));
653: val = "a";
654: assertTrue("isNumber(String) 17 Neg failed", !NumberUtils
655: .isNumber(val));
656: assertTrue(
657: "isNumber(String)/createNumber(String) 17 Neg failed",
658: !checkCreateNumber(val));
659: val = "11g";
660: assertTrue("isNumber(String) 18 Neg failed", !NumberUtils
661: .isNumber(val));
662: assertTrue(
663: "isNumber(String)/createNumber(String) 18 Neg failed",
664: !checkCreateNumber(val));
665: val = "11z";
666: assertTrue("isNumber(String) 19 Neg failed", !NumberUtils
667: .isNumber(val));
668: assertTrue(
669: "isNumber(String)/createNumber(String) 19 Neg failed",
670: !checkCreateNumber(val));
671: val = "11def";
672: assertTrue("isNumber(String) 20 Neg failed", !NumberUtils
673: .isNumber(val));
674: assertTrue(
675: "isNumber(String)/createNumber(String) 20 Neg failed",
676: !checkCreateNumber(val));
677: val = "11d11";
678: assertTrue("isNumber(String) 21 Neg failed", !NumberUtils
679: .isNumber(val));
680: assertTrue(
681: "isNumber(String)/createNumber(String) 21 Neg failed",
682: !checkCreateNumber(val));
683:
684: }
685:
686: public void testIsNumberInvalidInput() {
687: String val = "0x";
688: assertEquals("isNumber() with 0x wasn't false", false,
689: NumberUtils.isNumber(val));
690: val = "0x3x3";
691: assertEquals("isNumber() with 0x3x3 wasn't false", false,
692: NumberUtils.isNumber(val));
693: val = "20EE-3";
694: assertEquals("isNumber() with 20EE-3 wasn't false", false,
695: NumberUtils.isNumber(val));
696: val = "2435q";
697: assertEquals("isNumber() with 2435q wasn't false", false,
698: NumberUtils.isNumber(val));
699: val = ".";
700: assertEquals("isNumber() with . wasn't false", false,
701: NumberUtils.isNumber(val));
702:
703: }
704:
705: private boolean checkCreateNumber(String val) {
706: try {
707: Object obj = NumberUtils.createNumber(val);
708: if (obj == null) {
709: return false;
710: }
711: return true;
712: } catch (NumberFormatException e) {
713: return false;
714: } catch (NullPointerException e) {
715: return false;
716: }
717: }
718:
719: public void testPublicNoArgConstructor() {
720: try {
721: NumberUtils nu = new NumberUtils();
722: } catch (Exception e) {
723: fail("Error calling public no-arg constructor");
724: }
725: }
726:
727: }
|