KickJava   Java API By Example, From Geeks To Geeks.

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


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  * HistogramDatasetTests.java
29  * --------------------------
30  * (C) Copyright 2004-2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: HistogramDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 01-Mar-2004 : Version 1 (DG);
40  * 08-Jun-2005 : Added test for getSeriesKey(int) bug (DG);
41  * 03-Aug-2006 : Added testAddSeries() and testBinBoundaries() method (DG);
42  *
43  */

44
45 package org.jfree.data.statistics.junit;
46
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectInputStream JavaDoc;
51 import java.io.ObjectOutput JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.data.statistics.HistogramDataset;
59
60 /**
61  * Tests for the {@link HistogramDataset} class.
62  */

63 public class HistogramDatasetTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(HistogramDatasetTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public HistogramDatasetTests(String JavaDoc name) {
80         super(name);
81     }
82
83     private static final double EPSILON = 0.0000000001;
84     
85     /**
86      * Some checks that the correct values are assigned to bins.
87      */

88     public void testBins() {
89         double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
90         HistogramDataset hd = new HistogramDataset();
91         hd.addSeries("Series 1", values, 5);
92         assertEquals(hd.getYValue(0, 0), 3.0, EPSILON);
93         assertEquals(hd.getYValue(0, 1), 3.0, EPSILON);
94         assertEquals(hd.getYValue(0, 2), 2.0, EPSILON);
95         assertEquals(hd.getYValue(0, 3), 0.0, EPSILON);
96         assertEquals(hd.getYValue(0, 4), 1.0, EPSILON);
97     }
98     
99     /**
100      * Confirm that the equals method can distinguish all the required fields.
101      */

102     public void testEquals() {
103         
104         double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
105         HistogramDataset d1 = new HistogramDataset();
106         d1.addSeries("Series 1", values, 5);
107         HistogramDataset d2 = new HistogramDataset();
108         d2.addSeries("Series 1", values, 5);
109         
110         assertTrue(d1.equals(d2));
111         assertTrue(d2.equals(d1));
112
113     }
114
115     /**
116      * Confirm that cloning works.
117      */

118     public void testCloning() {
119         double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
120         HistogramDataset d1 = new HistogramDataset();
121         d1.addSeries("Series 1", values, 5);
122         HistogramDataset d2 = null;
123         try {
124             d2 = (HistogramDataset) d1.clone();
125         }
126         catch (CloneNotSupportedException JavaDoc e) {
127             System.err.println("Failed to clone.");
128         }
129         assertTrue(d1 != d2);
130         assertTrue(d1.getClass() == d2.getClass());
131         assertTrue(d1.equals(d2));
132     }
133
134     /**
135      * Serialize an instance, restore it, and check for equality.
136      */

137     public void testSerialization() {
138         double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
139         HistogramDataset d1 = new HistogramDataset();
140         d1.addSeries("Series 1", values, 5);
141         HistogramDataset d2 = null;
142
143         try {
144             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
145             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
146             out.writeObject(d1);
147             out.close();
148
149             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
150                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
151             );
152             d2 = (HistogramDataset) in.readObject();
153             in.close();
154         }
155         catch (Exception JavaDoc e) {
156             System.out.println(e.toString());
157         }
158         assertEquals(d1, d2);
159     }
160     
161     /**
162      * A test for a bug reported in the forum where the series name isn't being
163      * returned correctly.
164      */

165     public void testGetSeriesKey() {
166         double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
167         HistogramDataset d1 = new HistogramDataset();
168         d1.addSeries("Series 1", values, 5);
169         assertEquals("Series 1", d1.getSeriesKey(0));
170     }
171     
172     /**
173      * Some checks for the addSeries() method.
174      */

175     public void testAddSeries() {
176         double[] values = {-1.0, 0.0, 0.1, 0.9, 1.0, 1.1, 1.9, 2.0, 3.0};
177         HistogramDataset d = new HistogramDataset();
178         d.addSeries("S1", values, 2, 0.0, 2.0);
179         assertEquals(0.0, d.getStartXValue(0, 0), EPSILON);
180         assertEquals(1.0, d.getEndXValue(0, 0), EPSILON);
181         assertEquals(4.0, d.getYValue(0, 0), EPSILON);
182         
183         assertEquals(1.0, d.getStartXValue(0, 1), EPSILON);
184         assertEquals(2.0, d.getEndXValue(0, 1), EPSILON);
185         assertEquals(5.0, d.getYValue(0, 1), EPSILON);
186     }
187     
188     /**
189      * This test is derived from a reported bug.
190      */

191     public void testBinBoundaries() {
192         double[] values = { -5.000000000000286E-5 };
193         int bins = 1260;
194         double minimum = -0.06307522528160199;
195         double maximum = 0.06297522528160199;
196         HistogramDataset d = new HistogramDataset();
197         d.addSeries("S1", values, bins, minimum, maximum);
198         assertEquals(0.0, d.getYValue(0, 629), EPSILON);
199         assertEquals(1.0, d.getYValue(0, 630), EPSILON);
200         assertEquals(0.0, d.getYValue(0, 631), EPSILON);
201         assertTrue(values[0] > d.getStartXValue(0, 630));
202         assertTrue(values[0] < d.getEndXValue(0, 630));
203     }
204
205     /**
206      * Some checks for bug 1553088. An IndexOutOfBoundsException is thrown
207      * when a data value is *very* close to the upper limit of the last bin.
208      */

209     public void test1553088() {
210         double[] values = {-1.0, 0.0, -Double.MIN_VALUE, 3.0};
211         HistogramDataset d = new HistogramDataset();
212         d.addSeries("S1", values, 2, -1.0, 0.0);
213         assertEquals(-1.0, d.getStartXValue(0, 0), EPSILON);
214         assertEquals(-0.5, d.getEndXValue(0, 0), EPSILON);
215         assertEquals(1.0, d.getYValue(0, 0), EPSILON);
216         
217         assertEquals(-0.5, d.getStartXValue(0, 1), EPSILON);
218         assertEquals(0.0, d.getEndXValue(0, 1), EPSILON);
219         assertEquals(3.0, d.getYValue(0, 1), EPSILON);
220     }
221     
222 }
223
Popular Tags