Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test cases from hell
/* Original samples:
Sample 7:
Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
boolean includeLower, boolean includeUpper) {
double l = Double.NEGATIVE_INFINITY;
double u = Double.POSITIVE_INFINITY;
if (lowerTerm != null) {
l = parse(lowerTerm);
if (includeLower == false) {
l = Math.nextUp(l);
}
}
if (upperTerm != null) {
u = parse(upperTerm);
if (includeUpper == false) {
u = Math.nextDown(u);
}
}
return DoublePoint.newRangeQuery(field, l, u);
}
Sample 8:
public void testIgnoreUnknownFields() throws IOException {
XContentParser parser = XContentType.JSON.xContent().createParser(
"{\n"
+ " \"test\" : \"foo\",\n"
+ " \"junk\" : 2\n"
+ "}");
class TestStruct {
public final String test;
public TestStruct(String test) {
this.test = test;
}
}
ConstructingObjectParser<TestStruct, ParseFieldMatcherSupplier> objectParser = new ConstructingObjectParser<>("foo", true, a ->
new TestStruct((String) a[0]));
objectParser.declareString(constructorArg(), new ParseField("test"));
TestStruct s = objectParser.apply(parser, MATCHER);
assertEquals(s.test, "foo");
}
*/
public class Sample7 {
private static final String testField = "ououiau";
private static class Query {
private double l;
private double u;
public Query(double l, double u) {
this.l = l;
this.u = u;
}
}
private static class DoublePoint {
private static Query newRangeQuery(String field, double l, double u) {
assert testField.equals(field);
return new Query(l, u);
}
}
private static final double testNextUpRes = 37535.573753;
private static final double testNextDownRes = 35313177.753;
private static class Math {
public static double nextUp(double a) {
assert a == testLowerTermRes;
return testNextUpRes;
}
public static double nextDown(double a) {
assert a == testUpperTermRes;
return testNextDownRes;
}
}
private final static Object testLowerTerm = new Object();
private final static Object testUpperTerm = new Object();
private final static double testLowerTermRes = 75339.3;
private final static double testUpperTermRes = 539105.642;
public Double parse(Object o) {
if (o == testLowerTerm) return testLowerTermRes;
if (o == testUpperTerm) return testUpperTermRes;
return null;
}
Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
boolean includeLower, boolean includeUpper) {
double l = Double.NEGATIVE_INFINITY;
double u = Double.POSITIVE_INFINITY;
if (lowerTerm != null) {
l = parse(lowerTerm);
if (includeLower == false) {
l = Math.nextUp(l);
}
}
if (upperTerm != null) {
u = parse(upperTerm);
if (includeUpper == false) {
u = Math.nextDown(u);
}
}
return DoublePoint.newRangeQuery(field, l, u);
}
public static void main(String[] args) {
Sample7 s = new Sample7();
Query x;
x = s.rangeQuery(testField, null, null, true, true);
assert x.l == Double.NEGATIVE_INFINITY;
assert x.u == Double.POSITIVE_INFINITY;
x = s.rangeQuery(testField, testLowerTerm, null, true, true);
assert x.l == testLowerTermRes;
assert x.u == Double.POSITIVE_INFINITY;
x = s.rangeQuery(testField, testLowerTerm, null, false, true);
assert x.l == testNextUpRes;
assert x.u == Double.POSITIVE_INFINITY;
x = s.rangeQuery(testField, null, testUpperTerm, true, true);
assert x.l == Double.NEGATIVE_INFINITY;
assert x.u == testUpperTermRes;
x = s.rangeQuery(testField, testLowerTerm, testUpperTerm, true, true);
assert x.l == testLowerTermRes;
assert x.u == testUpperTermRes;
x = s.rangeQuery(testField, testLowerTerm, testUpperTerm, false, true);
assert x.l == testNextUpRes;
assert x.u == testUpperTermRes;
x = s.rangeQuery(testField, null, testUpperTerm, true, false);
assert x.l == Double.NEGATIVE_INFINITY;
assert x.u == testNextDownRes;
x = s.rangeQuery(testField, testLowerTerm, testUpperTerm, true, false);
assert x.l == testLowerTermRes;
assert x.u == testNextDownRes;
x = s.rangeQuery(testField, testLowerTerm, testUpperTerm, false, false);
assert x.l == testNextUpRes;
assert x.u == testNextDownRes;
}
}
import java.io.*;
import java.util.*;
import java.util.function.*;
public class Sample8 {
private static int lineNo;
private static class XContentParser {}
private static final XContentParser testParser = new XContentParser();
private static class XContentType {
private static class JSON {
private static XContent xContent() {
assert lineNo == 0;
lineNo++;
return new XContent();
}
}
}
private static class XContent {
private XContentParser createParser(String s) {
assert lineNo == 1;
lineNo++;
assert s.equals( "{\n"+ " \"test\" : \"foo\",\n"+ " \"junk\" : 2\n"+ "}");
return testParser;
}
}
private static class ConstructingObjectParser<A, B> {
private Function<String[], A> f;
public ConstructingObjectParser(String x, boolean b, Function<String[], A> f) {
assert lineNo == 2;
lineNo++;
assert "foo".equals(x);
assert b == true;
this.f = f;
}
public void declareString(Object a, ParseField p) {
assert lineNo == 3;
lineNo++;
assert a == testConstructorArg;
assert p.s.equals("test");
}
public A apply(XContentParser p, B matcher) {
assert lineNo == 4;
lineNo++;
assert matcher == MATCHER;
assert p == testParser;
return f.apply(new String[]{"foo"});
}
}
private static class ParseField {
private String s;
public ParseField(String s) {
this.s = s;
}
}
private static Object testConstructorArg = new Object();
private static Object constructorArg() { return testConstructorArg; }
private static class ParseFieldMatcherSupplier {}
private final static ParseFieldMatcherSupplier MATCHER = new ParseFieldMatcherSupplier();
private static void assertEquals(String a, String b) {
assert lineNo == 5;
lineNo++;
assert "foo".equals(a);
assert "foo".equals(b);
}
public void testIgnoreUnknownFields() throws IOException {
XContentParser parser = XContentType.JSON.xContent().createParser(
"{\n"
+ " \"test\" : \"foo\",\n"
+ " \"junk\" : 2\n"
+ "}");
class TestStruct {
public final String test;
public TestStruct(String test) {
this.test = test;
}
}
ConstructingObjectParser<TestStruct, ParseFieldMatcherSupplier> objectParser = new ConstructingObjectParser<>("foo", true, a ->
new TestStruct((String) a[0]));
objectParser.declareString(constructorArg(), new ParseField("test"));
TestStruct s = objectParser.apply(parser, MATCHER);
assertEquals(s.test, "foo");
}
public static void main(String[] args) throws Exception {
new Sample8().testIgnoreUnknownFields();
assert lineNo == 6;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment