|
22 | 22 | import static org.junit.jupiter.api.Assertions.assertNull; |
23 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 24 |
|
| 25 | +import java.math.BigDecimal; |
| 26 | +import java.math.BigInteger; |
25 | 27 | import java.text.DecimalFormatSymbols; |
26 | 28 | import java.util.Locale; |
27 | 29 |
|
@@ -132,6 +134,26 @@ void testDoubleRangeMinMaxNaN() { |
132 | 134 | assertFalse(validator.maxValue(Double.NaN, 20), "maxValue() NaN"); |
133 | 135 | } |
134 | 136 |
|
| 137 | + /** |
| 138 | + * Test the {@link Number} range checks against a bound that carries more precision than a {@code double}. |
| 139 | + * {@code 2^53} is the largest integer with an exact {@code double} representation, so {@code 2^53} + 1 cannot be narrowed |
| 140 | + * onto the value: a value of {@code 2^53} is below a minimum of {@code 2^53 + 1} and above a maximum of {@code 2^53 - 0.5}. |
| 141 | + */ |
| 142 | + @Test |
| 143 | + void testDoubleNumberRangeExactBound() { |
| 144 | + final DoubleValidator validator = (DoubleValidator) strictValidator; |
| 145 | + final long maxExactInt = 1L << 53; // 2^53 |
| 146 | + final Double value = Double.valueOf(maxExactInt); |
| 147 | + final BigInteger above = BigInteger.valueOf(maxExactInt).add(BigInteger.ONE); // 2^53 + 1 |
| 148 | + final BigInteger below = BigInteger.valueOf(maxExactInt).subtract(BigInteger.ONE); // 2^53 - 1 |
| 149 | + final BigDecimal justBelow = BigDecimal.valueOf(maxExactInt).subtract(BigDecimal.valueOf(0.5)); // 2^53 - 0.5 |
| 150 | + assertFalse(validator.minValue(value, above), "minValue() bound above value"); |
| 151 | + assertTrue(validator.minValue(value, below), "minValue() bound below value"); |
| 152 | + assertFalse(validator.maxValue(value, justBelow), "maxValue() bound below value"); |
| 153 | + assertTrue(validator.maxValue(value, above), "maxValue() bound above value"); |
| 154 | + assertFalse(validator.isInRange(value, above, above.add(BigInteger.ONE)), "isInRange() value below range"); |
| 155 | + } |
| 156 | + |
135 | 157 | /** |
136 | 158 | * Test DoubleValidator validate Methods |
137 | 159 | */ |
|
0 commit comments