Skip to content

Commit

Permalink
refactoring test case for non-casting binary functions
Browse files Browse the repository at this point in the history
  • Loading branch information
not-napoleon committed Nov 29, 2023
1 parent e802a2d commit 04b5211
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.DoubleFunction;
import java.util.function.Function;
Expand Down Expand Up @@ -160,6 +161,36 @@ public static List<TestCaseSupplier> forBinaryCastingToDouble(
List<String> warnings
) {
List<TestCaseSupplier> suppliers = new ArrayList<>();
casesCrossProduct(
(l, r) -> expected.apply(l.doubleValue(), r.doubleValue()),
lhsSuppliers,
rhsSuppliers,
(lhsType, rhsType) -> name
+ "["
+ lhsName
+ "="
+ castToDoubleEvaluator("Attribute[channel=0]", lhsType)
+ ", "
+ rhsName
+ "="
+ castToDoubleEvaluator("Attribute[channel=1]", rhsType)
+ "]",
warnings,
suppliers,
DataTypes.DOUBLE
);
return suppliers;
}

private static void casesCrossProduct(
BinaryOperator<Number> expected,
List<TypedDataSupplier> lhsSuppliers,
List<TypedDataSupplier> rhsSuppliers,
BiFunction<DataType, DataType, String> evaluatorToString,
List<String> warnings,
List<TestCaseSupplier> suppliers,
DataType expectedType
) {
for (TypedDataSupplier lhsSupplier : lhsSuppliers) {
for (TypedDataSupplier rhsSupplier : rhsSuppliers) {
String caseName = lhsSupplier.name() + ", " + rhsSupplier.name();
Expand All @@ -177,12 +208,10 @@ public static List<TestCaseSupplier> forBinaryCastingToDouble(
rhsSupplier.type(),
"rhs"
);
String lhsEvalName = castToDoubleEvaluator("Attribute[channel=0]", lhsSupplier.type());
String rhsEvalName = castToDoubleEvaluator("Attribute[channel=1]", rhsSupplier.type());
TestCase testCase = new TestCase(
List.of(lhsTyped, rhsTyped),
name + "[" + lhsName + "=" + lhsEvalName + ", " + rhsName + "=" + rhsEvalName + "]",
DataTypes.DOUBLE,
evaluatorToString.apply(lhsSupplier.type(), rhsSupplier.type()),
expectedType,
equalTo(expected.apply(lhs.doubleValue(), rhs.doubleValue()))
);
for (String warning : warnings) {
Expand All @@ -192,8 +221,6 @@ public static List<TestCaseSupplier> forBinaryCastingToDouble(
}));
}
}

return suppliers;
}

public static List<TypedDataSupplier> castToDoubleSuppliersFromRange(Double Min, Double Max) {
Expand All @@ -205,6 +232,30 @@ public static List<TypedDataSupplier> castToDoubleSuppliersFromRange(Double Min,
return suppliers;
}

public static List<TestCaseSupplier> forBinaryNumericNotCasting(
String name,
String lhsName,
String rhsName,
BinaryOperator<Number> expected,
DataType expectedType,
List<TypedDataSupplier> lhsSuppliers,
List<TypedDataSupplier> rhsSuppliers,
List<String> warnings

) {
List<TestCaseSupplier> suppliers = new ArrayList<>();
casesCrossProduct(
expected,
lhsSuppliers,
rhsSuppliers,
(lhsType, rhsType) -> name + "[" + lhsName + "=Attribute[channel=0], " + rhsName + "=Attribute[channel=1]]",
warnings,
suppliers,
expectedType
);
return suppliers;
}

/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#INTEGER}.
*/
Expand Down Expand Up @@ -456,7 +507,7 @@ private static void unary(
}
}

private static List<TypedDataSupplier> intCases(int min, int max) {
public static List<TypedDataSupplier> intCases(int min, int max) {
List<TypedDataSupplier> cases = new ArrayList<>();
if (0 <= max && 0 >= min) {
cases.add(new TypedDataSupplier("<0 int>", () -> 0, DataTypes.INTEGER));
Expand All @@ -480,7 +531,7 @@ private static List<TypedDataSupplier> intCases(int min, int max) {
return cases;
}

private static List<TypedDataSupplier> longCases(long min, long max) {
public static List<TypedDataSupplier> longCases(long min, long max) {
List<TypedDataSupplier> cases = new ArrayList<>();
if (0L <= max && 0L >= min) {
cases.add(new TypedDataSupplier("<0 long>", () -> 0L, DataTypes.LONG));
Expand All @@ -505,7 +556,7 @@ private static List<TypedDataSupplier> longCases(long min, long max) {
return cases;
}

private static List<TypedDataSupplier> ulongCases(BigInteger min, BigInteger max) {
public static List<TypedDataSupplier> ulongCases(BigInteger min, BigInteger max) {
List<TypedDataSupplier> cases = new ArrayList<>();

// Zero
Expand Down Expand Up @@ -545,7 +596,7 @@ private static List<TypedDataSupplier> ulongCases(BigInteger min, BigInteger max
return cases;
}

private static List<TypedDataSupplier> doubleCases(double min, double max) {
public static List<TypedDataSupplier> doubleCases(double min, double max) {
List<TypedDataSupplier> cases = new ArrayList<>();

// Zeros
Expand Down Expand Up @@ -633,7 +684,7 @@ private static List<TypedDataSupplier> dateCases() {
);
}

private static List<TypedDataSupplier> ipCases() {
public static List<TypedDataSupplier> ipCases() {
return List.of(
new TypedDataSupplier(
"<127.0.0.1 ip>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.Duration;
import java.time.Period;
import java.time.temporal.TemporalAmount;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -42,20 +43,20 @@ public AddTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSu

@ParametersFactory
public static Iterable<Object[]> parameters() {
return parameterSuppliersFromTypedData(List.of(new TestCaseSupplier("Int + Int", () -> {
// Ensure we don't have an overflow
int rhs = randomIntBetween((Integer.MIN_VALUE >> 1) - 1, (Integer.MAX_VALUE >> 1) - 1);
int lhs = randomIntBetween((Integer.MIN_VALUE >> 1) - 1, (Integer.MAX_VALUE >> 1) - 1);
return new TestCaseSupplier.TestCase(
List.of(
new TestCaseSupplier.TypedData(lhs, DataTypes.INTEGER, "lhs"),
new TestCaseSupplier.TypedData(rhs, DataTypes.INTEGER, "rhs")
),
"AddIntsEvaluator[lhs=Attribute[channel=0], rhs=Attribute[channel=1]]",
List<TestCaseSupplier> suppliers = new ArrayList<>();
suppliers.addAll(
TestCaseSupplier.forBinaryNumericNotCasting(
"AddIntsEvaluator",
"lhs",
"rhs",
(l, r) -> l.intValue() + r.intValue(),
DataTypes.INTEGER,
equalTo(lhs + rhs)
);
}), new TestCaseSupplier("Long + Long", () -> {
TestCaseSupplier.intCases((Integer.MIN_VALUE >> 1) - 1, (Integer.MAX_VALUE >> 1) - 1),
TestCaseSupplier.intCases((Integer.MIN_VALUE >> 1) - 1, (Integer.MAX_VALUE >> 1) - 1),
List.of()
)
);
suppliers.addAll(List.of(new TestCaseSupplier("Long + Long", () -> {
// Ensure we don't have an overflow
long rhs = randomLongBetween((Long.MIN_VALUE >> 1) - 1, (Long.MAX_VALUE >> 1) - 1);
long lhs = randomLongBetween((Long.MIN_VALUE >> 1) - 1, (Long.MAX_VALUE >> 1) - 1);
Expand Down Expand Up @@ -181,6 +182,7 @@ public static Iterable<Object[]> parameters() {
is(nullValue())
);
})));
return parameterSuppliersFromTypedData(suppliers);
}

@Override
Expand Down

0 comments on commit 04b5211

Please sign in to comment.