List of usage examples for java.lang Float POSITIVE_INFINITY
float POSITIVE_INFINITY
To view the source code for java.lang Float POSITIVE_INFINITY.
Click Source Link
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils.NumberObject} */// w w w . jav a 2 s.co m @SuppressWarnings("unchecked") @Test public void NumberObjectTest() { try { Class<?> numberObject = ClassLoader.getSystemClassLoader() .loadClass(NumberUtils.class.getName() + "$NumberObject"); Constructor<?> c = numberObject.getDeclaredConstructor(Class.class); c.setAccessible(true); Method ofType = numberObject.getMethod("of", Class.class); ofType.setAccessible(true); Method ofN = numberObject.getMethod("of", Number.class); ofN.setAccessible(true); Method parsable = numberObject.getDeclaredMethod("parsable", Number.class); parsable.setAccessible(true); Method contains = numberObject.getDeclaredMethod("contains", Number.class); contains.setAccessible(true); Method valueOf = numberObject.getDeclaredMethod("valueOf", Number.class); valueOf.setAccessible(true); for (Class<?> type : NUMBERS) { Object o = c.newInstance(type); Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); Object numob = ofType.invoke(null, type); assertEquals("ofType: " + type.getSimpleName(), o, numob); Number n = null; if (!type.isPrimitive()) { if (ClassUtils.isPrimitiveWrapper(type)) { n = (Number) ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class) .invoke(null, "1"); } else { n = (Number) type.getField("ONE").get(null); } if (type.equals(byte.class)) assertEquals("ofN: 1: " + type.getSimpleName(), o, ofN.invoke(null, n)); } assertEquals("parsable: -1: " + type.getSimpleName(), true, parsable.invoke(numob, -1)); assertEquals("parsable: 0: " + type.getSimpleName(), true, parsable.invoke(numob, 0)); assertEquals("parsable: 1: " + type.getSimpleName(), true, parsable.invoke(numob, 1)); assertEquals("parsable: null: " + type.getSimpleName(), !type.isPrimitive(), parsable.invoke(numob, (Number) null)); Object expected = ObjectUtils.isAny(wrapper, Float.class, Double.class, BigDecimal.class, BigInteger.class); assertEquals("parsable: Infinity: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.POSITIVE_INFINITY)); assertEquals("parsable: Infinity: Double: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE)); assertEquals("parsable: Infinity: Double: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.toBigInteger())); assertEquals("parsable: Infinity: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.POSITIVE_INFINITY)); assertEquals("parsable: Infinity: Float: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT)); assertEquals("parsable: Infinity: Float: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.toBigInteger())); assertEquals("parsable: -Infinity: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.NEGATIVE_INFINITY)); assertEquals("parsable: -Infinity: Double: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.negate())); assertEquals("parsable: -Infinity: Double: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.negate().toBigInteger())); assertEquals("parsable: -Infinity: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.NEGATIVE_INFINITY)); assertEquals("parsable: -Infinity: Float: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.negate())); assertEquals("parsable: -Infinity: Float: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.negate().toBigInteger())); expected = ObjectUtils.isAny(wrapper, Float.class, Double.class); assertEquals("parsable: NaN: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.NaN)); assertEquals("parsable: NaN: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.NaN)); if (Byte.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Short.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Short.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Short.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Short.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123.456f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 1234.56f)); } if (Short.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Integer.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Integer.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Integer.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Integer.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 12345.6f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 123456.789f)); } if (Integer.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Long.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Long.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123456.789f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Long.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Long.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123456.789f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 12345678912345678912.3456d)); } if (Long.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2))); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2))); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2))); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2))); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123456.789f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 12345678912345678912.3456f)); } if (Float.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, -Double.MAX_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Double.MAX_VALUE)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, -Double.MAX_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Double.MAX_VALUE)); } if (Double.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), true, parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate())); assertEquals("parsable: overflow: max: " + type.getSimpleName(), true, parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN))); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate())); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN))); } if (!ClassUtils.isPrimitiveWrapper(wrapper)) { assertEquals("parsable: fraction: " + type.getSimpleName(), BigDecimal.class.equals(type), parsable.invoke(numob, 123.456f)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123.456f)); } if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "123"); } else { expected = new BigDecimal("123"); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } for (Class<?> valueType : OBJECTS) { if (ClassUtils.isPrimitiveWrapper(valueType)) { n = (Number) valueType.getMethod("valueOf", String.class).invoke(null, "123"); } else { n = new BigDecimal("123"); if (BigInteger.class.equals(valueType)) n = ((BigDecimal) n).toBigInteger(); } assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); } n = 123.456f; if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString()); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, Integer.toString(((Float) n).intValue())); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = 1.23456789E-6d; if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString()); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, Integer.toString(((Double) n).intValue())); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = INFINITY_DOUBLE.pow(2); if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MAX_VALUE").get(null); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: Huge: " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals("valueOf: Huge: class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = INFINITY_DOUBLE.pow(2).negate(); if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getField("NEGATIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MIN_VALUE").get(null); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: Huge: negative: " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals("valueOf: Huge: negative: class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }