001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------------------
028: * BoxAndWhiskerCalculatorTests.java
029: * ---------------------------------
030: * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: BoxAndWhiskerCalculatorTests.java,v 1.1.2.2 2006/11/16 11:19:47 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 28-Aug-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.statistics.junit;
044:
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: import junit.framework.Test;
049: import junit.framework.TestCase;
050: import junit.framework.TestSuite;
051:
052: import org.jfree.data.statistics.BoxAndWhiskerCalculator;
053: import org.jfree.data.statistics.BoxAndWhiskerItem;
054:
055: /**
056: * Tests for the {@link BoxAndWhiskerCalculator} class.
057: */
058: public class BoxAndWhiskerCalculatorTests extends TestCase {
059:
060: /**
061: * Returns the tests as a test suite.
062: *
063: * @return The test suite.
064: */
065: public static Test suite() {
066: return new TestSuite(BoxAndWhiskerCalculatorTests.class);
067: }
068:
069: /**
070: * Constructs a new set of tests.
071: *
072: * @param name the name of the tests.
073: */
074: public BoxAndWhiskerCalculatorTests(String name) {
075: super (name);
076: }
077:
078: /**
079: * Some checks for the calculateBoxAndWhiskerStatistics() method.
080: */
081: public void testCalculateBoxAndWhiskerStatistics() {
082:
083: // try null list
084: boolean pass = false;
085: try {
086: BoxAndWhiskerCalculator
087: .calculateBoxAndWhiskerStatistics(null);
088: } catch (IllegalArgumentException e) {
089: pass = true;
090: }
091: assertTrue(pass);
092:
093: // try a list containing a single value
094: List values = new ArrayList();
095: values.add(new Double(1.1));
096: BoxAndWhiskerItem item = BoxAndWhiskerCalculator
097: .calculateBoxAndWhiskerStatistics(values);
098: assertEquals(1.1, item.getMean().doubleValue(), EPSILON);
099: assertEquals(1.1, item.getMedian().doubleValue(), EPSILON);
100: assertEquals(1.1, item.getQ1().doubleValue(), EPSILON);
101: assertEquals(1.1, item.getQ3().doubleValue(), EPSILON);
102: }
103:
104: private static final double EPSILON = 0.000000001;
105:
106: /**
107: * Tests the Q1 calculation.
108: */
109: public void testCalculateQ1() {
110:
111: // try null argument
112: boolean pass = false;
113: try {
114: BoxAndWhiskerCalculator.calculateQ1(null);
115: } catch (IllegalArgumentException e) {
116: pass = true;
117: }
118: assertTrue(pass);
119:
120: List values = new ArrayList();
121: double q1 = BoxAndWhiskerCalculator.calculateQ1(values);
122: assertTrue(Double.isNaN(q1));
123: values.add(new Double(1.0));
124: q1 = BoxAndWhiskerCalculator.calculateQ1(values);
125: assertEquals(q1, 1.0, EPSILON);
126: values.add(new Double(2.0));
127: q1 = BoxAndWhiskerCalculator.calculateQ1(values);
128: assertEquals(q1, 1.0, EPSILON);
129: values.add(new Double(3.0));
130: q1 = BoxAndWhiskerCalculator.calculateQ1(values);
131: assertEquals(q1, 1.5, EPSILON);
132: values.add(new Double(4.0));
133: q1 = BoxAndWhiskerCalculator.calculateQ1(values);
134: assertEquals(q1, 1.5, EPSILON);
135: }
136:
137: /**
138: * Tests the Q3 calculation.
139: */
140: public void testCalculateQ3() {
141: // try null argument
142: boolean pass = false;
143: try {
144: BoxAndWhiskerCalculator.calculateQ3(null);
145: } catch (IllegalArgumentException e) {
146: pass = true;
147: }
148: assertTrue(pass);
149:
150: List values = new ArrayList();
151: double q3 = BoxAndWhiskerCalculator.calculateQ3(values);
152: assertTrue(Double.isNaN(q3));
153: values.add(new Double(1.0));
154: q3 = BoxAndWhiskerCalculator.calculateQ3(values);
155: assertEquals(q3, 1.0, EPSILON);
156: values.add(new Double(2.0));
157: q3 = BoxAndWhiskerCalculator.calculateQ3(values);
158: assertEquals(q3, 2.0, EPSILON);
159: values.add(new Double(3.0));
160: q3 = BoxAndWhiskerCalculator.calculateQ3(values);
161: assertEquals(q3, 2.5, EPSILON);
162: values.add(new Double(4.0));
163: q3 = BoxAndWhiskerCalculator.calculateQ3(values);
164: assertEquals(q3, 3.5, EPSILON);
165: }
166:
167: /**
168: * The test case included in bug report 1593149.
169: */
170: public void test1593149() {
171: ArrayList theList = new ArrayList(5);
172: theList.add(0, new Double(1.0));
173: theList.add(1, new Double(2.0));
174: theList.add(2, new Double(Double.NaN));
175: theList.add(3, new Double(3.0));
176: theList.add(4, new Double(4.0));
177: BoxAndWhiskerItem theItem = BoxAndWhiskerCalculator
178: .calculateBoxAndWhiskerStatistics(theList);
179: assertEquals(1.0, theItem.getMinRegularValue().doubleValue(),
180: EPSILON);
181: assertEquals(4.0, theItem.getMaxRegularValue().doubleValue(),
182: EPSILON);
183: }
184: }
|