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.summary;
18  
19  import org.apache.commons.math4.legacy.stat.descriptive.StorelessUnivariateStatistic;
20  import org.apache.commons.math4.legacy.stat.descriptive.StorelessUnivariateStatisticAbstractTest;
21  import org.apache.commons.math4.legacy.stat.descriptive.UnivariateStatistic;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Test cases for the {@link UnivariateStatistic} class.
27   */
28  public class ProductTest extends StorelessUnivariateStatisticAbstractTest{
29  
30      protected Product stat;
31  
32      /**
33       * {@inheritDoc}
34       */
35      @Override
36      public UnivariateStatistic getUnivariateStatistic() {
37          return new Product();
38      }
39  
40      /**
41       * {@inheritDoc}
42       */
43      @Override
44      public double getTolerance() {
45          return 10E8;    //sic -- big absolute error due to only 15 digits of accuracy in double
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      public double expectedValue() {
53          return this.product;
54      }
55  
56      /** Expected value for  the testArray defined in UnivariateStatisticAbstractTest */
57      public double expectedWeightedValue() {
58          return this.weightedProduct;
59      }
60  
61      @Test
62      public void testSpecialValues() {
63          Product product = new Product();
64          Assert.assertEquals(1, product.getResult(), 0);
65          product.increment(1);
66          Assert.assertEquals(1, product.getResult(), 0);
67          product.increment(Double.POSITIVE_INFINITY);
68          Assert.assertEquals(Double.POSITIVE_INFINITY, product.getResult(), 0);
69          product.increment(Double.NEGATIVE_INFINITY);
70          Assert.assertEquals(Double.NEGATIVE_INFINITY, product.getResult(), 0);
71          product.increment(Double.NaN);
72          Assert.assertTrue(Double.isNaN(product.getResult()));
73          product.increment(1);
74          Assert.assertTrue(Double.isNaN(product.getResult()));
75      }
76  
77      @Test
78      public void testWeightedProduct() {
79          Product product = new Product();
80          Assert.assertEquals(expectedWeightedValue(),
81                              product.evaluate(testArray, testWeightsArray, 0, testArray.length),getTolerance());
82          Assert.assertEquals(expectedValue(),
83                              product.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance());
84      }
85  
86      @Override
87      protected void checkClearValue(StorelessUnivariateStatistic statistic){
88          Assert.assertEquals(1, statistic.getResult(), 0);
89      }
90  }