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    *      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  
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 java.util.List;
25  
26  import org.hipparchus.util.Precision;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Tests {@link WeightedObservedPoints}.
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      // Ensure that an instance returned by "toList()" is independent from
86      // the original container.
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          // Adding an element to "list" has no impact on "store".
98          list.add(new WeightedObservedPoint(1.2, 3.4, 5.6));
99          Assert.assertFalse(list.size() == store.toList().size());
100 
101         // Clearing "store" has no impact on "list".
102         store.clear();
103         Assert.assertFalse(list.size() == 0);
104     }
105 
106     /**
107      * Checks that the contents of the last element is equal to the
108      * contents of {@code p}.
109      *
110      * @param store Container.
111      * @param point Observation.
112      * @return {@code true} if both elements have the same contents.
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 }