Skip to content

Commit

Permalink
Add more tests, re #4
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Jun 8, 2020
1 parent f669abd commit c37c7d6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/libj/math/LongDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 27 additions & 1 deletion src/test/java/org/libj/math/LongDecimalArithmeticTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -386,6 +407,11 @@ public void testEncodeString() {
test(encodeString);
}

@Test
public void testNeg() {
test(neg);
}

@Test
public void testSetScale() {
test(setScale);
Expand Down
87 changes: 86 additions & 1 deletion src/test/java/org/libj/math/LongDecimalPredicateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,76 @@
import org.junit.Test;

public class LongDecimalPredicateTest extends LongDecimalTest {
private static final Operation<Short,Short> precision = new Operation<Short,Short>("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<BigDecimal,BigDecimal> toBigDecimal = new Operation<BigDecimal,BigDecimal>("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<Double,Double> toDouble = new Operation<Double,Double>("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<Integer,Integer> compare = new Operation<Integer,Integer>("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) {
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/test/java/org/libj/math/LongDecimalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c37c7d6

Please sign in to comment.