Skip to content

Commit

Permalink
Move to ESTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
nik9000 committed Sep 12, 2023
1 parent 74cb9b4 commit ee05f8f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,30 @@ public static long randomLongBetween(long min, long max) {
return RandomNumbers.randomLongBetween(random(), min, max);
}

/**
* The maximum value that can be represented as an unsigned long.
*/
public static final BigInteger UNSIGNED_LONG_MAX = BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE);

/**
* A unsigned long in a {@link BigInteger} between min (inclusive) and max (inclusive).
*/
public static BigInteger randomUnsignedLongBetween(BigInteger min, BigInteger max) {
if (min.compareTo(BigInteger.ZERO) < 0) {
throw new IllegalArgumentException("Must be between [0] and [" + UNSIGNED_LONG_MAX + "]");
}
if (0 < max.compareTo(UNSIGNED_LONG_MAX)) {
throw new IllegalArgumentException("Must be between [0] and [" + UNSIGNED_LONG_MAX + "]");
}
// Shift the min and max down into the long range
long minShifted = min.add(BigInteger.valueOf(Long.MIN_VALUE)).longValueExact();
long maxShifted = max.add(BigInteger.valueOf(Long.MIN_VALUE)).longValueExact();
// Grab a random number in that range
long randomShifted = randomLongBetween(minShifted, maxShifted);
// Shift back up into long range
return BigInteger.valueOf(randomShifted).subtract(BigInteger.valueOf(Long.MIN_VALUE));
}

/**
* Returns a "scaled" number of iterations for loops which can have a variable
* iteration count. This method is effectively
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.AssumptionViolatedException;

import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
Expand All @@ -37,12 +38,14 @@

import javax.crypto.KeyGenerator;

import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assume.assumeThat;
Expand Down Expand Up @@ -358,4 +361,25 @@ public void testFipsKeyGenWithNonFipsSecureRandom() {
assertThat(t.getClass().getCanonicalName(), is(equalTo("org.bouncycastle.crypto.fips.FipsUnapprovedOperationError")));
assertThat(t.getMessage(), is(equalTo("Attempt to create key with unapproved RNG: AES")));
}

public void testRandomUnsignedLongBetween() {
assertThat(
randomUnsignedLongBetween(BigInteger.ZERO, UNSIGNED_LONG_MAX),
both(greaterThanOrEqualTo(BigInteger.ZERO)).and(lessThanOrEqualTo(UNSIGNED_LONG_MAX))
);

BigInteger from = BigInteger.valueOf(randomLong()).subtract(BigInteger.valueOf(Long.MIN_VALUE));
BigInteger to = BigInteger.valueOf(randomLong()).subtract(BigInteger.valueOf(Long.MIN_VALUE));
if (from.compareTo(to) > 0) {
BigInteger s = from;
from = to;
to = s;
}
assertThat(randomUnsignedLongBetween(from, to), both(greaterThanOrEqualTo(from)).and(lessThanOrEqualTo(to)));
}

public void testRandomUnsignedLongBetweenDegenerate() {
BigInteger target = BigInteger.valueOf(randomLong()).subtract(BigInteger.valueOf(Long.MIN_VALUE));
assertThat(randomUnsignedLongBetween(target, target), equalTo(target));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
public record TestCaseSupplier(String name, List<DataType> types, Supplier<TestCase> supplier)
implements
Supplier<TestCaseSupplier.TestCase> {

public static final BigInteger MAX_UNSIGNED_LONG = NumericUtils.UNSIGNED_LONG_MAX;
/**
* Build a test case without types.
*
Expand Down Expand Up @@ -494,26 +492,16 @@ private static List<Map.Entry<String, Supplier<Object>>> ulongCases(BigInteger m
BigInteger lower1 = min.max(BigInteger.ONE);
BigInteger upper1 = max.min(BigInteger.valueOf(Long.MAX_VALUE));
if (lower1.compareTo(upper1) < 0) {
cases.add(
Map.entry(
"<small unsigned long>",
() -> BigInteger.valueOf(ESTestCase.randomLongBetween(lower1.longValue(), upper1.longValue()))
)
);
cases.add(Map.entry("<small unsigned long>", () -> ESTestCase.randomUnsignedLongBetween(lower1, upper1)));
} else if (lower1.compareTo(upper1) == 0) {
cases.add(Map.entry("<small unsigned long>", () -> lower1));
}

// Big values, greater than Long.MAX_VALUE
BigInteger lower2 = min.max(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE));
BigInteger upper2 = max.min(MAX_UNSIGNED_LONG);
BigInteger upper2 = max.min(ESTestCase.UNSIGNED_LONG_MAX);
if (lower2.compareTo(upper2) < 0) {
cases.add(
Map.entry(
"<big unsigned long>",
() -> lower2.add(BigInteger.valueOf(ESTestCase.randomLongBetween(0, upper2.subtract(lower2).longValue())))
)
);
cases.add(Map.entry("<big unsigned long>", () -> ESTestCase.randomUnsignedLongBetween(lower2, upper2)));
} else if (lower2.compareTo(upper2) == 0) {
cases.add(Map.entry("<big unsigned long>", () -> lower2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import java.util.List;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier.MAX_UNSIGNED_LONG;

public class ToStringTests extends AbstractFunctionTestCase {
public ToStringTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
this.testCase = testCaseSupplier.get();
Expand Down Expand Up @@ -60,7 +58,7 @@ public static Iterable<Object[]> parameters() {
DataTypes.KEYWORD,
ul -> new BytesRef(ul.toString()),
BigInteger.ZERO,
MAX_UNSIGNED_LONG,
UNSIGNED_LONG_MAX,
List.of()
);
TestCaseSupplier.forUnaryDouble(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.List;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier.MAX_UNSIGNED_LONG;

public class FloorTests extends AbstractFunctionTestCase {
public FloorTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
this.testCase = testCaseSupplier.get();
Expand All @@ -41,7 +39,7 @@ public static Iterable<Object[]> parameters() {
DataTypes.UNSIGNED_LONG,
ul -> NumericUtils.asLongUnsigned(ul),
BigInteger.ZERO,
MAX_UNSIGNED_LONG,
UNSIGNED_LONG_MAX,
List.of()
);
TestCaseSupplier.forUnaryDouble(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.List;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier.MAX_UNSIGNED_LONG;

public class Log10Tests extends AbstractFunctionTestCase {
public Log10Tests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
this.testCase = testCaseSupplier.get();
Expand Down Expand Up @@ -58,7 +56,7 @@ public static Iterable<Object[]> parameters() {
DataTypes.DOUBLE,
ul -> Math.log10(ul == null ? null : NumericUtils.unsignedLongToDouble(NumericUtils.asLongUnsigned(ul))),
BigInteger.ONE,
MAX_UNSIGNED_LONG,
UNSIGNED_LONG_MAX,
List.of()
);
TestCaseSupplier.forUnaryDouble(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.List;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier.MAX_UNSIGNED_LONG;

public class SqrtTests extends AbstractFunctionTestCase {
public SqrtTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
this.testCase = testCaseSupplier.get();
Expand Down Expand Up @@ -57,7 +55,7 @@ public static Iterable<Object[]> parameters() {
DataTypes.DOUBLE,
ul -> Math.sqrt(ul == null ? null : NumericUtils.unsignedLongToDouble(NumericUtils.asLongUnsigned(ul))),
BigInteger.ZERO,
MAX_UNSIGNED_LONG,
UNSIGNED_LONG_MAX,
List.of()
);
TestCaseSupplier.forUnaryDouble(
Expand Down

0 comments on commit ee05f8f

Please sign in to comment.