0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.commons.lang.math;
0018:
0019: import java.lang.reflect.Constructor;
0020: import java.lang.reflect.Modifier;
0021: import java.math.BigDecimal;
0022: import java.math.BigInteger;
0023:
0024: import junit.framework.Test;
0025: import junit.framework.TestCase;
0026: import junit.framework.TestSuite;
0027: import junit.textui.TestRunner;
0028: import org.apache.commons.lang.SystemUtils;
0029:
0030: /**
0031: * Unit tests {@link org.apache.commons.lang.math.NumberUtils}.
0032: *
0033: * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
0034: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
0035: * @author Eric Pugh
0036: * @author Phil Steitz
0037: * @author Stephen Colebourne
0038: * @author Matthew Hawthorne
0039: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
0040: * @version $Id: NumberUtilsTest.java 491076 2006-12-29 18:48:37Z bayard $
0041: */
0042: public class NumberUtilsTest extends TestCase {
0043:
0044: public NumberUtilsTest(String name) {
0045: super (name);
0046: }
0047:
0048: public static void main(String[] args) {
0049: TestRunner.run(suite());
0050: }
0051:
0052: public static Test suite() {
0053: TestSuite suite = new TestSuite(NumberUtilsTest.class);
0054: suite.setName("NumberUtils Tests");
0055: return suite;
0056: }
0057:
0058: //-----------------------------------------------------------------------
0059: public void testConstructor() {
0060: assertNotNull(new NumberUtils());
0061: Constructor[] cons = NumberUtils.class
0062: .getDeclaredConstructors();
0063: assertEquals(1, cons.length);
0064: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
0065: assertEquals(true, Modifier.isPublic(NumberUtils.class
0066: .getModifiers()));
0067: assertEquals(false, Modifier.isFinal(NumberUtils.class
0068: .getModifiers()));
0069: }
0070:
0071: //---------------------------------------------------------------------
0072:
0073: /**
0074: * Test for {@link NumberUtils#stringToInt(String)}.
0075: */
0076: public void testStringToIntString() {
0077: assertTrue("stringToInt(String) 1 failed", NumberUtils
0078: .stringToInt("12345") == 12345);
0079: assertTrue("stringToInt(String) 2 failed", NumberUtils
0080: .stringToInt("abc") == 0);
0081: assertTrue("stringToInt(empty) failed", NumberUtils
0082: .stringToInt("") == 0);
0083: assertTrue("stringToInt(null) failed", NumberUtils
0084: .stringToInt(null) == 0);
0085: }
0086:
0087: /**
0088: * Test for {@link NumberUtils#toInt(String)}.
0089: */
0090: public void testToIntString() {
0091: assertTrue("toInt(String) 1 failed",
0092: NumberUtils.toInt("12345") == 12345);
0093: assertTrue("toInt(String) 2 failed",
0094: NumberUtils.toInt("abc") == 0);
0095: assertTrue("toInt(empty) failed", NumberUtils.toInt("") == 0);
0096: assertTrue("toInt(null) failed", NumberUtils.toInt(null) == 0);
0097: }
0098:
0099: /**
0100: * Test for {@link NumberUtils#stringToInt(String, int)}.
0101: */
0102: public void testStringToIntStringI() {
0103: assertTrue("stringToInt(String,int) 1 failed", NumberUtils
0104: .stringToInt("12345", 5) == 12345);
0105: assertTrue("stringToInt(String,int) 2 failed", NumberUtils
0106: .stringToInt("1234.5", 5) == 5);
0107: }
0108:
0109: /**
0110: * Test for {@link NumberUtils#toInt(String, int)}.
0111: */
0112: public void testToIntStringI() {
0113: assertTrue("toInt(String,int) 1 failed", NumberUtils.toInt(
0114: "12345", 5) == 12345);
0115: assertTrue("toInt(String,int) 2 failed", NumberUtils.toInt(
0116: "1234.5", 5) == 5);
0117: }
0118:
0119: /**
0120: * Test for {@link NumberUtils#toLong(String)}.
0121: */
0122: public void testToLongString() {
0123: assertTrue("toLong(String) 1 failed", NumberUtils
0124: .toLong("12345") == 12345l);
0125: assertTrue("toLong(String) 2 failed",
0126: NumberUtils.toLong("abc") == 0l);
0127: assertTrue("toLong(String) 3 failed",
0128: NumberUtils.toLong("1L") == 0l);
0129: assertTrue("toLong(String) 4 failed",
0130: NumberUtils.toLong("1l") == 0l);
0131: assertTrue("toLong(Long.MAX_VALUE) failed", NumberUtils
0132: .toLong(Long.MAX_VALUE + "") == Long.MAX_VALUE);
0133: assertTrue("toLong(Long.MIN_VALUE) failed", NumberUtils
0134: .toLong(Long.MIN_VALUE + "") == Long.MIN_VALUE);
0135: assertTrue("toLong(empty) failed", NumberUtils.toLong("") == 0l);
0136: assertTrue("toLong(null) failed",
0137: NumberUtils.toLong(null) == 0l);
0138: }
0139:
0140: /**
0141: * Test for {@link NumberUtils#toLong(String, long)}.
0142: */
0143: public void testToLongStringL() {
0144: assertTrue("toLong(String,long) 1 failed", NumberUtils.toLong(
0145: "12345", 5l) == 12345l);
0146: assertTrue("toLong(String,long) 2 failed", NumberUtils.toLong(
0147: "1234.5", 5l) == 5l);
0148: }
0149:
0150: /**
0151: * Test for {@link NumberUtils#toFloat(String)}.
0152: */
0153: public void testToFloatString() {
0154: assertTrue("toFloat(String) 1 failed", NumberUtils
0155: .toFloat("-1.2345") == -1.2345f);
0156: assertTrue("toFloat(String) 2 failed", NumberUtils
0157: .toFloat("1.2345") == 1.2345f);
0158: assertTrue("toFloat(String) 3 failed", NumberUtils
0159: .toFloat("abc") == 0.0f);
0160: assertTrue("toFloat(Float.MAX_VALUE) failed", NumberUtils
0161: .toFloat(Float.MAX_VALUE + "") == Float.MAX_VALUE);
0162: assertTrue("toFloat(Float.MIN_VALUE) failed", NumberUtils
0163: .toFloat(Float.MIN_VALUE + "") == Float.MIN_VALUE);
0164: assertTrue("toFloat(empty) failed",
0165: NumberUtils.toFloat("") == 0.0f);
0166: assertTrue("toFloat(null) failed",
0167: NumberUtils.toFloat(null) == 0.0f);
0168: }
0169:
0170: /**
0171: * Test for {@link NumberUtils#toFloat(String, float)}.
0172: */
0173: public void testToFloatStringF() {
0174: assertTrue("toFloat(String,int) 1 failed", NumberUtils.toFloat(
0175: "1.2345", 5.1f) == 1.2345f);
0176: assertTrue("toFloat(String,int) 2 failed", NumberUtils.toFloat(
0177: "a", 5.0f) == 5.0f);
0178: }
0179:
0180: /**
0181: * Test for {@link NumberUtils#toDouble(String)}.
0182: */
0183: public void testStringToDoubleString() {
0184: assertTrue("toDouble(String) 1 failed", NumberUtils
0185: .toDouble("-1.2345") == -1.2345d);
0186: assertTrue("toDouble(String) 2 failed", NumberUtils
0187: .toDouble("1.2345") == 1.2345d);
0188: assertTrue("toDouble(String) 3 failed", NumberUtils
0189: .toDouble("abc") == 0.0d);
0190: assertTrue("toDouble(Double.MAX_VALUE) failed", NumberUtils
0191: .toDouble(Double.MAX_VALUE + "") == Double.MAX_VALUE);
0192: assertTrue("toDouble(Double.MIN_VALUE) failed", NumberUtils
0193: .toDouble(Double.MIN_VALUE + "") == Double.MIN_VALUE);
0194: assertTrue("toDouble(empty) failed",
0195: NumberUtils.toDouble("") == 0.0d);
0196: assertTrue("toDouble(null) failed",
0197: NumberUtils.toDouble(null) == 0.0d);
0198: }
0199:
0200: /**
0201: * Test for {@link NumberUtils#toDouble(String, double)}.
0202: */
0203: public void testStringToDoubleStringD() {
0204: assertTrue("toDouble(String,int) 1 failed", NumberUtils
0205: .toDouble("1.2345", 5.1d) == 1.2345d);
0206: assertTrue("toDouble(String,int) 2 failed", NumberUtils
0207: .toDouble("a", 5.0d) == 5.0d);
0208: }
0209:
0210: public void testCreateNumber() {
0211: // a lot of things can go wrong
0212: assertEquals("createNumber(String) 1 failed", new Float(
0213: "1234.5"), NumberUtils.createNumber("1234.5"));
0214: assertEquals("createNumber(String) 2 failed", new Integer(
0215: "12345"), NumberUtils.createNumber("12345"));
0216: assertEquals("createNumber(String) 3 failed", new Double(
0217: "1234.5"), NumberUtils.createNumber("1234.5D"));
0218: assertEquals("createNumber(String) 3 failed", new Double(
0219: "1234.5"), NumberUtils.createNumber("1234.5d"));
0220: assertEquals("createNumber(String) 4 failed", new Float(
0221: "1234.5"), NumberUtils.createNumber("1234.5F"));
0222: assertEquals("createNumber(String) 4 failed", new Float(
0223: "1234.5"), NumberUtils.createNumber("1234.5f"));
0224: assertEquals("createNumber(String) 5 failed", new Long(
0225: Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
0226: + (Integer.MAX_VALUE + 1L)));
0227: assertEquals("createNumber(String) 6 failed", new Long(12345),
0228: NumberUtils.createNumber("12345L"));
0229: assertEquals("createNumber(String) 6 failed", new Long(12345),
0230: NumberUtils.createNumber("12345l"));
0231: assertEquals("createNumber(String) 7 failed", new Float(
0232: "-1234.5"), NumberUtils.createNumber("-1234.5"));
0233: assertEquals("createNumber(String) 8 failed", new Integer(
0234: "-12345"), NumberUtils.createNumber("-12345"));
0235: assertTrue("createNumber(String) 9 failed",
0236: 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
0237: assertTrue("createNumber(String) 10 failed",
0238: -0xFADE == NumberUtils.createNumber("-0xFADE")
0239: .intValue());
0240: assertEquals("createNumber(String) 11 failed", new Double(
0241: "1.1E200"), NumberUtils.createNumber("1.1E200"));
0242: assertEquals("createNumber(String) 12 failed", new Float(
0243: "1.1E20"), NumberUtils.createNumber("1.1E20"));
0244: assertEquals("createNumber(String) 13 failed", new Double(
0245: "-1.1E200"), NumberUtils.createNumber("-1.1E200"));
0246: assertEquals("createNumber(String) 14 failed", new Double(
0247: "1.1E-200"), NumberUtils.createNumber("1.1E-200"));
0248: assertEquals("createNumber(null) failed", null, NumberUtils
0249: .createNumber(null));
0250: assertEquals("createNumber(String) failed", new BigInteger(
0251: "12345678901234567890"), NumberUtils
0252: .createNumber("12345678901234567890L"));
0253:
0254: // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
0255: if (SystemUtils.isJavaVersionAtLeast(1.3f)) {
0256: assertEquals("createNumber(String) 15 failed",
0257: new BigDecimal("1.1E-700"), NumberUtils
0258: .createNumber("1.1E-700F"));
0259: }
0260: assertEquals("createNumber(String) 16 failed", new Long("10"
0261: + Integer.MAX_VALUE), NumberUtils.createNumber("10"
0262: + Integer.MAX_VALUE + "L"));
0263: assertEquals("createNumber(String) 17 failed", new Long("10"
0264: + Integer.MAX_VALUE), NumberUtils.createNumber("10"
0265: + Integer.MAX_VALUE));
0266: assertEquals("createNumber(String) 18 failed", new BigInteger(
0267: "10" + Long.MAX_VALUE), NumberUtils.createNumber("10"
0268: + Long.MAX_VALUE));
0269: }
0270:
0271: public void testCreateFloat() {
0272: assertEquals("createFloat(String) failed", new Float("1234.5"),
0273: NumberUtils.createFloat("1234.5"));
0274: assertEquals("createFloat(null) failed", null, NumberUtils
0275: .createFloat(null));
0276: this .testCreateFloatFailure("");
0277: this .testCreateFloatFailure(" ");
0278: this .testCreateFloatFailure("\b\t\n\f\r");
0279: // Funky whitespaces
0280: this
0281: .testCreateFloatFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
0282: }
0283:
0284: protected void testCreateFloatFailure(String str) {
0285: try {
0286: Float value = NumberUtils.createFloat(str);
0287: fail("createFloat(blank) failed: " + value);
0288: } catch (NumberFormatException ex) {
0289: // empty
0290: }
0291: }
0292:
0293: public void testCreateDouble() {
0294: assertEquals("createDouble(String) failed",
0295: new Double("1234.5"), NumberUtils
0296: .createDouble("1234.5"));
0297: assertEquals("createDouble(null) failed", null, NumberUtils
0298: .createDouble(null));
0299: this .testCreateDoubleFailure("");
0300: this .testCreateDoubleFailure(" ");
0301: this .testCreateDoubleFailure("\b\t\n\f\r");
0302: // Funky whitespaces
0303: this
0304: .testCreateDoubleFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
0305: }
0306:
0307: protected void testCreateDoubleFailure(String str) {
0308: try {
0309: Double value = NumberUtils.createDouble(str);
0310: fail("createDouble(blank) failed: " + value);
0311: } catch (NumberFormatException ex) {
0312: // empty
0313: }
0314: }
0315:
0316: public void testCreateInteger() {
0317: assertEquals("createInteger(String) failed", new Integer(
0318: "12345"), NumberUtils.createInteger("12345"));
0319: assertEquals("createInteger(null) failed", null, NumberUtils
0320: .createInteger(null));
0321: this .testCreateIntegerFailure("");
0322: this .testCreateIntegerFailure(" ");
0323: this .testCreateIntegerFailure("\b\t\n\f\r");
0324: // Funky whitespaces
0325: this
0326: .testCreateIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
0327: }
0328:
0329: protected void testCreateIntegerFailure(String str) {
0330: try {
0331: Integer value = NumberUtils.createInteger(str);
0332: fail("createInteger(blank) failed: " + value);
0333: } catch (NumberFormatException ex) {
0334: // empty
0335: }
0336: }
0337:
0338: public void testCreateLong() {
0339: assertEquals("createLong(String) failed", new Long("12345"),
0340: NumberUtils.createLong("12345"));
0341: assertEquals("createLong(null) failed", null, NumberUtils
0342: .createLong(null));
0343: this .testCreateLongFailure("");
0344: this .testCreateLongFailure(" ");
0345: this .testCreateLongFailure("\b\t\n\f\r");
0346: // Funky whitespaces
0347: this
0348: .testCreateLongFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
0349: }
0350:
0351: protected void testCreateLongFailure(String str) {
0352: try {
0353: Long value = NumberUtils.createLong(str);
0354: fail("createLong(blank) failed: " + value);
0355: } catch (NumberFormatException ex) {
0356: // empty
0357: }
0358: }
0359:
0360: public void testCreateBigInteger() {
0361: assertEquals("createBigInteger(String) failed", new BigInteger(
0362: "12345"), NumberUtils.createBigInteger("12345"));
0363: assertEquals("createBigInteger(null) failed", null, NumberUtils
0364: .createBigInteger(null));
0365: this .testCreateBigIntegerFailure("");
0366: this .testCreateBigIntegerFailure(" ");
0367: this .testCreateBigIntegerFailure("\b\t\n\f\r");
0368: // Funky whitespaces
0369: this
0370: .testCreateBigIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
0371: }
0372:
0373: protected void testCreateBigIntegerFailure(String str) {
0374: try {
0375: BigInteger value = NumberUtils.createBigInteger(str);
0376: fail("createBigInteger(blank) failed: " + value);
0377: } catch (NumberFormatException ex) {
0378: // empty
0379: }
0380: }
0381:
0382: public void testCreateBigDecimal() {
0383: assertEquals("createBigDecimal(String) failed", new BigDecimal(
0384: "1234.5"), NumberUtils.createBigDecimal("1234.5"));
0385: assertEquals("createBigDecimal(null) failed", null, NumberUtils
0386: .createBigDecimal(null));
0387: this .testCreateBigDecimalFailure("");
0388: this .testCreateBigDecimalFailure(" ");
0389: this .testCreateBigDecimalFailure("\b\t\n\f\r");
0390: // Funky whitespaces
0391: this
0392: .testCreateBigDecimalFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
0393: }
0394:
0395: protected void testCreateBigDecimalFailure(String str) {
0396: try {
0397: BigDecimal value = NumberUtils.createBigDecimal(str);
0398: fail("createBigDecimal(blank) failed: " + value);
0399: } catch (NumberFormatException ex) {
0400: // empty
0401: }
0402: }
0403:
0404: // min/max tests
0405: // ----------------------------------------------------------------------
0406: public void testMinLong() {
0407: final long[] l = null;
0408: try {
0409: NumberUtils.min(l);
0410: fail("No exception was thrown for null input.");
0411: } catch (IllegalArgumentException ex) {
0412: }
0413:
0414: try {
0415: NumberUtils.min(new long[0]);
0416: fail("No exception was thrown for empty input.");
0417: } catch (IllegalArgumentException ex) {
0418: }
0419:
0420: assertEquals("min(long[]) failed for array length 1", 5,
0421: NumberUtils.min(new long[] { 5 }));
0422:
0423: assertEquals("min(long[]) failed for array length 2", 6,
0424: NumberUtils.min(new long[] { 6, 9 }));
0425:
0426: assertEquals(-10, NumberUtils.min(new long[] { -10, -5, 0, 5,
0427: 10 }));
0428: assertEquals(-10, NumberUtils.min(new long[] { -5, 0, -10, 5,
0429: 10 }));
0430: }
0431:
0432: public void testMinInt() {
0433: final int[] i = null;
0434: try {
0435: NumberUtils.min(i);
0436: fail("No exception was thrown for null input.");
0437: } catch (IllegalArgumentException ex) {
0438: }
0439:
0440: try {
0441: NumberUtils.min(new int[0]);
0442: fail("No exception was thrown for empty input.");
0443: } catch (IllegalArgumentException ex) {
0444: }
0445:
0446: assertEquals("min(int[]) failed for array length 1", 5,
0447: NumberUtils.min(new int[] { 5 }));
0448:
0449: assertEquals("min(int[]) failed for array length 2", 6,
0450: NumberUtils.min(new int[] { 6, 9 }));
0451:
0452: assertEquals(-10, NumberUtils
0453: .min(new int[] { -10, -5, 0, 5, 10 }));
0454: assertEquals(-10, NumberUtils
0455: .min(new int[] { -5, 0, -10, 5, 10 }));
0456: }
0457:
0458: public void testMinShort() {
0459: final short[] s = null;
0460: try {
0461: NumberUtils.min(s);
0462: fail("No exception was thrown for null input.");
0463: } catch (IllegalArgumentException ex) {
0464: }
0465:
0466: try {
0467: NumberUtils.min(new short[0]);
0468: fail("No exception was thrown for empty input.");
0469: } catch (IllegalArgumentException ex) {
0470: }
0471:
0472: assertEquals("min(short[]) failed for array length 1", 5,
0473: NumberUtils.min(new short[] { 5 }));
0474:
0475: assertEquals("min(short[]) failed for array length 2", 6,
0476: NumberUtils.min(new short[] { 6, 9 }));
0477:
0478: assertEquals(-10, NumberUtils.min(new short[] { -10, -5, 0, 5,
0479: 10 }));
0480: assertEquals(-10, NumberUtils.min(new short[] { -5, 0, -10, 5,
0481: 10 }));
0482: }
0483:
0484: public void testMinByte() {
0485: final byte[] b = null;
0486: try {
0487: NumberUtils.min(b);
0488: fail("No exception was thrown for null input.");
0489: } catch (IllegalArgumentException ex) {
0490: }
0491:
0492: try {
0493: NumberUtils.min(new byte[0]);
0494: fail("No exception was thrown for empty input.");
0495: } catch (IllegalArgumentException ex) {
0496: }
0497:
0498: assertEquals("min(byte[]) failed for array length 1", 5,
0499: NumberUtils.min(new byte[] { 5 }));
0500:
0501: assertEquals("min(byte[]) failed for array length 2", 6,
0502: NumberUtils.min(new byte[] { 6, 9 }));
0503:
0504: assertEquals(-10, NumberUtils.min(new byte[] { -10, -5, 0, 5,
0505: 10 }));
0506: assertEquals(-10, NumberUtils.min(new byte[] { -5, 0, -10, 5,
0507: 10 }));
0508: }
0509:
0510: public void testMinDouble() {
0511: final double[] d = null;
0512: try {
0513: NumberUtils.min(d);
0514: fail("No exception was thrown for null input.");
0515: } catch (IllegalArgumentException ex) {
0516: }
0517:
0518: try {
0519: NumberUtils.min(new double[0]);
0520: fail("No exception was thrown for empty input.");
0521: } catch (IllegalArgumentException ex) {
0522: }
0523:
0524: assertEquals("min(double[]) failed for array length 1", 5.12,
0525: NumberUtils.min(new double[] { 5.12 }), 0);
0526:
0527: assertEquals("min(double[]) failed for array length 2", 6.23,
0528: NumberUtils.min(new double[] { 6.23, 9.34 }), 0);
0529:
0530: assertEquals("min(double[]) failed for array length 5", -10.45,
0531: NumberUtils.min(new double[] { -10.45, -5.56, 0, 5.67,
0532: 10.78 }), 0);
0533: assertEquals(-10, NumberUtils.min(new double[] { -10, -5, 0, 5,
0534: 10 }), 0.0001);
0535: assertEquals(-10, NumberUtils.min(new double[] { -5, 0, -10, 5,
0536: 10 }), 0.0001);
0537: }
0538:
0539: public void testMinFloat() {
0540: final float[] f = null;
0541: try {
0542: NumberUtils.min(f);
0543: fail("No exception was thrown for null input.");
0544: } catch (IllegalArgumentException ex) {
0545: }
0546:
0547: try {
0548: NumberUtils.min(new float[0]);
0549: fail("No exception was thrown for empty input.");
0550: } catch (IllegalArgumentException ex) {
0551: }
0552:
0553: assertEquals("min(float[]) failed for array length 1", 5.9f,
0554: NumberUtils.min(new float[] { 5.9f }), 0);
0555:
0556: assertEquals("min(float[]) failed for array length 2", 6.8f,
0557: NumberUtils.min(new float[] { 6.8f, 9.7f }), 0);
0558:
0559: assertEquals("min(float[]) failed for array length 5", -10.6f,
0560: NumberUtils.min(new float[] { -10.6f, -5.5f, 0, 5.4f,
0561: 10.3f }), 0);
0562: assertEquals(-10, NumberUtils.min(new float[] { -10, -5, 0, 5,
0563: 10 }), 0.0001f);
0564: assertEquals(-10, NumberUtils.min(new float[] { -5, 0, -10, 5,
0565: 10 }), 0.0001f);
0566: }
0567:
0568: public void testMaxLong() {
0569: final long[] l = null;
0570: try {
0571: NumberUtils.max(l);
0572: fail("No exception was thrown for null input.");
0573: } catch (IllegalArgumentException ex) {
0574: }
0575:
0576: try {
0577: NumberUtils.max(new long[0]);
0578: fail("No exception was thrown for empty input.");
0579: } catch (IllegalArgumentException ex) {
0580: }
0581:
0582: assertEquals("max(long[]) failed for array length 1", 5,
0583: NumberUtils.max(new long[] { 5 }));
0584:
0585: assertEquals("max(long[]) failed for array length 2", 9,
0586: NumberUtils.max(new long[] { 6, 9 }));
0587:
0588: assertEquals("max(long[]) failed for array length 5", 10,
0589: NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
0590: assertEquals(10, NumberUtils
0591: .max(new long[] { -10, -5, 0, 5, 10 }));
0592: assertEquals(10, NumberUtils
0593: .max(new long[] { -5, 0, 10, 5, -10 }));
0594: }
0595:
0596: public void testMaxInt() {
0597: final int[] i = null;
0598: try {
0599: NumberUtils.max(i);
0600: fail("No exception was thrown for null input.");
0601: } catch (IllegalArgumentException ex) {
0602: }
0603:
0604: try {
0605: NumberUtils.max(new int[0]);
0606: fail("No exception was thrown for empty input.");
0607: } catch (IllegalArgumentException ex) {
0608: }
0609:
0610: assertEquals("max(int[]) failed for array length 1", 5,
0611: NumberUtils.max(new int[] { 5 }));
0612:
0613: assertEquals("max(int[]) failed for array length 2", 9,
0614: NumberUtils.max(new int[] { 6, 9 }));
0615:
0616: assertEquals("max(int[]) failed for array length 5", 10,
0617: NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
0618: assertEquals(10, NumberUtils
0619: .max(new int[] { -10, -5, 0, 5, 10 }));
0620: assertEquals(10, NumberUtils
0621: .max(new int[] { -5, 0, 10, 5, -10 }));
0622: }
0623:
0624: public void testMaxShort() {
0625: final short[] s = null;
0626: try {
0627: NumberUtils.max(s);
0628: fail("No exception was thrown for null input.");
0629: } catch (IllegalArgumentException ex) {
0630: }
0631:
0632: try {
0633: NumberUtils.max(new short[0]);
0634: fail("No exception was thrown for empty input.");
0635: } catch (IllegalArgumentException ex) {
0636: }
0637:
0638: assertEquals("max(short[]) failed for array length 1", 5,
0639: NumberUtils.max(new short[] { 5 }));
0640:
0641: assertEquals("max(short[]) failed for array length 2", 9,
0642: NumberUtils.max(new short[] { 6, 9 }));
0643:
0644: assertEquals("max(short[]) failed for array length 5", 10,
0645: NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
0646: assertEquals(10, NumberUtils.max(new short[] { -10, -5, 0, 5,
0647: 10 }));
0648: assertEquals(10, NumberUtils.max(new short[] { -5, 0, 10, 5,
0649: -10 }));
0650: }
0651:
0652: public void testMaxByte() {
0653: final byte[] b = null;
0654: try {
0655: NumberUtils.max(b);
0656: fail("No exception was thrown for null input.");
0657: } catch (IllegalArgumentException ex) {
0658: }
0659:
0660: try {
0661: NumberUtils.max(new byte[0]);
0662: fail("No exception was thrown for empty input.");
0663: } catch (IllegalArgumentException ex) {
0664: }
0665:
0666: assertEquals("max(byte[]) failed for array length 1", 5,
0667: NumberUtils.max(new byte[] { 5 }));
0668:
0669: assertEquals("max(byte[]) failed for array length 2", 9,
0670: NumberUtils.max(new byte[] { 6, 9 }));
0671:
0672: assertEquals("max(byte[]) failed for array length 5", 10,
0673: NumberUtils.max(new byte[] { -10, -5, 0, 5, 10 }));
0674: assertEquals(10, NumberUtils
0675: .max(new byte[] { -10, -5, 0, 5, 10 }));
0676: assertEquals(10, NumberUtils
0677: .max(new byte[] { -5, 0, 10, 5, -10 }));
0678: }
0679:
0680: public void testMaxDouble() {
0681: final double[] d = null;
0682: try {
0683: NumberUtils.max(d);
0684: fail("No exception was thrown for null input.");
0685: } catch (IllegalArgumentException ex) {
0686: }
0687:
0688: try {
0689: NumberUtils.max(new double[0]);
0690: fail("No exception was thrown for empty input.");
0691: } catch (IllegalArgumentException ex) {
0692: }
0693:
0694: assertEquals("max(double[]) failed for array length 1", 5.1f,
0695: NumberUtils.max(new double[] { 5.1f }), 0);
0696:
0697: assertEquals("max(double[]) failed for array length 2", 9.2f,
0698: NumberUtils.max(new double[] { 6.3f, 9.2f }), 0);
0699:
0700: assertEquals("max(double[]) failed for float length 5", 10.4f,
0701: NumberUtils.max(new double[] { -10.5f, -5.6f, 0, 5.7f,
0702: 10.4f }), 0);
0703: assertEquals(10, NumberUtils.max(new double[] { -10, -5, 0, 5,
0704: 10 }), 0.0001);
0705: assertEquals(10, NumberUtils.max(new double[] { -5, 0, 10, 5,
0706: -10 }), 0.0001);
0707: }
0708:
0709: public void testMaxFloat() {
0710: final float[] f = null;
0711: try {
0712: NumberUtils.max(f);
0713: fail("No exception was thrown for null input.");
0714: } catch (IllegalArgumentException ex) {
0715: }
0716:
0717: try {
0718: NumberUtils.max(new float[0]);
0719: fail("No exception was thrown for empty input.");
0720: } catch (IllegalArgumentException ex) {
0721: }
0722:
0723: assertEquals("max(float[]) failed for array length 1", 5.1f,
0724: NumberUtils.max(new float[] { 5.1f }), 0);
0725:
0726: assertEquals("max(float[]) failed for array length 2", 9.2f,
0727: NumberUtils.max(new float[] { 6.3f, 9.2f }), 0);
0728:
0729: assertEquals("max(float[]) failed for float length 5", 10.4f,
0730: NumberUtils.max(new float[] { -10.5f, -5.6f, 0, 5.7f,
0731: 10.4f }), 0);
0732: assertEquals(10, NumberUtils.max(new float[] { -10, -5, 0, 5,
0733: 10 }), 0.0001f);
0734: assertEquals(10, NumberUtils.max(new float[] { -5, 0, 10, 5,
0735: -10 }), 0.0001f);
0736: }
0737:
0738: public void testMinimumLong() {
0739: assertEquals("minimum(long,long,long) 1 failed", 12345L,
0740: NumberUtils.min(12345L, 12345L + 1L, 12345L + 2L));
0741: assertEquals("minimum(long,long,long) 2 failed", 12345L,
0742: NumberUtils.min(12345L + 1L, 12345L, 12345 + 2L));
0743: assertEquals("minimum(long,long,long) 3 failed", 12345L,
0744: NumberUtils.min(12345L + 1L, 12345L + 2L, 12345L));
0745: assertEquals("minimum(long,long,long) 4 failed", 12345L,
0746: NumberUtils.min(12345L + 1L, 12345L, 12345L));
0747: assertEquals("minimum(long,long,long) 5 failed", 12345L,
0748: NumberUtils.min(12345L, 12345L, 12345L));
0749: }
0750:
0751: public void testMinimumInt() {
0752: assertEquals("minimum(int,int,int) 1 failed", 12345,
0753: NumberUtils.min(12345, 12345 + 1, 12345 + 2));
0754: assertEquals("minimum(int,int,int) 2 failed", 12345,
0755: NumberUtils.min(12345 + 1, 12345, 12345 + 2));
0756: assertEquals("minimum(int,int,int) 3 failed", 12345,
0757: NumberUtils.min(12345 + 1, 12345 + 2, 12345));
0758: assertEquals("minimum(int,int,int) 4 failed", 12345,
0759: NumberUtils.min(12345 + 1, 12345, 12345));
0760: assertEquals("minimum(int,int,int) 5 failed", 12345,
0761: NumberUtils.min(12345, 12345, 12345));
0762: }
0763:
0764: public void testMinimumShort() {
0765: short low = 1234;
0766: short mid = 1234 + 1;
0767: short high = 1234 + 2;
0768: assertEquals("minimum(short,short,short) 1 failed", low,
0769: NumberUtils.min(low, mid, high));
0770: assertEquals("minimum(short,short,short) 1 failed", low,
0771: NumberUtils.min(mid, low, high));
0772: assertEquals("minimum(short,short,short) 1 failed", low,
0773: NumberUtils.min(mid, high, low));
0774: assertEquals("minimum(short,short,short) 1 failed", low,
0775: NumberUtils.min(low, mid, low));
0776: }
0777:
0778: public void testMinimumByte() {
0779: byte low = 123;
0780: byte mid = 123 + 1;
0781: byte high = 123 + 2;
0782: assertEquals("minimum(byte,byte,byte) 1 failed", low,
0783: NumberUtils.min(low, mid, high));
0784: assertEquals("minimum(byte,byte,byte) 1 failed", low,
0785: NumberUtils.min(mid, low, high));
0786: assertEquals("minimum(byte,byte,byte) 1 failed", low,
0787: NumberUtils.min(mid, high, low));
0788: assertEquals("minimum(byte,byte,byte) 1 failed", low,
0789: NumberUtils.min(low, mid, low));
0790: }
0791:
0792: public void testMinimumDouble() {
0793: double low = 12.3;
0794: double mid = 12.3 + 1;
0795: double high = 12.3 + 2;
0796: assertEquals(low, NumberUtils.min(low, mid, high), 0.0001);
0797: assertEquals(low, NumberUtils.min(mid, low, high), 0.0001);
0798: assertEquals(low, NumberUtils.min(mid, high, low), 0.0001);
0799: assertEquals(low, NumberUtils.min(low, mid, low), 0.0001);
0800: assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001);
0801: }
0802:
0803: public void testMinimumFloat() {
0804: float low = 12.3f;
0805: float mid = 12.3f + 1;
0806: float high = 12.3f + 2;
0807: assertEquals(low, NumberUtils.min(low, mid, high), 0.0001f);
0808: assertEquals(low, NumberUtils.min(mid, low, high), 0.0001f);
0809: assertEquals(low, NumberUtils.min(mid, high, low), 0.0001f);
0810: assertEquals(low, NumberUtils.min(low, mid, low), 0.0001f);
0811: assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001f);
0812: }
0813:
0814: public void testMaximumLong() {
0815: assertEquals("maximum(long,long,long) 1 failed", 12345L,
0816: NumberUtils.max(12345L, 12345L - 1L, 12345L - 2L));
0817: assertEquals("maximum(long,long,long) 2 failed", 12345L,
0818: NumberUtils.max(12345L - 1L, 12345L, 12345L - 2L));
0819: assertEquals("maximum(long,long,long) 3 failed", 12345L,
0820: NumberUtils.max(12345L - 1L, 12345L - 2L, 12345L));
0821: assertEquals("maximum(long,long,long) 4 failed", 12345L,
0822: NumberUtils.max(12345L - 1L, 12345L, 12345L));
0823: assertEquals("maximum(long,long,long) 5 failed", 12345L,
0824: NumberUtils.max(12345L, 12345L, 12345L));
0825: }
0826:
0827: public void testMaximumInt() {
0828: assertEquals("maximum(int,int,int) 1 failed", 12345,
0829: NumberUtils.max(12345, 12345 - 1, 12345 - 2));
0830: assertEquals("maximum(int,int,int) 2 failed", 12345,
0831: NumberUtils.max(12345 - 1, 12345, 12345 - 2));
0832: assertEquals("maximum(int,int,int) 3 failed", 12345,
0833: NumberUtils.max(12345 - 1, 12345 - 2, 12345));
0834: assertEquals("maximum(int,int,int) 4 failed", 12345,
0835: NumberUtils.max(12345 - 1, 12345, 12345));
0836: assertEquals("maximum(int,int,int) 5 failed", 12345,
0837: NumberUtils.max(12345, 12345, 12345));
0838: }
0839:
0840: public void testMaximumShort() {
0841: short low = 1234;
0842: short mid = 1234 + 1;
0843: short high = 1234 + 2;
0844: assertEquals("maximum(short,short,short) 1 failed", high,
0845: NumberUtils.max(low, mid, high));
0846: assertEquals("maximum(short,short,short) 1 failed", high,
0847: NumberUtils.max(mid, low, high));
0848: assertEquals("maximum(short,short,short) 1 failed", high,
0849: NumberUtils.max(mid, high, low));
0850: assertEquals("maximum(short,short,short) 1 failed", high,
0851: NumberUtils.max(high, mid, high));
0852: }
0853:
0854: public void testMaximumByte() {
0855: byte low = 123;
0856: byte mid = 123 + 1;
0857: byte high = 123 + 2;
0858: assertEquals("maximum(byte,byte,byte) 1 failed", high,
0859: NumberUtils.max(low, mid, high));
0860: assertEquals("maximum(byte,byte,byte) 1 failed", high,
0861: NumberUtils.max(mid, low, high));
0862: assertEquals("maximum(byte,byte,byte) 1 failed", high,
0863: NumberUtils.max(mid, high, low));
0864: assertEquals("maximum(byte,byte,byte) 1 failed", high,
0865: NumberUtils.max(high, mid, high));
0866: }
0867:
0868: public void testMaximumDouble() {
0869: double low = 12.3;
0870: double mid = 12.3 + 1;
0871: double high = 12.3 + 2;
0872: assertEquals(high, NumberUtils.max(low, mid, high), 0.0001);
0873: assertEquals(high, NumberUtils.max(mid, low, high), 0.0001);
0874: assertEquals(high, NumberUtils.max(mid, high, low), 0.0001);
0875: assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001);
0876: assertEquals(high, NumberUtils.max(high, mid, high), 0.0001);
0877: }
0878:
0879: public void testMaximumFloat() {
0880: float low = 12.3f;
0881: float mid = 12.3f + 1;
0882: float high = 12.3f + 2;
0883: assertEquals(high, NumberUtils.max(low, mid, high), 0.0001f);
0884: assertEquals(high, NumberUtils.max(mid, low, high), 0.0001f);
0885: assertEquals(high, NumberUtils.max(mid, high, low), 0.0001f);
0886: assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001f);
0887: assertEquals(high, NumberUtils.max(high, mid, high), 0.0001f);
0888: }
0889:
0890: public void testCompareDouble() {
0891: assertTrue(NumberUtils.compare(Double.NaN, Double.NaN) == 0);
0892: assertTrue(NumberUtils.compare(Double.NaN,
0893: Double.POSITIVE_INFINITY) == +1);
0894: assertTrue(NumberUtils.compare(Double.NaN, Double.MAX_VALUE) == +1);
0895: assertTrue(NumberUtils.compare(Double.NaN, 1.2d) == +1);
0896: assertTrue(NumberUtils.compare(Double.NaN, 0.0d) == +1);
0897: assertTrue(NumberUtils.compare(Double.NaN, -0.0d) == +1);
0898: assertTrue(NumberUtils.compare(Double.NaN, -1.2d) == +1);
0899: assertTrue(NumberUtils.compare(Double.NaN, -Double.MAX_VALUE) == +1);
0900: assertTrue(NumberUtils.compare(Double.NaN,
0901: Double.NEGATIVE_INFINITY) == +1);
0902:
0903: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
0904: Double.NaN) == -1);
0905: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
0906: Double.POSITIVE_INFINITY) == 0);
0907: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
0908: Double.MAX_VALUE) == +1);
0909: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 1.2d) == +1);
0910: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 0.0d) == +1);
0911: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -0.0d) == +1);
0912: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -1.2d) == +1);
0913: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
0914: -Double.MAX_VALUE) == +1);
0915: assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY,
0916: Double.NEGATIVE_INFINITY) == +1);
0917:
0918: assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NaN) == -1);
0919: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
0920: Double.POSITIVE_INFINITY) == -1);
0921: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
0922: Double.MAX_VALUE) == 0);
0923: assertTrue(NumberUtils.compare(Double.MAX_VALUE, 1.2d) == +1);
0924: assertTrue(NumberUtils.compare(Double.MAX_VALUE, 0.0d) == +1);
0925: assertTrue(NumberUtils.compare(Double.MAX_VALUE, -0.0d) == +1);
0926: assertTrue(NumberUtils.compare(Double.MAX_VALUE, -1.2d) == +1);
0927: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
0928: -Double.MAX_VALUE) == +1);
0929: assertTrue(NumberUtils.compare(Double.MAX_VALUE,
0930: Double.NEGATIVE_INFINITY) == +1);
0931:
0932: assertTrue(NumberUtils.compare(1.2d, Double.NaN) == -1);
0933: assertTrue(NumberUtils.compare(1.2d, Double.POSITIVE_INFINITY) == -1);
0934: assertTrue(NumberUtils.compare(1.2d, Double.MAX_VALUE) == -1);
0935: assertTrue(NumberUtils.compare(1.2d, 1.2d) == 0);
0936: assertTrue(NumberUtils.compare(1.2d, 0.0d) == +1);
0937: assertTrue(NumberUtils.compare(1.2d, -0.0d) == +1);
0938: assertTrue(NumberUtils.compare(1.2d, -1.2d) == +1);
0939: assertTrue(NumberUtils.compare(1.2d, -Double.MAX_VALUE) == +1);
0940: assertTrue(NumberUtils.compare(1.2d, Double.NEGATIVE_INFINITY) == +1);
0941:
0942: assertTrue(NumberUtils.compare(0.0d, Double.NaN) == -1);
0943: assertTrue(NumberUtils.compare(0.0d, Double.POSITIVE_INFINITY) == -1);
0944: assertTrue(NumberUtils.compare(0.0d, Double.MAX_VALUE) == -1);
0945: assertTrue(NumberUtils.compare(0.0d, 1.2d) == -1);
0946: assertTrue(NumberUtils.compare(0.0d, 0.0d) == 0);
0947: assertTrue(NumberUtils.compare(0.0d, -0.0d) == +1);
0948: assertTrue(NumberUtils.compare(0.0d, -1.2d) == +1);
0949: assertTrue(NumberUtils.compare(0.0d, -Double.MAX_VALUE) == +1);
0950: assertTrue(NumberUtils.compare(0.0d, Double.NEGATIVE_INFINITY) == +1);
0951:
0952: assertTrue(NumberUtils.compare(-0.0d, Double.NaN) == -1);
0953: assertTrue(NumberUtils.compare(-0.0d, Double.POSITIVE_INFINITY) == -1);
0954: assertTrue(NumberUtils.compare(-0.0d, Double.MAX_VALUE) == -1);
0955: assertTrue(NumberUtils.compare(-0.0d, 1.2d) == -1);
0956: assertTrue(NumberUtils.compare(-0.0d, 0.0d) == -1);
0957: assertTrue(NumberUtils.compare(-0.0d, -0.0d) == 0);
0958: assertTrue(NumberUtils.compare(-0.0d, -1.2d) == +1);
0959: assertTrue(NumberUtils.compare(-0.0d, -Double.MAX_VALUE) == +1);
0960: assertTrue(NumberUtils.compare(-0.0d, Double.NEGATIVE_INFINITY) == +1);
0961:
0962: assertTrue(NumberUtils.compare(-1.2d, Double.NaN) == -1);
0963: assertTrue(NumberUtils.compare(-1.2d, Double.POSITIVE_INFINITY) == -1);
0964: assertTrue(NumberUtils.compare(-1.2d, Double.MAX_VALUE) == -1);
0965: assertTrue(NumberUtils.compare(-1.2d, 1.2d) == -1);
0966: assertTrue(NumberUtils.compare(-1.2d, 0.0d) == -1);
0967: assertTrue(NumberUtils.compare(-1.2d, -0.0d) == -1);
0968: assertTrue(NumberUtils.compare(-1.2d, -1.2d) == 0);
0969: assertTrue(NumberUtils.compare(-1.2d, -Double.MAX_VALUE) == +1);
0970: assertTrue(NumberUtils.compare(-1.2d, Double.NEGATIVE_INFINITY) == +1);
0971:
0972: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NaN) == -1);
0973: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
0974: Double.POSITIVE_INFINITY) == -1);
0975: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
0976: Double.MAX_VALUE) == -1);
0977: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 1.2d) == -1);
0978: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 0.0d) == -1);
0979: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -0.0d) == -1);
0980: assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -1.2d) == -1);
0981: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
0982: -Double.MAX_VALUE) == 0);
0983: assertTrue(NumberUtils.compare(-Double.MAX_VALUE,
0984: Double.NEGATIVE_INFINITY) == +1);
0985:
0986: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
0987: Double.NaN) == -1);
0988: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
0989: Double.POSITIVE_INFINITY) == -1);
0990: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
0991: Double.MAX_VALUE) == -1);
0992: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 1.2d) == -1);
0993: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 0.0d) == -1);
0994: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -0.0d) == -1);
0995: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -1.2d) == -1);
0996: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
0997: -Double.MAX_VALUE) == -1);
0998: assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY,
0999: Double.NEGATIVE_INFINITY) == 0);
1000: }
1001:
1002: public void testCompareFloat() {
1003: assertTrue(NumberUtils.compare(Float.NaN, Float.NaN) == 0);
1004: assertTrue(NumberUtils.compare(Float.NaN,
1005: Float.POSITIVE_INFINITY) == +1);
1006: assertTrue(NumberUtils.compare(Float.NaN, Float.MAX_VALUE) == +1);
1007: assertTrue(NumberUtils.compare(Float.NaN, 1.2f) == +1);
1008: assertTrue(NumberUtils.compare(Float.NaN, 0.0f) == +1);
1009: assertTrue(NumberUtils.compare(Float.NaN, -0.0f) == +1);
1010: assertTrue(NumberUtils.compare(Float.NaN, -1.2f) == +1);
1011: assertTrue(NumberUtils.compare(Float.NaN, -Float.MAX_VALUE) == +1);
1012: assertTrue(NumberUtils.compare(Float.NaN,
1013: Float.NEGATIVE_INFINITY) == +1);
1014:
1015: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
1016: Float.NaN) == -1);
1017: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
1018: Float.POSITIVE_INFINITY) == 0);
1019: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
1020: Float.MAX_VALUE) == +1);
1021: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 1.2f) == +1);
1022: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 0.0f) == +1);
1023: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -0.0f) == +1);
1024: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -1.2f) == +1);
1025: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
1026: -Float.MAX_VALUE) == +1);
1027: assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY,
1028: Float.NEGATIVE_INFINITY) == +1);
1029:
1030: assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NaN) == -1);
1031: assertTrue(NumberUtils.compare(Float.MAX_VALUE,
1032: Float.POSITIVE_INFINITY) == -1);
1033: assertTrue(NumberUtils
1034: .compare(Float.MAX_VALUE, Float.MAX_VALUE) == 0);
1035: assertTrue(NumberUtils.compare(Float.MAX_VALUE, 1.2f) == +1);
1036: assertTrue(NumberUtils.compare(Float.MAX_VALUE, 0.0f) == +1);
1037: assertTrue(NumberUtils.compare(Float.MAX_VALUE, -0.0f) == +1);
1038: assertTrue(NumberUtils.compare(Float.MAX_VALUE, -1.2f) == +1);
1039: assertTrue(NumberUtils.compare(Float.MAX_VALUE,
1040: -Float.MAX_VALUE) == +1);
1041: assertTrue(NumberUtils.compare(Float.MAX_VALUE,
1042: Float.NEGATIVE_INFINITY) == +1);
1043:
1044: assertTrue(NumberUtils.compare(1.2f, Float.NaN) == -1);
1045: assertTrue(NumberUtils.compare(1.2f, Float.POSITIVE_INFINITY) == -1);
1046: assertTrue(NumberUtils.compare(1.2f, Float.MAX_VALUE) == -1);
1047: assertTrue(NumberUtils.compare(1.2f, 1.2f) == 0);
1048: assertTrue(NumberUtils.compare(1.2f, 0.0f) == +1);
1049: assertTrue(NumberUtils.compare(1.2f, -0.0f) == +1);
1050: assertTrue(NumberUtils.compare(1.2f, -1.2f) == +1);
1051: assertTrue(NumberUtils.compare(1.2f, -Float.MAX_VALUE) == +1);
1052: assertTrue(NumberUtils.compare(1.2f, Float.NEGATIVE_INFINITY) == +1);
1053:
1054: assertTrue(NumberUtils.compare(0.0f, Float.NaN) == -1);
1055: assertTrue(NumberUtils.compare(0.0f, Float.POSITIVE_INFINITY) == -1);
1056: assertTrue(NumberUtils.compare(0.0f, Float.MAX_VALUE) == -1);
1057: assertTrue(NumberUtils.compare(0.0f, 1.2f) == -1);
1058: assertTrue(NumberUtils.compare(0.0f, 0.0f) == 0);
1059: assertTrue(NumberUtils.compare(0.0f, -0.0f) == +1);
1060: assertTrue(NumberUtils.compare(0.0f, -1.2f) == +1);
1061: assertTrue(NumberUtils.compare(0.0f, -Float.MAX_VALUE) == +1);
1062: assertTrue(NumberUtils.compare(0.0f, Float.NEGATIVE_INFINITY) == +1);
1063:
1064: assertTrue(NumberUtils.compare(-0.0f, Float.NaN) == -1);
1065: assertTrue(NumberUtils.compare(-0.0f, Float.POSITIVE_INFINITY) == -1);
1066: assertTrue(NumberUtils.compare(-0.0f, Float.MAX_VALUE) == -1);
1067: assertTrue(NumberUtils.compare(-0.0f, 1.2f) == -1);
1068: assertTrue(NumberUtils.compare(-0.0f, 0.0f) == -1);
1069: assertTrue(NumberUtils.compare(-0.0f, -0.0f) == 0);
1070: assertTrue(NumberUtils.compare(-0.0f, -1.2f) == +1);
1071: assertTrue(NumberUtils.compare(-0.0f, -Float.MAX_VALUE) == +1);
1072: assertTrue(NumberUtils.compare(-0.0f, Float.NEGATIVE_INFINITY) == +1);
1073:
1074: assertTrue(NumberUtils.compare(-1.2f, Float.NaN) == -1);
1075: assertTrue(NumberUtils.compare(-1.2f, Float.POSITIVE_INFINITY) == -1);
1076: assertTrue(NumberUtils.compare(-1.2f, Float.MAX_VALUE) == -1);
1077: assertTrue(NumberUtils.compare(-1.2f, 1.2f) == -1);
1078: assertTrue(NumberUtils.compare(-1.2f, 0.0f) == -1);
1079: assertTrue(NumberUtils.compare(-1.2f, -0.0f) == -1);
1080: assertTrue(NumberUtils.compare(-1.2f, -1.2f) == 0);
1081: assertTrue(NumberUtils.compare(-1.2f, -Float.MAX_VALUE) == +1);
1082: assertTrue(NumberUtils.compare(-1.2f, Float.NEGATIVE_INFINITY) == +1);
1083:
1084: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NaN) == -1);
1085: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
1086: Float.POSITIVE_INFINITY) == -1);
1087: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
1088: Float.MAX_VALUE) == -1);
1089: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 1.2f) == -1);
1090: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 0.0f) == -1);
1091: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -0.0f) == -1);
1092: assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -1.2f) == -1);
1093: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
1094: -Float.MAX_VALUE) == 0);
1095: assertTrue(NumberUtils.compare(-Float.MAX_VALUE,
1096: Float.NEGATIVE_INFINITY) == +1);
1097:
1098: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
1099: Float.NaN) == -1);
1100: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
1101: Float.POSITIVE_INFINITY) == -1);
1102: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
1103: Float.MAX_VALUE) == -1);
1104: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 1.2f) == -1);
1105: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 0.0f) == -1);
1106: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -0.0f) == -1);
1107: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -1.2f) == -1);
1108: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
1109: -Float.MAX_VALUE) == -1);
1110: assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY,
1111: Float.NEGATIVE_INFINITY) == 0);
1112: }
1113:
1114: public void testIsDigits() {
1115: assertEquals("isDigits(null) failed", false, NumberUtils
1116: .isDigits(null));
1117: assertEquals("isDigits('') failed", false, NumberUtils
1118: .isDigits(""));
1119: assertEquals("isDigits(String) failed", true, NumberUtils
1120: .isDigits("12345"));
1121: assertEquals("isDigits(String) neg 1 failed", false,
1122: NumberUtils.isDigits("1234.5"));
1123: assertEquals("isDigits(String) neg 3 failed", false,
1124: NumberUtils.isDigits("1ab"));
1125: assertEquals("isDigits(String) neg 4 failed", false,
1126: NumberUtils.isDigits("abc"));
1127: }
1128:
1129: /**
1130: * Tests isNumber(String) and tests that createNumber(String) returns
1131: * a valid number iff isNumber(String) returns false.
1132: */
1133: public void testIsNumber() {
1134: String val = "12345";
1135: assertTrue("isNumber(String) 1 failed", NumberUtils
1136: .isNumber(val));
1137: assertTrue("isNumber(String)/createNumber(String) 1 failed",
1138: checkCreateNumber(val));
1139: val = "1234.5";
1140: assertTrue("isNumber(String) 2 failed", NumberUtils
1141: .isNumber(val));
1142: assertTrue("isNumber(String)/createNumber(String) 2 failed",
1143: checkCreateNumber(val));
1144: val = ".12345";
1145: assertTrue("isNumber(String) 3 failed", NumberUtils
1146: .isNumber(val));
1147: assertTrue("isNumber(String)/createNumber(String) 3 failed",
1148: checkCreateNumber(val));
1149: val = "1234E5";
1150: assertTrue("isNumber(String) 4 failed", NumberUtils
1151: .isNumber(val));
1152: assertTrue("isNumber(String)/createNumber(String) 4 failed",
1153: checkCreateNumber(val));
1154: val = "1234E+5";
1155: assertTrue("isNumber(String) 5 failed", NumberUtils
1156: .isNumber(val));
1157: assertTrue("isNumber(String)/createNumber(String) 5 failed",
1158: checkCreateNumber(val));
1159: val = "1234E-5";
1160: assertTrue("isNumber(String) 6 failed", NumberUtils
1161: .isNumber(val));
1162: assertTrue("isNumber(String)/createNumber(String) 6 failed",
1163: checkCreateNumber(val));
1164: val = "123.4E5";
1165: assertTrue("isNumber(String) 7 failed", NumberUtils
1166: .isNumber(val));
1167: assertTrue("isNumber(String)/createNumber(String) 7 failed",
1168: checkCreateNumber(val));
1169: val = "-1234";
1170: assertTrue("isNumber(String) 8 failed", NumberUtils
1171: .isNumber(val));
1172: assertTrue("isNumber(String)/createNumber(String) 8 failed",
1173: checkCreateNumber(val));
1174: val = "-1234.5";
1175: assertTrue("isNumber(String) 9 failed", NumberUtils
1176: .isNumber(val));
1177: assertTrue("isNumber(String)/createNumber(String) 9 failed",
1178: checkCreateNumber(val));
1179: val = "-.12345";
1180: assertTrue("isNumber(String) 10 failed", NumberUtils
1181: .isNumber(val));
1182: assertTrue("isNumber(String)/createNumber(String) 10 failed",
1183: checkCreateNumber(val));
1184: val = "-1234E5";
1185: assertTrue("isNumber(String) 11 failed", NumberUtils
1186: .isNumber(val));
1187: assertTrue("isNumber(String)/createNumber(String) 11 failed",
1188: checkCreateNumber(val));
1189: val = "0";
1190: assertTrue("isNumber(String) 12 failed", NumberUtils
1191: .isNumber(val));
1192: assertTrue("isNumber(String)/createNumber(String) 12 failed",
1193: checkCreateNumber(val));
1194: val = "-0";
1195: assertTrue("isNumber(String) 13 failed", NumberUtils
1196: .isNumber(val));
1197: assertTrue("isNumber(String)/createNumber(String) 13 failed",
1198: checkCreateNumber(val));
1199: val = "01234";
1200: assertTrue("isNumber(String) 14 failed", NumberUtils
1201: .isNumber(val));
1202: assertTrue("isNumber(String)/createNumber(String) 14 failed",
1203: checkCreateNumber(val));
1204: val = "-01234";
1205: assertTrue("isNumber(String) 15 failed", NumberUtils
1206: .isNumber(val));
1207: assertTrue("isNumber(String)/createNumber(String) 15 failed",
1208: checkCreateNumber(val));
1209: val = "0xABC123";
1210: assertTrue("isNumber(String) 16 failed", NumberUtils
1211: .isNumber(val));
1212: assertTrue("isNumber(String)/createNumber(String) 16 failed",
1213: checkCreateNumber(val));
1214: val = "0x0";
1215: assertTrue("isNumber(String) 17 failed", NumberUtils
1216: .isNumber(val));
1217: assertTrue("isNumber(String)/createNumber(String) 17 failed",
1218: checkCreateNumber(val));
1219: val = "123.4E21D";
1220: assertTrue("isNumber(String) 19 failed", NumberUtils
1221: .isNumber(val));
1222: assertTrue("isNumber(String)/createNumber(String) 19 failed",
1223: checkCreateNumber(val));
1224: val = "-221.23F";
1225: assertTrue("isNumber(String) 20 failed", NumberUtils
1226: .isNumber(val));
1227: assertTrue("isNumber(String)/createNumber(String) 20 failed",
1228: checkCreateNumber(val));
1229: val = "22338L";
1230: assertTrue("isNumber(String) 21 failed", NumberUtils
1231: .isNumber(val));
1232: assertTrue("isNumber(String)/createNumber(String) 21 failed",
1233: checkCreateNumber(val));
1234: val = null;
1235: assertTrue("isNumber(String) 1 Neg failed", !NumberUtils
1236: .isNumber(val));
1237: assertTrue(
1238: "isNumber(String)/createNumber(String) 1 Neg failed",
1239: !checkCreateNumber(val));
1240: val = "";
1241: assertTrue("isNumber(String) 2 Neg failed", !NumberUtils
1242: .isNumber(val));
1243: assertTrue(
1244: "isNumber(String)/createNumber(String) 2 Neg failed",
1245: !checkCreateNumber(val));
1246: val = "--2.3";
1247: assertTrue("isNumber(String) 3 Neg failed", !NumberUtils
1248: .isNumber(val));
1249: assertTrue(
1250: "isNumber(String)/createNumber(String) 3 Neg failed",
1251: !checkCreateNumber(val));
1252: val = ".12.3";
1253: assertTrue("isNumber(String) 4 Neg failed", !NumberUtils
1254: .isNumber(val));
1255: assertTrue(
1256: "isNumber(String)/createNumber(String) 4 Neg failed",
1257: !checkCreateNumber(val));
1258: val = "-123E";
1259: assertTrue("isNumber(String) 5 Neg failed", !NumberUtils
1260: .isNumber(val));
1261: assertTrue(
1262: "isNumber(String)/createNumber(String) 5 Neg failed",
1263: !checkCreateNumber(val));
1264: val = "-123E+-212";
1265: assertTrue("isNumber(String) 6 Neg failed", !NumberUtils
1266: .isNumber(val));
1267: assertTrue(
1268: "isNumber(String)/createNumber(String) 6 Neg failed",
1269: !checkCreateNumber(val));
1270: val = "-123E2.12";
1271: assertTrue("isNumber(String) 7 Neg failed", !NumberUtils
1272: .isNumber(val));
1273: assertTrue(
1274: "isNumber(String)/createNumber(String) 7 Neg failed",
1275: !checkCreateNumber(val));
1276: val = "0xGF";
1277: assertTrue("isNumber(String) 8 Neg failed", !NumberUtils
1278: .isNumber(val));
1279: assertTrue(
1280: "isNumber(String)/createNumber(String) 8 Neg failed",
1281: !checkCreateNumber(val));
1282: val = "0xFAE-1";
1283: assertTrue("isNumber(String) 9 Neg failed", !NumberUtils
1284: .isNumber(val));
1285: assertTrue(
1286: "isNumber(String)/createNumber(String) 9 Neg failed",
1287: !checkCreateNumber(val));
1288: val = ".";
1289: assertTrue("isNumber(String) 10 Neg failed", !NumberUtils
1290: .isNumber(val));
1291: assertTrue(
1292: "isNumber(String)/createNumber(String) 10 Neg failed",
1293: !checkCreateNumber(val));
1294: val = "-0ABC123";
1295: assertTrue("isNumber(String) 11 Neg failed", !NumberUtils
1296: .isNumber(val));
1297: assertTrue(
1298: "isNumber(String)/createNumber(String) 11 Neg failed",
1299: !checkCreateNumber(val));
1300: val = "123.4E-D";
1301: assertTrue("isNumber(String) 12 Neg failed", !NumberUtils
1302: .isNumber(val));
1303: assertTrue(
1304: "isNumber(String)/createNumber(String) 12 Neg failed",
1305: !checkCreateNumber(val));
1306: val = "123.4ED";
1307: assertTrue("isNumber(String) 13 Neg failed", !NumberUtils
1308: .isNumber(val));
1309: assertTrue(
1310: "isNumber(String)/createNumber(String) 13 Neg failed",
1311: !checkCreateNumber(val));
1312: val = "1234E5l";
1313: assertTrue("isNumber(String) 14 Neg failed", !NumberUtils
1314: .isNumber(val));
1315: assertTrue(
1316: "isNumber(String)/createNumber(String) 14 Neg failed",
1317: !checkCreateNumber(val));
1318: val = "11a";
1319: assertTrue("isNumber(String) 15 Neg failed", !NumberUtils
1320: .isNumber(val));
1321: assertTrue(
1322: "isNumber(String)/createNumber(String) 15 Neg failed",
1323: !checkCreateNumber(val));
1324: val = "1a";
1325: assertTrue("isNumber(String) 16 Neg failed", !NumberUtils
1326: .isNumber(val));
1327: assertTrue(
1328: "isNumber(String)/createNumber(String) 16 Neg failed",
1329: !checkCreateNumber(val));
1330: val = "a";
1331: assertTrue("isNumber(String) 17 Neg failed", !NumberUtils
1332: .isNumber(val));
1333: assertTrue(
1334: "isNumber(String)/createNumber(String) 17 Neg failed",
1335: !checkCreateNumber(val));
1336: val = "11g";
1337: assertTrue("isNumber(String) 18 Neg failed", !NumberUtils
1338: .isNumber(val));
1339: assertTrue(
1340: "isNumber(String)/createNumber(String) 18 Neg failed",
1341: !checkCreateNumber(val));
1342: val = "11z";
1343: assertTrue("isNumber(String) 19 Neg failed", !NumberUtils
1344: .isNumber(val));
1345: assertTrue(
1346: "isNumber(String)/createNumber(String) 19 Neg failed",
1347: !checkCreateNumber(val));
1348: val = "11def";
1349: assertTrue("isNumber(String) 20 Neg failed", !NumberUtils
1350: .isNumber(val));
1351: assertTrue(
1352: "isNumber(String)/createNumber(String) 20 Neg failed",
1353: !checkCreateNumber(val));
1354: val = "11d11";
1355: assertTrue("isNumber(String) 21 Neg failed", !NumberUtils
1356: .isNumber(val));
1357: assertTrue(
1358: "isNumber(String)/createNumber(String) 21 Neg failed",
1359: !checkCreateNumber(val));
1360: val = "11 11";
1361: assertTrue("isNumber(String) 22 Neg failed", !NumberUtils
1362: .isNumber(val));
1363: assertTrue(
1364: "isNumber(String)/createNumber(String) 22 Neg failed",
1365: !checkCreateNumber(val));
1366: val = " 1111";
1367: assertTrue("isNumber(String) 23 Neg failed", !NumberUtils
1368: .isNumber(val));
1369: assertTrue(
1370: "isNumber(String)/createNumber(String) 23 Neg failed",
1371: !checkCreateNumber(val));
1372: val = "1111 ";
1373: assertTrue("isNumber(String) 24 Neg failed", !NumberUtils
1374: .isNumber(val));
1375: assertTrue(
1376: "isNumber(String)/createNumber(String) 24 Neg failed",
1377: !checkCreateNumber(val));
1378:
1379: }
1380:
1381: private boolean checkCreateNumber(String val) {
1382: try {
1383: Object obj = NumberUtils.createNumber(val);
1384: if (obj == null) {
1385: return false;
1386: }
1387: return true;
1388: } catch (NumberFormatException e) {
1389: return false;
1390: }
1391: }
1392:
1393: public void testConstants() {
1394: assertTrue(NumberUtils.LONG_ZERO instanceof Long);
1395: assertTrue(NumberUtils.LONG_ONE instanceof Long);
1396: assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long);
1397: assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer);
1398: assertTrue(NumberUtils.INTEGER_ONE instanceof Integer);
1399: assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer);
1400: assertTrue(NumberUtils.SHORT_ZERO instanceof Short);
1401: assertTrue(NumberUtils.SHORT_ONE instanceof Short);
1402: assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short);
1403: assertTrue(NumberUtils.BYTE_ZERO instanceof Byte);
1404: assertTrue(NumberUtils.BYTE_ONE instanceof Byte);
1405: assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte);
1406: assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double);
1407: assertTrue(NumberUtils.DOUBLE_ONE instanceof Double);
1408: assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double);
1409: assertTrue(NumberUtils.FLOAT_ZERO instanceof Float);
1410: assertTrue(NumberUtils.FLOAT_ONE instanceof Float);
1411: assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float);
1412:
1413: assertTrue(NumberUtils.LONG_ZERO.longValue() == 0);
1414: assertTrue(NumberUtils.LONG_ONE.longValue() == 1);
1415: assertTrue(NumberUtils.LONG_MINUS_ONE.longValue() == -1);
1416: assertTrue(NumberUtils.INTEGER_ZERO.intValue() == 0);
1417: assertTrue(NumberUtils.INTEGER_ONE.intValue() == 1);
1418: assertTrue(NumberUtils.INTEGER_MINUS_ONE.intValue() == -1);
1419: assertTrue(NumberUtils.SHORT_ZERO.shortValue() == 0);
1420: assertTrue(NumberUtils.SHORT_ONE.shortValue() == 1);
1421: assertTrue(NumberUtils.SHORT_MINUS_ONE.shortValue() == -1);
1422: assertTrue(NumberUtils.BYTE_ZERO.byteValue() == 0);
1423: assertTrue(NumberUtils.BYTE_ONE.byteValue() == 1);
1424: assertTrue(NumberUtils.BYTE_MINUS_ONE.byteValue() == -1);
1425: assertTrue(NumberUtils.DOUBLE_ZERO.doubleValue() == 0.0d);
1426: assertTrue(NumberUtils.DOUBLE_ONE.doubleValue() == 1.0d);
1427: assertTrue(NumberUtils.DOUBLE_MINUS_ONE.doubleValue() == -1.0d);
1428: assertTrue(NumberUtils.FLOAT_ZERO.floatValue() == 0.0f);
1429: assertTrue(NumberUtils.FLOAT_ONE.floatValue() == 1.0f);
1430: assertTrue(NumberUtils.FLOAT_MINUS_ONE.floatValue() == -1.0f);
1431: }
1432:
1433: public void testLang300() {
1434: NumberUtils.createNumber("-1l");
1435: NumberUtils.createNumber("01l");
1436: NumberUtils.createNumber("1l");
1437: }
1438:
1439: }
|