blob: 96b7a7334dda6b2a068183065dc6584de3d4a88e [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.math;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Test;
/**
* Unit tests {@link org.apache.commons.lang3.math.NumberUtils}.
*
* @version $Id$
*/
public class NumberUtilsTest {
//-----------------------------------------------------------------------
@Test
public void testConstructor() {
assertNotNull(new NumberUtils());
final Constructor<?>[] cons = NumberUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertTrue(Modifier.isPublic(cons[0].getModifiers()));
assertTrue(Modifier.isPublic(NumberUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(NumberUtils.class.getModifiers()));
}
//---------------------------------------------------------------------
/**
* Test for {@link NumberUtils#toInt(String)}.
*/
@Test
public void testToIntString() {
assertTrue("toInt(String) 1 failed", NumberUtils.toInt("12345") == 12345);
assertTrue("toInt(String) 2 failed", NumberUtils.toInt("abc") == 0);
assertTrue("toInt(empty) failed", NumberUtils.toInt("") == 0);
assertTrue("toInt(null) failed", NumberUtils.toInt(null) == 0);
}
/**
* Test for {@link NumberUtils#toInt(String, int)}.
*/
@Test
public void testToIntStringI() {
assertTrue("toInt(String,int) 1 failed", NumberUtils.toInt("12345", 5) == 12345);
assertTrue("toInt(String,int) 2 failed", NumberUtils.toInt("1234.5", 5) == 5);
}
/**
* Test for {@link NumberUtils#toLong(String)}.
*/
@Test
public void testToLongString() {
assertTrue("toLong(String) 1 failed", NumberUtils.toLong("12345") == 12345l);
assertTrue("toLong(String) 2 failed", NumberUtils.toLong("abc") == 0l);
assertTrue("toLong(String) 3 failed", NumberUtils.toLong("1L") == 0l);
assertTrue("toLong(String) 4 failed", NumberUtils.toLong("1l") == 0l);
assertTrue("toLong(Long.MAX_VALUE) failed", NumberUtils.toLong(Long.MAX_VALUE+"") == Long.MAX_VALUE);
assertTrue("toLong(Long.MIN_VALUE) failed", NumberUtils.toLong(Long.MIN_VALUE+"") == Long.MIN_VALUE);
assertTrue("toLong(empty) failed", NumberUtils.toLong("") == 0l);
assertTrue("toLong(null) failed", NumberUtils.toLong(null) == 0l);
}
/**
* Test for {@link NumberUtils#toLong(String, long)}.
*/
@Test
public void testToLongStringL() {
assertTrue("toLong(String,long) 1 failed", NumberUtils.toLong("12345", 5l) == 12345l);
assertTrue("toLong(String,long) 2 failed", NumberUtils.toLong("1234.5", 5l) == 5l);
}
/**
* Test for {@link NumberUtils#toFloat(String)}.
*/
@Test
public void testToFloatString() {
assertTrue("toFloat(String) 1 failed", NumberUtils.toFloat("-1.2345") == -1.2345f);
assertTrue("toFloat(String) 2 failed", NumberUtils.toFloat("1.2345") == 1.2345f);
assertTrue("toFloat(String) 3 failed", NumberUtils.toFloat("abc") == 0.0f);
assertTrue("toFloat(Float.MAX_VALUE) failed", NumberUtils.toFloat(Float.MAX_VALUE+"") == Float.MAX_VALUE);
assertTrue("toFloat(Float.MIN_VALUE) failed", NumberUtils.toFloat(Float.MIN_VALUE+"") == Float.MIN_VALUE);
assertTrue("toFloat(empty) failed", NumberUtils.toFloat("") == 0.0f);
assertTrue("toFloat(null) failed", NumberUtils.toFloat(null) == 0.0f);
}
/**
* Test for {@link NumberUtils#toFloat(String, float)}.
*/
@Test
public void testToFloatStringF() {
assertTrue("toFloat(String,int) 1 failed", NumberUtils.toFloat("1.2345", 5.1f) == 1.2345f);
assertTrue("toFloat(String,int) 2 failed", NumberUtils.toFloat("a", 5.0f) == 5.0f);
}
/**
* Test for {(@link NumberUtils#createNumber(String)}
*/
@Test
public void testStringCreateNumberEnsureNoPrecisionLoss(){
final String shouldBeFloat = "1.23";
final String shouldBeDouble = "3.40282354e+38";
final String shouldBeBigDecimal = "1.797693134862315759e+308";
assertTrue(NumberUtils.createNumber(shouldBeFloat) instanceof Float);
assertTrue(NumberUtils.createNumber(shouldBeDouble) instanceof Double);
assertTrue(NumberUtils.createNumber(shouldBeBigDecimal) instanceof BigDecimal);
}
/**
* Test for {@link NumberUtils#toDouble(String)}.
*/
@Test
public void testStringToDoubleString() {
assertTrue("toDouble(String) 1 failed", NumberUtils.toDouble("-1.2345") == -1.2345d);
assertTrue("toDouble(String) 2 failed", NumberUtils.toDouble("1.2345") == 1.2345d);
assertTrue("toDouble(String) 3 failed", NumberUtils.toDouble("abc") == 0.0d);
assertTrue("toDouble(Double.MAX_VALUE) failed", NumberUtils.toDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE);
assertTrue("toDouble(Double.MIN_VALUE) failed", NumberUtils.toDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE);
assertTrue("toDouble(empty) failed", NumberUtils.toDouble("") == 0.0d);
assertTrue("toDouble(null) failed", NumberUtils.toDouble(null) == 0.0d);
}
/**
* Test for {@link NumberUtils#toDouble(String, double)}.
*/
@Test
public void testStringToDoubleStringD() {
assertTrue("toDouble(String,int) 1 failed", NumberUtils.toDouble("1.2345", 5.1d) == 1.2345d);
assertTrue("toDouble(String,int) 2 failed", NumberUtils.toDouble("a", 5.0d) == 5.0d);
}
/**
* Test for {@link NumberUtils#toByte(String)}.
*/
@Test
public void testToByteString() {
assertTrue("toByte(String) 1 failed", NumberUtils.toByte("123") == 123);
assertTrue("toByte(String) 2 failed", NumberUtils.toByte("abc") == 0);
assertTrue("toByte(empty) failed", NumberUtils.toByte("") == 0);
assertTrue("toByte(null) failed", NumberUtils.toByte(null) == 0);
}
/**
* Test for {@link NumberUtils#toByte(String, byte)}.
*/
@Test
public void testToByteStringI() {
assertTrue("toByte(String,byte) 1 failed", NumberUtils.toByte("123", (byte) 5) == 123);
assertTrue("toByte(String,byte) 2 failed", NumberUtils.toByte("12.3", (byte) 5) == 5);
}
/**
* Test for {@link NumberUtils#toShort(String)}.
*/
@Test
public void testToShortString() {
assertTrue("toShort(String) 1 failed", NumberUtils.toShort("12345") == 12345);
assertTrue("toShort(String) 2 failed", NumberUtils.toShort("abc") == 0);
assertTrue("toShort(empty) failed", NumberUtils.toShort("") == 0);
assertTrue("toShort(null) failed", NumberUtils.toShort(null) == 0);
}
/**
* Test for {@link NumberUtils#toShort(String, short)}.
*/
@Test
public void testToShortStringI() {
assertTrue("toShort(String,short) 1 failed", NumberUtils.toShort("12345", (short) 5) == 12345);
assertTrue("toShort(String,short) 2 failed", NumberUtils.toShort("1234.5", (short) 5) == 5);
}
@Test
public void testCreateNumber() {
// a lot of things can go wrong
assertEquals("createNumber(String) 1 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5"));
assertEquals("createNumber(String) 2 failed", Integer.valueOf("12345"), NumberUtils.createNumber("12345"));
assertEquals("createNumber(String) 3 failed", Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5D"));
assertEquals("createNumber(String) 3 failed", Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5d"));
assertEquals("createNumber(String) 4 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5F"));
assertEquals("createNumber(String) 4 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5f"));
assertEquals("createNumber(String) 5 failed", Long.valueOf(Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
+ (Integer.MAX_VALUE + 1L)));
assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345L"));
assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345l"));
assertEquals("createNumber(String) 7 failed", Float.valueOf("-1234.5"), NumberUtils.createNumber("-1234.5"));
assertEquals("createNumber(String) 8 failed", Integer.valueOf("-12345"), NumberUtils.createNumber("-12345"));
assertTrue("createNumber(String) 9a failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
assertTrue("createNumber(String) 9b failed", 0xFADE == NumberUtils.createNumber("0Xfade").intValue());
assertTrue("createNumber(String) 10a failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue());
assertTrue("createNumber(String) 10b failed", -0xFADE == NumberUtils.createNumber("-0Xfade").intValue());
assertEquals("createNumber(String) 11 failed", Double.valueOf("1.1E200"), NumberUtils.createNumber("1.1E200"));
assertEquals("createNumber(String) 12 failed", Float.valueOf("1.1E20"), NumberUtils.createNumber("1.1E20"));
assertEquals("createNumber(String) 13 failed", Double.valueOf("-1.1E200"), NumberUtils.createNumber("-1.1E200"));
assertEquals("createNumber(String) 14 failed", Double.valueOf("1.1E-200"), NumberUtils.createNumber("1.1E-200"));
assertEquals("createNumber(null) failed", null, NumberUtils.createNumber(null));
assertEquals("createNumber(String) failed", new BigInteger("12345678901234567890"), NumberUtils
.createNumber("12345678901234567890L"));
assertEquals("createNumber(String) 15 failed", new BigDecimal("1.1E-700"), NumberUtils
.createNumber("1.1E-700F"));
assertEquals("createNumber(String) 16 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
.createNumber("10" + Integer.MAX_VALUE + "L"));
assertEquals("createNumber(String) 17 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
.createNumber("10" + Integer.MAX_VALUE));
assertEquals("createNumber(String) 18 failed", new BigInteger("10" + Long.MAX_VALUE), NumberUtils
.createNumber("10" + Long.MAX_VALUE));
// LANG-521
assertEquals("createNumber(String) LANG-521 failed", Float.valueOf("2."), NumberUtils.createNumber("2."));
// LANG-638
assertFalse("createNumber(String) succeeded", checkCreateNumber("1eE"));
// LANG-693
assertEquals("createNumber(String) LANG-693 failed", Double.valueOf(Double.MAX_VALUE), NumberUtils
.createNumber("" + Double.MAX_VALUE));
// LANG-822
// ensure that the underlying negative number would create a BigDecimal
final Number bigNum = NumberUtils.createNumber("-1.1E-700F");
assertNotNull(bigNum);
assertEquals(BigDecimal.class, bigNum.getClass());
}
@Test
public void TestLang747() {
assertEquals(Integer.valueOf(0x8000), NumberUtils.createNumber("0x8000"));
assertEquals(Integer.valueOf(0x80000), NumberUtils.createNumber("0x80000"));
assertEquals(Integer.valueOf(0x800000), NumberUtils.createNumber("0x800000"));
assertEquals(Integer.valueOf(0x8000000), NumberUtils.createNumber("0x8000000"));
assertEquals(Integer.valueOf(0x7FFFFFFF), NumberUtils.createNumber("0x7FFFFFFF"));
assertEquals(Long.valueOf(0x80000000L), NumberUtils.createNumber("0x80000000"));
assertEquals(Long.valueOf(0xFFFFFFFFL), NumberUtils.createNumber("0xFFFFFFFF"));
// Leading zero tests
assertEquals(Integer.valueOf(0x8000000), NumberUtils.createNumber("0x08000000"));
assertEquals(Integer.valueOf(0x7FFFFFFF), NumberUtils.createNumber("0x007FFFFFFF"));
assertEquals(Long.valueOf(0x80000000L), NumberUtils.createNumber("0x080000000"));
assertEquals(Long.valueOf(0xFFFFFFFFL), NumberUtils.createNumber("0x00FFFFFFFF"));
assertEquals(Long.valueOf(0x800000000L), NumberUtils.createNumber("0x800000000"));
assertEquals(Long.valueOf(0x8000000000L), NumberUtils.createNumber("0x8000000000"));
assertEquals(Long.valueOf(0x80000000000L), NumberUtils.createNumber("0x80000000000"));
assertEquals(Long.valueOf(0x800000000000L), NumberUtils.createNumber("0x800000000000"));
assertEquals(Long.valueOf(0x8000000000000L), NumberUtils.createNumber("0x8000000000000"));
assertEquals(Long.valueOf(0x80000000000000L), NumberUtils.createNumber("0x80000000000000"));
assertEquals(Long.valueOf(0x800000000000000L), NumberUtils.createNumber("0x800000000000000"));
assertEquals(Long.valueOf(0x7FFFFFFFFFFFFFFFL), NumberUtils.createNumber("0x7FFFFFFFFFFFFFFF"));
// N.B. Cannot use a hex constant such as 0x8000000000000000L here as that is interpreted as a negative long
assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x8000000000000000"));
assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFF"));
// Leading zero tests
assertEquals(Long.valueOf(0x80000000000000L), NumberUtils.createNumber("0x00080000000000000"));
assertEquals(Long.valueOf(0x800000000000000L), NumberUtils.createNumber("0x0800000000000000"));
assertEquals(Long.valueOf(0x7FFFFFFFFFFFFFFFL), NumberUtils.createNumber("0x07FFFFFFFFFFFFFFF"));
// N.B. Cannot use a hex constant such as 0x8000000000000000L here as that is interpreted as a negative long
assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x00008000000000000000"));
assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0x0FFFFFFFFFFFFFFFF"));
}
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when preceded by -- rather than -
public void testCreateNumberFailure_1() {
NumberUtils.createNumber("--1.1E-700F");
}
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when both e and E are present (with decimal)
public void testCreateNumberFailure_2() {
NumberUtils.createNumber("-1.1E+0-7e00");
}
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when both e and E are present (no decimal)
public void testCreateNumberFailure_3() {
NumberUtils.createNumber("-11E+0-7e00");
}
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when both e and E are present (no decimal)
public void testCreateNumberFailure_4() {
NumberUtils.createNumber("1eE+00001");
}
// Tests to show when magnitude causes switch to next Number type
// Will probably need to be adjusted if code is changed to check precision (LANG-693)
@Test
public void testCreateNumberMagnitude() {
// Test Float.MAX_VALUE, and same with +1 in final digit to check conversion changes to next Number type
assertEquals(Float.valueOf(Float.MAX_VALUE), NumberUtils.createNumber("3.4028235e+38"));
assertEquals(Double.valueOf(3.4028236e+38), NumberUtils.createNumber("3.4028236e+38"));
// Test Double.MAX_VALUE
assertEquals(Double.valueOf(Double.MAX_VALUE), NumberUtils.createNumber("1.7976931348623157e+308"));
// Test with +2 in final digit (+1 does not cause roll-over to BigDecimal)
assertEquals(new BigDecimal("1.7976931348623159e+308"), NumberUtils.createNumber("1.7976931348623159e+308"));
assertEquals(Integer.valueOf(0x12345678), NumberUtils.createNumber("0x12345678"));
assertEquals(Long.valueOf(0x123456789L), NumberUtils.createNumber("0x123456789"));
assertEquals(Long.valueOf(0x7fffffffffffffffL), NumberUtils.createNumber("0x7fffffffffffffff"));
// Does not appear to be a way to create a literal BigInteger of this magnitude
assertEquals(new BigInteger("7fffffffffffffff0",16), NumberUtils.createNumber("0x7fffffffffffffff0"));
assertEquals(Long.valueOf(0x7fffffffffffffffL), NumberUtils.createNumber("#7fffffffffffffff"));
assertEquals(new BigInteger("7fffffffffffffff0",16), NumberUtils.createNumber("#7fffffffffffffff0"));
assertEquals(Integer.valueOf(017777777777), NumberUtils.createNumber("017777777777")); // 31 bits
assertEquals(Long.valueOf(037777777777L), NumberUtils.createNumber("037777777777")); // 32 bits
assertEquals(Long.valueOf(0777777777777777777777L), NumberUtils.createNumber("0777777777777777777777")); // 63 bits
assertEquals(new BigInteger("1777777777777777777777",8), NumberUtils.createNumber("01777777777777777777777"));// 64 bits
}
@Test
public void testCreateFloat() {
assertEquals("createFloat(String) failed", Float.valueOf("1234.5"), NumberUtils.createFloat("1234.5"));
assertEquals("createFloat(null) failed", null, NumberUtils.createFloat(null));
this.testCreateFloatFailure("");
this.testCreateFloatFailure(" ");
this.testCreateFloatFailure("\b\t\n\f\r");
// Funky whitespaces
this.testCreateFloatFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
}
protected void testCreateFloatFailure(final String str) {
try {
final Float value = NumberUtils.createFloat(str);
fail("createFloat(\"" + str + "\") should have failed: " + value);
} catch (final NumberFormatException ex) {
// empty
}
}
@Test
public void testCreateDouble() {
assertEquals("createDouble(String) failed", Double.valueOf("1234.5"), NumberUtils.createDouble("1234.5"));
assertEquals("createDouble(null) failed", null, NumberUtils.createDouble(null));
this.testCreateDoubleFailure("");
this.testCreateDoubleFailure(" ");
this.testCreateDoubleFailure("\b\t\n\f\r");
// Funky whitespaces
this.testCreateDoubleFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
}
protected void testCreateDoubleFailure(final String str) {
try {
final Double value = NumberUtils.createDouble(str);
fail("createDouble(\"" + str + "\") should have failed: " + value);
} catch (final NumberFormatException ex) {
// empty
}
}
@Test
public void testCreateInteger() {
assertEquals("createInteger(String) failed", Integer.valueOf("12345"), NumberUtils.createInteger("12345"));
assertEquals("createInteger(null) failed", null, NumberUtils.createInteger(null));
this.testCreateIntegerFailure("");
this.testCreateIntegerFailure(" ");
this.testCreateIntegerFailure("\b\t\n\f\r");
// Funky whitespaces
this.testCreateIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
}
protected void testCreateIntegerFailure(final String str) {
try {
final Integer value = NumberUtils.createInteger(str);
fail("createInteger(\"" + str + "\") should have failed: " + value);
} catch (final NumberFormatException ex) {
// empty
}
}
@Test
public void testCreateLong() {
assertEquals("createLong(String) failed", Long.valueOf("12345"), NumberUtils.createLong("12345"));
assertEquals("createLong(null) failed", null, NumberUtils.createLong(null));
this.testCreateLongFailure("");
this.testCreateLongFailure(" ");
this.testCreateLongFailure("\b\t\n\f\r");
// Funky whitespaces
this.testCreateLongFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
}
protected void testCreateLongFailure(final String str) {
try {
final Long value = NumberUtils.createLong(str);
fail("createLong(\"" + str + "\") should have failed: " + value);
} catch (final NumberFormatException ex) {
// empty
}
}
@Test
public void testCreateBigInteger() {
assertEquals("createBigInteger(String) failed", new BigInteger("12345"), NumberUtils.createBigInteger("12345"));
assertEquals("createBigInteger(null) failed", null, NumberUtils.createBigInteger(null));
this.testCreateBigIntegerFailure("");
this.testCreateBigIntegerFailure(" ");
this.testCreateBigIntegerFailure("\b\t\n\f\r");
// Funky whitespaces
this.testCreateBigIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
assertEquals("createBigInteger(String) failed", new BigInteger("255"), NumberUtils.createBigInteger("0xff"));
assertEquals("createBigInteger(String) failed", new BigInteger("255"), NumberUtils.createBigInteger("#ff"));
assertEquals("createBigInteger(String) failed", new BigInteger("-255"), NumberUtils.createBigInteger("-0xff"));
assertEquals("createBigInteger(String) failed", new BigInteger("255"), NumberUtils.createBigInteger("0377"));
assertEquals("createBigInteger(String) failed", new BigInteger("-255"), NumberUtils.createBigInteger("-0377"));
assertEquals("createBigInteger(String) failed", new BigInteger("-255"), NumberUtils.createBigInteger("-0377"));
assertEquals("createBigInteger(String) failed", new BigInteger("-0"), NumberUtils.createBigInteger("-0"));
assertEquals("createBigInteger(String) failed", new BigInteger("0"), NumberUtils.createBigInteger("0"));
testCreateBigIntegerFailure("#");
testCreateBigIntegerFailure("-#");
testCreateBigIntegerFailure("0x");
testCreateBigIntegerFailure("-0x");
}
protected void testCreateBigIntegerFailure(final String str) {
try {
final BigInteger value = NumberUtils.createBigInteger(str);
fail("createBigInteger(\"" + str + "\") should have failed: " + value);
} catch (final NumberFormatException ex) {
// empty
}
}
@Test
public void testCreateBigDecimal() {
assertEquals("createBigDecimal(String) failed", new BigDecimal("1234.5"), NumberUtils.createBigDecimal("1234.5"));
assertEquals("createBigDecimal(null) failed", null, NumberUtils.createBigDecimal(null));
this.testCreateBigDecimalFailure("");
this.testCreateBigDecimalFailure(" ");
this.testCreateBigDecimalFailure("\b\t\n\f\r");
// Funky whitespaces
this.testCreateBigDecimalFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
this.testCreateBigDecimalFailure("-"); // sign alone not valid
this.testCreateBigDecimalFailure("--"); // comment in NumberUtils suggests some implementations may incorrectly allow this
this.testCreateBigDecimalFailure("--0");
this.testCreateBigDecimalFailure("+"); // sign alone not valid
this.testCreateBigDecimalFailure("++"); // in case this was also allowed by some JVMs
this.testCreateBigDecimalFailure("++0");
}
protected void testCreateBigDecimalFailure(final String str) {
try {
final BigDecimal value = NumberUtils.createBigDecimal(str);
fail("createBigDecimal(\"" + str + "\") should have failed: " + value);
} catch (final NumberFormatException ex) {
// empty
}
}
// min/max tests
// ----------------------------------------------------------------------
@Test(expected = IllegalArgumentException.class)
public void testMinLong_nullArray() {
NumberUtils.min((long[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMinLong_emptyArray() {
NumberUtils.min(new long[0]);
}
@Test
public void testMinLong() {
assertEquals(
"min(long[]) failed for array length 1",
5,
NumberUtils.min(new long[] { 5 }));
assertEquals(
"min(long[]) failed for array length 2",
6,
NumberUtils.min(new long[] { 6, 9 }));
assertEquals(-10, NumberUtils.min(new long[] { -10, -5, 0, 5, 10 }));
assertEquals(-10, NumberUtils.min(new long[] { -5, 0, -10, 5, 10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMinInt_nullArray() {
NumberUtils.min((int[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMinInt_emptyArray() {
NumberUtils.min(new int[0]);
}
@Test
public void testMinInt() {
assertEquals(
"min(int[]) failed for array length 1",
5,
NumberUtils.min(new int[] { 5 }));
assertEquals(
"min(int[]) failed for array length 2",
6,
NumberUtils.min(new int[] { 6, 9 }));
assertEquals(-10, NumberUtils.min(new int[] { -10, -5, 0, 5, 10 }));
assertEquals(-10, NumberUtils.min(new int[] { -5, 0, -10, 5, 10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMinShort_nullArray() {
NumberUtils.min((short[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMinShort_emptyArray() {
NumberUtils.min(new short[0]);
}
@Test
public void testMinShort() {
assertEquals(
"min(short[]) failed for array length 1",
5,
NumberUtils.min(new short[] { 5 }));
assertEquals(
"min(short[]) failed for array length 2",
6,
NumberUtils.min(new short[] { 6, 9 }));
assertEquals(-10, NumberUtils.min(new short[] { -10, -5, 0, 5, 10 }));
assertEquals(-10, NumberUtils.min(new short[] { -5, 0, -10, 5, 10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMinByte_nullArray() {
NumberUtils.min((byte[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMinByte_emptyArray() {
NumberUtils.min(new byte[0]);
}
@Test
public void testMinByte() {
assertEquals(
"min(byte[]) failed for array length 1",
5,
NumberUtils.min(new byte[] { 5 }));
assertEquals(
"min(byte[]) failed for array length 2",
6,
NumberUtils.min(new byte[] { 6, 9 }));
assertEquals(-10, NumberUtils.min(new byte[] { -10, -5, 0, 5, 10 }));
assertEquals(-10, NumberUtils.min(new byte[] { -5, 0, -10, 5, 10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMinDouble_nullArray() {
NumberUtils.min((double[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMinDouble_emptyArray() {
NumberUtils.min(new double[0]);
}
@Test
public void testMinDouble() {
assertEquals(
"min(double[]) failed for array length 1",
5.12,
NumberUtils.min(new double[] { 5.12 }),
0);
assertEquals(
"min(double[]) failed for array length 2",
6.23,
NumberUtils.min(new double[] { 6.23, 9.34 }),
0);
assertEquals(
"min(double[]) failed for array length 5",
-10.45,
NumberUtils.min(new double[] { -10.45, -5.56, 0, 5.67, 10.78 }),
0);
assertEquals(-10, NumberUtils.min(new double[] { -10, -5, 0, 5, 10 }), 0.0001);
assertEquals(-10, NumberUtils.min(new double[] { -5, 0, -10, 5, 10 }), 0.0001);
}
@Test(expected = IllegalArgumentException.class)
public void testMinFloat_nullArray() {
NumberUtils.min((float[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMinFloat_emptyArray() {
NumberUtils.min(new float[0]);
}
@Test
public void testMinFloat() {
assertEquals(
"min(float[]) failed for array length 1",
5.9f,
NumberUtils.min(new float[] { 5.9f }),
0);
assertEquals(
"min(float[]) failed for array length 2",
6.8f,
NumberUtils.min(new float[] { 6.8f, 9.7f }),
0);
assertEquals(
"min(float[]) failed for array length 5",
-10.6f,
NumberUtils.min(new float[] { -10.6f, -5.5f, 0, 5.4f, 10.3f }),
0);
assertEquals(-10, NumberUtils.min(new float[] { -10, -5, 0, 5, 10 }), 0.0001f);
assertEquals(-10, NumberUtils.min(new float[] { -5, 0, -10, 5, 10 }), 0.0001f);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxLong_nullArray() {
NumberUtils.max((long[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxLong_emptyArray() {
NumberUtils.max(new long[0]);
}
@Test
public void testMaxLong() {
assertEquals(
"max(long[]) failed for array length 1",
5,
NumberUtils.max(new long[] { 5 }));
assertEquals(
"max(long[]) failed for array length 2",
9,
NumberUtils.max(new long[] { 6, 9 }));
assertEquals(
"max(long[]) failed for array length 5",
10,
NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new long[] { -5, 0, 10, 5, -10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMaxInt_nullArray() {
NumberUtils.max((int[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxInt_emptyArray() {
NumberUtils.max(new int[0]);
}
@Test
public void testMaxInt() {
assertEquals(
"max(int[]) failed for array length 1",
5,
NumberUtils.max(new int[] { 5 }));
assertEquals(
"max(int[]) failed for array length 2",
9,
NumberUtils.max(new int[] { 6, 9 }));
assertEquals(
"max(int[]) failed for array length 5",
10,
NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new int[] { -5, 0, 10, 5, -10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMaxShort_nullArray() {
NumberUtils.max((short[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxShort_emptyArray() {
NumberUtils.max(new short[0]);
}
@Test
public void testMaxShort() {
assertEquals(
"max(short[]) failed for array length 1",
5,
NumberUtils.max(new short[] { 5 }));
assertEquals(
"max(short[]) failed for array length 2",
9,
NumberUtils.max(new short[] { 6, 9 }));
assertEquals(
"max(short[]) failed for array length 5",
10,
NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new short[] { -5, 0, 10, 5, -10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMaxByte_nullArray() {
NumberUtils.max((byte[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxByte_emptyArray() {
NumberUtils.max(new byte[0]);
}
@Test
public void testMaxByte() {
assertEquals(
"max(byte[]) failed for array length 1",
5,
NumberUtils.max(new byte[] { 5 }));
assertEquals(
"max(byte[]) failed for array length 2",
9,
NumberUtils.max(new byte[] { 6, 9 }));
assertEquals(
"max(byte[]) failed for array length 5",
10,
NumberUtils.max(new byte[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new byte[] { -10, -5, 0, 5, 10 }));
assertEquals(10, NumberUtils.max(new byte[] { -5, 0, 10, 5, -10 }));
}
@Test(expected = IllegalArgumentException.class)
public void testMaxDouble_nullArray() {
NumberUtils.max((double[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxDouble_emptyArray() {
NumberUtils.max(new double[0]);
}
@Test
public void testMaxDouble() {
final double[] d = null;
try {
NumberUtils.max(d);
fail("No exception was thrown for null input.");
} catch (final IllegalArgumentException ex) {}
try {
NumberUtils.max(new double[0]);
fail("No exception was thrown for empty input.");
} catch (final IllegalArgumentException ex) {}
assertEquals(
"max(double[]) failed for array length 1",
5.1f,
NumberUtils.max(new double[] { 5.1f }),
0);
assertEquals(
"max(double[]) failed for array length 2",
9.2f,
NumberUtils.max(new double[] { 6.3f, 9.2f }),
0);
assertEquals(
"max(double[]) failed for float length 5",
10.4f,
NumberUtils.max(new double[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
0);
assertEquals(10, NumberUtils.max(new double[] { -10, -5, 0, 5, 10 }), 0.0001);
assertEquals(10, NumberUtils.max(new double[] { -5, 0, 10, 5, -10 }), 0.0001);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxFloat_nullArray() {
NumberUtils.max((float[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void testMaxFloat_emptyArray() {
NumberUtils.max(new float[0]);
}
@Test
public void testMaxFloat() {
assertEquals(
"max(float[]) failed for array length 1",
5.1f,
NumberUtils.max(new float[] { 5.1f }),
0);
assertEquals(
"max(float[]) failed for array length 2",
9.2f,
NumberUtils.max(new float[] { 6.3f, 9.2f }),
0);
assertEquals(
"max(float[]) failed for float length 5",
10.4f,
NumberUtils.max(new float[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
0);
assertEquals(10, NumberUtils.max(new float[] { -10, -5, 0, 5, 10 }), 0.0001f);
assertEquals(10, NumberUtils.max(new float[] { -5, 0, 10, 5, -10 }), 0.0001f);
}
@Test
public void testMinimumLong() {
assertEquals("minimum(long,long,long) 1 failed", 12345L, NumberUtils.min(12345L, 12345L + 1L, 12345L + 2L));
assertEquals("minimum(long,long,long) 2 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L, 12345 + 2L));
assertEquals("minimum(long,long,long) 3 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L + 2L, 12345L));
assertEquals("minimum(long,long,long) 4 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L, 12345L));
assertEquals("minimum(long,long,long) 5 failed", 12345L, NumberUtils.min(12345L, 12345L, 12345L));
}
@Test
public void testMinimumInt() {
assertEquals("minimum(int,int,int) 1 failed", 12345, NumberUtils.min(12345, 12345 + 1, 12345 + 2));
assertEquals("minimum(int,int,int) 2 failed", 12345, NumberUtils.min(12345 + 1, 12345, 12345 + 2));
assertEquals("minimum(int,int,int) 3 failed", 12345, NumberUtils.min(12345 + 1, 12345 + 2, 12345));
assertEquals("minimum(int,int,int) 4 failed", 12345, NumberUtils.min(12345 + 1, 12345, 12345));
assertEquals("minimum(int,int,int) 5 failed", 12345, NumberUtils.min(12345, 12345, 12345));
}
@Test
public void testMinimumShort() {
final short low = 1234;
final short mid = 1234 + 1;
final short high = 1234 + 2;
assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(low, mid, high));
assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(mid, low, high));
assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(mid, high, low));
assertEquals("minimum(short,short,short) 1 failed", low, NumberUtils.min(low, mid, low));
}
@Test
public void testMinimumByte() {
final byte low = 123;
final byte mid = 123 + 1;
final byte high = 123 + 2;
assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(low, mid, high));
assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(mid, low, high));
assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(mid, high, low));
assertEquals("minimum(byte,byte,byte) 1 failed", low, NumberUtils.min(low, mid, low));
}
@Test
public void testMinimumDouble() {
final double low = 12.3;
final double mid = 12.3 + 1;
final double high = 12.3 + 2;
assertEquals(low, NumberUtils.min(low, mid, high), 0.0001);
assertEquals(low, NumberUtils.min(mid, low, high), 0.0001);
assertEquals(low, NumberUtils.min(mid, high, low), 0.0001);
assertEquals(low, NumberUtils.min(low, mid, low), 0.0001);
assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001);
}
@Test
public void testMinimumFloat() {
final float low = 12.3f;
final float mid = 12.3f + 1;
final float high = 12.3f + 2;
assertEquals(low, NumberUtils.min(low, mid, high), 0.0001f);
assertEquals(low, NumberUtils.min(mid, low, high), 0.0001f);
assertEquals(low, NumberUtils.min(mid, high, low), 0.0001f);
assertEquals(low, NumberUtils.min(low, mid, low), 0.0001f);
assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001f);
}
@Test
public void testMaximumLong() {
assertEquals("maximum(long,long,long) 1 failed", 12345L, NumberUtils.max(12345L, 12345L - 1L, 12345L - 2L));
assertEquals("maximum(long,long,long) 2 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L - 2L));
assertEquals("maximum(long,long,long) 3 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L - 2L, 12345L));
assertEquals("maximum(long,long,long) 4 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L));
assertEquals("maximum(long,long,long) 5 failed", 12345L, NumberUtils.max(12345L, 12345L, 12345L));
}
@Test
public void testMaximumInt() {
assertEquals("maximum(int,int,int) 1 failed", 12345, NumberUtils.max(12345, 12345 - 1, 12345 - 2));
assertEquals("maximum(int,int,int) 2 failed", 12345, NumberUtils.max(12345 - 1, 12345, 12345 - 2));
assertEquals("maximum(int,int,int) 3 failed", 12345, NumberUtils.max(12345 - 1, 12345 - 2, 12345));
assertEquals("maximum(int,int,int) 4 failed", 12345, NumberUtils.max(12345 - 1, 12345, 12345));
assertEquals("maximum(int,int,int) 5 failed", 12345, NumberUtils.max(12345, 12345, 12345));
}
@Test
public void testMaximumShort() {
final short low = 1234;
final short mid = 1234 + 1;
final short high = 1234 + 2;
assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(low, mid, high));
assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(mid, low, high));
assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(mid, high, low));
assertEquals("maximum(short,short,short) 1 failed", high, NumberUtils.max(high, mid, high));
}
@Test
public void testMaximumByte() {
final byte low = 123;
final byte mid = 123 + 1;
final byte high = 123 + 2;
assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(low, mid, high));
assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(mid, low, high));
assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(mid, high, low));
assertEquals("maximum(byte,byte,byte) 1 failed", high, NumberUtils.max(high, mid, high));
}
@Test
public void testMaximumDouble() {
final double low = 12.3;
final double mid = 12.3 + 1;
final double high = 12.3 + 2;
assertEquals(high, NumberUtils.max(low, mid, high), 0.0001);
assertEquals(high, NumberUtils.max(mid, low, high), 0.0001);
assertEquals(high, NumberUtils.max(mid, high, low), 0.0001);
assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001);
assertEquals(high, NumberUtils.max(high, mid, high), 0.0001);
}
@Test
public void testMaximumFloat() {
final float low = 12.3f;
final float mid = 12.3f + 1;
final float high = 12.3f + 2;
assertEquals(high, NumberUtils.max(low, mid, high), 0.0001f);
assertEquals(high, NumberUtils.max(mid, low, high), 0.0001f);
assertEquals(high, NumberUtils.max(mid, high, low), 0.0001f);
assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001f);
assertEquals(high, NumberUtils.max(high, mid, high), 0.0001f);
}
// Testing JDK against old Lang functionality
@Test
public void testCompareDouble() {
assertTrue(Double.compare(Double.NaN, Double.NaN) == 0);
assertTrue(Double.compare(Double.NaN, Double.POSITIVE_INFINITY) == +1);
assertTrue(Double.compare(Double.NaN, Double.MAX_VALUE) == +1);
assertTrue(Double.compare(Double.NaN, 1.2d) == +1);
assertTrue(Double.compare(Double.NaN, 0.0d) == +1);
assertTrue(Double.compare(Double.NaN, -0.0d) == +1);
assertTrue(Double.compare(Double.NaN, -1.2d) == +1);
assertTrue(Double.compare(Double.NaN, -Double.MAX_VALUE) == +1);
assertTrue(Double.compare(Double.NaN, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, Double.NaN) == -1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) == 0);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, Double.MAX_VALUE) == +1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, 1.2d) == +1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, 0.0d) == +1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, -0.0d) == +1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, -1.2d) == +1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, -Double.MAX_VALUE) == +1);
assertTrue(Double.compare(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(Double.MAX_VALUE, Double.NaN) == -1);
assertTrue(Double.compare(Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1);
assertTrue(Double.compare(Double.MAX_VALUE, Double.MAX_VALUE) == 0);
assertTrue(Double.compare(Double.MAX_VALUE, 1.2d) == +1);
assertTrue(Double.compare(Double.MAX_VALUE, 0.0d) == +1);
assertTrue(Double.compare(Double.MAX_VALUE, -0.0d) == +1);
assertTrue(Double.compare(Double.MAX_VALUE, -1.2d) == +1);
assertTrue(Double.compare(Double.MAX_VALUE, -Double.MAX_VALUE) == +1);
assertTrue(Double.compare(Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(1.2d, Double.NaN) == -1);
assertTrue(Double.compare(1.2d, Double.POSITIVE_INFINITY) == -1);
assertTrue(Double.compare(1.2d, Double.MAX_VALUE) == -1);
assertTrue(Double.compare(1.2d, 1.2d) == 0);
assertTrue(Double.compare(1.2d, 0.0d) == +1);
assertTrue(Double.compare(1.2d, -0.0d) == +1);
assertTrue(Double.compare(1.2d, -1.2d) == +1);
assertTrue(Double.compare(1.2d, -Double.MAX_VALUE) == +1);
assertTrue(Double.compare(1.2d, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(0.0d, Double.NaN) == -1);
assertTrue(Double.compare(0.0d, Double.POSITIVE_INFINITY) == -1);
assertTrue(Double.compare(0.0d, Double.MAX_VALUE) == -1);
assertTrue(Double.compare(0.0d, 1.2d) == -1);
assertTrue(Double.compare(0.0d, 0.0d) == 0);
assertTrue(Double.compare(0.0d, -0.0d) == +1);
assertTrue(Double.compare(0.0d, -1.2d) == +1);
assertTrue(Double.compare(0.0d, -Double.MAX_VALUE) == +1);
assertTrue(Double.compare(0.0d, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(-0.0d, Double.NaN) == -1);
assertTrue(Double.compare(-0.0d, Double.POSITIVE_INFINITY) == -1);
assertTrue(Double.compare(-0.0d, Double.MAX_VALUE) == -1);
assertTrue(Double.compare(-0.0d, 1.2d) == -1);
assertTrue(Double.compare(-0.0d, 0.0d) == -1);
assertTrue(Double.compare(-0.0d, -0.0d) == 0);
assertTrue(Double.compare(-0.0d, -1.2d) == +1);
assertTrue(Double.compare(-0.0d, -Double.MAX_VALUE) == +1);
assertTrue(Double.compare(-0.0d, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(-1.2d, Double.NaN) == -1);
assertTrue(Double.compare(-1.2d, Double.POSITIVE_INFINITY) == -1);
assertTrue(Double.compare(-1.2d, Double.MAX_VALUE) == -1);
assertTrue(Double.compare(-1.2d, 1.2d) == -1);
assertTrue(Double.compare(-1.2d, 0.0d) == -1);
assertTrue(Double.compare(-1.2d, -0.0d) == -1);
assertTrue(Double.compare(-1.2d, -1.2d) == 0);
assertTrue(Double.compare(-1.2d, -Double.MAX_VALUE) == +1);
assertTrue(Double.compare(-1.2d, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(-Double.MAX_VALUE, Double.NaN) == -1);
assertTrue(Double.compare(-Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1);
assertTrue(Double.compare(-Double.MAX_VALUE, Double.MAX_VALUE) == -1);
assertTrue(Double.compare(-Double.MAX_VALUE, 1.2d) == -1);
assertTrue(Double.compare(-Double.MAX_VALUE, 0.0d) == -1);
assertTrue(Double.compare(-Double.MAX_VALUE, -0.0d) == -1);
assertTrue(Double.compare(-Double.MAX_VALUE, -1.2d) == -1);
assertTrue(Double.compare(-Double.MAX_VALUE, -Double.MAX_VALUE) == 0);
assertTrue(Double.compare(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, Double.NaN) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, Double.MAX_VALUE) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, 1.2d) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, 0.0d) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, -0.0d) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, -1.2d) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE) == -1);
assertTrue(Double.compare(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) == 0);
}
@Test
public void testCompareFloat() {
assertTrue(Float.compare(Float.NaN, Float.NaN) == 0);
assertTrue(Float.compare(Float.NaN, Float.POSITIVE_INFINITY) == +1);
assertTrue(Float.compare(Float.NaN, Float.MAX_VALUE) == +1);
assertTrue(Float.compare(Float.NaN, 1.2f) == +1);
assertTrue(Float.compare(Float.NaN, 0.0f) == +1);
assertTrue(Float.compare(Float.NaN, -0.0f) == +1);
assertTrue(Float.compare(Float.NaN, -1.2f) == +1);
assertTrue(Float.compare(Float.NaN, -Float.MAX_VALUE) == +1);
assertTrue(Float.compare(Float.NaN, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, Float.NaN) == -1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) == 0);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, Float.MAX_VALUE) == +1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, 1.2f) == +1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, 0.0f) == +1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, -0.0f) == +1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, -1.2f) == +1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, -Float.MAX_VALUE) == +1);
assertTrue(Float.compare(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(Float.MAX_VALUE, Float.NaN) == -1);
assertTrue(Float.compare(Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1);
assertTrue(Float.compare(Float.MAX_VALUE, Float.MAX_VALUE) == 0);
assertTrue(Float.compare(Float.MAX_VALUE, 1.2f) == +1);
assertTrue(Float.compare(Float.MAX_VALUE, 0.0f) == +1);
assertTrue(Float.compare(Float.MAX_VALUE, -0.0f) == +1);
assertTrue(Float.compare(Float.MAX_VALUE, -1.2f) == +1);
assertTrue(Float.compare(Float.MAX_VALUE, -Float.MAX_VALUE) == +1);
assertTrue(Float.compare(Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(1.2f, Float.NaN) == -1);
assertTrue(Float.compare(1.2f, Float.POSITIVE_INFINITY) == -1);
assertTrue(Float.compare(1.2f, Float.MAX_VALUE) == -1);
assertTrue(Float.compare(1.2f, 1.2f) == 0);
assertTrue(Float.compare(1.2f, 0.0f) == +1);
assertTrue(Float.compare(1.2f, -0.0f) == +1);
assertTrue(Float.compare(1.2f, -1.2f) == +1);
assertTrue(Float.compare(1.2f, -Float.MAX_VALUE) == +1);
assertTrue(Float.compare(1.2f, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(0.0f, Float.NaN) == -1);
assertTrue(Float.compare(0.0f, Float.POSITIVE_INFINITY) == -1);
assertTrue(Float.compare(0.0f, Float.MAX_VALUE) == -1);
assertTrue(Float.compare(0.0f, 1.2f) == -1);
assertTrue(Float.compare(0.0f, 0.0f) == 0);
assertTrue(Float.compare(0.0f, -0.0f) == +1);
assertTrue(Float.compare(0.0f, -1.2f) == +1);
assertTrue(Float.compare(0.0f, -Float.MAX_VALUE) == +1);
assertTrue(Float.compare(0.0f, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(-0.0f, Float.NaN) == -1);
assertTrue(Float.compare(-0.0f, Float.POSITIVE_INFINITY) == -1);
assertTrue(Float.compare(-0.0f, Float.MAX_VALUE) == -1);
assertTrue(Float.compare(-0.0f, 1.2f) == -1);
assertTrue(Float.compare(-0.0f, 0.0f) == -1);
assertTrue(Float.compare(-0.0f, -0.0f) == 0);
assertTrue(Float.compare(-0.0f, -1.2f) == +1);
assertTrue(Float.compare(-0.0f, -Float.MAX_VALUE) == +1);
assertTrue(Float.compare(-0.0f, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(-1.2f, Float.NaN) == -1);
assertTrue(Float.compare(-1.2f, Float.POSITIVE_INFINITY) == -1);
assertTrue(Float.compare(-1.2f, Float.MAX_VALUE) == -1);
assertTrue(Float.compare(-1.2f, 1.2f) == -1);
assertTrue(Float.compare(-1.2f, 0.0f) == -1);
assertTrue(Float.compare(-1.2f, -0.0f) == -1);
assertTrue(Float.compare(-1.2f, -1.2f) == 0);
assertTrue(Float.compare(-1.2f, -Float.MAX_VALUE) == +1);
assertTrue(Float.compare(-1.2f, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(-Float.MAX_VALUE, Float.NaN) == -1);
assertTrue(Float.compare(-Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1);
assertTrue(Float.compare(-Float.MAX_VALUE, Float.MAX_VALUE) == -1);
assertTrue(Float.compare(-Float.MAX_VALUE, 1.2f) == -1);
assertTrue(Float.compare(-Float.MAX_VALUE, 0.0f) == -1);
assertTrue(Float.compare(-Float.MAX_VALUE, -0.0f) == -1);
assertTrue(Float.compare(-Float.MAX_VALUE, -1.2f) == -1);
assertTrue(Float.compare(-Float.MAX_VALUE, -Float.MAX_VALUE) == 0);
assertTrue(Float.compare(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, Float.NaN) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, Float.MAX_VALUE) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, 1.2f) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, 0.0f) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, -0.0f) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, -1.2f) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, -Float.MAX_VALUE) == -1);
assertTrue(Float.compare(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) == 0);
}
@Test
public void testIsDigits() {
assertFalse("isDigits(null) failed", NumberUtils.isDigits(null));
assertFalse("isDigits('') failed", NumberUtils.isDigits(""));
assertTrue("isDigits(String) failed", NumberUtils.isDigits("12345"));
assertFalse("isDigits(String) neg 1 failed", NumberUtils.isDigits("1234.5"));
assertFalse("isDigits(String) neg 3 failed", NumberUtils.isDigits("1ab"));
assertFalse("isDigits(String) neg 4 failed", NumberUtils.isDigits("abc"));
}
/**
* Tests isNumber(String) and tests that createNumber(String) returns
* a valid number iff isNumber(String) returns false.
*/
@Test
public void testIsNumber() {
compareIsNumberWithCreateNumber("12345", true);
compareIsNumberWithCreateNumber("1234.5", true);
compareIsNumberWithCreateNumber(".12345", true);
compareIsNumberWithCreateNumber("1234E5", true);
compareIsNumberWithCreateNumber("1234E+5", true);
compareIsNumberWithCreateNumber("1234E-5", true);
compareIsNumberWithCreateNumber("123.4E5", true);
compareIsNumberWithCreateNumber("-1234", true);
compareIsNumberWithCreateNumber("-1234.5", true);
compareIsNumberWithCreateNumber("-.12345", true);
compareIsNumberWithCreateNumber("-1234E5", true);
compareIsNumberWithCreateNumber("0", true);
compareIsNumberWithCreateNumber("-0", true);
compareIsNumberWithCreateNumber("01234", true);
compareIsNumberWithCreateNumber("-01234", true);
compareIsNumberWithCreateNumber("-0xABC123", true);
compareIsNumberWithCreateNumber("-0x0", true);
compareIsNumberWithCreateNumber("123.4E21D", true);
compareIsNumberWithCreateNumber("-221.23F", true);
compareIsNumberWithCreateNumber("22338L", true);
compareIsNumberWithCreateNumber(null, false);
compareIsNumberWithCreateNumber("", false);
compareIsNumberWithCreateNumber("--2.3", false);
compareIsNumberWithCreateNumber(".12.3", false);
compareIsNumberWithCreateNumber("-123E", false);
compareIsNumberWithCreateNumber("-123E+-212", false);
compareIsNumberWithCreateNumber("-123E2.12", false);
compareIsNumberWithCreateNumber("0xGF", false);
compareIsNumberWithCreateNumber("0xFAE-1", false);
compareIsNumberWithCreateNumber(".", false);
compareIsNumberWithCreateNumber("-0ABC123", false);
compareIsNumberWithCreateNumber("123.4E-D", false);
compareIsNumberWithCreateNumber("123.4ED", false);
compareIsNumberWithCreateNumber("1234E5l", false);
compareIsNumberWithCreateNumber("11a", false);
compareIsNumberWithCreateNumber("1a", false);
compareIsNumberWithCreateNumber("a", false);
compareIsNumberWithCreateNumber("11g", false);
compareIsNumberWithCreateNumber("11z", false);
compareIsNumberWithCreateNumber("11def", false);
compareIsNumberWithCreateNumber("11d11", false);
compareIsNumberWithCreateNumber("11 11", false);
compareIsNumberWithCreateNumber(" 1111", false);
compareIsNumberWithCreateNumber("1111 ", false);
compareIsNumberWithCreateNumber("2.", true); // LANG-521
compareIsNumberWithCreateNumber("1.1L", false); // LANG-664
}
@Test
public void testLANG971() {
compareIsNumberWithCreateNumber("0085", false);
compareIsNumberWithCreateNumber("085", false);
compareIsNumberWithCreateNumber("08", false);
compareIsNumberWithCreateNumber("07", true);
compareIsNumberWithCreateNumber("00", true);
}
@Test
public void testLANG992() {
compareIsNumberWithCreateNumber("0.0", true);
compareIsNumberWithCreateNumber("0.4790", true);
}
@Test
public void testLANG972() {
compareIsNumberWithCreateNumber("0xABCD", true);
compareIsNumberWithCreateNumber("0XABCD", true);
}
private void compareIsNumberWithCreateNumber(final String val, final boolean expected) {
final boolean isValid = NumberUtils.isNumber(val);
final boolean canCreate = checkCreateNumber(val);
if (isValid == expected && canCreate == expected) {
return;
}
fail("Expecting "+ expected + " for isNumber/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
}
@Test
public void testIsParsable() {
assertFalse( NumberUtils.isParsable(null) );
assertFalse( NumberUtils.isParsable("") );
assertFalse( NumberUtils.isParsable("0xC1AB") );
assertFalse( NumberUtils.isParsable("65CBA2") );
assertFalse( NumberUtils.isParsable("pendro") );
assertFalse( NumberUtils.isParsable("64,2") );
assertFalse( NumberUtils.isParsable("64.2.2") );
assertFalse( NumberUtils.isParsable("64.") );
assertFalse( NumberUtils.isParsable("64L") );
assertTrue( NumberUtils.isParsable("64.2") );
assertTrue( NumberUtils.isParsable("64") );
assertTrue(NumberUtils.isParsable("018"));
}
private boolean checkCreateNumber(final String val) {
try {
final Object obj = NumberUtils.createNumber(val);
if (obj == null) {
return false;
}
return true;
} catch (final NumberFormatException e) {
return false;
}
}
@SuppressWarnings("cast") // suppress instanceof warning check
@Test
public void testConstants() {
assertTrue(NumberUtils.LONG_ZERO instanceof Long);
assertTrue(NumberUtils.LONG_ONE instanceof Long);
assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long);
assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer);
assertTrue(NumberUtils.INTEGER_ONE instanceof Integer);
assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer);
assertTrue(NumberUtils.SHORT_ZERO instanceof Short);
assertTrue(NumberUtils.SHORT_ONE instanceof Short);
assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short);
assertTrue(NumberUtils.BYTE_ZERO instanceof Byte);
assertTrue(NumberUtils.BYTE_ONE instanceof Byte);
assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte);
assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double);
assertTrue(NumberUtils.DOUBLE_ONE instanceof Double);
assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double);
assertTrue(NumberUtils.FLOAT_ZERO instanceof Float);
assertTrue(NumberUtils.FLOAT_ONE instanceof Float);
assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float);
assertTrue(NumberUtils.LONG_ZERO.longValue() == 0);
assertTrue(NumberUtils.LONG_ONE.longValue() == 1);
assertTrue(NumberUtils.LONG_MINUS_ONE.longValue() == -1);
assertTrue(NumberUtils.INTEGER_ZERO.intValue() == 0);
assertTrue(NumberUtils.INTEGER_ONE.intValue() == 1);
assertTrue(NumberUtils.INTEGER_MINUS_ONE.intValue() == -1);
assertTrue(NumberUtils.SHORT_ZERO.shortValue() == 0);
assertTrue(NumberUtils.SHORT_ONE.shortValue() == 1);
assertTrue(NumberUtils.SHORT_MINUS_ONE.shortValue() == -1);
assertTrue(NumberUtils.BYTE_ZERO.byteValue() == 0);
assertTrue(NumberUtils.BYTE_ONE.byteValue() == 1);
assertTrue(NumberUtils.BYTE_MINUS_ONE.byteValue() == -1);
assertTrue(NumberUtils.DOUBLE_ZERO.doubleValue() == 0.0d);
assertTrue(NumberUtils.DOUBLE_ONE.doubleValue() == 1.0d);
assertTrue(NumberUtils.DOUBLE_MINUS_ONE.doubleValue() == -1.0d);
assertTrue(NumberUtils.FLOAT_ZERO.floatValue() == 0.0f);
assertTrue(NumberUtils.FLOAT_ONE.floatValue() == 1.0f);
assertTrue(NumberUtils.FLOAT_MINUS_ONE.floatValue() == -1.0f);
}
@Test
public void testLang300() {
NumberUtils.createNumber("-1l");
NumberUtils.createNumber("01l");
NumberUtils.createNumber("1l");
}
@Test
public void testLang381() {
assertTrue(Double.isNaN(NumberUtils.min(1.2, 2.5, Double.NaN)));
assertTrue(Double.isNaN(NumberUtils.max(1.2, 2.5, Double.NaN)));
assertTrue(Float.isNaN(NumberUtils.min(1.2f, 2.5f, Float.NaN)));
assertTrue(Float.isNaN(NumberUtils.max(1.2f, 2.5f, Float.NaN)));
final double[] a = new double[] { 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertTrue(Double.isNaN(NumberUtils.max(a)));
assertTrue(Double.isNaN(NumberUtils.min(a)));
final double[] b = new double[] { Double.NaN, 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
assertTrue(Double.isNaN(NumberUtils.max(b)));
assertTrue(Double.isNaN(NumberUtils.min(b)));
final float[] aF = new float[] { 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertTrue(Float.isNaN(NumberUtils.max(aF)));
final float[] bF = new float[] { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
assertTrue(Float.isNaN(NumberUtils.max(bF)));
}
}