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: * HistogramDatasetTests.java
029: * --------------------------
030: * (C) Copyright 2004-2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: HistogramDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 01-Mar-2004 : Version 1 (DG);
040: * 08-Jun-2005 : Added test for getSeriesKey(int) bug (DG);
041: * 03-Aug-2006 : Added testAddSeries() and testBinBoundaries() method (DG);
042: *
043: */
044:
045: package org.jfree.data.statistics.junit;
046:
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.data.statistics.HistogramDataset;
059:
060: /**
061: * Tests for the {@link HistogramDataset} class.
062: */
063: public class HistogramDatasetTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(HistogramDatasetTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public HistogramDatasetTests(String name) {
080: super (name);
081: }
082:
083: private static final double EPSILON = 0.0000000001;
084:
085: /**
086: * Some checks that the correct values are assigned to bins.
087: */
088: public void testBins() {
089: double[] values = { 1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3,
090: 4.5 };
091: HistogramDataset hd = new HistogramDataset();
092: hd.addSeries("Series 1", values, 5);
093: assertEquals(hd.getYValue(0, 0), 3.0, EPSILON);
094: assertEquals(hd.getYValue(0, 1), 3.0, EPSILON);
095: assertEquals(hd.getYValue(0, 2), 2.0, EPSILON);
096: assertEquals(hd.getYValue(0, 3), 0.0, EPSILON);
097: assertEquals(hd.getYValue(0, 4), 1.0, EPSILON);
098: }
099:
100: /**
101: * Confirm that the equals method can distinguish all the required fields.
102: */
103: public void testEquals() {
104:
105: double[] values = { 1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3,
106: 4.5 };
107: HistogramDataset d1 = new HistogramDataset();
108: d1.addSeries("Series 1", values, 5);
109: HistogramDataset d2 = new HistogramDataset();
110: d2.addSeries("Series 1", values, 5);
111:
112: assertTrue(d1.equals(d2));
113: assertTrue(d2.equals(d1));
114:
115: }
116:
117: /**
118: * Confirm that cloning works.
119: */
120: public void testCloning() {
121: double[] values = { 1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3,
122: 4.5 };
123: HistogramDataset d1 = new HistogramDataset();
124: d1.addSeries("Series 1", values, 5);
125: HistogramDataset d2 = null;
126: try {
127: d2 = (HistogramDataset) d1.clone();
128: } catch (CloneNotSupportedException e) {
129: System.err.println("Failed to clone.");
130: }
131: assertTrue(d1 != d2);
132: assertTrue(d1.getClass() == d2.getClass());
133: assertTrue(d1.equals(d2));
134: }
135:
136: /**
137: * Serialize an instance, restore it, and check for equality.
138: */
139: public void testSerialization() {
140: double[] values = { 1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3,
141: 4.5 };
142: HistogramDataset d1 = new HistogramDataset();
143: d1.addSeries("Series 1", values, 5);
144: HistogramDataset d2 = null;
145:
146: try {
147: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
148: ObjectOutput out = new ObjectOutputStream(buffer);
149: out.writeObject(d1);
150: out.close();
151:
152: ObjectInput in = new ObjectInputStream(
153: new ByteArrayInputStream(buffer.toByteArray()));
154: d2 = (HistogramDataset) in.readObject();
155: in.close();
156: } catch (Exception e) {
157: System.out.println(e.toString());
158: }
159: assertEquals(d1, d2);
160: }
161:
162: /**
163: * A test for a bug reported in the forum where the series name isn't being
164: * returned correctly.
165: */
166: public void testGetSeriesKey() {
167: double[] values = { 1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3,
168: 4.5 };
169: HistogramDataset d1 = new HistogramDataset();
170: d1.addSeries("Series 1", values, 5);
171: assertEquals("Series 1", d1.getSeriesKey(0));
172: }
173:
174: /**
175: * Some checks for the addSeries() method.
176: */
177: public void testAddSeries() {
178: double[] values = { -1.0, 0.0, 0.1, 0.9, 1.0, 1.1, 1.9, 2.0,
179: 3.0 };
180: HistogramDataset d = new HistogramDataset();
181: d.addSeries("S1", values, 2, 0.0, 2.0);
182: assertEquals(0.0, d.getStartXValue(0, 0), EPSILON);
183: assertEquals(1.0, d.getEndXValue(0, 0), EPSILON);
184: assertEquals(4.0, d.getYValue(0, 0), EPSILON);
185:
186: assertEquals(1.0, d.getStartXValue(0, 1), EPSILON);
187: assertEquals(2.0, d.getEndXValue(0, 1), EPSILON);
188: assertEquals(5.0, d.getYValue(0, 1), EPSILON);
189: }
190:
191: /**
192: * This test is derived from a reported bug.
193: */
194: public void testBinBoundaries() {
195: double[] values = { -5.000000000000286E-5 };
196: int bins = 1260;
197: double minimum = -0.06307522528160199;
198: double maximum = 0.06297522528160199;
199: HistogramDataset d = new HistogramDataset();
200: d.addSeries("S1", values, bins, minimum, maximum);
201: assertEquals(0.0, d.getYValue(0, 629), EPSILON);
202: assertEquals(1.0, d.getYValue(0, 630), EPSILON);
203: assertEquals(0.0, d.getYValue(0, 631), EPSILON);
204: assertTrue(values[0] > d.getStartXValue(0, 630));
205: assertTrue(values[0] < d.getEndXValue(0, 630));
206: }
207:
208: /**
209: * Some checks for bug 1553088. An IndexOutOfBoundsException is thrown
210: * when a data value is *very* close to the upper limit of the last bin.
211: */
212: public void test1553088() {
213: double[] values = { -1.0, 0.0, -Double.MIN_VALUE, 3.0 };
214: HistogramDataset d = new HistogramDataset();
215: d.addSeries("S1", values, 2, -1.0, 0.0);
216: assertEquals(-1.0, d.getStartXValue(0, 0), EPSILON);
217: assertEquals(-0.5, d.getEndXValue(0, 0), EPSILON);
218: assertEquals(1.0, d.getYValue(0, 0), EPSILON);
219:
220: assertEquals(-0.5, d.getStartXValue(0, 1), EPSILON);
221: assertEquals(0.0, d.getEndXValue(0, 1), EPSILON);
222: assertEquals(3.0, d.getYValue(0, 1), EPSILON);
223: }
224:
225: }
|