Skip to content

Commit

Permalink
ESQL: Added BytesRef offset randomization to function tests (#109207)
Browse files Browse the repository at this point in the history
* Added BytesRef offset randomization to ESQL function tests

* Added extra array length and randomized paddings
  • Loading branch information
ivancea committed Jun 13, 2024
1 parent b36df9d commit d58da2c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,46 @@ public interface ExpectedEvaluatorToString {
Matcher<String> evaluatorToString(int nullPosition, TestCaseSupplier.TypedData nullData, Matcher<String> original);
}

/**
* Modifies suppliers to generate BytesRefs with random offsets.
*/
protected static List<TestCaseSupplier> randomizeBytesRefsOffset(List<TestCaseSupplier> testCaseSuppliers) {
return testCaseSuppliers.stream().map(supplier -> new TestCaseSupplier(supplier.name(), supplier.types(), () -> {
var testCase = supplier.supplier().get();

var newData = testCase.getData().stream().map(typedData -> {
if (typedData.data() instanceof BytesRef bytesRef) {
var offset = randomIntBetween(0, 10);
var extraLength = randomIntBetween(0, 10);
var newBytesArray = randomByteArrayOfLength(bytesRef.length + offset + extraLength);

System.arraycopy(bytesRef.bytes, bytesRef.offset, newBytesArray, offset, bytesRef.length);

var newBytesRef = new BytesRef(newBytesArray, offset, bytesRef.length);
var newTypedData = new TestCaseSupplier.TypedData(newBytesRef, typedData.type(), typedData.name());

if (typedData.isForceLiteral()) {
newTypedData.forceLiteral();
}

return newTypedData;
}
return typedData;
}).toList();

return new TestCaseSupplier.TestCase(
newData,
testCase.evaluatorToString(),
testCase.expectedType(),
testCase.getMatcher(),
testCase.getExpectedWarnings(),
testCase.getExpectedTypeError(),
testCase.foldingExceptionClass(),
testCase.foldingExceptionMessage()
);
})).toList();
}

protected static List<TestCaseSupplier> anyNullIsNull(
List<TestCaseSupplier> testCaseSuppliers,
ExpectedType expectedType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static Iterable<Object[]> parameters() {
})
);

return parameterSuppliersFromTypedData(errorsForCasesWithoutExamples(anyNullIsNull(true, suppliers)));
return parameterSuppliersFromTypedData(errorsForCasesWithoutExamples(anyNullIsNull(true, randomizeBytesRefsOffset(suppliers))));
}

@Override
Expand Down

0 comments on commit d58da2c

Please sign in to comment.