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.summary;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  
27  import org.hipparchus.stat.descriptive.StorelessUnivariateStatistic;
28  import org.hipparchus.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
29  import org.junit.Test;
30  
31  /**
32   * Test cases for the {@link Sum} class.
33   */
34  public class SumTest extends StorelessUnivariateStatisticAbstractTest {
35  
36      @Override
37      public Sum getUnivariateStatistic() {
38          return new Sum();
39      }
40  
41      @Override
42      public double expectedValue() {
43          return this.sum;
44      }
45  
46      /** Expected value for the testArray defined in UnivariateStatisticAbstractTest */
47      public double expectedWeightedValue() {
48          return this.weightedSum;
49      }
50  
51      @Test
52      public void testSpecialValues() {
53          Sum sum = getUnivariateStatistic();
54          assertEquals(0, sum.getResult(), 0);
55          sum.increment(1);
56          assertEquals(1, sum.getResult(), 0);
57          sum.increment(Double.POSITIVE_INFINITY);
58          assertEquals(Double.POSITIVE_INFINITY, sum.getResult(), 0);
59          sum.increment(Double.NEGATIVE_INFINITY);
60          assertTrue(Double.isNaN(sum.getResult()));
61          sum.increment(1);
62          assertTrue(Double.isNaN(sum.getResult()));
63      }
64  
65      @Test
66      public void testWeightedSum() {
67          Sum sum = new Sum();
68          assertEquals(expectedWeightedValue(),
69                       sum.evaluate(testArray, testWeightsArray, 0, testArray.length), getTolerance());
70          assertEquals(expectedValue(),
71                       sum.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance());
72      }
73  
74      @Override
75      protected void checkClearValue(StorelessUnivariateStatistic statistic) {
76          assertEquals(0, statistic.getResult(), 0);
77      }
78  
79  }