/*
 * Copyright 2003-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math.stat.univariate;

import org.apache.commons.math.TestUtils;
import org.apache.commons.math.stat.univariate.moment.SecondMoment;

/**
 * Test cases for {@link StorelessUnivariateStatistic} classes.
 * @version $Revision: 1.14 $ $Date: 2004/06/29 06:11:22 $
 */
public abstract class StorelessUnivariateStatisticAbstractTest
    extends UnivariateStatisticAbstractTest {

    public StorelessUnivariateStatisticAbstractTest(String name) {
        super(name);
    }

    /** Return a new instance of the statistic */
    public abstract UnivariateStatistic getUnivariateStatistic();

    /**Expected value for  the testArray defined in UnivariateStatisticAbstractTest */
    public abstract double expectedValue();
    
    /** Verify that calling increment() in a loop over testArray results in correct state */
    public void testIncrementation() throws Exception {

        StorelessUnivariateStatistic statistic =
            (StorelessUnivariateStatistic) getUnivariateStatistic();

        statistic.clear();

        for (int i = 0; i < testArray.length; i++) {
            statistic.increment(testArray[i]);
        }

        assertEquals(expectedValue(), statistic.getResult(), getTolerance());
        //TODO:  add test for getN() once type is fixed

        statistic.clear();

        assertTrue(Double.isNaN(statistic.getResult()));

    }

    public void testSerialization() throws Exception {

        StorelessUnivariateStatistic statistic =
            (StorelessUnivariateStatistic) getUnivariateStatistic();
        
        TestUtils.checkSerializedEquality(statistic);

        statistic.clear();

        for (int i = 0; i < testArray.length; i++) {
            statistic.increment(testArray[i]);
            if(i % 5 == 0)
                statistic = (StorelessUnivariateStatistic)TestUtils.serializeAndRecover(statistic); 
        }
        
        TestUtils.checkSerializedEquality(statistic);
        
        assertEquals(expectedValue(), statistic.getResult(), getTolerance());

        statistic.clear();

        assertTrue(Double.isNaN(statistic.getResult()));

    }
    
    public void testEqualsAndHashCode() {
        StorelessUnivariateStatistic statistic =
            (StorelessUnivariateStatistic) getUnivariateStatistic();
        StorelessUnivariateStatistic statistic2 = null;
        
        assertTrue("non-null, compared to null", !statistic.equals(statistic2));
        assertTrue("reflexive, non-null", statistic.equals(statistic));
        
        int emptyHash = statistic.hashCode();
        statistic2 = (StorelessUnivariateStatistic) getUnivariateStatistic();
        assertTrue("empty stats should be equal", statistic.equals(statistic2));
        assertEquals("empty stats should have the same hashcode", 
                emptyHash, statistic2.hashCode());
        
        statistic.increment(1d);
        assertTrue("reflexive, non-empty", statistic.equals(statistic));
        assertTrue("non-empty, compared to empty", !statistic.equals(statistic2));
        assertTrue("non-empty, compared to empty", !statistic2.equals(statistic));
        assertTrue("non-empty stat should have different hashcode from empty stat",
                statistic.hashCode() != emptyHash);
        
        statistic2.increment(1d);
        assertTrue("stats with same data should be equal", statistic.equals(statistic2));
        assertEquals("stats with same data should have the same hashcode", 
                statistic.hashCode(), statistic2.hashCode());
        
        statistic.increment(Double.POSITIVE_INFINITY);
        assertTrue("stats with different n's should not be equal", !statistic2.equals(statistic));
        assertTrue("stats with different n's should have different hashcodes",
                statistic.hashCode() != statistic2.hashCode());
        
        statistic2.increment(Double.POSITIVE_INFINITY);
        assertTrue("stats with same data should be equal", statistic.equals(statistic2));
        assertEquals("stats with same data should have the same hashcode", 
                statistic.hashCode(), statistic2.hashCode()); 
        
        statistic.clear();
        statistic2.clear();
        assertTrue("cleared stats should be equal", statistic.equals(statistic2));
        assertEquals("cleared stats should have thashcode of empty stat", 
                emptyHash, statistic2.hashCode());
        assertEquals("cleared stats should have thashcode of empty stat", 
                emptyHash, statistic.hashCode());
        
    }
    
    public void testMomentSmallSamples() {
        UnivariateStatistic stat = getUnivariateStatistic();
        if (stat instanceof SecondMoment) {
            SecondMoment moment = (SecondMoment) getUnivariateStatistic();
            assertTrue(Double.isNaN(moment.getResult()));
            moment.increment(1d);
            assertEquals(0d, moment.getResult(), 0);
        }
    }

}

@@ -872,17 +872,17 @@
 ion: 1.1
-4
+5
  $ $Date
@@ -893,21 +893,21 @@
 04/0
-6/29 06:11:22
+7/07 12:50:50
  $%0A 
@@ -5554,12 +5554,404 @@
  %7D%0A    %7D
+%0A    %0A    /** %0A     * Make sure that evaluate(double%5B%5D) and inrementAll(double%5B%5D), %0A     * getResult() give same results.%0A     */%0A    public void testConsistency() %7B%0A        StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();%0A        stat.incrementAll(testArray);%0A        assertEquals(stat.getResult(), stat.evaluate(testArray), getTolerance());%0A    %7D
 %0A%0A%7D%0A

Add evaluate/increment consistency test.