diff --git a/src/main/java/org/libj/math/LongDecimal.java b/src/main/java/org/libj/math/LongDecimal.java index bb6db787..483c8e43 100644 --- a/src/main/java/org/libj/math/LongDecimal.java +++ b/src/main/java/org/libj/math/LongDecimal.java @@ -233,7 +233,7 @@ else if (scale < 0) { * {@link LongDecimal#encode(long,short,byte,long) encoded} value and * and sign {@code bits}. */ - public static BigDecimal toBidDecimal(final long encoded, final byte bits) { + public static BigDecimal toBigDecimal(final long encoded, final byte bits) { final long value = decodeValue(encoded, bits); final short scale = decodeScale(encoded, bits); return BigDecimal.valueOf(value, scale); diff --git a/src/test/java/org/libj/math/LongDecimalArithmeticTest.java b/src/test/java/org/libj/math/LongDecimalArithmeticTest.java index 2c8886e4..a8dd6908 100644 --- a/src/test/java/org/libj/math/LongDecimalArithmeticTest.java +++ b/src/test/java/org/libj/math/LongDecimalArithmeticTest.java @@ -207,7 +207,7 @@ BigDecimal run(final BigDecimal bd1, final BigDecimal bd2, final BigDecimal expe final short s = decodeScale(result, bits); final BigDecimal expectedScaled = expected.setScale(s, RoundingMode.FLOOR); - final BigDecimal actual = toBidDecimal(result, bits); + final BigDecimal actual = toBigDecimal(result, bits); final BigDecimal error = expectedScaled.subtract(actual).abs().divide(expected, precision16); errors[bits] = errors[bits] == null ? error : errors[bits].max(error); @@ -261,6 +261,27 @@ final BigDecimal epsilon(final byte bits) { } } + private static final ArithmeticOperation neg = new FunctionOperation("neg", long.class) { + @Override + Long test(final long ld1, final long ld2, final BigDecimal bd1, final BigDecimal bd2, final byte bits, final long defaultValue, final long[] time) { + long ts = System.nanoTime(); + final long result = neg(ld1, bits, defaultValue); + ts = System.nanoTime() - ts; + if (result != defaultValue) + time[0] += ts; + + return result; + } + + @Override + BigDecimal control(final BigDecimal bd1, final BigDecimal bd2, final long[] time) { + final long ts = System.nanoTime(); + final BigDecimal result = bd1.negate(); + time[1] += System.nanoTime() - ts; + return result; + } + }; + private static final ArithmeticOperation encodeBigDecimal = new FunctionOperation("encode", BigDecimal.class) { @Override Long test(final long ld1, final long ld2, final BigDecimal bd1, final BigDecimal bd2, final byte bits, final long defaultValue, final long[] time) { @@ -386,6 +407,11 @@ public void testEncodeString() { test(encodeString); } + @Test + public void testNeg() { + test(neg); + } + @Test public void testSetScale() { test(setScale); diff --git a/src/test/java/org/libj/math/LongDecimalPredicateTest.java b/src/test/java/org/libj/math/LongDecimalPredicateTest.java index 42c62348..7551f12c 100644 --- a/src/test/java/org/libj/math/LongDecimalPredicateTest.java +++ b/src/test/java/org/libj/math/LongDecimalPredicateTest.java @@ -23,6 +23,76 @@ import org.junit.Test; public class LongDecimalPredicateTest extends LongDecimalTest { + private static final Operation precision = new Operation("precision", long.class, "~") { + @Override + BigDecimal run(final BigDecimal bd1, final BigDecimal bd2, final Short expected, final Short actual, final byte bits, final long defaultValue, final BigDecimal[] errors, final boolean[] failures) { + return expected.compareTo(actual) == 0 ? null : BigDecimal.ONE; + } + + @Override + Short test(final long ld1, final long ld2, final BigDecimal bd1, final BigDecimal bd2, final byte bits, final long defaultValue, final long[] time) { + final long ts = System.nanoTime(); + final short result = LongDecimal.precision(ld1, bits); + time[0] += System.nanoTime() - ts; + return result; + } + + @Override + Short control(final BigDecimal bd1, final BigDecimal bd2, final long[] time) { + final long ts = System.nanoTime(); + final short result = (short)bd1.precision(); + time[1] += System.nanoTime() - ts; + return result; + } + }; + + private static final Operation toBigDecimal = new Operation("toBigDecimal", long.class, "~") { + @Override + BigDecimal run(final BigDecimal bd1, final BigDecimal bd2, final BigDecimal expected, final BigDecimal actual, final byte bits, final long defaultValue, final BigDecimal[] errors, final boolean[] failures) { + return expected.compareTo(actual) == 0 ? null : BigDecimal.ONE; + } + + @Override + BigDecimal test(final long ld1, final long ld2, final BigDecimal bd1, final BigDecimal bd2, final byte bits, final long defaultValue, final long[] time) { + final long ts = System.nanoTime(); + final BigDecimal result = LongDecimal.toBigDecimal(ld1, bits); + time[0] += System.nanoTime() - ts; + return result; + } + + @Override + BigDecimal control(final BigDecimal bd1, final BigDecimal bd2, final long[] time) { + final String str = bd1.toString(); + final long ts = System.nanoTime(); + final BigDecimal result = new BigDecimal(str); + time[1] += System.nanoTime() - ts; + return result; + } + }; + + private static final Operation toDouble = new Operation("toDouble", long.class, "~") { + @Override + BigDecimal run(final BigDecimal bd1, final BigDecimal bd2, final Double expected, final Double actual, final byte bits, final long defaultValue, final BigDecimal[] errors, final boolean[] failures) { + return expected.compareTo(actual) == 0 ? null : BigDecimal.ONE; + } + + @Override + Double test(final long ld1, final long ld2, final BigDecimal bd1, final BigDecimal bd2, final byte bits, final long defaultValue, final long[] time) { + final long ts = System.nanoTime(); + final double result = LongDecimal.toDouble(ld1, bits); + time[0] += System.nanoTime() - ts; + return result; + } + + @Override + Double control(final BigDecimal bd1, final BigDecimal bd2, final long[] time) { + final long ts = System.nanoTime(); + final double result = bd1.doubleValue(); + time[1] += System.nanoTime() - ts; + return result; + } + }; + private static final Operation compare = new Operation("compare", long.class, "<>") { @Override BigDecimal run(final BigDecimal bd1, final BigDecimal bd2, final Integer expected, final Integer actual, final byte bits, final long defaultValue, final BigDecimal[] errors, final boolean[] failures) { @@ -53,7 +123,7 @@ private PredicateOperation(final String label, final Class arg, final String @Override BigDecimal run(final BigDecimal bd1, final BigDecimal bd2, final BigDecimal expected, final Long actual, final byte bits, final long defaultValue, final BigDecimal[] errors, final boolean[] failures) { - return expected.compareTo(toBidDecimal(actual, bits)) == 0 ? null : BigDecimal.ONE; + return expected.compareTo(toBigDecimal(actual, bits)) == 0 ? null : BigDecimal.ONE; } } @@ -261,4 +331,19 @@ public void testGte() { public void testToString() { test(toString); } + + @Test + public void testToBigDecimal() { + test(toBigDecimal); + } + + @Test + public void testToDouble() { + test(toDouble); + } + + @Test + public void testPrecision() { + test(precision); + } } \ No newline at end of file diff --git a/src/test/java/org/libj/math/LongDecimalTest.java b/src/test/java/org/libj/math/LongDecimalTest.java index 3ba5fdae..6ef4ca59 100644 --- a/src/test/java/org/libj/math/LongDecimalTest.java +++ b/src/test/java/org/libj/math/LongDecimalTest.java @@ -144,8 +144,8 @@ else if (i < preliminary.length) { ld2 = operation.randomEncoded(b); } - final BigDecimal bd1 = toBidDecimal(ld1, b); - final BigDecimal bd2 = toBidDecimal(ld2, b); + final BigDecimal bd1 = toBigDecimal(ld1, b); + final BigDecimal bd2 = toBigDecimal(ld2, b); Object actual = defaultValue; Object expected = defaultValue; BigDecimal error = BigDecimal.ZERO;