1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.utils.formatting;
18
19 import org.hipparchus.util.FastMath;
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.Test;
22
23 import java.io.IOException;
24 import java.util.Locale;
25
26 public class FastDoubleFormatterTest {
27
28 @Test
29 public void testRegularPositive() {
30 Assertions.assertEquals(" 1", new FastDoubleFormatter(3, 0).toString(1.2));
31 Assertions.assertEquals("1.2", new FastDoubleFormatter(3, 1).toString(1.2));
32 Assertions.assertEquals(" 1.2", new FastDoubleFormatter(4, 1).toString(1.2));
33 Assertions.assertEquals(" 1.2", new FastDoubleFormatter(5, 1).toString(1.2));
34 Assertions.assertEquals(" 1.200", new FastDoubleFormatter(7, 3).toString(1.2));
35 }
36
37 @Test
38 public void testRegularNegative() {
39 Assertions.assertEquals(" -1", new FastDoubleFormatter(3, 0).toString(-1.2));
40 Assertions.assertEquals("-1.2", new FastDoubleFormatter(3, 1).toString(-1.2));
41 Assertions.assertEquals("-1.2", new FastDoubleFormatter(4, 1).toString(-1.2));
42 Assertions.assertEquals(" -1.2", new FastDoubleFormatter(5, 1).toString(-1.2));
43 Assertions.assertEquals(" -1.200", new FastDoubleFormatter(7, 3).toString(-1.2));
44 }
45
46 @Test
47 public void testSmallNegative() {
48 Assertions.assertEquals(" -0", new FastDoubleFormatter(4, 0).toString(-0.0625));
49 Assertions.assertEquals("-0.1", new FastDoubleFormatter(4, 1).toString(-0.0625));
50 Assertions.assertEquals("-0.06", new FastDoubleFormatter(5, 2).toString(-0.0625));
51 Assertions.assertEquals("-0.063", new FastDoubleFormatter(6, 3).toString(-0.0625));
52 Assertions.assertEquals("-0.0625", new FastDoubleFormatter(7, 4).toString(-0.0625));
53 }
54
55 @Test
56 public void testTrailingZeroPositive() {
57 Assertions.assertEquals("17.0", new FastDoubleFormatter(4, 1).toString(17.0));
58 Assertions.assertEquals("17.00", new FastDoubleFormatter(5, 2).toString(17.0));
59 Assertions.assertEquals("17.000", new FastDoubleFormatter(6, 3).toString(17.0));
60 Assertions.assertEquals("17.0000", new FastDoubleFormatter(7, 4).toString(17.0));
61 Assertions.assertEquals("17.00000", new FastDoubleFormatter(8, 5).toString(17.0));
62 Assertions.assertEquals("17.000000", new FastDoubleFormatter(9, 6).toString(17.0));
63 }
64
65 @Test
66 public void testTrailingZeroNegative() {
67 Assertions.assertEquals("-17.0", new FastDoubleFormatter(4, 1).toString(-17.0));
68 Assertions.assertEquals("-17.00", new FastDoubleFormatter(5, 2).toString(-17.0));
69 Assertions.assertEquals("-17.000", new FastDoubleFormatter(6, 3).toString(-17.0));
70 Assertions.assertEquals("-17.0000", new FastDoubleFormatter(7, 4).toString(-17.0));
71 Assertions.assertEquals("-17.00000", new FastDoubleFormatter(8, 5).toString(-17.0));
72 Assertions.assertEquals("-17.000000", new FastDoubleFormatter(9, 6).toString(-17.0));
73 }
74
75 @Test
76 public void testRoundingPositive() {
77 Assertions.assertEquals(" 1.24", new FastDoubleFormatter(5, 2).toString(1.2401));
78 Assertions.assertEquals(" 1.24", new FastDoubleFormatter(5, 2).toString(1.2399));
79 }
80
81 @Test
82 public void testRoundingNegative() {
83 Assertions.assertEquals("-1.24", new FastDoubleFormatter(5, 2).toString(-1.2401));
84 Assertions.assertEquals("-1.24", new FastDoubleFormatter(5, 2).toString(-1.2399));
85 }
86
87 @Test
88 public void testCarryPositive() {
89 Assertions.assertEquals(" 2.00", new FastDoubleFormatter(5, 2).toString(2.0001));
90 Assertions.assertEquals(" 2.00", new FastDoubleFormatter(5, 2).toString(1.9999));
91 }
92
93 @Test
94 public void testCarryNegative() {
95 Assertions.assertEquals("-2.00", new FastDoubleFormatter(5, 2).toString(-2.0001));
96 Assertions.assertEquals("-2.00", new FastDoubleFormatter(5, 2).toString(-1.9999));
97 }
98
99 @Test
100 public void testSpecialNumbers() {
101 Assertions.assertEquals(" NaN", new FastDoubleFormatter(10, 1).toString(Double.NaN));
102 Assertions.assertEquals(" Infinity", new FastDoubleFormatter(10, 1).toString(Double.POSITIVE_INFINITY));
103 Assertions.assertEquals(" -Infinity", new FastDoubleFormatter(10, 1).toString(Double.NEGATIVE_INFINITY));
104 }
105
106 @Test
107 public void testSignedZero() {
108 Assertions.assertEquals(" 0.0", new FastDoubleFormatter(5, 1).toString(0.0));
109 Assertions.assertEquals(" -0.0", new FastDoubleFormatter(5, 1).toString(-0.0));
110 }
111
112 @Test
113 public void testChainCalls() throws IOException {
114 final StringBuilder builder = new StringBuilder();
115 new FastDoubleFormatter(5, 1).appendTo(builder, 1.2);
116 builder.append(' ');
117 new FastDoubleFormatter(6, 3).appendTo(builder, -3.4);
118 builder.append(' ');
119 new FastDoubleFormatter(3, 0).appendTo(builder, Double.NaN);
120 Assertions.assertEquals(" 1.2 -3.400 NaN", builder.toString());
121 }
122
123 @Test
124 public void testGetters() {
125 Assertions.assertEquals(4, new FastDoubleFormatter(4, 1).getWidth());
126 Assertions.assertEquals(1, new FastDoubleFormatter(4, 1).getPrecision());
127 }
128
129 }