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.data;
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.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.util.Arrays;
26  
27  public class PolynomialParserTest {
28  
29      @Test
30      public void testEmptyData() {
31          double[] coefficients =
32                  new PolynomialParser('x', PolynomialParser.Unit.NO_UNITS).parse("");
33          Assertions.assertNull(coefficients);
34      }
35  
36      @Test
37      public void testConstant() {
38          double[] coefficients =
39                  new PolynomialParser('x', PolynomialParser.Unit.NO_UNITS).parse("3");
40          Assertions.assertEquals(1, coefficients.length);
41          Assertions.assertEquals(3, coefficients[0], 1.0e-14);
42      }
43  
44      @Test
45      public void testHighDegreeMonomialSuperscripts() {
46          double[] coefficients =
47                  new PolynomialParser('\u03c4', PolynomialParser.Unit.NO_UNITS).parse(" −1234.567″ × τ⁴⁵");
48          Assertions.assertEquals(46, coefficients.length);
49          Assertions.assertEquals(FastMath.toRadians(-1234.567 / 3600), coefficients[45], 1.0e-14);
50          for (int i = 0; i < coefficients.length - 1; ++i) {
51              Assertions.assertEquals(0.0, coefficients[i], 1.0e-14);
52          }
53      }
54  
55      @Test
56      public void testHighDegreeMonomialCaret() {
57          double[] coefficients =
58                  new PolynomialParser('\u03c4', PolynomialParser.Unit.NO_UNITS).parse(" −1234.567″ × τ^45");
59          Assertions.assertEquals(46, coefficients.length);
60          Assertions.assertEquals(FastMath.toRadians(-1234.567 / 3600), coefficients[45], 1.0e-14);
61          for (int i = 0; i < coefficients.length - 1; ++i) {
62              Assertions.assertEquals(0.0, coefficients[i], 1.0e-14);
63          }
64      }
65  
66      @Test
67      public void testDefaultRadians() {
68          double[] coefficients =
69                  new PolynomialParser('a', PolynomialParser.Unit.RADIANS).parse("1 − 1 × a");
70          Assertions.assertEquals(2, coefficients.length);
71          Assertions.assertEquals(+1.0, coefficients[0], 1.0e-14);
72          Assertions.assertEquals(-1.0, coefficients[1], 1.0e-14);
73      }
74  
75      @Test
76      public void testDefaultDegrees() {
77          double[] coefficients =
78                  new PolynomialParser('a', PolynomialParser.Unit.DEGREES).parse("1 − 1 × a");
79          Assertions.assertEquals(2, coefficients.length);
80          Assertions.assertEquals(FastMath.toRadians(+1.0), coefficients[0], 1.0e-14);
81          Assertions.assertEquals(FastMath.toRadians(-1.0), coefficients[1], 1.0e-14);
82      }
83  
84      @Test
85      public void testDefaultArcSeconds() {
86          double[] coefficients =
87                  new PolynomialParser('a', PolynomialParser.Unit.ARC_SECONDS).parse("1 − 1 × a");
88          Assertions.assertEquals(2, coefficients.length);
89          Assertions.assertEquals(FastMath.toRadians(+1.0 / 3600.0), coefficients[0], 1.0e-14);
90          Assertions.assertEquals(FastMath.toRadians(-1.0 / 3600.0), coefficients[1], 1.0e-14);
91      }
92  
93      @Test
94      public void testDefaultMilliArcSeconds() {
95          double[] coefficients =
96                  new PolynomialParser('a', PolynomialParser.Unit.MILLI_ARC_SECONDS).parse("1 − 1 × a");
97          Assertions.assertEquals(2, coefficients.length);
98          Assertions.assertEquals(FastMath.toRadians(+1.0 / 3600000.0), coefficients[0], 1.0e-14);
99          Assertions.assertEquals(FastMath.toRadians(-1.0 / 3600000.0), coefficients[1], 1.0e-14);
100     }
101 
102     @Test
103     public void testDefaultMicroArcSeconds() {
104         double[] coefficients =
105                 new PolynomialParser('a', PolynomialParser.Unit.MICRO_ARC_SECONDS).parse("1 − 1 × a");
106         Assertions.assertEquals(2, coefficients.length);
107         Assertions.assertEquals(FastMath.toRadians(+1.0 / 3600000000.0), coefficients[0], 1.0e-14);
108         Assertions.assertEquals(FastMath.toRadians(-1.0 / 3600000000.0), coefficients[1], 1.0e-14);
109     }
110 
111     @Test
112     public void testDefaultNoUnits() {
113         double[] coefficients =
114                 new PolynomialParser('a', PolynomialParser.Unit.NO_UNITS).parse("1 − 1 × a");
115         Assertions.assertEquals(2, coefficients.length);
116         Assertions.assertEquals(+1.0, coefficients[0], 1.0e-14);
117         Assertions.assertEquals(-1.0, coefficients[1], 1.0e-14);
118     }
119 
120     @Test
121     public void testExplicitEmbeddedUnits() {
122         for (final PolynomialParser.Unit defaultUnit : PolynomialParser.Unit.values()) {
123             double[] coefficients = new PolynomialParser('a', defaultUnit).parse("1°.25 − 1″.2 × a");
124             Assertions.assertEquals(2, coefficients.length);
125             Assertions.assertEquals(FastMath.toRadians(+1.25), coefficients[0], 1.0e-14);
126             Assertions.assertEquals(FastMath.toRadians(-1.2 / 3600.0), coefficients[1], 1.0e-14);
127         }
128     }
129 
130     @Test
131     public void testExplicitAppendedUnits() {
132         for (final PolynomialParser.Unit defaultUnit : PolynomialParser.Unit.values()) {
133             double[] coefficients = new PolynomialParser('a', defaultUnit).parse("1° − 1″ × a");
134             Assertions.assertEquals(2, coefficients.length);
135             Assertions.assertEquals(FastMath.toRadians(+1.0), coefficients[0], 1.0e-14);
136             Assertions.assertEquals(FastMath.toRadians(-1.0 / 3600.0), coefficients[1], 1.0e-14);
137         }
138     }
139 
140     @Test
141     public void testIERSData1() {
142         PolynomialParser parser = new PolynomialParser('t', PolynomialParser.Unit.MICRO_ARC_SECONDS);
143         double[] coefficients = parser.parse("125.04455501° − 6962890.5431″t + 7.4722″t² + 0.007702″t³ − 0.00005939″t⁴");
144         Assertions.assertEquals(5, coefficients.length);
145         Assertions.assertEquals(FastMath.toRadians(125.04455501), coefficients[0], 1.0e-14);
146         Assertions.assertEquals(FastMath.toRadians(-6962890.5431 / 3600), coefficients[1], 1.0e-14);
147         Assertions.assertEquals(FastMath.toRadians(7.4722 / 3600), coefficients[2], 1.0e-14);
148         Assertions.assertEquals(FastMath.toRadians(0.007702 / 3600), coefficients[3], 1.0e-14);
149         Assertions.assertEquals(FastMath.toRadians(-0.00005939 / 3600.0), coefficients[4], 1.0e-14);
150     }
151 
152     @Test
153     public void testIERSData2() {
154         PolynomialParser parser = new PolynomialParser('t', PolynomialParser.Unit.ARC_SECONDS);
155         double[] coefficients = parser.parse("0.02438175 × t + 0.00000538691 × t²");
156         Assertions.assertEquals(3, coefficients.length);
157         Assertions.assertEquals(FastMath.toRadians(0), coefficients[0], 1.0e-14);
158         Assertions.assertEquals(FastMath.toRadians(0.02438175 / 3600), coefficients[1], 1.0e-14);
159         Assertions.assertEquals(FastMath.toRadians(0.00000538691 / 3600.0), coefficients[2], 1.0e-14);
160     }
161 
162     @Test
163     public void testIERSData3() {
164         PolynomialParser parser = new PolynomialParser('t', PolynomialParser.Unit.MICRO_ARC_SECONDS);
165         double[] coefficients = parser.parse("0''.014506 + 4612''.15739966t + 1''.39667721t^2 - 0''.00009344t^3 + 0''.00001882t^4");
166         Assertions.assertEquals(5, coefficients.length);
167         Assertions.assertEquals(FastMath.toRadians(0.014506 / 3600), coefficients[0], 1.0e-14);
168         Assertions.assertEquals(FastMath.toRadians(4612.15739966 / 3600), coefficients[1], 1.0e-14);
169         Assertions.assertEquals(FastMath.toRadians(1.39667721 / 3600), coefficients[2], 1.0e-14);
170         Assertions.assertEquals(FastMath.toRadians(-0.00009344 / 3600), coefficients[3], 1.0e-14);
171         Assertions.assertEquals(FastMath.toRadians(0.00001882 / 3600.0), coefficients[4], 1.0e-14);
172     }
173 
174     @Test
175     public void testIERSData4() {
176         PolynomialParser parser = new PolynomialParser('t', PolynomialParser.Unit.MICRO_ARC_SECONDS);
177         double[] coefficients = parser.parse("-16616.99 + 2004191742.88 t - 427219.05 t^2 - 198620.54 t^3 - 46.05 t^4 + 5.98 t^5");
178         Assertions.assertEquals(6, coefficients.length);
179         Assertions.assertEquals(FastMath.toRadians(-16616.99 / 3600000000.0), coefficients[0], 1.0e-14);
180         Assertions.assertEquals(FastMath.toRadians(2004191742.88 / 3600000000.0), coefficients[1], 1.0e-14);
181         Assertions.assertEquals(FastMath.toRadians(-427219.05 / 3600000000.0), coefficients[2], 1.0e-14);
182         Assertions.assertEquals(FastMath.toRadians(-198620.54 / 3600000000.0), coefficients[3], 1.0e-14);
183         Assertions.assertEquals(FastMath.toRadians(-46.05 / 3600000000.0), coefficients[4], 1.0e-14);
184         Assertions.assertEquals(FastMath.toRadians(5.98 / 3600000000.0), coefficients[5], 1.0e-14);
185     }
186 
187     @Test
188     public void testEnum() throws NoSuchMethodException, SecurityException,
189                                   IllegalAccessException, IllegalArgumentException,
190                                   InvocationTargetException {
191         Class<?> e = null;
192         for (final Class<?> c : PolynomialParser.class.getDeclaredClasses()) {
193             if (c.getName().endsWith("Unit")) {
194                 e = c;
195             }
196         }
197         Method m = e.getDeclaredMethod("valueOf", String.class);
198         m.setAccessible(true);
199         for (String n : Arrays.asList("RADIANS", "DEGREES", "ARC_SECONDS",
200                                       "MILLI_ARC_SECONDS", "MICRO_ARC_SECONDS", "NO_UNITS")) {
201             Assertions.assertEquals(n, m.invoke(null, n).toString());
202         }
203         try {
204             m.invoke(null, "inexistent");
205             Assertions.fail("an exception should have been thrown");
206         } catch (InvocationTargetException ite) {
207             Assertions.assertTrue(ite.getCause() instanceof IllegalArgumentException);
208         }
209     }
210 
211 }