1   /* Copyright 2002-2025 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.utils;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  
22  public class AccurateFormatterTest {
23  
24      @Test
25      public void testNumberStatic() {
26          // these tests come from the RyuDouble tests
27          Assertions.assertEquals("4.940656E-318",          AccurateFormatter.format( 4.940656E-318d));
28          Assertions.assertEquals("1.18575755E-316",        AccurateFormatter.format( 1.18575755E-316d));
29          Assertions.assertEquals("2.989102097996E-312",    AccurateFormatter.format( 2.989102097996E-312d));
30          Assertions.assertEquals("9.0608011534336E15",     AccurateFormatter.format( 9.0608011534336E15d));
31          Assertions.assertEquals("4.708356024711512E18",   AccurateFormatter.format( 4.708356024711512E18));
32          Assertions.assertEquals("9.409340012568248E18",   AccurateFormatter.format( 9.409340012568248E18));
33          Assertions.assertEquals("1.8531501765868567E21",  AccurateFormatter.format( 1.8531501765868567E21));
34          Assertions.assertEquals("-3.347727380279489E33",  AccurateFormatter.format(-3.347727380279489E33));
35          Assertions.assertEquals("-6.9741824662760956E19", AccurateFormatter.format(-6.9741824662760956E19));
36          Assertions.assertEquals("4.3816050601147837E18",  AccurateFormatter.format( 4.3816050601147837E18));
37      }
38  
39      @Test
40      public void testDateNonTruncatedStatic() {
41          Assertions.assertEquals("2021-03-26T09:45:32.4576",
42                  AccurateFormatter.format(2021, 3, 26, 9, 45, 32.4576));
43          Assertions.assertEquals("2021-03-26T09:45:00.00000000000001",
44                  AccurateFormatter.format(2021, 3, 26, 9, 45, 1.0e-14));
45      }
46  
47      @Test
48      public void testDateTruncatedStatic() {
49          Assertions.assertEquals("2021-03-26T09:45:00.0",
50                  AccurateFormatter.format(2021, 3, 26, 9, 45, 1.0e-16));
51      }
52  
53      @Test
54      public void testNumber() {
55          // these tests come from the RyuDouble tests
56          Assertions.assertEquals("4.940656E-318",          new AccurateFormatter().toString( 4.940656E-318d));
57          Assertions.assertEquals("1.18575755E-316",        new AccurateFormatter().toString( 1.18575755E-316d));
58          Assertions.assertEquals("2.989102097996E-312",    new AccurateFormatter().toString( 2.989102097996E-312d));
59          Assertions.assertEquals("9.0608011534336E15",     new AccurateFormatter().toString( 9.0608011534336E15d));
60          Assertions.assertEquals("4.708356024711512E18",   new AccurateFormatter().toString( 4.708356024711512E18d));
61          Assertions.assertEquals("9.409340012568248E18",   new AccurateFormatter().toString( 9.409340012568248E18d));
62          Assertions.assertEquals("1.8531501765868567E21",  new AccurateFormatter().toString( 1.8531501765868567E21d));
63          Assertions.assertEquals("-3.347727380279489E33",  new AccurateFormatter().toString(-3.347727380279489E33d));
64          Assertions.assertEquals("-6.9741824662760956E19", new AccurateFormatter().toString(-6.9741824662760956E19d));
65          Assertions.assertEquals("4.3816050601147837E18",  new AccurateFormatter().toString( 4.3816050601147837E18d));
66      }
67  
68      @Test
69      public void testDateNonTruncated() {
70          Assertions.assertEquals("2021-03-26T09:45:32.4576",
71                              new AccurateFormatter().toString(2021, 3, 26, 9, 45, 32.4576d));
72          Assertions.assertEquals("2021-03-26T09:45:00.00000000000001",
73                              new AccurateFormatter().toString(2021, 3, 26, 9, 45, 1.0e-14d));
74      }
75  
76      @Test
77      public void testDateTruncated() {
78          Assertions.assertEquals("2021-03-26T09:45:00.0",
79                              new AccurateFormatter().toString(2021, 3, 26, 9, 45, 1.0e-16d));
80      }
81  
82      /**
83       * This test contains edge cases that are or would result in invalid date times.
84       * This demonstrates that the formatter will NOT catch/correct any invalid inputs and shows how the invalid inputs will
85       * be formatted.
86       */
87      @Test
88      public void testInvalidEdgeCases() {
89          Assertions.assertEquals("2021-03-26T09:45:59.99999999999999",
90                  new AccurateFormatter().toString(2021, 3, 26, 9, 45, Math.nextDown(60.0)));
91          Assertions.assertEquals("20210-300-260T900:450:600.0",
92                  new AccurateFormatter().toString(20210, 300, 260, 900, 450, 600.0));
93          Assertions.assertEquals("-2021--3--26T-9:-45:00.0",
94                  new AccurateFormatter().toString(-2021, -3, -26, -9, -45, -1.0));
95      }
96  
97  }
98