1   /* Copyright 2002-2025 Bryan Cazabonne
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    * Bryan Cazabonne 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.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.util.Binary64Field;
22  import org.hipparchus.util.MathArrays;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  
30  class FieldDataDictionaryTest {
31  
32  
33      @Test
34      public void testEmpty() {
35          doTestEmpty(Binary64Field.getInstance());
36      }
37  
38      @Test
39      public void testPutGet() {
40          doTestPutGet(Binary64Field.getInstance());
41      }
42  
43      @Test
44      public void testFromDictionary() {
45          doTestFromDictionary(Binary64Field.getInstance());
46      }
47  
48      @Test
49      public void testFromMap() {
50          doTestFromMap(Binary64Field.getInstance());
51      }
52  
53      @Test
54      public void testArraysAreCopied() {
55          doTestArraysAreCopied(Binary64Field.getInstance());
56      }
57  
58      @Test
59      public void testScaledIncrementField() {
60          doTestScaledIncrementField(Binary64Field.getInstance());
61      }
62  
63      @Test
64      public void testScaledIncrementDouble() {
65          doTestScaledIncrementDouble(Binary64Field.getInstance());
66      }
67  
68      @Test
69      public void testDataManagement() {
70          doTestDataManagement(Binary64Field.getInstance());
71      }
72  
73      @Test
74      public void testReplace() {
75          doTestReplace(Binary64Field.getInstance());
76      }
77  
78      @Test
79      public void testPutAllMap() {
80          doTestPutAllMap(Binary64Field.getInstance());
81      }
82  
83      @Test
84      public void testPutAllDictionary() {
85          doTestPutAllDictionary(Binary64Field.getInstance());
86      }
87  
88      @Test
89      public void testToMap() {
90          doTestToMap(Binary64Field.getInstance());
91      }
92  
93      private <T extends CalculusFieldElement<T>> void doTestEmpty(Field<T> field) {
94          Assertions.assertTrue(new FieldDataDictionary<>(field).getData().isEmpty());
95      }
96  
97      private <T extends CalculusFieldElement<T>> void doTestPutGet(Field<T> field) {
98  
99          FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
100         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
101         dictionary.put("b",       convertToObject(field, new double[] { 4.0 }));
102         dictionary.put("another", convertToObject(field, new double[] { 17.0 }));
103 
104         checkArray(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"));
105         checkArray(new double[] { 17.0 },          dictionary.get("another"));
106         checkArray(new double[] { 4.0 },           dictionary.get("b"));
107 
108         Assertions.assertNull(dictionary.get("not-a-key"));
109 
110     }
111 
112     private <T extends CalculusFieldElement<T>> void doTestFromDictionary(Field<T> field) {
113         FieldDataDictionary<T> original = new FieldDataDictionary<>(field);
114         original.put("a",       new double[] { 1.0, 2.0, 3.0 });
115         original.put("b",       new double[] { 4.0 });
116         original.put("another", new double[] { 17.0 });
117 
118         FieldDataDictionary<T> copy = new FieldDataDictionary<>(original);
119 
120         checkArray(new double[] { 1.0, 2.0, 3.0 }, copy.get("a"));
121         checkArray(new double[] { 17.0 },          copy.get("another"));
122         checkArray(new double[] { 4.0 },           copy.get("b"));
123 
124         Assertions.assertNull(copy.get("not-a-key"));
125 
126     }
127 
128     private <T extends CalculusFieldElement<T>> void doTestFromMap(Field<T> field) {
129         final Map<String, Object> map = new HashMap<>();
130         map.put("a",       convertToObject(field, new double[] { 1.0, 2.0, 3.0 }));
131         map.put("b",       convertToObject(field, new double[] { 4.0 }));
132         map.put("another", convertToObject(field, new double[] { 17.0 }));
133 
134         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field, map);
135 
136         checkArray(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"));
137         checkArray(new double[] { 17.0 },          dictionary.get("another"));
138         checkArray(new double[] { 4.0 },           dictionary.get("b"));
139 
140         Assertions.assertNull(dictionary.get("not-a-key"));
141 
142     }
143 
144     private <T extends CalculusFieldElement<T>> void doTestArraysAreCopied(Field<T> field) {
145         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
146         Object original = convertToObject(field, new double[] { 1.0, 2.0, 3.0 });
147         dictionary.put("a", original);
148         Object retrieved = dictionary.get("a");
149         checkArray(new double[] { 1.0, 2.0, 3.0 }, retrieved);
150         Assertions.assertNotSame(original, retrieved);
151     }
152 
153     private <T extends CalculusFieldElement<T>> void doTestScaledIncrementField(Field<T> field) {
154         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
155         dictionary.put("a",       convertToObject(field, new double[] { 1.0, 2.0, 3.0 }));
156         FieldArrayDictionary<T> other = new FieldArrayDictionary<>(field);
157         other.put("aDot",       convertToFieldArray(field, new double[] { 3.0, 2.0, 1.0 }));
158         dictionary.getEntry("a").scaledIncrement(field.getZero().newInstance(2.0), other.getEntry("aDot"));
159         checkArray(new double[] { 7.0, 6.0, 5.0 }, dictionary.get("a"));
160 
161         FieldDataDictionary<T> dictionaryString = new FieldDataDictionary<>(field);
162         dictionaryString.put("b", "hello");
163         dictionaryString.getEntry("b").scaledIncrement(2.0, other.getEntry("aDot"));
164         Assertions.assertEquals("hello", dictionaryString.get("b"));
165     }
166 
167     private <T extends CalculusFieldElement<T>> void doTestScaledIncrementDouble(Field<T> field) {
168         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
169         dictionary.put("a",       convertToObject(field, new double[] { 1.0, 2.0, 3.0 }));
170         FieldArrayDictionary<T> other = new FieldArrayDictionary<>(field);
171         other.put("aDot",       convertToFieldArray(field, new double[] { 3.0, 2.0, 1.0 }));
172         dictionary.getEntry("a").scaledIncrement(2.0, other.getEntry("aDot"));
173         checkArray(new double[] { 7.0, 6.0, 5.0 }, dictionary.get("a"));
174 
175         FieldDataDictionary<T> dictionaryString = new FieldDataDictionary<>(field);
176         dictionaryString.put("b", "hello");
177         dictionaryString.getEntry("b").scaledIncrement(2.0, other.getEntry("aDot"));
178         Assertions.assertEquals("hello", dictionaryString.get("b"));
179     }
180 
181     @Test
182     void testToFieldArrayDictionary() {
183         doTestToFieldArrayDictionary(Binary64Field.getInstance());
184     }
185 
186     private <T extends CalculusFieldElement<T>> void doTestToFieldArrayDictionary(Field<T> field) {
187         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
188         dictionary.put("a", convertToObject(field, new double[] { 1.0, 2.0, 3.0 }));
189         FieldArrayDictionary<T> fieldArrayDictionary1 = dictionary.toFieldArrayDictionary();
190         checkArray(new double[] { 1.0, 2.0, 3.0 }, fieldArrayDictionary1.get("a"));
191 
192         FieldDataDictionary<T> dictionaryString = new FieldDataDictionary<>(field);
193         dictionaryString.put("b", "hello");
194         FieldArrayDictionary<T> fieldArrayDictionary2 = dictionaryString.toFieldArrayDictionary();
195         Assertions.assertEquals(0, fieldArrayDictionary2.getData().size());
196     }
197 
198     private <T extends CalculusFieldElement<T>> void doTestDataManagement(Field<T> field) {
199         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
200         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
201         dictionary.put("b",       new double[] { 4.0 });
202         dictionary.put("another", new double[] { 17.0 });
203 
204         Assertions.assertEquals(3, dictionary.size());
205         Assertions.assertEquals("{a[3], b[1], another[1]}", dictionary.toString());
206 
207         Assertions.assertTrue(dictionary.remove("another"));
208         Assertions.assertEquals(2, dictionary.size());
209         Assertions.assertFalse(dictionary.remove("not-a-key"));
210         Assertions.assertEquals(2, dictionary.size());
211 
212         Assertions.assertEquals("a", dictionary.getData().get(0).getKey());
213         Assertions.assertEquals("b", dictionary.getData().get(1).getKey());
214 
215         dictionary.clear();
216         Assertions.assertTrue(dictionary.getData().isEmpty());
217 
218         Assertions.assertSame(field, dictionary.getField());
219 
220     }
221 
222     private <T extends CalculusFieldElement<T>> void doTestReplace(Field<T> field) {
223 
224         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
225         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
226         dictionary.put("b",       new double[] { 4.0 });
227         dictionary.put("another", new double[] { 17.0 });
228         Assertions.assertEquals(3, dictionary.size());
229 
230         dictionary.put("b",       new double[] { -1.0, -1.0 });
231         Assertions.assertEquals(3, dictionary.size());
232 
233         checkArray(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"));
234         checkArray(new double[] { 17.0 },          dictionary.get("another"));
235         checkArray(new double[] { -1.0, -1.0 },    dictionary.get("b"));
236         Assertions.assertEquals("a",       dictionary.getData().get(0).getKey());
237         Assertions.assertEquals("another", dictionary.getData().get(1).getKey());
238         Assertions.assertEquals("b",       dictionary.getData().get(2).getKey());
239 
240     }
241 
242     private <T extends CalculusFieldElement<T>> void doTestPutAllMap(Field<T> field) {
243 
244         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
245         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
246         dictionary.put("b",       new double[] { 4.0 });
247         dictionary.put("another", new double[] { 17.0 });
248         Assertions.assertEquals(3, dictionary.size());
249 
250         final Map<String, Object> map = new HashMap<>();
251         map.put("f", convertToObject(field, new double[] {  12.0 }));
252         map.put("g", convertToObject(field, new double[] { -12.0 }));
253         map.put("b", convertToObject(field, new double[] {  19.0 }));
254 
255         dictionary.putAll(map);
256         Assertions.assertEquals(5, dictionary.size());
257 
258         checkArray(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"));
259         checkArray(new double[] {  19.0 },         dictionary.get("b"));
260         checkArray(new double[] {  17.0 },         dictionary.get("another"));
261         checkArray(new double[] {  12.0 },         dictionary.get("f"));
262         checkArray(new double[] { -12.0 },         dictionary.get("g"));
263 
264     }
265 
266     private <T extends CalculusFieldElement<T>> void doTestPutAllDictionary(Field<T> field) {
267 
268         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
269         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
270         dictionary.put("b",       new double[] { 4.0 });
271         dictionary.put("another", new double[] { 17.0 });
272         Assertions.assertEquals(3, dictionary.size());
273 
274         FieldDataDictionary<T> other = new FieldDataDictionary<>(field);
275         other.put("f", new double[] {  12.0 });
276         other.put("g", new double[] { -12.0 });
277         other.put("b", new double[] {  19.0 });
278 
279         dictionary.putAll(other);
280         Assertions.assertEquals(5, dictionary.size());
281 
282         checkArray(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"));
283         checkArray(new double[] {  19.0 },         dictionary.get("b"));
284         checkArray(new double[] {  17.0 },         dictionary.get("another"));
285         checkArray(new double[] {  12.0 },         dictionary.get("f"));
286         checkArray(new double[] { -12.0 },         dictionary.get("g"));
287 
288     }
289 
290     private <T extends CalculusFieldElement<T>> void doTestToMap(Field<T> field) {
291         FieldDataDictionary<T> dictionary = new FieldDataDictionary<>(field);
292         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
293         dictionary.put("b",       new double[] { 4.0 });
294         dictionary.put("another", new double[] { 17.0 });
295         Assertions.assertEquals(3, dictionary.size());
296 
297         Map<String, Object> map = dictionary.toMap();
298         Assertions.assertEquals(3, map.size());
299 
300         checkArray(new double[] { 1.0, 2.0, 3.0 }, map.get("a"));
301         checkArray(new double[] {   4.0 },         map.get("b"));
302         checkArray(new double[] {  17.0 },         map.get("another"));
303 
304         dictionary.clear();
305         Assertions.assertEquals(0, dictionary.size());
306         Assertions.assertEquals(3, map.size());
307         map.put("z", MathArrays.buildArray(field, 0));
308         Assertions.assertEquals(4, map.size());
309         Assertions.assertEquals(0, dictionary.size());
310 
311     }
312 
313     @SuppressWarnings("rawtypes")
314     private <T extends CalculusFieldElement<T>> void checkArray(final double[] expected, final Object actual) {
315         if (actual instanceof double[]) {
316             double[] actualField = (double[]) actual;
317             Assertions.assertEquals(expected.length, actualField.length);
318             for (int i = 0; i < expected.length; ++i) {
319                 Assertions.assertEquals(expected[i], actualField[i], 1.0E-15);
320             }
321         } else {
322             CalculusFieldElement[] actualField = (CalculusFieldElement[]) actual;
323             Assertions.assertEquals(expected.length, actualField.length);
324             for (int i = 0; i < expected.length; ++i) {
325                 Assertions.assertEquals(expected[i], actualField[i].getReal(), 1.0E-15);
326             }
327         }
328     }
329 
330     private <T extends CalculusFieldElement<T>> Object convertToObject(final Field<T> field, final double[] a) {
331         final T[] converted = MathArrays.buildArray(field, a.length);
332         for (int i = 0; i < a.length; ++i) {
333             converted[i] = field.getZero().newInstance(a[i]);
334         }
335         return converted;
336     }
337 
338     private <T extends CalculusFieldElement<T>> T[] convertToFieldArray(final Field<T> field, final double[] a) {
339         final T[] converted = MathArrays.buildArray(field, a.length);
340         for (int i = 0; i < a.length; ++i) {
341             converted[i] = field.getZero().newInstance(a[i]);
342         }
343         return converted;
344     }
345 
346 }