Skip to content

Commit

Permalink
remove encoding results from toUnsignedLong tests
Browse files Browse the repository at this point in the history
  • Loading branch information
not-napoleon committed Dec 7, 2023
1 parent 1b9e95c commit 32b0579
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.apache.lucene.document.InetAddressPoint;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.AbstractConvertFunction;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
Expand Down Expand Up @@ -51,6 +53,8 @@
public record TestCaseSupplier(String name, List<DataType> types, Supplier<TestCase> supplier)
implements
Supplier<TestCaseSupplier.TestCase> {

private static Logger logger = LogManager.getLogger(TestCaseSupplier.class);
/**
* Build a test case without types.
*
Expand Down Expand Up @@ -695,6 +699,8 @@ public static void unary(
supplier.type(),
"value"
);
logger.info("Value is " + value + " of type " + value.getClass());
logger.info("expectedValue is " + expectedValue.apply(value));
TestCase testCase = new TestCase(
List.of(typed),
expectedEvaluatorToString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import java.util.function.Supplier;

import static org.elasticsearch.xpack.ql.type.DataTypeConverter.safeToUnsignedLong;
import static org.elasticsearch.xpack.ql.util.NumericUtils.ONE_AS_UNSIGNED_LONG;
import static org.elasticsearch.xpack.ql.util.NumericUtils.UNSIGNED_LONG_MAX_AS_DOUBLE;
import static org.elasticsearch.xpack.ql.util.NumericUtils.ZERO_AS_UNSIGNED_LONG;
import static org.elasticsearch.xpack.ql.util.NumericUtils.asLongUnsigned;

public class ToUnsignedLongTests extends AbstractFunctionTestCase {
public ToUnsignedLongTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
Expand All @@ -47,7 +44,7 @@ public static Iterable<Object[]> parameters() {
suppliers,
read,
DataTypes.UNSIGNED_LONG,
NumericUtils::asLongUnsigned,
(n) -> n,
BigInteger.ZERO,
UNSIGNED_LONG_MAX,
List.of()
Expand All @@ -57,7 +54,7 @@ public static Iterable<Object[]> parameters() {
suppliers,
evaluatorName.apply("Boolean"),
DataTypes.UNSIGNED_LONG,
b -> b ? ONE_AS_UNSIGNED_LONG : ZERO_AS_UNSIGNED_LONG,
b -> b ? BigInteger.ONE : BigInteger.ZERO,
List.of()
);

Expand All @@ -66,7 +63,7 @@ public static Iterable<Object[]> parameters() {
suppliers,
evaluatorName.apply("Long"),
DataTypes.UNSIGNED_LONG,
instant -> asLongUnsigned(instant.toEpochMilli()),
instant -> BigInteger.valueOf(instant.toEpochMilli()),
List.of()
);
// random strings that don't look like an unsigned_long
Expand All @@ -85,7 +82,7 @@ public static Iterable<Object[]> parameters() {
suppliers,
evaluatorName.apply("Double"),
DataTypes.UNSIGNED_LONG,
d -> asLongUnsigned(BigDecimal.valueOf(d).toBigInteger()), // note: not: new BigDecimal(d).toBigInteger
d -> BigDecimal.valueOf(d).toBigInteger(), // note: not: new BigDecimal(d).toBigInteger
0d,
UNSIGNED_LONG_MAX_AS_DOUBLE,
List.of()
Expand Down Expand Up @@ -122,7 +119,7 @@ public static Iterable<Object[]> parameters() {
suppliers,
evaluatorName.apply("Long"),
DataTypes.UNSIGNED_LONG,
NumericUtils::asLongUnsigned,
(n) -> BigInteger.valueOf(n),
0L,
Long.MAX_VALUE,
List.of()
Expand All @@ -146,7 +143,7 @@ public static Iterable<Object[]> parameters() {
suppliers,
evaluatorName.apply("Int"),
DataTypes.UNSIGNED_LONG,
NumericUtils::asLongUnsigned,
(n) -> BigInteger.valueOf(n),
0,
Integer.MAX_VALUE,
List.of()
Expand Down Expand Up @@ -180,7 +177,7 @@ public static Iterable<Object[]> parameters() {
)
.toList(),
DataTypes.UNSIGNED_LONG,
bytesRef -> asLongUnsigned(safeToUnsignedLong(((BytesRef) bytesRef).utf8ToString())),
bytesRef -> safeToUnsignedLong(((BytesRef) bytesRef).utf8ToString()),
List.of()
);
// strings of random doubles within unsigned_long's range
Expand All @@ -198,7 +195,7 @@ public static Iterable<Object[]> parameters() {
)
.toList(),
DataTypes.UNSIGNED_LONG,
bytesRef -> asLongUnsigned(safeToUnsignedLong(((BytesRef) bytesRef).utf8ToString())),
bytesRef -> safeToUnsignedLong(((BytesRef) bytesRef).utf8ToString()),
List.of()
);
// strings of random doubles outside unsigned_long's range, negative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.function.BiFunction;

import static org.elasticsearch.xpack.ql.util.NumericUtils.asLongUnsigned;
import static org.elasticsearch.xpack.ql.util.NumericUtils.unsignedLongAsBigInteger;
import static org.elasticsearch.xpack.ql.util.NumericUtils.unsignedLongAsNumber;
import static org.elasticsearch.xpack.ql.util.StringUtils.parseIntegral;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -115,6 +116,11 @@ public void testUnsignedLongMultiplyExact() {
assertThat(mulExact("0", "0"), equalTo("0"));
}

public void testRoundTripConversion() {
BigInteger b = randomUnsignedLongBetween(BigInteger.ZERO, UNSIGNED_LONG_MAX);
assertThat(b, equalTo(unsignedLongAsBigInteger(asLongUnsigned(b))));
}

private static String addExact(String x, String y) {
return exactOperation(x, y, NumericUtils::unsignedLongAddExact);
}
Expand Down

0 comments on commit 32b0579

Please sign in to comment.