1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.imaging.common;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.text.NumberFormat;
24 import java.util.Arrays;
25 import java.util.stream.Stream;
26
27 import org.apache.commons.imaging.AbstractImagingTest;
28 import org.apache.commons.imaging.internal.Debug;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.params.ParameterizedTest;
31 import org.junit.jupiter.params.provider.MethodSource;
32
33 public class RationalNumberTest extends AbstractImagingTest {
34
35 public static Stream<Double> data() {
36 return Arrays.asList(0d,
37 0.1d,
38 0.01d,
39 0.001d,
40 0.0001d,
41 0.00001d,
42 0.000001d,
43 0.0000001d,
44 0.123456789d,
45 1d,
46 0.9d,
47 1.1d,
48 2.0d,
49 123.123d,
50 123123123.123d,
51
52 (double) 123 / 45678,
53 (double) 12 / 34,
54 (double) 1 / 2,
55 (double) 1 / 3,
56 (double) 1 / 4,
57 (double) 1 / 5,
58 (double) 1 / 6,
59 (double) 1 / 7,
60 (double) 1 / 8,
61 (double) 1 / 9,
62 (double) 3 / 2,
63 (double) 2 / 3,
64 (double) 3 / 4,
65 (double) 4 / 5,
66 (double) 5 / 6,
67 (double) 6 / 7,
68 (double) 7 / 8,
69 (double) 8 / 9,
70 (double) 3 / 5,
71 (double) 3 / 6,
72 (double) 3 / 7,
73 (double) 3 / 8,
74 (double) 3 / 9,
75
76 -0.1d,
77 -0.01d,
78 -0.001d,
79 -0.0001d,
80 -0.00001d,
81 -0.000001d,
82 -0.0000001d,
83 -0.123456789d,
84 -1d,
85 -0.9d,
86 -1.1d,
87 -2.0d,
88 -123.123d,
89 -123123123.123d,
90
91 34d,
92
93 (double) Integer.MAX_VALUE,
94 Integer.MAX_VALUE + 0.1,
95 Integer.MAX_VALUE - 0.1,
96 (double) -Integer.MAX_VALUE,
97 -(Integer.MAX_VALUE + 0.1),
98 -(Integer.MAX_VALUE - 0.1),
99
100 (double) Long.MAX_VALUE,
101 Long.MAX_VALUE + 0.1,
102 Long.MAX_VALUE - 0.1,
103 (double) -Long.MAX_VALUE,
104 -(Long.MAX_VALUE + 0.1),
105 -(Long.MAX_VALUE - 0.1)
106 ).stream();
107 }
108
109 @ParameterizedTest
110 @MethodSource("data")
111 public void testRationalNumber(final double testValue) {
112 final RationalNumber rational = RationalNumber.valueOf(testValue);
113 final double difference = Math.abs(testValue - rational.doubleValue());
114
115 final NumberFormat nf = NumberFormat.getInstance();
116 nf.setMaximumFractionDigits(15);
117
118
119
120 Debug.debug("value: " + nf.format(testValue));
121 Debug.debug("rational: " + rational);
122 Debug.debug("difference: " + difference);
123 Debug.debug();
124 }
125
126 @Test
127 public void testSpecialRationalNumber() {
128 final RationalNumber test = new RationalNumber(0xF5937B1F, 70_000_000, true);
129 assertEquals(58.858331871428570, test.doubleValue(), 1.0e-14, "Unsigned integer support failed for double conversion");
130 assertEquals(58.858334f, test.floatValue(), 1.0e-6f, "Float conversion failed");
131 assertEquals(58L, test.longValue(), "Long value conversion failed");
132 assertEquals(58, test.intValue(), "Int value conversion failed");
133 assertThrows(NumberFormatException.class, () -> test.negate(), "Failed to detect negation of large unsigned value");
134
135 }
136
137 }