-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericTest.java
47 lines (37 loc) · 1.64 KB
/
NumericTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.skraba.byexample.junit5;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notANumber;
import org.junit.jupiter.api.Test;
/** Simple assertions on numeric primitives. */
class NumericTest {
@Test
void testBasicDouble() {
double piApprox = 355d / 113d;
assertThat(piApprox, not(closeTo(Math.PI, 0.0000002)));
assertThat(piApprox, closeTo(Math.PI, 0.0000003));
}
@Test
void testBasicDoubleNaN() {
double canonicalNaN = Double.NaN;
assertThat(canonicalNaN, notANumber());
//noinspection ConstantConditions
assertThat(canonicalNaN == canonicalNaN, is(false));
//noinspection ConstantConditions
assertThat(Double.valueOf(canonicalNaN).equals(canonicalNaN), is(true));
long longCanonicalNaN = Double.doubleToLongBits(canonicalNaN);
assertThat(longCanonicalNaN, is(Double.doubleToRawLongBits(canonicalNaN)));
assertThat(canonicalNaN, equalTo(canonicalNaN));
assertThat(canonicalNaN, is(Double.longBitsToDouble(longCanonicalNaN)));
assertThat(Double.doubleToLongBits(canonicalNaN), is(longCanonicalNaN));
// There are other NaNs
double otherNaN = Double.longBitsToDouble(longCanonicalNaN + 1);
assertThat(otherNaN, notANumber());
assertThat(otherNaN, is(canonicalNaN));
assertThat(Double.doubleToLongBits(otherNaN), is(Double.doubleToLongBits(canonicalNaN)));
assertThat(Double.doubleToRawLongBits(otherNaN), not(Double.doubleToRawLongBits(canonicalNaN)));
}
}