KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > statistics > junit > StatisticsTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------
28  * StatisticsTests.java
29  * --------------------
30  * (C) Copyright 2004-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StatisticsTests.java,v 1.1.2.2 2006/11/16 11:19:47 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 25-Mar-2004 : Version 1 (DG);
40  * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
41  *
42  */

43
44 package org.jfree.data.statistics.junit;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.List JavaDoc;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.data.statistics.Statistics;
56
57 /**
58  * Tests for the {@link Statistics} class.
59  */

60 public class StatisticsTests extends TestCase {
61     
62     /**
63      * Returns the tests as a test suite.
64      *
65      * @return The test suite.
66      */

67     public static Test suite() {
68         return new TestSuite(StatisticsTests.class);
69     }
70
71     /**
72      * Constructs a new set of tests.
73      *
74      * @param name the name of the tests.
75      */

76     public StatisticsTests(String JavaDoc name) {
77         super(name);
78     }
79
80     /**
81      * Some checks for the calculateMean(Number[]) and
82      * calculateMean(Number[], boolean) methods.
83      */

84     public void testCalculateMean_Array() {
85         
86         // try null array
87
boolean pass = false;
88         try {
89             Statistics.calculateMean((Number JavaDoc[]) null);
90         }
91         catch (IllegalArgumentException JavaDoc e) {
92             pass = true;
93         }
94         assertTrue(pass);
95         
96         pass = false;
97         try {
98             Statistics.calculateMean((Number JavaDoc[]) null, false);
99         }
100         catch (IllegalArgumentException JavaDoc e) {
101             pass = true;
102         }
103         assertTrue(pass);
104         
105         // try an array containing no items
106
assertTrue(Double.isNaN(Statistics.calculateMean(new Number JavaDoc[0])));
107         assertTrue(Double.isNaN(Statistics.calculateMean(new Number JavaDoc[0],
108                 false)));
109         
110         // try an array containing a single Number
111
Number JavaDoc[] values = new Number JavaDoc[] { new Double JavaDoc(1.0) };
112         assertEquals(1.0, Statistics.calculateMean(values), EPSILON);
113         assertEquals(1.0, Statistics.calculateMean(values, true), EPSILON);
114         assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON);
115         
116         // try an array containing a single Number and a null
117
values = new Number JavaDoc[] { new Double JavaDoc(1.0), null };
118         assertTrue(Double.isNaN(Statistics.calculateMean(values)));
119         assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
120         assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON);
121
122         // try an array containing a single Number and a NaN
123
values = new Number JavaDoc[] { new Double JavaDoc(1.0), new Double JavaDoc(Double.NaN) };
124         assertTrue(Double.isNaN(Statistics.calculateMean(values)));
125         assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
126         assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON);
127     }
128     
129     /**
130      * Some checks for the calculateMean(Collection) and
131      * calculateMean(Collection, boolean) methods.
132      */

133     public void testCalculateMean_Collection() {
134         
135         // try a null collection
136
boolean pass = false;
137         try {
138             Statistics.calculateMean((Collection JavaDoc) null);
139         }
140         catch (IllegalArgumentException JavaDoc e) {
141             pass = true;
142         }
143         assertTrue(pass);
144         
145         pass = false;
146         try {
147             Statistics.calculateMean((Collection JavaDoc) null, false);
148         }
149         catch (IllegalArgumentException JavaDoc e) {
150             pass = true;
151         }
152         assertTrue(pass);
153         
154         // try an empty collection
155
List JavaDoc values = new ArrayList JavaDoc();
156         assertTrue(Double.isNaN(Statistics.calculateMean(values)));
157         assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
158         assertTrue(Double.isNaN(Statistics.calculateMean(values, false)));
159         
160         // try a collection with a single number
161
values.add(new Double JavaDoc(9.0));
162         assertEquals(9.0, Statistics.calculateMean(values), EPSILON);
163         assertEquals(9.0, Statistics.calculateMean(values, true), EPSILON);
164         assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON);
165         
166         // try a collection with a single number plus a null
167
values.add(null);
168         assertTrue(Double.isNaN(Statistics.calculateMean(values)));
169         assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
170         assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON);
171
172         // try a collection with a single number plus a NaN
173
values.clear();
174         values.add(new Double JavaDoc(9.0));
175         values.add(new Double JavaDoc(Double.NaN));
176         assertTrue(Double.isNaN(Statistics.calculateMean(values)));
177         assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
178         assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON);
179
180         // try a collection with several numbers
181
values = new ArrayList JavaDoc();
182         values.add(new Double JavaDoc(9.0));
183         values.add(new Double JavaDoc(3.0));
184         values.add(new Double JavaDoc(2.0));
185         values.add(new Double JavaDoc(2.0));
186         double mean = Statistics.calculateMean(values);
187         assertEquals(4.0, mean, EPSILON);
188         
189         // a Collection containing a NaN will return Double.NaN for the result
190
values.add(new Double JavaDoc(Double.NaN));
191         assertTrue(Double.isNaN(Statistics.calculateMean(values)));
192     }
193     
194     static final double EPSILON = 0.0000000001;
195     
196     /**
197      * Some checks for the calculateMedian(List, boolean) method.
198      */

199     public void testCalculateMedian() {
200         
201         // check null list
202
assertTrue(Double.isNaN(Statistics.calculateMedian(null, false)));
203         assertTrue(Double.isNaN(Statistics.calculateMedian(null, true)));
204         
205         // check empty list
206
List JavaDoc list = new ArrayList JavaDoc();
207         assertTrue(Double.isNaN(Statistics.calculateMedian(list, false)));
208         assertTrue(Double.isNaN(Statistics.calculateMedian(list, true)));
209         
210         // check list containing null
211
list.add(null);
212         boolean pass = false;
213         try {
214             Statistics.calculateMedian(list, false);
215         }
216         catch (NullPointerException JavaDoc e) {
217             pass = true;
218         }
219         assertTrue(pass);
220         
221         pass = false;
222         try {
223             Statistics.calculateMedian(list, true);
224         }
225         catch (NullPointerException JavaDoc e) {
226             pass = true;
227         }
228         assertTrue(pass);
229
230         // check a list containing a non-Number object
231
list.clear();
232         list.add("Not a number");
233         pass = false;
234         try {
235             Statistics.calculateMedian(list, false);
236         }
237         catch (ClassCastException JavaDoc e) {
238             pass = true;
239         }
240         assertTrue(pass);
241         
242         pass = false;
243         try {
244             Statistics.calculateMedian(list, true);
245         }
246         catch (ClassCastException JavaDoc e) {
247             pass = true;
248         }
249         assertTrue(pass);
250         
251     }
252     
253     /**
254      * A test for the calculateMedian() method.
255      */

256     public void testCalculateMedian1() {
257         List JavaDoc values = new ArrayList JavaDoc();
258         values.add(new Double JavaDoc(1.0));
259         double median = Statistics.calculateMedian(values);
260         assertEquals(1.0, median, 0.0000001);
261     }
262
263     /**
264      * A test for the calculateMedian() method.
265      */

266     public void testCalculateMedian2() {
267         List JavaDoc values = new ArrayList JavaDoc();
268         values.add(new Double JavaDoc(2.0));
269         values.add(new Double JavaDoc(1.0));
270         double median = Statistics.calculateMedian(values);
271         assertEquals(1.5, median, 0.0000001);
272     }
273
274     /**
275      * A test for the calculateMedian() method.
276      */

277     public void testCalculateMedian3() {
278         List JavaDoc values = new ArrayList JavaDoc();
279         values.add(new Double JavaDoc(1.0));
280         values.add(new Double JavaDoc(2.0));
281         values.add(new Double JavaDoc(3.0));
282         values.add(new Double JavaDoc(6.0));
283         values.add(new Double JavaDoc(5.0));
284         values.add(new Double JavaDoc(4.0));
285         double median = Statistics.calculateMedian(values);
286         assertEquals(3.5, median, 0.0000001);
287     }
288
289     /**
290      * A test for the calculateMedian() method.
291      */

292     public void testCalculateMedian4() {
293         List JavaDoc values = new ArrayList JavaDoc();
294         values.add(new Double JavaDoc(7.0));
295         values.add(new Double JavaDoc(2.0));
296         values.add(new Double JavaDoc(3.0));
297         values.add(new Double JavaDoc(5.0));
298         values.add(new Double JavaDoc(4.0));
299         values.add(new Double JavaDoc(6.0));
300         values.add(new Double JavaDoc(1.0));
301         double median = Statistics.calculateMedian(values);
302         assertEquals(4.0, median, 0.0000001);
303     }
304     
305     /**
306      * A test using some real data that caused a problem at one point.
307      */

308     public void testCalculateMedian5() {
309         List JavaDoc values = new ArrayList JavaDoc();
310         values.add(new Double JavaDoc(11.228692993861783));
311         values.add(new Double JavaDoc(11.30823353859889));
312         values.add(new Double JavaDoc(11.75312904769314));
313         values.add(new Double JavaDoc(11.825102897465314));
314         values.add(new Double JavaDoc(10.184252778401783));
315         values.add(new Double JavaDoc(12.207951828057766));
316         values.add(new Double JavaDoc(10.68841994040566));
317         values.add(new Double JavaDoc(12.099522004479438));
318         values.add(new Double JavaDoc(11.508874945056881));
319         values.add(new Double JavaDoc(12.052517729558513));
320         values.add(new Double JavaDoc(12.401481645578734));
321         values.add(new Double JavaDoc(12.185377793028543));
322         values.add(new Double JavaDoc(10.666372951930315));
323         values.add(new Double JavaDoc(11.680978041499548));
324         values.add(new Double JavaDoc(11.06528277406718));
325         values.add(new Double JavaDoc(11.36876492904596));
326         values.add(new Double JavaDoc(11.927565516175939));
327         values.add(new Double JavaDoc(11.39307785978655));
328         values.add(new Double JavaDoc(11.989603679523857));
329         values.add(new Double JavaDoc(12.009834360354864));
330         values.add(new Double JavaDoc(10.653351822461559));
331         values.add(new Double JavaDoc(11.851776254376754));
332         values.add(new Double JavaDoc(11.045441544755946));
333         values.add(new Double JavaDoc(11.993674040560624));
334         values.add(new Double JavaDoc(12.898219965238944));
335         values.add(new Double JavaDoc(11.97095782819647));
336         values.add(new Double JavaDoc(11.73234406745488));
337         values.add(new Double JavaDoc(11.649006017243991));
338         values.add(new Double JavaDoc(12.20549704915365));
339         values.add(new Double JavaDoc(11.799723639384919));
340         values.add(new Double JavaDoc(11.896208658005628));
341         values.add(new Double JavaDoc(12.164149111823424));
342         values.add(new Double JavaDoc(12.042795103513766));
343         values.add(new Double JavaDoc(12.114839532596426));
344         values.add(new Double JavaDoc(12.166609097075824));
345         values.add(new Double JavaDoc(12.183017546225935));
346         values.add(new Double JavaDoc(11.622009125845342));
347         values.add(new Double JavaDoc(11.289365786738633));
348         values.add(new Double JavaDoc(12.462984323671568));
349         values.add(new Double JavaDoc(11.573494921030598));
350         values.add(new Double JavaDoc(10.862867940485804));
351         values.add(new Double JavaDoc(12.018186939664872));
352         values.add(new Double JavaDoc(10.418046849313018));
353         values.add(new Double JavaDoc(11.326344465881341));
354         double median = Statistics.calculateMedian(values, true);
355         assertEquals(11.812413268425116, median, 0.000001);
356         Collections.sort(values);
357         double median2 = Statistics.calculateMedian(values, false);
358         assertEquals(11.812413268425116, median2, 0.000001);
359     }
360
361     /**
362      * A test for the calculateMedian() method.
363      */

364     public void testCalculateMedian6() {
365         List JavaDoc values = new ArrayList JavaDoc();
366         values.add(new Double JavaDoc(7.0));
367         values.add(new Double JavaDoc(2.0));
368         values.add(new Double JavaDoc(3.0));
369         values.add(new Double JavaDoc(5.0));
370         values.add(new Double JavaDoc(4.0));
371         values.add(new Double JavaDoc(6.0));
372         values.add(new Double JavaDoc(1.0));
373         double median = Statistics.calculateMedian(values, 0, 2);
374         assertEquals(3.0, median, 0.0000001);
375     }
376     
377     /**
378      * A simple test for the correlation calculation.
379      */

380     public void testCorrelation1() {
381         Number JavaDoc[] data1 = new Number JavaDoc[3];
382         data1[0] = new Double JavaDoc(1);
383         data1[1] = new Double JavaDoc(2);
384         data1[2] = new Double JavaDoc(3);
385         Number JavaDoc[] data2 = new Number JavaDoc[3];
386         data2[0] = new Double JavaDoc(1);
387         data2[1] = new Double JavaDoc(2);
388         data2[2] = new Double JavaDoc(3);
389         double r = Statistics.getCorrelation(data1, data2);
390         assertEquals(1.0, r, 0.00000001);
391     }
392
393     /**
394      * A simple test for the correlation calculation.
395      *
396      * http://trochim.human.cornell.edu/kb/statcorr.htm
397      */

398     public void testCorrelation2() {
399         Number JavaDoc[] data1 = new Number JavaDoc[20];
400         data1[0] = new Double JavaDoc(68);
401         data1[1] = new Double JavaDoc(71);
402         data1[2] = new Double JavaDoc(62);
403         data1[3] = new Double JavaDoc(75);
404         data1[4] = new Double JavaDoc(58);
405         data1[5] = new Double JavaDoc(60);
406         data1[6] = new Double JavaDoc(67);
407         data1[7] = new Double JavaDoc(68);
408         data1[8] = new Double JavaDoc(71);
409         data1[9] = new Double JavaDoc(69);
410         data1[10] = new Double JavaDoc(68);
411         data1[11] = new Double JavaDoc(67);
412         data1[12] = new Double JavaDoc(63);
413         data1[13] = new Double JavaDoc(62);
414         data1[14] = new Double JavaDoc(60);
415         data1[15] = new Double JavaDoc(63);
416         data1[16] = new Double JavaDoc(65);
417         data1[17] = new Double JavaDoc(67);
418         data1[18] = new Double JavaDoc(63);
419         data1[19] = new Double JavaDoc(61);
420         Number JavaDoc[] data2 = new Number JavaDoc[20];
421         data2[0] = new Double JavaDoc(4.1);
422         data2[1] = new Double JavaDoc(4.6);
423         data2[2] = new Double JavaDoc(3.8);
424         data2[3] = new Double JavaDoc(4.4);
425         data2[4] = new Double JavaDoc(3.2);
426         data2[5] = new Double JavaDoc(3.1);
427         data2[6] = new Double JavaDoc(3.8);
428         data2[7] = new Double JavaDoc(4.1);
429         data2[8] = new Double JavaDoc(4.3);
430         data2[9] = new Double JavaDoc(3.7);
431         data2[10] = new Double JavaDoc(3.5);
432         data2[11] = new Double JavaDoc(3.2);
433         data2[12] = new Double JavaDoc(3.7);
434         data2[13] = new Double JavaDoc(3.3);
435         data2[14] = new Double JavaDoc(3.4);
436         data2[15] = new Double JavaDoc(4.0);
437         data2[16] = new Double JavaDoc(4.1);
438         data2[17] = new Double JavaDoc(3.8);
439         data2[18] = new Double JavaDoc(3.4);
440         data2[19] = new Double JavaDoc(3.6);
441         double r = Statistics.getCorrelation(data1, data2);
442         assertEquals(0.7306356862792885, r, 0.000000000001);
443     }
444     
445     /**
446      * Some checks for the getStdDev() method.
447      */

448     public void testGetStdDev() {
449         
450         // try null argument
451
boolean pass = false;
452         try {
453             Statistics.getStdDev(null);
454         }
455         catch (IllegalArgumentException JavaDoc e) {
456             pass = true;
457         }
458         assertTrue(pass);
459         
460         // try zero length array
461
pass = false;
462         try {
463             Statistics.getStdDev(new Double JavaDoc[0]);
464         }
465         catch (IllegalArgumentException JavaDoc e) {
466             pass = true;
467         }
468         assertTrue(pass);
469         
470         // try single value
471
assertTrue(Double.isNaN(Statistics.getStdDev(new Double JavaDoc[]
472                 {new Double JavaDoc(1.0)})));
473     }
474
475 }
476
Popular Tags