01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.math.stat.descriptive;
17:
18: import junit.framework.Test;
19: import junit.framework.TestCase;
20: import junit.framework.TestSuite;
21:
22: import org.apache.commons.math.TestUtils;
23:
24: /**
25: * Test cases for the {@link StatisticalSummaryValues} class.
26: *
27: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
28: */
29:
30: public final class StatisticalSummaryValuesTest extends TestCase {
31:
32: public StatisticalSummaryValuesTest(String name) {
33: super (name);
34: }
35:
36: public void setUp() {
37: }
38:
39: public static Test suite() {
40: TestSuite suite = new TestSuite(
41: StatisticalSummaryValuesTest.class);
42: suite.setName("StatisticalSummaryValues Tests");
43: return suite;
44: }
45:
46: public void testSerialization() {
47: StatisticalSummaryValues u = new StatisticalSummaryValues(1, 2,
48: 3, 4, 5, 6);
49: TestUtils.checkSerializedEquality(u);
50: StatisticalSummaryValues t = (StatisticalSummaryValues) TestUtils
51: .serializeAndRecover(u);
52: verifyEquality(u, t);
53: }
54:
55: public void testEqualsAndHashCode() {
56: StatisticalSummaryValues u = new StatisticalSummaryValues(1, 2,
57: 3, 4, 5, 6);
58: StatisticalSummaryValues t = null;
59: int emptyHash = u.hashCode();
60: assertTrue("reflexive", u.equals(u));
61: assertFalse("non-null compared to null", u.equals(t));
62: assertFalse("wrong type", u.equals(new Double(0)));
63: t = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
64: assertTrue("instances with same data should be equal", t
65: .equals(u));
66: assertEquals("hash code", u.hashCode(), t.hashCode());
67:
68: u = new StatisticalSummaryValues(Double.NaN, 2, 3, 4, 5, 6);
69: t = new StatisticalSummaryValues(1, Double.NaN, 3, 4, 5, 6);
70: assertFalse(
71: "instances based on different data should be different",
72: (u.equals(t) || t.equals(u)));
73: }
74:
75: private void verifyEquality(StatisticalSummaryValues s,
76: StatisticalSummaryValues u) {
77: assertEquals("N", s.getN(), u.getN());
78: TestUtils.assertEquals("sum", s.getSum(), u.getSum(), 0);
79: TestUtils.assertEquals("var", s.getVariance(), u.getVariance(),
80: 0);
81: TestUtils.assertEquals("std", s.getStandardDeviation(), u
82: .getStandardDeviation(), 0);
83: TestUtils.assertEquals("mean", s.getMean(), u.getMean(), 0);
84: TestUtils.assertEquals("min", s.getMin(), u.getMin(), 0);
85: TestUtils.assertEquals("max", s.getMax(), u.getMax(), 0);
86: }
87: }
|