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  package org.apache.commons.math4.legacy.fitting;
18  
19  import java.util.List;
20  
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.apache.commons.numbers.core.Precision;
24  
25  /**
26   * Tests {@link WeightedObservedPoints}.
27   *
28   */
29  public class WeightedObservedPointsTest {
30      @Test
31      public void testAdd1() {
32          final WeightedObservedPoints store = new WeightedObservedPoints();
33  
34          final double x = 1.2;
35          final double y = 34.56;
36          final double w = 0.789;
37  
38          store.add(w, x, y);
39  
40          Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
41      }
42  
43      @Test
44      public void testAdd2() {
45          final WeightedObservedPoints store = new WeightedObservedPoints();
46  
47          final double x = 1.2;
48          final double y = 34.56;
49          final double w = 0.789;
50  
51          store.add(new WeightedObservedPoint(w, x, y));
52  
53          Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
54      }
55  
56      @Test
57      public void testAdd3() {
58          final WeightedObservedPoints store = new WeightedObservedPoints();
59  
60          final double x = 1.2;
61          final double y = 34.56;
62  
63          store.add(x, y);
64  
65          Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(1, x, y)));
66      }
67  
68      @Test
69      public void testClear() {
70          final WeightedObservedPoints store = new WeightedObservedPoints();
71  
72          store.add(new WeightedObservedPoint(1, 2, 3));
73          store.add(new WeightedObservedPoint(2, -1, -2));
74          Assert.assertTrue(store.toList().size() == 2);
75  
76          store.clear();
77          Assert.assertTrue(store.toList().isEmpty());
78      }
79  
80      // Ensure that an instance returned by "toList()" is independent from
81      // the original container.
82      @Test
83      public void testToListCopy() {
84          final WeightedObservedPoints store = new WeightedObservedPoints();
85  
86          store.add(new WeightedObservedPoint(1, 2, 3));
87          store.add(new WeightedObservedPoint(2, -3, -4));
88  
89          final List<WeightedObservedPoint> list = store.toList();
90          Assert.assertEquals(2, list.size());
91  
92          // Adding an element to "list" has no impact on "store".
93          list.add(new WeightedObservedPoint(1.2, 3.4, 5.6));
94          Assert.assertNotEquals(list.size(), store.toList().size());
95  
96          // Clearing "store" has no impact on "list".
97          store.clear();
98          Assert.assertFalse(list.isEmpty());
99      }
100 
101     /**
102      * Checks that the contents of the last element is equal to the
103      * contents of {@code p}.
104      *
105      * @param store Container.
106      * @param point Observation.
107      * @return {@code true} if both elements have the same contents.
108      */
109     private boolean lastElementIsSame(WeightedObservedPoints store,
110                                       WeightedObservedPoint point) {
111         final List<WeightedObservedPoint> list = store.toList();
112         final WeightedObservedPoint lastPoint = list.get(list.size() - 1);
113 
114         if (!Precision.equals(lastPoint.getX(), point.getX())) {
115             return false;
116         }
117         if (!Precision.equals(lastPoint.getY(), point.getY())) {
118             return false;
119         }
120         if (!Precision.equals(lastPoint.getWeight(), point.getWeight())) {
121             return false;
122         }
123 
124         return true;
125     }
126 }