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.stat.descriptive.moment;
18  
19  import org.apache.commons.math4.legacy.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
20  import org.apache.commons.math4.legacy.stat.descriptive.UnivariateStatistic;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  /**
25   * Test cases for the {@link FirstMoment} class.
26   */
27  public class FirstMomentTest extends StorelessUnivariateStatisticAbstractTest{
28  
29      /** descriptive statistic. */
30      protected FirstMoment stat;
31  
32      /**
33       * @see org.apache.commons.math4.legacy.stat.descriptive.UnivariateStatisticAbstractTest#getUnivariateStatistic()
34       */
35      @Override
36      public UnivariateStatistic getUnivariateStatistic() {
37          return new FirstMoment();
38      }
39  
40      /**
41       * @see org.apache.commons.math4.legacy.stat.descriptive.UnivariateStatisticAbstractTest#expectedValue()
42       */
43      @Override
44      public double expectedValue() {
45          return this.mean;
46      }
47  
48      /**
49       * Added in an attempt to resolve MATH-1146
50       * Commented out tests that won't pass with the current implementation.
51       */
52      @Test
53      public void testSpecialValues() {
54          final FirstMoment mean = new FirstMoment();
55  
56  //         mean.clear();
57  //         mean.increment(Double.POSITIVE_INFINITY);
58  //         mean.increment(1d);
59  //         Assert.assertEquals(Double.POSITIVE_INFINITY, mean.getResult(), 0d);
60  
61  //         mean.clear();
62  //         mean.increment(Double.POSITIVE_INFINITY);
63  //         mean.increment(-1d);
64  //         Assert.assertEquals(Double.POSITIVE_INFINITY, mean.getResult(), 0d);
65  
66  //         mean.clear();
67  //         mean.increment(Double.NEGATIVE_INFINITY);
68  //         mean.increment(1d);
69  //         Assert.assertEquals(Double.NEGATIVE_INFINITY, mean.getResult(), 0d);
70  
71  //         mean.clear();
72  //         mean.increment(Double.NEGATIVE_INFINITY);
73  //         mean.increment(-1d);
74  //         Assert.assertEquals(Double.NEGATIVE_INFINITY, mean.getResult(), 0d);
75  
76  //         mean.clear();
77  //         mean.increment(Double.POSITIVE_INFINITY);
78  //         mean.increment(Double.POSITIVE_INFINITY);
79  //         Assert.assertEquals(Double.POSITIVE_INFINITY, mean.getResult(), 0d);
80  
81  //         mean.clear();
82  //         mean.increment(Double.NEGATIVE_INFINITY);
83  //         mean.increment(Double.NEGATIVE_INFINITY);
84  //         Assert.assertEquals(Double.NEGATIVE_INFINITY, mean.getResult(), 0d);
85  
86          mean.clear();
87          mean.increment(Double.POSITIVE_INFINITY);
88          mean.increment(Double.NEGATIVE_INFINITY);
89          Assert.assertTrue(Double.isNaN(mean.getResult()));
90  
91          mean.clear();
92          mean.increment(Double.NEGATIVE_INFINITY);
93          mean.increment(Double.POSITIVE_INFINITY);
94          Assert.assertTrue(Double.isNaN(mean.getResult()));
95  
96          mean.clear();
97          mean.increment(Double.NaN);
98          mean.increment(Double.POSITIVE_INFINITY);
99          Assert.assertTrue(Double.isNaN(mean.getResult()));
100 
101         mean.clear();
102         mean.increment(Double.NaN);
103         mean.increment(Double.NEGATIVE_INFINITY);
104         Assert.assertTrue(Double.isNaN(mean.getResult()));
105 
106         mean.clear();
107         mean.increment(Double.NaN);
108         mean.increment(0d);
109         Assert.assertTrue(Double.isNaN(mean.getResult()));
110     }
111 }