Skip to content

Commit

Permalink
Unit test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Mar 11, 2024
1 parent 3f7c279 commit de5a1cd
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.github.spannm.jackcess.expr.EvalException;
import io.github.spannm.jackcess.expr.Expression;
import io.github.spannm.jackcess.impl.expr.DefaultFunctionsTest.CustomFormatSource.CustomFormatArgumentsProvider;
import io.github.spannm.jackcess.impl.expr.ExpressionatorTest.TestContext;
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestUtil;
Expand Down Expand Up @@ -912,6 +913,19 @@ static Object eval(String _expr) {
String format();

String[] testValues() default {};

static class CustomFormatArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<Arguments> provideArguments(ExtensionContext context) {
return context.getElement().map(elem -> AnnotationSupport.findRepeatableAnnotations(elem, CustomFormatSource.class)).orElse(List.of()).stream()
.flatMap(src -> IntStream.range(0, src.testValues().length).filter(i -> i % 2 == 0).mapToObj(i -> {
String expected = src.testValues()[i];
String val = src.testValues()[i + 1];
return Arguments.of(val, src.format(), expected);
}));
}
}

}

@Target(ElementType.METHOD)
Expand All @@ -920,16 +934,4 @@ static Object eval(String _expr) {
CustomFormatSource[] value();
}

static class CustomFormatArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<Arguments> provideArguments(ExtensionContext context) {
return context.getElement().map(elem -> AnnotationSupport.findRepeatableAnnotations(elem, CustomFormatSource.class)).orElse(List.of()).stream()
.flatMap(src -> IntStream.range(0, src.testValues().length).filter(i -> i % 2 == 0).mapToObj(i -> {
String expected = src.testValues()[i];
String val = src.testValues()[i + 1];
return Arguments.of(val, src.format(), expected);
}));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestUtil;
import io.github.spannm.jackcess.test.converter.CsvToLocalDateTime;
import io.github.spannm.jackcess.test.converter.CsvToStringArray;
import io.github.spannm.jackcess.test.source.IntMatrixSource;
import io.github.spannm.jackcess.test.source.IntRangeSource;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -56,35 +55,50 @@ static double[] getDoublesTestData() {
9.20456200d, 10.325d};
}

@ParameterizedTest(name = "[{index}] {0} --> {1}")
@CsvSource(delimiter = ';', value = {
"\"A\"; <ELiteralValue>{\"A\"}",
"13; <ELiteralValue>{13}",
"-42; <EUnaryOp>{- <ELiteralValue>{42}}",
"(+37); <EParen>{(<EUnaryOp>{+ <ELiteralValue>{37}})}"
@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = {
"+", "-", "*", "/", "\\", "^", "&", "Mod"
})
void testParseSimpleExpr1(String exprStr, String debugStr) {
validateExpr(exprStr, debugStr);
void testParseEBinaryOp(String op) {
String opName = "EBinaryOp";
validateExpr("\"A\" " + op + " \"B\"", "<" + opName + ">{<ELiteralValue>{\"A\"} " + op + " <ELiteralValue>{\"B\"}}");
}

@ParameterizedTest(name = "[{index}] {0} --> {1}")
@CsvSource(delimiter = ';', value = {
"EBinaryOp; +, -, *, /, \\, ^, &, Mod",
"ECompOp; <, <=, >, >=, =, <>",
"ELogicalOp; And, Or, Eqv, Xor, Imp",
@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = {
"<", "<=", ">", ">=", "=", "<>"
})
void testParseSimpleExpr2(String opName, @ConvertWith(CsvToStringArray.class) String... ops) {
for (String op : ops) {
validateExpr("\"A\" " + op + " \"B\"", "<" + opName + ">{<ELiteralValue>{\"A\"} " + op + " <ELiteralValue>{\"B\"}}");
}
void testParseECompOp(String op) {
String opName = "ECompOp";
validateExpr("\"A\" " + op + " \"B\"", "<" + opName + ">{<ELiteralValue>{\"A\"} " + op + " <ELiteralValue>{\"B\"}}");
}

@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = {
"And", "Or", "Eqv", "Xor", "Imp"
})
void testParseELogicalOp(String op) {
String opName = "ELogicalOp";
validateExpr("\"A\" " + op + " \"B\"", "<" + opName + ">{<ELiteralValue>{\"A\"} " + op + " <ELiteralValue>{\"B\"}}");
}

@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = {"True", "False", "Null"})
void testParseSimpleExpr3(String constStr) {
void testParseEConstValue(String constStr) {
validateExpr(constStr, "<EConstValue>{" + constStr + "}");
}

@ParameterizedTest(name = "[{index}] {0} --> {1}")
@CsvSource(delimiter = ';', value = {
"\"A\"; <ELiteralValue>{\"A\"}",
"13; <ELiteralValue>{13}",
"-42; <EUnaryOp>{- <ELiteralValue>{42}}",
"(+37); <EParen>{(<EUnaryOp>{+ <ELiteralValue>{37}})}"
})
void testParseSimpleExpr1(String exprStr, String debugStr) {
validateExpr(exprStr, debugStr);
}

@ParameterizedTest(name = "[{index}] {0} --> {1}")
@CsvSource(delimiter = ';',
quoteCharacter = '§',
Expand All @@ -102,7 +116,7 @@ void testParseSimpleExpr3(String constStr) {
"' \"A\" '; <ELiteralValue>{\" \"\"A\"\" \"}; \" \"\"A\"\" \"",
"<=1 And >=0; <ELogicalOp>{<ECompOp>{<EThisValue>{<THIS_COL>} <= <ELiteralValue>{1}} And <ECompOp>{<EThisValue>{<THIS_COL>} >= <ELiteralValue>{0}}}; <= 1 And >= 0",
})
void testParseSimpleExpr4(String exprStr, String debugStr, String cleanStr) {
void testParseSimpleExpr2(String exprStr, String debugStr, String cleanStr) {
validateExpr(exprStr, debugStr, Objects.requireNonNullElse(cleanStr, exprStr));
}

Expand Down Expand Up @@ -154,25 +168,25 @@ void testSimpleMathExpressions3(int i, int j) {
@MethodSource("getDoublesTestData")
void testSimpleMathExpressions4(double d1) {
BigDecimal bd1 = toBD(d1);
for (double j : getDoublesTestData()) {
BigDecimal bd2 = toBD(j);
assertEquals(toBD(bd1.add(bd2)), eval(d1 + " + " + j));
assertEquals(toBD(bd1.subtract(bd2)), eval(d1 + " - " + j));
assertEquals(toBD(bd1.multiply(bd2)), eval(d1 + " * " + j));
if (roundToLongInt(j) == 0) {
evalFail(d1 + " \\ " + j, ArithmeticException.class);
for (double d : getDoublesTestData()) {
BigDecimal bd2 = toBD(d);
assertEquals(toBD(bd1.add(bd2)), eval(d1 + " + " + d));
assertEquals(toBD(bd1.subtract(bd2)), eval(d1 + " - " + d));
assertEquals(toBD(bd1.multiply(bd2)), eval(d1 + " * " + d));
if (roundToLongInt(d) == 0) {
evalFail(d1 + " \\ " + d, ArithmeticException.class);
} else {
assertEquals(roundToLongInt(d1) / roundToLongInt(j), eval(d1 + " \\ " + j));
assertEquals(roundToLongInt(d1) / roundToLongInt(d), eval(d1 + " \\ " + d));
}
if (roundToLongInt(j) == 0) {
evalFail(d1 + " Mod " + j, ArithmeticException.class);
if (roundToLongInt(d) == 0) {
evalFail(d1 + " Mod " + d, ArithmeticException.class);
} else {
assertEquals(roundToLongInt(d1) % roundToLongInt(j), eval(d1 + " Mod " + j));
assertEquals(roundToLongInt(d1) % roundToLongInt(d), eval(d1 + " Mod " + d));
}
if (j == 0.0d) {
evalFail(d1 + " / " + j, ArithmeticException.class);
if (d == 0.0d) {
evalFail(d1 + " / " + d, ArithmeticException.class);
} else {
assertEquals(toBD(BuiltinOperators.divide(bd1, bd2)), eval(d1 + " / " + j));
assertEquals(toBD(BuiltinOperators.divide(bd1, bd2)), eval(d1 + " / " + d));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,86 +18,106 @@

import io.github.spannm.jackcess.expr.NumericConfig;
import io.github.spannm.jackcess.test.AbstractBaseTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.math.BigDecimal;
import java.util.stream.Stream;

/**
*
* @author James Ahlborn
*/
class NumberFormatterTest extends AbstractBaseTest {
private final NumberFormatter _numFmt = new NumberFormatter(
NumericConfig.US_NUMERIC_CONFIG.getDecimalFormatSymbols());
private final NumberFormatter numFmt = new NumberFormatter(NumericConfig.US_NUMERIC_CONFIG.getDecimalFormatSymbols());

@Test
void testDoubleFormat() {
assertEquals("894984737284944", _numFmt.format(894984737284944d));
assertEquals("-894984737284944", _numFmt.format(-894984737284944d));
assertEquals("8949.84737284944", _numFmt.format(8949.84737284944d));
assertEquals("8949847372844", _numFmt.format(8949847372844d));
assertEquals("8949.847384944", _numFmt.format(8949.847384944d));
assertEquals("8.94985647372849E+16", _numFmt.format(89498564737284944d));
assertEquals("-8.94985647372849E+16", _numFmt.format(-89498564737284944d));
assertEquals("895649.847372849", _numFmt.format(895649.84737284944d));
assertEquals("300", _numFmt.format(300d));
assertEquals("-300", _numFmt.format(-300d));
assertEquals("0.3", _numFmt.format(0.3d));
assertEquals("0.1", _numFmt.format(0.1d));
assertEquals("2.3423421E-12", _numFmt.format(0.0000000000023423421d));
assertEquals("2.3423421E-11", _numFmt.format(0.000000000023423421d));
assertEquals("2.3423421E-10", _numFmt.format(0.00000000023423421d));
assertEquals("-2.3423421E-10", _numFmt.format(-0.00000000023423421d));
assertEquals("2.34234214E-12", _numFmt.format(0.00000000000234234214d));
assertEquals("2.342342156E-12", _numFmt.format(0.000000000002342342156d));
assertEquals("0.000000023423421", _numFmt.format(0.000000023423421d));
assertEquals("2.342342133E-07", _numFmt.format(0.0000002342342133d));
assertEquals("1.#INF", _numFmt.format(Double.POSITIVE_INFINITY));
assertEquals("-1.#INF", _numFmt.format(Double.NEGATIVE_INFINITY));
assertEquals("1.#QNAN", _numFmt.format(Double.NaN));
static Stream<Arguments> getDoubleFormatData() {
return Stream.of(
args("894984737284944", 894984737284944d),
args("-894984737284944", -894984737284944d),
args("8949.84737284944", 8949.84737284944d),
args("8949847372844", 8949847372844d),
args("8949.847384944", 8949.847384944d),
args("8.94985647372849E+16", 89498564737284944d),
args("-8.94985647372849E+16", -89498564737284944d),
args("895649.847372849", 895649.84737284944d),
args("300", 300d),
args("-300", -300d),
args("0.3", 0.3d),
args("0.1", 0.1d),
args("2.3423421E-12", 0.0000000000023423421d),
args("2.3423421E-11", 0.000000000023423421d),
args("2.3423421E-10", 0.00000000023423421d),
args("-2.3423421E-10", -0.00000000023423421d),
args("2.34234214E-12", 0.00000000000234234214d),
args("2.342342156E-12", 0.000000000002342342156d),
args("0.000000023423421", 0.000000023423421d),
args("2.342342133E-07", 0.0000002342342133d),
args("1.#INF", Double.POSITIVE_INFINITY),
args("-1.#INF", Double.NEGATIVE_INFINITY),
args("1.#QNAN", Double.NaN)
);
}
@ParameterizedTest(name = "[{index}] {1} --> {0}")
@MethodSource("getDoubleFormatData")
void testDoubleFormat(String _expected, Double _input) {
assertEquals(_expected, numFmt.format(_input));
}

@Test
void testFloatFormat() {
assertEquals("8949847", _numFmt.format(8949847f));
assertEquals("-8949847", _numFmt.format(-8949847f));
assertEquals("8949.847", _numFmt.format(8949.847f));
assertEquals("894984", _numFmt.format(894984f));
assertEquals("8949.84", _numFmt.format(8949.84f));
assertEquals("8.949856E+16", _numFmt.format(89498564737284944f));
assertEquals("-8.949856E+16", _numFmt.format(-89498564737284944f));
assertEquals("895649.9", _numFmt.format(895649.84737284944f));
assertEquals("300", _numFmt.format(300f));
assertEquals("-300", _numFmt.format(-300f));
assertEquals("0.3", _numFmt.format(0.3f));
assertEquals("0.1", _numFmt.format(0.1f));
assertEquals("2.342342E-12", _numFmt.format(0.0000000000023423421f));
assertEquals("2.342342E-11", _numFmt.format(0.000000000023423421f));
assertEquals("2.342342E-10", _numFmt.format(0.00000000023423421f));
assertEquals("-2.342342E-10", _numFmt.format(-0.00000000023423421f));
assertEquals("2.342342E-12", _numFmt.format(0.00000000000234234214f));
assertEquals("2.342342E-12", _numFmt.format(0.000000000002342342156f));
assertEquals("0.0000234", _numFmt.format(0.0000234f));
assertEquals("2.342E-05", _numFmt.format(0.00002342f));
assertEquals("1.#INF", _numFmt.format(Float.POSITIVE_INFINITY));
assertEquals("-1.#INF", _numFmt.format(Float.NEGATIVE_INFINITY));
assertEquals("1.#QNAN", _numFmt.format(Float.NaN));
static Stream<Arguments> getFloatFormatData() {
return Stream.of(
args("8949847", 8949847f),
args("-8949847", -8949847f),
args("8949.847", 8949.847f),
args("894984", 894984f),
args("8949.84", 8949.84f),
args("8.949856E+16", 89498564737284944f),
args("-8.949856E+16", -89498564737284944f),
args("895649.9", 895649.84737284944f),
args("300", 300f),
args("-300", -300f),
args("0.3", 0.3f),
args("0.1", 0.1f),
args("2.342342E-12", 0.0000000000023423421f),
args("2.342342E-11", 0.000000000023423421f),
args("2.342342E-10", 0.00000000023423421f),
args("-2.342342E-10", -0.00000000023423421f),
args("2.342342E-12", 0.00000000000234234214f),
args("2.342342E-12", 0.000000000002342342156f),
args("0.0000234", 0.0000234f),
args("2.342E-05", 0.00002342f),
args("1.#INF", Float.POSITIVE_INFINITY),
args("-1.#INF", Float.NEGATIVE_INFINITY),
args("1.#QNAN", Float.NaN)
);
}
@ParameterizedTest(name = "[{index}] {1} --> {0}")
@MethodSource("getFloatFormatData")
void testFloatFormat(String _expected, Float _input) {
assertEquals(_expected, numFmt.format(_input));
}

@Test
void testDecimalFormat() {
assertEquals("9874539485972.2342342234234", _numFmt.format(new BigDecimal("9874539485972.2342342234234")));
assertEquals("9874539485972.234234223423468", _numFmt.format(new BigDecimal("9874539485972.2342342234234678")));
assertEquals("-9874539485972.234234223423468", _numFmt.format(new BigDecimal("-9874539485972.2342342234234678")));
assertEquals("9.874539485972234234223423468E+31", _numFmt.format(new BigDecimal("98745394859722342342234234678000")));
assertEquals("9.874539485972234234223423468E+31", _numFmt.format(new BigDecimal("98745394859722342342234234678000")));
assertEquals("-9.874539485972234234223423468E+31", _numFmt.format(new BigDecimal("-98745394859722342342234234678000")));
assertEquals("300", _numFmt.format(new BigDecimal("300.0")));
assertEquals("-300", _numFmt.format(new BigDecimal("-300.000")));
assertEquals("0.3", _numFmt.format(new BigDecimal("0.3")));
assertEquals("0.1", _numFmt.format(new BigDecimal("0.1000")));
assertEquals("0.0000000000023423428930458", _numFmt.format(new BigDecimal("0.0000000000023423428930458")));
assertEquals("2.3423428930458389038451E-12", _numFmt.format(new BigDecimal("0.0000000000023423428930458389038451")));
assertEquals("2.342342893045838903845134766E-12", _numFmt.format(new BigDecimal("0.0000000000023423428930458389038451347656")));
static Stream<Arguments> getBigDecimalFormatData() {
return Stream.of(
args("9874539485972.2342342234234", "9874539485972.2342342234234"),
args("9874539485972.234234223423468", "9874539485972.2342342234234678"),
args("-9874539485972.234234223423468", "-9874539485972.2342342234234678"),
args("9.874539485972234234223423468E+31", "98745394859722342342234234678000"),
args("9.874539485972234234223423468E+31", "98745394859722342342234234678000"),
args("-9.874539485972234234223423468E+31", "-98745394859722342342234234678000"),
args("300", "300.0"),
args("-300", "-300.000"),
args("0.3", "0.3"),
args("0.1", "0.1000"),
args("0.0000000000023423428930458", "0.0000000000023423428930458"),
args("2.3423428930458389038451E-12", "0.0000000000023423428930458389038451"),
args("2.342342893045838903845134766E-12", "0.0000000000023423428930458389038451347656")
);
}
@ParameterizedTest(name = "[{index}] {1} --> {0}")
@MethodSource("getBigDecimalFormatData")
void testDecimalFormat(String _expected, String _input) {
assertEquals(_expected, numFmt.format(new BigDecimal(_input)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public Stream<Arguments> provideArguments(ExtensionContext context) {
if (optSrc.isEmpty()) {
return Stream.empty();
}
TestDbSource testDbSource = optSrc.get();
Basename basename = Objects.requireNonNull(testDbSource.basename());
List<TestDb> dbs = testDbSource.readOnly() ? getReadOnlyDbs(basename) : getDbs(basename);
TestDbSource src = optSrc.get();
Basename basename = Objects.requireNonNull(src.basename());
List<TestDb> dbs = src.readOnly() ? getReadOnlyDbs(basename) : getDbs(basename);
return dbs.stream().map(Arguments::of);
}

Expand Down

0 comments on commit de5a1cd

Please sign in to comment.