1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.fitting;
23
24 import java.util.List;
25
26 import org.hipparchus.util.Precision;
27 import org.junit.Assert;
28 import org.junit.Test;
29
30
31
32
33
34 public class WeightedObservedPointsTest {
35 @Test
36 public void testAdd1() {
37 final WeightedObservedPoints store = new WeightedObservedPoints();
38
39 final double x = 1.2;
40 final double y = 34.56;
41 final double w = 0.789;
42
43 store.add(w, x, y);
44
45 Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
46 }
47
48 @Test
49 public void testAdd2() {
50 final WeightedObservedPoints store = new WeightedObservedPoints();
51
52 final double x = 1.2;
53 final double y = 34.56;
54 final double w = 0.789;
55
56 store.add(new WeightedObservedPoint(w, x, y));
57
58 Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
59 }
60
61 @Test
62 public void testAdd3() {
63 final WeightedObservedPoints store = new WeightedObservedPoints();
64
65 final double x = 1.2;
66 final double y = 34.56;
67
68 store.add(x, y);
69
70 Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(1, x, y)));
71 }
72
73 @Test
74 public void testClear() {
75 final WeightedObservedPoints store = new WeightedObservedPoints();
76
77 store.add(new WeightedObservedPoint(1, 2, 3));
78 store.add(new WeightedObservedPoint(2, -1, -2));
79 Assert.assertTrue(store.toList().size() == 2);
80
81 store.clear();
82 Assert.assertTrue(store.toList().size() == 0);
83 }
84
85
86
87 @Test
88 public void testToListCopy() {
89 final WeightedObservedPoints store = new WeightedObservedPoints();
90
91 store.add(new WeightedObservedPoint(1, 2, 3));
92 store.add(new WeightedObservedPoint(2, -3, -4));
93
94 final List<WeightedObservedPoint> list = store.toList();
95 Assert.assertTrue(list.size() == 2);
96
97
98 list.add(new WeightedObservedPoint(1.2, 3.4, 5.6));
99 Assert.assertFalse(list.size() == store.toList().size());
100
101
102 store.clear();
103 Assert.assertFalse(list.size() == 0);
104 }
105
106
107
108
109
110
111
112
113
114 private boolean lastElementIsSame(WeightedObservedPoints store,
115 WeightedObservedPoint point) {
116 final List<WeightedObservedPoint> list = store.toList();
117 final WeightedObservedPoint lastPoint = list.get(list.size() - 1);
118
119 if (!Precision.equals(lastPoint.getX(), point.getX())) {
120 return false;
121 }
122 if (!Precision.equals(lastPoint.getY(), point.getY())) {
123 return false;
124 }
125 if (!Precision.equals(lastPoint.getWeight(), point.getWeight())) {
126 return false;
127 }
128
129 return true;
130 }
131 }