Skip to content

Commit 2936352

Browse files
committed
DD tests for the documented primitive conversion behaviour
1 parent 1cd5e2c commit 2936352

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

  • commons-numbers-core/src/test/java/org/apache/commons/numbers/core

commons-numbers-core/src/test/java/org/apache/commons/numbers/core/DDTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,54 @@ static Stream<Arguments> testLongIntValue() {
251251
return builder.build();
252252
}
253253

254+
@Test
255+
void testLongValue() {
256+
// Largest long
257+
Assertions.assertEquals(Long.MAX_VALUE, DD.of(Double.POSITIVE_INFINITY).longValue());
258+
Assertions.assertEquals(Long.MAX_VALUE, DD.of(Double.MAX_VALUE).longValue());
259+
Assertions.assertEquals(Long.MAX_VALUE, DD.of(0x1.0p63).longValue());
260+
Assertions.assertEquals(Long.MAX_VALUE, DD.of(Long.MAX_VALUE).longValue());
261+
Assertions.assertEquals(Long.MAX_VALUE - 1, DD.of(Long.MAX_VALUE - 1).longValue());
262+
// Largest negative long
263+
Assertions.assertEquals(Long.MIN_VALUE, DD.of(Double.NEGATIVE_INFINITY).longValue());
264+
Assertions.assertEquals(Long.MIN_VALUE, DD.of(-Double.MAX_VALUE).longValue());
265+
Assertions.assertEquals(Long.MIN_VALUE, DD.of(-0x1.0p63).longValue());
266+
Assertions.assertEquals(Long.MIN_VALUE, DD.of(Long.MIN_VALUE).longValue());
267+
Assertions.assertEquals(Long.MIN_VALUE + 1, DD.of(Long.MIN_VALUE + 1).longValue());
268+
// Nan
269+
Assertions.assertEquals(0, DD.of(Double.NaN).longValue());
270+
// Truncation to zero
271+
for (final double x : new double[] {0.0, 0.5, 2.75, 123.45}) {
272+
final long expected = (long) x;
273+
Assertions.assertEquals(expected, DD.of(x).longValue());
274+
Assertions.assertEquals(-expected, DD.of(-x).longValue());
275+
}
276+
}
277+
278+
@Test
279+
void testIntValue() {
280+
// Largest int
281+
Assertions.assertEquals(Integer.MAX_VALUE, DD.of(Double.POSITIVE_INFINITY).intValue());
282+
Assertions.assertEquals(Integer.MAX_VALUE, DD.of(Double.MAX_VALUE).intValue());
283+
Assertions.assertEquals(Integer.MAX_VALUE, DD.of(0x1.0p31).intValue());
284+
Assertions.assertEquals(Integer.MAX_VALUE, DD.of(Integer.MAX_VALUE).intValue());
285+
Assertions.assertEquals(Integer.MAX_VALUE - 1, DD.of(Integer.MAX_VALUE - 1).intValue());
286+
// Largest negative int
287+
Assertions.assertEquals(Integer.MIN_VALUE, DD.of(Double.NEGATIVE_INFINITY).intValue());
288+
Assertions.assertEquals(Integer.MIN_VALUE, DD.of(-Double.MAX_VALUE).intValue());
289+
Assertions.assertEquals(Integer.MIN_VALUE, DD.of(-0x1.0p31).intValue());
290+
Assertions.assertEquals(Integer.MIN_VALUE, DD.of(Integer.MIN_VALUE).intValue());
291+
Assertions.assertEquals(Integer.MIN_VALUE + 1, DD.of(Integer.MIN_VALUE + 1).intValue());
292+
// Nan
293+
Assertions.assertEquals(0, DD.of(Double.NaN).intValue());
294+
// Truncation to zero
295+
for (final double x : new double[] {0.0, 0.5, 2.75, 123.45}) {
296+
final int expected = (int) x;
297+
Assertions.assertEquals(expected, DD.of(x).intValue());
298+
Assertions.assertEquals(-expected, DD.of(-x).intValue());
299+
}
300+
}
301+
254302
/**
255303
* Test the value is exact after a sum of longs.
256304
* This exercises {@link DD#of(long)}, {@link DD#bigDecimalValue()} and {@link DD#longValue()}.
@@ -283,6 +331,8 @@ static Stream<long[]> testLongSum() {
283331
// Edge cases around min/max long value, including overflow a long then recover
284332
final long min = Long.MIN_VALUE;
285333
final long max = Long.MAX_VALUE;
334+
// Has all bits in the upper 53-bits; then some more in the remaining lower bits.
335+
final long big = 92874 + (long) Math.nextDown(0x1.0p63);
286336
builder.add(new long[] {min, 1});
287337
builder.add(new long[] {min, -1, 1});
288338
builder.add(new long[] {min, -33, 67});
@@ -292,6 +342,9 @@ static Stream<long[]> testLongSum() {
292342
builder.add(new long[] {max, 1, -1});
293343
builder.add(new long[] {max, 13, -14});
294344
builder.add(new long[] {max, max, min});
345+
builder.add(new long[] {big, -1});
346+
builder.add(new long[] {big, 1});
347+
builder.add(new long[] {big, max, -12345, min});
295348
// Random
296349
final UniformRandomProvider rng = createRNG();
297350
for (int i = 1; i < 20; i++) {

0 commit comments

Comments
 (0)