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 Product} class.
33   */
34  public class ProductTest extends StorelessUnivariateStatisticAbstractTest {
35  
36      @Override
37      public Product getUnivariateStatistic() {
38          return new Product();
39      }
40  
41      @Override
42      public double getTolerance() {
43          return 10E8;    //sic -- big absolute error due to only 15 digits of accuracy in double
44      }
45  
46      @Override
47      public double expectedValue() {
48          return this.product;
49      }
50  
51      /** Expected value for the testArray defined in UnivariateStatisticAbstractTest */
52      public double expectedWeightedValue() {
53          return this.weightedProduct;
54      }
55  
56      @Test
57      public void testSpecialValues() {
58          Product product = getUnivariateStatistic();
59          assertEquals(1, product.getResult(), 0);
60          product.increment(1);
61          assertEquals(1, product.getResult(), 0);
62          product.increment(Double.POSITIVE_INFINITY);
63          assertEquals(Double.POSITIVE_INFINITY, product.getResult(), 0);
64          product.increment(Double.NEGATIVE_INFINITY);
65          assertEquals(Double.NEGATIVE_INFINITY, product.getResult(), 0);
66          product.increment(Double.NaN);
67          assertTrue(Double.isNaN(product.getResult()));
68          product.increment(1);
69          assertTrue(Double.isNaN(product.getResult()));
70      }
71  
72      @Test
73      public void testWeightedProduct() {
74          Product product = new Product();
75          assertEquals(expectedWeightedValue(),
76                       product.evaluate(testArray, testWeightsArray, 0, testArray.length),getTolerance());
77          assertEquals(expectedValue(),
78                       product.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance());
79      }
80  
81      @Override
82      protected void checkClearValue(StorelessUnivariateStatistic statistic){
83          assertEquals(1, statistic.getResult(), 0);
84      }
85  
86  }