1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.hipparchus.util;
15
16 import org.hipparchus.exception.MathIllegalArgumentException;
17 import org.hipparchus.exception.NullArgumentException;
18 import org.hipparchus.random.RandomDataGenerator;
19 import org.hipparchus.util.ResizableDoubleArray.ExpansionMode;
20 import org.junit.jupiter.api.AfterEach;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23
24 import java.util.Arrays;
25
26 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
27 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29 import static org.junit.jupiter.api.Assertions.assertNotEquals;
30 import static org.junit.jupiter.api.Assertions.fail;
31
32
33
34
35 class ResizableDoubleArrayTest {
36
37 protected ResizableDoubleArray da = null;
38
39
40 protected ResizableDoubleArray ra = null;
41
42 @AfterEach
43 void tearDown()
44 throws Exception {
45 da = null;
46 ra = null;
47 }
48
49 @BeforeEach
50 void setUp()
51 throws Exception {
52 da = new ResizableDoubleArray();
53 ra = new ResizableDoubleArray();
54 }
55
56 @Test
57 void testConstructors() {
58 float defaultExpansionFactor = 2.0f;
59 double defaultContractionCriteria = 2.5;
60 ExpansionMode defaultMode = ResizableDoubleArray.ExpansionMode.MULTIPLICATIVE;
61
62 ResizableDoubleArray testDa = new ResizableDoubleArray(2);
63 assertEquals(0, testDa.getNumElements());
64 assertEquals(2, testDa.getCapacity());
65 assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);
66 assertEquals(defaultContractionCriteria, testDa.getContractionCriterion(), 0);
67 assertEquals(defaultMode, testDa.getExpansionMode());
68 try {
69 da = new ResizableDoubleArray(-1);
70 fail("Expecting MathIllegalArgumentException");
71 } catch (MathIllegalArgumentException ex) {
72
73 }
74
75 testDa = new ResizableDoubleArray((double[]) null);
76 assertEquals(0, testDa.getNumElements());
77
78 double[] initialArray = new double[] { 0, 1, 2 };
79
80 testDa = new ResizableDoubleArray(initialArray);
81 assertEquals(3, testDa.getNumElements());
82
83 testDa = new ResizableDoubleArray(2, 2.0);
84 assertEquals(0, testDa.getNumElements());
85 assertEquals(2, testDa.getCapacity());
86 assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);
87 assertEquals(defaultContractionCriteria, testDa.getContractionCriterion(), 0);
88 assertEquals(defaultMode, testDa.getExpansionMode());
89
90 try {
91 da = new ResizableDoubleArray(2, 0.5);
92 fail("Expecting MathIllegalArgumentException");
93 } catch (MathIllegalArgumentException ex) {
94
95 }
96
97 testDa = new ResizableDoubleArray(2, 3.0);
98 assertEquals(3.0f, testDa.getExpansionFactor(), 0);
99 assertEquals(3.5f, testDa.getContractionCriterion(), 0);
100
101 testDa = new ResizableDoubleArray(2, 2.0, 3.0);
102 assertEquals(0, testDa.getNumElements());
103 assertEquals(2, testDa.getCapacity());
104 assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);
105 assertEquals(3.0f, testDa.getContractionCriterion(), 0);
106 assertEquals(defaultMode, testDa.getExpansionMode());
107
108 try {
109 da = new ResizableDoubleArray(2, 2.0, 1.5);
110 fail("Expecting MathIllegalArgumentException");
111 } catch (MathIllegalArgumentException ex) {
112
113 }
114
115 testDa = new ResizableDoubleArray(2, 2.0, 3.0, ResizableDoubleArray.ExpansionMode.ADDITIVE);
116 assertEquals(0, testDa.getNumElements());
117 assertEquals(2, testDa.getCapacity());
118 assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);
119 assertEquals(3.0f, testDa.getContractionCriterion(), 0);
120 assertEquals(ResizableDoubleArray.ExpansionMode.ADDITIVE, testDa.getExpansionMode());
121
122 try {
123 da = new ResizableDoubleArray(2, 2.0d, 2.5d, null);
124 fail("Expecting NullArgumentException");
125 } catch (NullArgumentException ex) {
126
127 }
128
129
130 testDa = new ResizableDoubleArray(2, 2.0, 3.0, ResizableDoubleArray.ExpansionMode.ADDITIVE);
131 testDa.addElement(2.0);
132 testDa.addElement(3.2);
133 ResizableDoubleArray copyDa = new ResizableDoubleArray(testDa);
134 assertEquals(copyDa, testDa);
135 assertEquals(testDa, copyDa);
136
137
138 final double[] values = { 1 };
139 testDa = new ResizableDoubleArray(values);
140 assertArrayEquals(values, testDa.getElements(), 0);
141 assertEquals(1, testDa.getNumElements());
142 assertEquals(1, testDa.getElement(0), 0);
143 }
144
145 @Test
146 void testGetValues() {
147 double[] controlArray = {
148 2.0, 4.0, 6.0
149 };
150
151 da.addElement(2.0);
152 da.addElement(4.0);
153 da.addElement(6.0);
154
155 double[] testArray = da.getElements();
156
157 for (int i = 0; i < da.getNumElements(); i++) {
158 assertEquals(testArray[i],
159 controlArray[i], Double.MIN_VALUE, "The testArray values should equal the controlArray values, " +
160 "index i: " + i + " does not match");
161 }
162 }
163
164 @Test
165 void testMinMax() {
166 da.addElement(2.0);
167 da.addElement(22.0);
168 da.addElement(-2.0);
169 da.addElement(21.0);
170 da.addElement(22.0);
171 da.addElement(42.0);
172 da.addElement(62.0);
173 da.addElement(22.0);
174 da.addElement(122.0);
175 da.addElement(1212.0);
176
177 assertEquals(-2.0,
178 Arrays.stream(da.getElements()).min().getAsDouble(),
179 Double.MIN_VALUE,
180 "Min should be -2.0");
181
182 assertEquals(1212.0,
183 Arrays.stream(da.getElements()).max().getAsDouble(),
184 Double.MIN_VALUE,
185 "Max should be 1212.0");
186 }
187
188 @Test
189 void testSetElementArbitraryExpansion1() {
190
191
192 da.addElement(2.0);
193 da.addElement(4.0);
194 da.addElement(6.0);
195 da.setElement(1, 3.0);
196
197
198 da.setElement(1000, 3.4);
199
200 assertEquals(1001, da.getNumElements(), "The number of elements should now be 1001, it isn't");
201
202 assertEquals(0.0, da.getElement(760), Double.MIN_VALUE, "Uninitialized Elements are default value of 0.0, index 766 wasn't");
203
204 assertEquals(3.4,
205 da.getElement(1000), Double.MIN_VALUE, "The 1000th index should be 3.4, it isn't");
206 assertEquals(2.0,
207 da.getElement(0), Double.MIN_VALUE, "The 0th index should be 2.0, it isn't");
208 }
209
210 @Test
211 void testSetElementArbitraryExpansion2() {
212
213
214 da.addElement(2.0);
215 da.addElement(4.0);
216 da.addElement(6.0);
217 assertEquals(16, da.getCapacity());
218 assertEquals(3, da.getNumElements());
219 da.setElement(3, 7.0);
220 assertEquals(16, da.getCapacity());
221 assertEquals(4, da.getNumElements());
222 da.setElement(10, 10.0);
223 assertEquals(16, da.getCapacity());
224 assertEquals(11, da.getNumElements());
225 da.setElement(9, 10.0);
226 assertEquals(16, da.getCapacity());
227 assertEquals(11, da.getNumElements());
228
229 try {
230 da.setElement(-2, 3);
231 fail("Expecting ArrayIndexOutOfBoundsException for negative index");
232 } catch (ArrayIndexOutOfBoundsException ex) {
233
234 }
235
236
237
238 ResizableDoubleArray testDa =
239 new ResizableDoubleArray(2, 2.0, 3.0, ResizableDoubleArray.ExpansionMode.ADDITIVE);
240 assertEquals(2, testDa.getCapacity());
241 testDa.addElement(1d);
242 testDa.addElement(1d);
243 assertEquals(2, testDa.getCapacity());
244 testDa.addElement(1d);
245 assertEquals(4, testDa.getCapacity());
246 }
247
248 @Test
249 void testAdd1000() {
250 for (int i = 0; i < 1000; i++) {
251 da.addElement(i);
252 }
253
254 assertEquals(1000, da.getNumElements(), "Number of elements should be equal to 1000 after adding 1000 values");
255
256 assertEquals(56.0,
257 da.getElement(56), Double.MIN_VALUE, "The element at the 56th index should be 56");
258
259 assertEquals(1024,
260 da.getCapacity(),
261 "Internal Storage length should be 1024 if we started out with initial capacity of " +
262 "16 and an expansion factor of 2.0");
263 }
264
265 @Test
266 void testAddElements() {
267 ResizableDoubleArray testDa = new ResizableDoubleArray();
268
269
270 testDa.addElements(new double[] { 4, 5, 6 });
271 assertEquals(3, testDa.getNumElements(), 0);
272 assertEquals(4, testDa.getElement(0), 0);
273 assertEquals(5, testDa.getElement(1), 0);
274 assertEquals(6, testDa.getElement(2), 0);
275
276 testDa.addElements(new double[] { 4, 5, 6 });
277 assertEquals(6, testDa.getNumElements());
278
279
280 testDa = new ResizableDoubleArray(2, 2.0, 2.5, ResizableDoubleArray.ExpansionMode.ADDITIVE);
281 assertEquals(2, testDa.getCapacity());
282 testDa.addElements(new double[] { 1d });
283 testDa.addElements(new double[] { 2d });
284 testDa.addElements(new double[] { 3d });
285 assertEquals(1d, testDa.getElement(0), 0);
286 assertEquals(2d, testDa.getElement(1), 0);
287 assertEquals(3d, testDa.getElement(2), 0);
288 assertEquals(4, testDa.getCapacity());
289 assertEquals(3, testDa.getNumElements());
290 }
291
292 @Test
293 void testAddElementRolling() {
294 ra.addElement(0.5);
295 ra.addElement(1.0);
296 ra.addElement(1.0);
297 ra.addElement(1.0);
298 ra.addElement(1.0);
299 ra.addElement(1.0);
300 ra.addElementRolling(2.0);
301
302 assertEquals(6,
303 ra.getNumElements(),
304 "There should be 6 elements in the eda");
305 assertEquals(2.0,
306 Arrays.stream(ra.getElements()).max().getAsDouble(),
307 Double.MIN_VALUE,
308 "The max element should be 2.0");
309 assertEquals(1.0,
310 Arrays.stream(ra.getElements()).min().getAsDouble(),
311 Double.MIN_VALUE,
312 "The min element should be 1.0");
313
314 for (int i = 0; i < 1024; i++) {
315 ra.addElementRolling(i);
316 }
317
318 assertEquals(6, ra.getNumElements(), "We just inserted 1024 rolling elements, num elements should still be 6");
319
320
321 da.clear();
322 da.addElement(1);
323 da.addElement(2);
324 da.addElementRolling(3);
325 assertEquals(3, da.getElement(1), 0);
326 da.addElementRolling(4);
327 assertEquals(3, da.getElement(0), 0);
328 assertEquals(4, da.getElement(1), 0);
329 da.addElement(5);
330 assertEquals(5, da.getElement(2), 0);
331 da.addElementRolling(6);
332 assertEquals(4, da.getElement(0), 0);
333 assertEquals(5, da.getElement(1), 0);
334 assertEquals(6, da.getElement(2), 0);
335
336
337 ResizableDoubleArray testDa =
338 new ResizableDoubleArray(2, 2.0, 2.5, ResizableDoubleArray.ExpansionMode.ADDITIVE);
339 assertEquals(2, testDa.getCapacity());
340 testDa.addElement(1d);
341 testDa.addElement(2d);
342 testDa.addElement(3d);
343 assertEquals(1d, testDa.getElement(0), 0);
344 assertEquals(2d, testDa.getElement(1), 0);
345 assertEquals(3d, testDa.getElement(2), 0);
346 assertEquals(4, testDa.getCapacity());
347 assertEquals(3, testDa.getNumElements());
348 testDa.addElementRolling(4d);
349 assertEquals(2d, testDa.getElement(0), 0);
350 assertEquals(3d, testDa.getElement(1), 0);
351 assertEquals(4d, testDa.getElement(2), 0);
352 assertEquals(4, testDa.getCapacity());
353 assertEquals(3, testDa.getNumElements());
354 testDa.addElementRolling(5d);
355 assertEquals(3d, testDa.getElement(0), 0);
356 assertEquals(4d, testDa.getElement(1), 0);
357 assertEquals(5d, testDa.getElement(2), 0);
358 assertEquals(4, testDa.getCapacity());
359 assertEquals(3, testDa.getNumElements());
360 try {
361 testDa.getElement(4);
362 fail("Expecting ArrayIndexOutOfBoundsException");
363 } catch (ArrayIndexOutOfBoundsException ex) {
364
365 }
366 try {
367 testDa.getElement(-1);
368 fail("Expecting ArrayIndexOutOfBoundsException");
369 } catch (ArrayIndexOutOfBoundsException ex) {
370
371 }
372 }
373
374 @Test
375 void testSetNumberOfElements() {
376 da.addElement(1.0);
377 da.addElement(1.0);
378 da.addElement(1.0);
379 da.addElement(1.0);
380 da.addElement(1.0);
381 da.addElement(1.0);
382 assertEquals(6, da.getNumElements(), "Number of elements should equal 6");
383
384 da.setNumElements(3);
385 assertEquals(3, da.getNumElements(), "Number of elements should equal 3");
386
387 try {
388 da.setNumElements(-3);
389 fail("Setting number of elements to negative should've thrown an exception");
390 } catch (MathIllegalArgumentException iae) {
391 }
392
393 da.setNumElements(1024);
394 assertEquals(1024, da.getNumElements(), "Number of elements should now be 1024");
395 assertEquals(0.0, da.getElement(453), Double.MIN_VALUE, "Element 453 should be a default double");
396 }
397
398 @Test
399 void testWithInitialCapacity() {
400
401 ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
402 assertEquals(0, eDA2.getNumElements(), "Initial number of elements should be 0");
403
404 final RandomDataGenerator gen = new RandomDataGenerator(1000);
405 final int iterations = gen.nextInt(100, 1000);
406
407 for (int i = 0; i < iterations; i++) {
408 eDA2.addElement(i);
409 }
410
411 assertEquals(iterations, eDA2.getNumElements(), "Number of elements should be equal to " + iterations);
412
413 eDA2.addElement(2.0);
414
415 assertEquals(iterations + 1, eDA2.getNumElements(), "Number of elements should be equals to " +
416 (iterations + 1));
417 }
418
419 @Test
420 void testWithInitialCapacityAndExpansionFactor() {
421
422 ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0, 3.5);
423 assertEquals(0, eDA3.getNumElements(), "Initial number of elements should be 0");
424
425 final RandomDataGenerator gen = new RandomDataGenerator(1000);
426 final int iterations = gen.nextInt(100, 1000);
427
428 for (int i = 0; i < iterations; i++) {
429 eDA3.addElement(i);
430 }
431
432 assertEquals(iterations, eDA3.getNumElements(), "Number of elements should be equal to " + iterations);
433
434 eDA3.addElement(2.0);
435
436 assertEquals(iterations + 1, eDA3.getNumElements(), "Number of elements should be equals to " +
437 (iterations + 1));
438
439 assertEquals(3.0f,
440 eDA3.getExpansionFactor(), Double.MIN_VALUE, "Expansion factor should equal 3.0");
441 }
442
443 @Test
444 void testDiscard() {
445 da.addElement(2.0);
446 da.addElement(2.0);
447 da.addElement(2.0);
448 da.addElement(2.0);
449 da.addElement(2.0);
450 da.addElement(2.0);
451 da.addElement(2.0);
452 da.addElement(2.0);
453 da.addElement(2.0);
454 da.addElement(2.0);
455 da.addElement(2.0);
456 assertEquals(11, da.getNumElements(), "Number of elements should be 11");
457
458 da.discardFrontElements(5);
459 assertEquals(6, da.getNumElements(), "Number of elements should be 6");
460
461 da.addElement(2.0);
462 da.addElement(2.0);
463 da.addElement(2.0);
464 da.addElement(2.0);
465 assertEquals(10, da.getNumElements(), "Number of elements should be 10");
466
467 da.discardMostRecentElements(2);
468 assertEquals(8, da.getNumElements(), "Number of elements should be 8");
469
470 try {
471 da.discardFrontElements(-1);
472 fail("Trying to discard a negative number of element is not allowed");
473 } catch (Exception e) {
474 }
475
476 try {
477 da.discardMostRecentElements(-1);
478 fail("Trying to discard a negative number of element is not allowed");
479 } catch (Exception e) {
480 }
481
482 try {
483 da.discardFrontElements(10000);
484 fail("You can't discard more elements than the array contains");
485 } catch (Exception e) {
486 }
487
488 try {
489 da.discardMostRecentElements(10000);
490 fail("You can't discard more elements than the array contains");
491 } catch (Exception e) {
492 }
493
494 }
495
496 @Test
497 void testSubstitute() {
498
499 da.addElement(2.0);
500 da.addElement(2.0);
501 da.addElement(2.0);
502 da.addElement(2.0);
503 da.addElement(2.0);
504 da.addElement(2.0);
505 da.addElement(2.0);
506 da.addElement(2.0);
507 da.addElement(2.0);
508 da.addElement(2.0);
509 da.addElement(2.0);
510 assertEquals(11, da.getNumElements(), "Number of elements should be 11");
511
512 da.substituteMostRecentElement(24);
513
514 assertEquals(11, da.getNumElements(), "Number of elements should be 11");
515
516 assertDoesNotThrow(() -> da.discardMostRecentElements(10), "Trying to discard a negative number of element is not allowed");
517
518 da.substituteMostRecentElement(24);
519
520 assertEquals(1, da.getNumElements(), "Number of elements should be 1");
521
522 }
523
524 @SuppressWarnings("unlikely-arg-type")
525 @Test
526 void testEqualsAndHashCode()
527 throws Exception {
528
529
530 ResizableDoubleArray first = new ResizableDoubleArray();
531 Double other = Double.valueOf(2);
532 assertNotEquals(first, other);
533
534
535 other = null;
536 assertNotEquals(first, other);
537
538
539 assertEquals(first, first);
540
541
542 ResizableDoubleArray second = new ResizableDoubleArray();
543 verifyEquality(first, second);
544
545
546 ResizableDoubleArray third = new ResizableDoubleArray(3, 2.0, 2.0);
547 verifyInequality(third, first);
548 ResizableDoubleArray fourth = new ResizableDoubleArray(3, 2.0, 2.0);
549 ResizableDoubleArray fifth = new ResizableDoubleArray(2, 2.0, 2.0);
550 verifyEquality(third, fourth);
551 verifyInequality(third, fifth);
552 third.addElement(4.1);
553 third.addElement(4.2);
554 third.addElement(4.3);
555 fourth.addElement(4.1);
556 fourth.addElement(4.2);
557 fourth.addElement(4.3);
558 verifyEquality(third, fourth);
559
560
561 fourth.addElement(4.4);
562 verifyInequality(third, fourth);
563 third.addElement(4.4);
564 verifyEquality(third, fourth);
565 fourth.addElement(4.4);
566 verifyInequality(third, fourth);
567 third.addElement(4.4);
568 verifyEquality(third, fourth);
569 fourth.addElementRolling(4.5);
570 third.addElementRolling(4.5);
571 verifyEquality(third, fourth);
572
573
574 third.discardFrontElements(1);
575 verifyInequality(third, fourth);
576 fourth.discardFrontElements(1);
577 verifyEquality(third, fourth);
578
579
580 third.discardMostRecentElements(2);
581 fourth.discardMostRecentElements(2);
582 verifyEquality(third, fourth);
583
584
585 third.addElement(18);
586 fourth.addElement(17);
587 third.addElement(17);
588 fourth.addElement(18);
589 verifyInequality(third, fourth);
590
591
592 verifyEquality(fourth, new ResizableDoubleArray(fourth));
593
594
595 verifyEquality(fourth, fourth.copy());
596 }
597
598 @Test
599 void testGetArrayRef() {
600 final ResizableDoubleArray a = new ResizableDoubleArray();
601
602
603 final int index = 20;
604 final double v1 = 1.2;
605 a.setElement(index, v1);
606
607
608 final double v2 = v1 + 3.4;
609 final double[] aInternalArray = a.getArrayRef();
610 aInternalArray[a.getStartIndex() + index] = v2;
611
612 assertEquals(v2, a.getElement(index), 0d);
613 }
614
615 @Test
616 void testCompute() {
617 final ResizableDoubleArray a = new ResizableDoubleArray();
618 final int max = 20;
619 for (int i = 1; i <= max; i++) {
620 a.setElement(i, i);
621 }
622
623 final MathArrays.Function add = new MathArrays.Function() {
624
625 @Override
626 public double evaluate(double[] a, int index, int num) {
627 double sum = 0;
628 final int max = index + num;
629 for (int i = index; i < max; i++) {
630 sum += a[i];
631 }
632 return sum;
633 }
634
635 @Override
636 public double evaluate(double[] a) {
637 return evaluate(a, 0, a.length);
638 }
639 };
640
641 final double sum = a.compute(add);
642 assertEquals(0.5 * max * (max + 1), sum, 0);
643 }
644
645 private void verifyEquality(ResizableDoubleArray a,
646 ResizableDoubleArray b) {
647 assertEquals(b, a);
648 assertEquals(a, b);
649 assertEquals(a.hashCode(), b.hashCode());
650 }
651
652 private void verifyInequality(ResizableDoubleArray a,
653 ResizableDoubleArray b) {
654 assertNotEquals(b, a);
655 assertNotEquals(a, b);
656 assertNotEquals(a.hashCode(), b.hashCode());
657 }
658
659 }