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  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.function.Consumer;
25  
26  public class DoubleArrayDictionaryTest {
27  
28      @Test
29      public void testEmpty() {
30          Assertions.assertTrue(new DoubleArrayDictionary().getData().isEmpty());
31      }
32  
33      @Test
34      public void testPutGet() {
35  
36          DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
37          dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
38          dictionary.put("b",       new double[] { 4.0 });
39          dictionary.put("another", new double[] { 17.0 });
40  
41          Assertions.assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"),       1.0e-15);
42          Assertions.assertArrayEquals(new double[] { 17.0 },          dictionary.get("another"), 1.0e-15);
43          Assertions.assertArrayEquals(new double[] { 4.0 },           dictionary.get("b"),       1.0e-15);
44  
45          Assertions.assertNull(dictionary.get("not-a-key"));
46  
47      }
48  
49      @Test
50      public void testFromDictionary() {
51          DoubleArrayDictionary original = new DoubleArrayDictionary();
52          original.put("a",       new double[] { 1.0, 2.0, 3.0 });
53          original.put("b",       new double[] { 4.0 });
54          original.put("another", new double[] { 17.0 });
55  
56          DoubleArrayDictionary copy = new DoubleArrayDictionary(original);
57  
58          Assertions.assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, copy.get("a"),       1.0e-15);
59          Assertions.assertArrayEquals(new double[] { 17.0 },          copy.get("another"), 1.0e-15);
60          Assertions.assertArrayEquals(new double[] { 4.0 },           copy.get("b"),       1.0e-15);
61  
62          Assertions.assertNull(copy.get("not-a-key"));
63  
64      }
65  
66      @Test
67      public void testFromMap() {
68          final Map<String, double[]> map = new HashMap<>();
69          map.put("a",       new double[] { 1.0, 2.0, 3.0 });
70          map.put("b",       new double[] { 4.0 });
71          map.put("another", new double[] { 17.0 });
72  
73          DoubleArrayDictionary dictionary = new DoubleArrayDictionary(map);
74  
75          Assertions.assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"),       1.0e-15);
76          Assertions.assertArrayEquals(new double[] { 17.0 },          dictionary.get("another"), 1.0e-15);
77          Assertions.assertArrayEquals(new double[] { 4.0 },           dictionary.get("b"),       1.0e-15);
78  
79          Assertions.assertNull(dictionary.get("not-a-key"));
80  
81      }
82  
83      @Test
84      public void testArraysAreCopied() {
85          DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
86          double[] original = new double[] { 1.0, 2.0, 3.0 };
87          dictionary.put("a", original);
88          double[] retrieved = dictionary.get("a");
89          Assertions.assertArrayEquals(original, retrieved, 1.0e-15);
90          Assertions.assertNotSame(original, retrieved);
91      }
92  
93      @Test
94      public void testIncrement() {
95          DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
96          dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
97          dictionary.getEntry("a").increment(new double[] { 2.0, 4.0, 8.0 });
98          Assertions.assertArrayEquals(new double[] { 3.0, 6.0, 11.0 }, dictionary.get("a"), 1.0e-15);
99      }
100 
101     @Test
102     public void testScaledIncrement() {
103         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
104         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
105         DoubleArrayDictionary other = new DoubleArrayDictionary();
106         other.put("aDot",       new double[] { 3.0, 2.0, 1.0 });
107         dictionary.getEntry("a").scaledIncrement(2.0, other.getEntry("aDot"));
108         Assertions.assertArrayEquals(new double[] { 7.0, 6.0, 5.0 }, dictionary.get("a"), 1.0e-15);
109     }
110 
111     @Test
112     public void testZero() {
113         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
114         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
115         dictionary.getEntry("a").zero();
116         Assertions.assertArrayEquals(new double[] { 0.0, 0.0, 0.0 }, dictionary.get("a"), 1.0e-15);
117     }
118 
119     @Test
120     public void testSize() {
121         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
122         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
123          Assertions.assertEquals(3, dictionary.getEntry("a").size(), 1.0e-15);
124     }
125 
126     @Test
127     public void testDataManagement() {
128         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
129         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
130         dictionary.put("b",       new double[] { 4.0 });
131         dictionary.put("another", new double[] { 17.0 });
132 
133         Assertions.assertEquals(3, dictionary.size());
134         Assertions.assertEquals("{a[3], b[1], another[1]}", dictionary.toString());
135 
136         Assertions.assertTrue(dictionary.remove("another"));
137         Assertions.assertEquals(2, dictionary.size());
138         Assertions.assertFalse(dictionary.remove("not-a-key"));
139         Assertions.assertEquals(2, dictionary.size());
140 
141         Assertions.assertEquals("a", dictionary.getData().get(0).getKey());
142         Assertions.assertEquals("b", dictionary.getData().get(1).getKey());
143 
144         dictionary.clear();
145         Assertions.assertTrue(dictionary.getData().isEmpty());
146 
147     }
148 
149     @Test
150     public void testReplace() {
151 
152         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
153         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
154         dictionary.put("b",       new double[] { 4.0 });
155         dictionary.put("another", new double[] { 17.0 });
156         Assertions.assertEquals(3, dictionary.size());
157 
158         dictionary.put("b",       new double[] { -1.0, -1.0 });
159         Assertions.assertEquals(3, dictionary.size());
160 
161         Assertions.assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"),       1.0e-15);
162         Assertions.assertArrayEquals(new double[] { 17.0 },          dictionary.get("another"), 1.0e-15);
163         Assertions.assertArrayEquals(new double[] { -1.0, -1.0 },    dictionary.get("b"),       1.0e-15);
164         Assertions.assertEquals("a",       dictionary.getData().get(0).getKey());
165         Assertions.assertEquals("another", dictionary.getData().get(1).getKey());
166         Assertions.assertEquals("b",       dictionary.getData().get(2).getKey());
167 
168     }
169 
170     @Test
171     public void testPutAllMap() {
172 
173         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
174         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
175         dictionary.put("b",       new double[] { 4.0 });
176         dictionary.put("another", new double[] { 17.0 });
177         Assertions.assertEquals(3, dictionary.size());
178 
179         final Map<String, double[]> map = new HashMap<>();
180         map.put("f", new double[] {  12.0 });
181         map.put("g", new double[] { -12.0 });
182         map.put("b", new double[] {  19.0 });
183 
184         dictionary.putAll(map);
185         Assertions.assertEquals(5, dictionary.size());
186 
187         Assertions.assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"),       1.0e-15);
188         Assertions.assertArrayEquals(new double[] {  19.0 },         dictionary.get("b"),       1.0e-15);
189         Assertions.assertArrayEquals(new double[] {  17.0 },         dictionary.get("another"), 1.0e-15);
190         Assertions.assertArrayEquals(new double[] {  12.0 },         dictionary.get("f"),       1.0e-15);
191         Assertions.assertArrayEquals(new double[] { -12.0 },         dictionary.get("g"),       1.0e-15);
192 
193     }
194 
195     @Test
196     public void testPutAllDictionary() {
197 
198         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
199         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
200         dictionary.put("b",       new double[] { 4.0 });
201         dictionary.put("another", new double[] { 17.0 });
202         Assertions.assertEquals(3, dictionary.size());
203 
204         DoubleArrayDictionary other = new DoubleArrayDictionary();
205         other.put("f", new double[] {  12.0 });
206         other.put("g", new double[] { -12.0 });
207         other.put("b", new double[] {  19.0 });
208 
209         dictionary.putAll(other);
210         Assertions.assertEquals(5, dictionary.size());
211 
212         Assertions.assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, dictionary.get("a"),       1.0e-15);
213         Assertions.assertArrayEquals(new double[] {  19.0 },         dictionary.get("b"),       1.0e-15);
214         Assertions.assertArrayEquals(new double[] {  17.0 },         dictionary.get("another"), 1.0e-15);
215         Assertions.assertArrayEquals(new double[] {  12.0 },         dictionary.get("f"),       1.0e-15);
216         Assertions.assertArrayEquals(new double[] { -12.0 },         dictionary.get("g"),       1.0e-15);
217 
218     }
219 
220     @Test
221     public void testToMap() {
222         DoubleArrayDictionary dictionary = new DoubleArrayDictionary();
223         dictionary.put("a",       new double[] { 1.0, 2.0, 3.0 });
224         dictionary.put("b",       new double[] { 4.0 });
225         dictionary.put("another", new double[] { 17.0 });
226         Assertions.assertEquals(3, dictionary.size());
227 
228         Map<String, double[]> map = dictionary.toMap();
229         Assertions.assertEquals(3, map.size());
230 
231         Assertions.assertArrayEquals(new double[] { 1.0, 2.0, 3.0 }, map.get("a"),       1.0e-15);
232         Assertions.assertArrayEquals(new double[] {   4.0 },         map.get("b"),       1.0e-15);
233         Assertions.assertArrayEquals(new double[] {  17.0 },         map.get("another"), 1.0e-15);
234 
235         dictionary.clear();
236         Assertions.assertEquals(0, dictionary.size());
237         Assertions.assertEquals(3, map.size());
238         map.put("z", new double[] {});
239         Assertions.assertEquals(4, map.size());
240         Assertions.assertEquals(0, dictionary.size());
241 
242     }
243 }