001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.stat.descriptive;
018    
019    
020    import junit.framework.Test;
021    import junit.framework.TestCase;
022    import junit.framework.TestSuite;
023    
024    import org.apache.commons.math.TestUtils;
025    /**
026     * Test cases for the {@link StatisticalSummaryValues} class.
027     *
028     * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
029     */
030    
031    public final class StatisticalSummaryValuesTest extends TestCase {
032        
033        
034        public StatisticalSummaryValuesTest(String name) {
035            super(name);
036        }
037        
038        public static Test suite() {
039            TestSuite suite = new TestSuite(StatisticalSummaryValuesTest.class);
040            suite.setName("StatisticalSummaryValues Tests");
041            return suite;
042        }
043          
044        public void testSerialization() {
045            StatisticalSummaryValues u = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
046            TestUtils.checkSerializedEquality(u); 
047            StatisticalSummaryValues t = (StatisticalSummaryValues) TestUtils.serializeAndRecover(u);
048            verifyEquality(u, t);
049        }
050        
051        public void testEqualsAndHashCode() {
052            StatisticalSummaryValues u  = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
053            StatisticalSummaryValues t = null;
054            assertTrue("reflexive", u.equals(u));
055            assertFalse("non-null compared to null", u.equals(t));
056            assertFalse("wrong type", u.equals(Double.valueOf(0)));
057            t = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
058            assertTrue("instances with same data should be equal", t.equals(u));
059            assertEquals("hash code", u.hashCode(), t.hashCode());
060            
061            u = new StatisticalSummaryValues(Double.NaN, 2, 3, 4, 5, 6);
062            t = new StatisticalSummaryValues(1, Double.NaN, 3, 4, 5, 6);
063            assertFalse("instances based on different data should be different", 
064                    (u.equals(t) ||t.equals(u)));
065        }
066        
067        private void verifyEquality(StatisticalSummaryValues s, StatisticalSummaryValues u) {
068            assertEquals("N",s.getN(),u.getN());
069            TestUtils.assertEquals("sum",s.getSum(),u.getSum(), 0);
070            TestUtils.assertEquals("var",s.getVariance(),u.getVariance(), 0);
071            TestUtils.assertEquals("std",s.getStandardDeviation(),u.getStandardDeviation(), 0);
072            TestUtils.assertEquals("mean",s.getMean(),u.getMean(), 0);
073            TestUtils.assertEquals("min",s.getMin(),u.getMin(), 0);
074            TestUtils.assertEquals("max",s.getMax(),u.getMax(), 0);   
075        }
076    }