View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.fitting;
23  
24  import org.hipparchus.util.Precision;
25  import org.junit.jupiter.api.Test;
26  
27  import java.util.List;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  /**
34   * Tests {@link WeightedObservedPoints}.
35   *
36   */
37  class WeightedObservedPointsTest {
38      @Test
39      void testAdd1() {
40          final WeightedObservedPoints store = new WeightedObservedPoints();
41  
42          final double x = 1.2;
43          final double y = 34.56;
44          final double w = 0.789;
45  
46          store.add(w, x, y);
47  
48          assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
49      }
50  
51      @Test
52      void testAdd2() {
53          final WeightedObservedPoints store = new WeightedObservedPoints();
54  
55          final double x = 1.2;
56          final double y = 34.56;
57          final double w = 0.789;
58  
59          store.add(new WeightedObservedPoint(w, x, y));
60  
61          assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
62      }
63  
64      @Test
65      void testAdd3() {
66          final WeightedObservedPoints store = new WeightedObservedPoints();
67  
68          final double x = 1.2;
69          final double y = 34.56;
70  
71          store.add(x, y);
72  
73          assertTrue(lastElementIsSame(store, new WeightedObservedPoint(1, x, y)));
74      }
75  
76      @Test
77      void testClear() {
78          final WeightedObservedPoints store = new WeightedObservedPoints();
79  
80          store.add(new WeightedObservedPoint(1, 2, 3));
81          store.add(new WeightedObservedPoint(2, -1, -2));
82          assertEquals(2, store.toList().size());
83  
84          store.clear();
85          assertEquals(0, store.toList().size());
86      }
87  
88      // Ensure that an instance returned by "toList()" is independent from
89      // the original container.
90      @Test
91      void testToListCopy() {
92          final WeightedObservedPoints store = new WeightedObservedPoints();
93  
94          store.add(new WeightedObservedPoint(1, 2, 3));
95          store.add(new WeightedObservedPoint(2, -3, -4));
96  
97          final List<WeightedObservedPoint> list = store.toList();
98          assertEquals(2, list.size());
99  
100         // Adding an element to "list" has no impact on "store".
101         list.add(new WeightedObservedPoint(1.2, 3.4, 5.6));
102         assertFalse(list.size() == store.toList().size());
103 
104         // Clearing "store" has no impact on "list".
105         store.clear();
106         assertFalse(list.size() == 0);
107     }
108 
109     /**
110      * Checks that the contents of the last element is equal to the
111      * contents of {@code p}.
112      *
113      * @param store Container.
114      * @param point Observation.
115      * @return {@code true} if both elements have the same contents.
116      */
117     private boolean lastElementIsSame(WeightedObservedPoints store,
118                                       WeightedObservedPoint point) {
119         final List<WeightedObservedPoint> list = store.toList();
120         final WeightedObservedPoint lastPoint = list.get(list.size() - 1);
121 
122         if (!Precision.equals(lastPoint.getX(), point.getX())) {
123             return false;
124         }
125         if (!Precision.equals(lastPoint.getY(), point.getY())) {
126             return false;
127         }
128         if (!Precision.equals(lastPoint.getWeight(), point.getWeight())) {
129             return false;
130         }
131 
132         return true;
133     }
134 }