-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Reject out of range numbers for float, double and half_float #25826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2d0e5c1
c0fff6f
6e0a6ea
7d4f315
d1ebd6f
1358fed
44983d8
d072e69
ecf3424
b5d231d
187982a
d236158
7423c4d
8e88c9d
11ce7c8
3902911
656cf63
cd38455
b578b5a
3c666a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,10 +45,14 @@ | |
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class NumberFieldTypeTests extends FieldTypeTestCase { | ||
|
||
NumberType type; | ||
|
@@ -300,8 +304,8 @@ public void testHalfFloatRange() throws IOException { | |
IndexSearcher searcher = newSearcher(reader); | ||
final int numQueries = 1000; | ||
for (int i = 0; i < numQueries; ++i) { | ||
float l = (randomFloat() * 2 - 1) * 70000; | ||
float u = (randomFloat() * 2 - 1) * 70000; | ||
float l = (randomFloat() * 2 - 1) * 65504; | ||
float u = (randomFloat() * 2 - 1) * 65504; | ||
boolean includeLower = randomBoolean(); | ||
boolean includeUpper = randomBoolean(); | ||
Query floatQ = NumberFieldMapper.NumberType.FLOAT.rangeQuery("float", l, u, includeLower, includeUpper, false); | ||
|
@@ -382,4 +386,59 @@ public void doTestDocValueRangeQueries(NumberType type, Supplier<Number> valueSu | |
reader.close(); | ||
dir.close(); | ||
} | ||
|
||
public void testParseOutOfRangeValues() throws IOException { | ||
final List<OutOfRangeSpec<Object>> inputs = Arrays.asList( | ||
OutOfRangeSpec.of(NumberType.HALF_FLOAT, "65520", "[half_float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.FLOAT, "3.4028235E39", "[float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.DOUBLE, "1.7976931348623157E309", "[double] supports only finite values"), | ||
|
||
OutOfRangeSpec.of(NumberType.HALF_FLOAT, 65520f, "[half_float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.FLOAT, 3.4028235E39d, "[float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.DOUBLE, new BigDecimal("1.7976931348623157E309"), "[double] supports only finite values"), | ||
|
||
OutOfRangeSpec.of(NumberType.HALF_FLOAT, -65520f, "[half_float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.FLOAT, -3.4028235E39d, "[float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.DOUBLE, new BigDecimal("-1.7976931348623157E309"), "[double] supports only finite values"), | ||
|
||
OutOfRangeSpec.of(NumberType.HALF_FLOAT, Float.NaN, "[half_float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.FLOAT, Float.NaN, "[float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.DOUBLE, Double.NaN, "[double] supports only finite values"), | ||
|
||
OutOfRangeSpec.of(NumberType.HALF_FLOAT, Float.POSITIVE_INFINITY, "[half_float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.FLOAT, Float.POSITIVE_INFINITY, "[float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.DOUBLE, Double.POSITIVE_INFINITY, "[double] supports only finite values"), | ||
|
||
OutOfRangeSpec.of(NumberType.HALF_FLOAT, Float.NEGATIVE_INFINITY, "[half_float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.FLOAT, Float.NEGATIVE_INFINITY, "[float] supports only finite values"), | ||
OutOfRangeSpec.of(NumberType.DOUBLE, Double.NEGATIVE_INFINITY, "[double] supports only finite values") | ||
); | ||
|
||
for (OutOfRangeSpec<Object> item: inputs) { | ||
try { | ||
item.type.parse(item.value, false); | ||
fail("Parsing exception expected for [" + item.type + "] with value [" + item.value + "]"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat("Incorrect error message for [" + item.type + "] with value [" + item.value + "]", | ||
e.getMessage(), containsString(item.message)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Despite expectThrows is more concise and readable, I think we should keep try/fail/catch/assert because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough |
||
} | ||
} | ||
|
||
static class OutOfRangeSpec<V> { | ||
|
||
final NumberType type; | ||
final V value; | ||
final String message; | ||
|
||
static <V> OutOfRangeSpec<V> of(NumberType t, V v, String m) { | ||
return new OutOfRangeSpec<>(t, v, m); | ||
} | ||
|
||
OutOfRangeSpec(NumberType t, V v, String m) { | ||
type = t; | ||
value = v; | ||
message = m; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this helper method does not look very useful