KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > stat > univariate > BeanListUnivariateImplTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.univariate;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.math.TestUtils;
26 import org.apache.commons.math.stat.StatUtils;
27
28 /**
29  * Test cases for the {@link BeanListUnivariateImpl} class.
30  *
31  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
32  */

33
34 public final class BeanListUnivariateImplTest extends TestCase {
35     
36     private double one = 1;
37     private float two = 2;
38     private int three = 3;
39     private double mean = 2;
40     private double sumSq = 18;
41     private double sum = 8;
42     private double var = 0.666666666666666666667;
43     private double std = Math.sqrt(var);
44     private double n = 4;
45     private double min = 1;
46     private double max = 3;
47     private double skewness = 0;
48     private double kurtosis = 0.5;
49     private double tolerance = 10E-15;
50     
51     
52     private List JavaDoc patientList = null;
53     
54     public BeanListUnivariateImplTest(String JavaDoc name) {
55         super(name);
56     }
57     
58     public void setUp() {
59         patientList = new ArrayList JavaDoc();
60
61         // Create and add patient bean 1
62
VitalStats vs1 = new VitalStats( new Double JavaDoc(120.0),
63                                          new Double JavaDoc(96.4) );
64         Patient p1 = new Patient( vs1, new Integer JavaDoc( 35 ) );
65         patientList.add( p1 );
66
67         // Create and add patient bean 2
68
VitalStats vs2 = new VitalStats( new Double JavaDoc(70.0),
69                                          new Double JavaDoc(97.4) );
70         Patient p2 = new Patient( vs2, new Integer JavaDoc( 23 ) );
71         patientList.add( p2 );
72
73         // Create and add patient bean 3
74
VitalStats vs3 = new VitalStats( new Double JavaDoc(90.0),
75                                          new Double JavaDoc(98.6) );
76         Patient p3 = new Patient( vs3, new Integer JavaDoc( 42 ) );
77         patientList.add( p3 );
78     }
79     
80     public static Test suite() {
81         TestSuite suite = new TestSuite(BeanListUnivariateImplTest.class);
82         suite.setName("Frequency Tests");
83         return suite;
84     }
85     
86     /** test stats */
87     public void testStats() {
88         DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" );
89         double[] values = {35d, 23d, 42d};
90         assertEquals("total count",3,u.getN(),tolerance);
91         assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
92         assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
93         assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
94         assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);
95         u.clear();
96         assertEquals("total count",0,u.getN(),tolerance);
97     }
98     
99     public void testPropStats() {
100
101         DescriptiveStatistics heartU = new BeanListUnivariateImpl( patientList,
102                                           "vitalStats.heartRate" );
103
104         assertEquals( "Mean heart rate unexpected", 93.333,
105                       heartU.getMean(), 0.001 );
106         assertEquals( "Max heart rate unexpected", 120.0,
107                       heartU.getMax(), 0.001 );
108
109         DescriptiveStatistics ageU = new BeanListUnivariateImpl( patientList,
110                                                            "age" );
111
112         assertEquals( "Mean age unexpected", 33.333,
113                       ageU.getMean(), 0.001 );
114         assertEquals( "Max age unexpected", 42.0,
115                       ageU.getMax(), 0.001 );
116
117     }
118     
119     public void testSetPropertyName(){
120         BeanListUnivariateImpl u = new BeanListUnivariateImpl(null);
121         String JavaDoc expected = "property";
122         u.setPropertyName(expected);
123         assertEquals(expected, u.getPropertyName());
124     }
125     
126     public void testAddValue() {
127         DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" );
128         u.addValue(10);
129         double[] values = {35d, 23d, 42d, 10d};
130         assertEquals("total count",4,u.getN(),tolerance);
131         assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
132         assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
133         assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
134         assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);
135         u.clear();
136         assertEquals("total count",0,u.getN(),tolerance);
137     }
138     
139     /** test stats */
140     public void testSerialization() {
141         
142         double[] values = {35d, 23d, 42d};
143         
144         DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" );
145         assertEquals("total count",3,u.getN(),tolerance);
146         assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
147         assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
148         assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
149         assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);
150         
151         
152         DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
153         assertEquals("total count",3,u2.getN(),tolerance);
154         assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance);
155         assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance);
156         assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance);
157         assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance);
158
159         u.clear();
160         assertEquals("total count",0,u.getN(),tolerance);
161         
162         u2.clear();
163         assertEquals("total count",0,u2.getN(),tolerance);
164             
165     }
166     
167     public class VitalStats {
168
169         private Double JavaDoc heartrate;
170         private Double JavaDoc temperature;
171
172         public VitalStats() {
173         }
174
175         public VitalStats(Double JavaDoc heartrate, Double JavaDoc temperature) {
176             setHeartRate( heartrate );
177             setTemperature( temperature );
178         }
179
180         public Double JavaDoc getHeartRate() {
181             return heartrate;
182         }
183
184         public void setHeartRate(Double JavaDoc heartrate) {
185             this.heartrate = heartrate;
186         }
187
188         public Double JavaDoc getTemperature() {
189             return temperature;
190         }
191
192         public void setTemperature(Double JavaDoc temperature) {
193             this.temperature = temperature;
194         }
195     }
196     
197     public class Patient {
198
199         private VitalStats vitalStats;
200         private Integer JavaDoc age;
201
202         public Patient() {
203         }
204
205         public Patient(VitalStats vitalStats, Integer JavaDoc age) {
206             setVitalStats( vitalStats );
207             setAge( age );
208         }
209
210         public VitalStats getVitalStats() {
211             return( vitalStats );
212         }
213
214         public void setVitalStats(VitalStats vitalStats) {
215             this.vitalStats = vitalStats;
216         }
217
218         public Integer JavaDoc getAge() {
219             return age;
220         }
221
222         public void setAge(Integer JavaDoc age) {
223             this.age = age;
224         }
225     }
226 }
227
228
Popular Tags