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.stat.descriptive.moment;
23  
24  import static org.junit.Assert.assertTrue;
25  
26  import org.hipparchus.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
27  import org.junit.Test;
28  
29  /**
30   * Test cases for the {@link FirstMoment} class.
31   */
32  public class FirstMomentTest extends StorelessUnivariateStatisticAbstractTest {
33  
34      @Override
35      public FirstMoment getUnivariateStatistic() {
36          return new FirstMoment();
37      }
38  
39      @Override
40      public double expectedValue() {
41          return this.mean;
42      }
43  
44      @Test
45      public void testSpecialValues() {
46          final FirstMoment mean = new FirstMoment();
47  
48          mean.clear();
49          mean.increment(Double.POSITIVE_INFINITY);
50          mean.increment(1d);
51          assertTrue(Double.isNaN(mean.getResult()));
52  
53          mean.clear();
54          mean.increment(Double.POSITIVE_INFINITY);
55          mean.increment(-1d);
56          assertTrue(Double.isNaN(mean.getResult()));
57  
58          mean.clear();
59          mean.increment(Double.NEGATIVE_INFINITY);
60          mean.increment(1d);
61          assertTrue(Double.isNaN(mean.getResult()));
62  
63          mean.clear();
64          mean.increment(Double.NEGATIVE_INFINITY);
65          mean.increment(-1d);
66          assertTrue(Double.isNaN(mean.getResult()));
67  
68          mean.clear();
69          mean.increment(Double.POSITIVE_INFINITY);
70          mean.increment(Double.POSITIVE_INFINITY);
71          assertTrue(Double.isNaN(mean.getResult()));
72  
73          mean.clear();
74          mean.increment(Double.NEGATIVE_INFINITY);
75          mean.increment(Double.NEGATIVE_INFINITY);
76          assertTrue(Double.isNaN(mean.getResult()));
77  
78          mean.clear();
79          mean.increment(Double.POSITIVE_INFINITY);
80          mean.increment(Double.NEGATIVE_INFINITY);
81          assertTrue(Double.isNaN(mean.getResult()));
82  
83          mean.clear();
84          mean.increment(Double.NEGATIVE_INFINITY);
85          mean.increment(Double.POSITIVE_INFINITY);
86          assertTrue(Double.isNaN(mean.getResult()));
87  
88          mean.clear();
89          mean.increment(Double.NaN);
90          mean.increment(Double.POSITIVE_INFINITY);
91          assertTrue(Double.isNaN(mean.getResult()));
92  
93          mean.clear();
94          mean.increment(Double.NaN);
95          mean.increment(Double.NEGATIVE_INFINITY);
96          assertTrue(Double.isNaN(mean.getResult()));
97  
98          mean.clear();
99          mean.increment(Double.NaN);
100         mean.increment(0d);
101         assertTrue(Double.isNaN(mean.getResult()));
102     }
103 }