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