001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * DatasetUtilitiesTests.java
029: * --------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DatasetUtilitiesTests.java,v 1.1.2.2 2007/05/08 14:47:07 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Sep-2003 : Version 1 (DG);
040: * 23-Mar-2004 : Added test for maximumStackedRangeValue() method (DG);
041: * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
042: * 07-Jan-2005 : Updated for method name changes (DG);
043: * 03-Feb-2005 : Added testFindStackedRangeBounds2() method (DG);
044: *
045: */
046:
047: package org.jfree.data.general.junit;
048:
049: import junit.framework.Test;
050: import junit.framework.TestCase;
051: import junit.framework.TestSuite;
052:
053: import org.jfree.data.DomainOrder;
054: import org.jfree.data.KeyToGroupMap;
055: import org.jfree.data.Range;
056: import org.jfree.data.category.CategoryDataset;
057: import org.jfree.data.category.DefaultCategoryDataset;
058: import org.jfree.data.general.DatasetUtilities;
059: import org.jfree.data.general.DefaultPieDataset;
060: import org.jfree.data.general.PieDataset;
061: import org.jfree.data.xy.DefaultTableXYDataset;
062: import org.jfree.data.xy.DefaultXYDataset;
063: import org.jfree.data.xy.TableXYDataset;
064: import org.jfree.data.xy.XYDataset;
065: import org.jfree.data.xy.XYSeries;
066: import org.jfree.data.xy.XYSeriesCollection;
067:
068: /**
069: * Tests for the {@link DatasetUtilities} class.
070: */
071: public class DatasetUtilitiesTests extends TestCase {
072:
073: private static final double EPSILON = 0.0000000001;
074:
075: /**
076: * Returns the tests as a test suite.
077: *
078: * @return The test suite.
079: */
080: public static Test suite() {
081: return new TestSuite(DatasetUtilitiesTests.class);
082: }
083:
084: /**
085: * Constructs a new set of tests.
086: *
087: * @param name the name of the tests.
088: */
089: public DatasetUtilitiesTests(String name) {
090: super (name);
091: }
092:
093: /**
094: * Some tests to verify that Java does what I think it does!
095: */
096: public void testJava() {
097: assertTrue(Double.isNaN(Math.min(1.0, Double.NaN)));
098: assertTrue(Double.isNaN(Math.max(1.0, Double.NaN)));
099: }
100:
101: /**
102: * Some tests for the calculatePieDatasetTotal() method.
103: */
104: public void testCalculatePieDatasetTotal() {
105: DefaultPieDataset d = new DefaultPieDataset();
106: assertEquals(0.0, DatasetUtilities.calculatePieDatasetTotal(d),
107: EPSILON);
108: d.setValue("A", 1.0);
109: assertEquals(1.0, DatasetUtilities.calculatePieDatasetTotal(d),
110: EPSILON);
111: d.setValue("B", 3.0);
112: assertEquals(4.0, DatasetUtilities.calculatePieDatasetTotal(d),
113: EPSILON);
114: }
115:
116: /**
117: * Some tests for the findDomainBounds() method.
118: */
119: public void testFindDomainBounds() {
120: XYDataset dataset = createXYDataset1();
121: Range r = DatasetUtilities.findDomainBounds(dataset);
122: assertEquals(1.0, r.getLowerBound(), EPSILON);
123: assertEquals(3.0, r.getUpperBound(), EPSILON);
124: }
125:
126: /**
127: * Some tests for the iterateDomainBounds() method.
128: */
129: public void testIterateDomainBounds() {
130: XYDataset dataset = createXYDataset1();
131: Range r = DatasetUtilities.iterateDomainBounds(dataset);
132: assertEquals(1.0, r.getLowerBound(), EPSILON);
133: assertEquals(3.0, r.getUpperBound(), EPSILON);
134: }
135:
136: /**
137: * Some tests for the findRangeExtent() method.
138: */
139: public void testFindRangeBounds1() {
140: CategoryDataset dataset = createCategoryDataset1();
141: Range r = DatasetUtilities.findRangeBounds(dataset);
142: assertEquals(1.0, r.getLowerBound(), EPSILON);
143: assertEquals(6.0, r.getUpperBound(), EPSILON);
144: }
145:
146: /**
147: * Some tests for the findRangeBounds() method.
148: */
149: public void testFindRangeBounds2() {
150: XYDataset dataset = createXYDataset1();
151: Range r = DatasetUtilities.findRangeBounds(dataset);
152: assertEquals(100.0, r.getLowerBound(), EPSILON);
153: assertEquals(105.0, r.getUpperBound(), EPSILON);
154: }
155:
156: /**
157: * Some tests for the iterateCategoryRangeBounds() method.
158: */
159: public void testIterateCategoryRangeBounds() {
160: CategoryDataset dataset = createCategoryDataset1();
161: Range r = DatasetUtilities.iterateCategoryRangeBounds(dataset,
162: false);
163: assertEquals(1.0, r.getLowerBound(), EPSILON);
164: assertEquals(6.0, r.getUpperBound(), EPSILON);
165: }
166:
167: /**
168: * Some tests for the iterateXYRangeBounds() method.
169: */
170: public void testIterateXYRangeBounds() {
171: XYDataset dataset = createXYDataset1();
172: Range r = DatasetUtilities.iterateXYRangeBounds(dataset);
173: assertEquals(100.0, r.getLowerBound(), EPSILON);
174: assertEquals(105.0, r.getUpperBound(), EPSILON);
175: }
176:
177: /**
178: * Check the range returned when a series contains a null value.
179: */
180: public void testIterateXYRangeBounds2() {
181: XYSeries s1 = new XYSeries("S1");
182: s1.add(1.0, 1.1);
183: s1.add(2.0, null);
184: s1.add(3.0, 3.3);
185: XYSeriesCollection dataset = new XYSeriesCollection(s1);
186: Range r = DatasetUtilities.iterateXYRangeBounds(dataset);
187: assertEquals(1.1, r.getLowerBound(), EPSILON);
188: assertEquals(3.3, r.getUpperBound(), EPSILON);
189: }
190:
191: /**
192: * Some tests for the findMinimumDomainValue() method.
193: */
194: public void testFindMinimumDomainValue() {
195: XYDataset dataset = createXYDataset1();
196: Number minimum = DatasetUtilities
197: .findMinimumDomainValue(dataset);
198: assertEquals(new Double(1.0), minimum);
199: }
200:
201: /**
202: * Some tests for the findMaximumDomainValue() method.
203: */
204: public void testFindMaximumDomainValue() {
205: XYDataset dataset = createXYDataset1();
206: Number maximum = DatasetUtilities
207: .findMaximumDomainValue(dataset);
208: assertEquals(new Double(3.0), maximum);
209: }
210:
211: /**
212: * Some tests for the findMinimumRangeValue() method.
213: */
214: public void testFindMinimumRangeValue() {
215: CategoryDataset d1 = createCategoryDataset1();
216: Number min1 = DatasetUtilities.findMinimumRangeValue(d1);
217: assertEquals(new Double(1.0), min1);
218:
219: XYDataset d2 = createXYDataset1();
220: Number min2 = DatasetUtilities.findMinimumRangeValue(d2);
221: assertEquals(new Double(100.0), min2);
222: }
223:
224: /**
225: * Some tests for the findMaximumRangeValue() method.
226: */
227: public void testFindMaximumRangeValue() {
228: CategoryDataset d1 = createCategoryDataset1();
229: Number max1 = DatasetUtilities.findMaximumRangeValue(d1);
230: assertEquals(new Double(6.0), max1);
231:
232: XYDataset dataset = createXYDataset1();
233: Number maximum = DatasetUtilities
234: .findMaximumRangeValue(dataset);
235: assertEquals(new Double(105.0), maximum);
236: }
237:
238: /**
239: * A quick test of the min and max range value methods.
240: */
241: public void testMinMaxRange() {
242: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
243: dataset.addValue(100.0, "Series 1", "Type 1");
244: dataset.addValue(101.1, "Series 1", "Type 2");
245: Number min = DatasetUtilities.findMinimumRangeValue(dataset);
246: assertTrue(min.doubleValue() < 100.1);
247: Number max = DatasetUtilities.findMaximumRangeValue(dataset);
248: assertTrue(max.doubleValue() > 101.0);
249: }
250:
251: /**
252: * A test to reproduce bug report 803660.
253: */
254: public void test803660() {
255: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
256: dataset.addValue(100.0, "Series 1", "Type 1");
257: dataset.addValue(101.1, "Series 1", "Type 2");
258: Number n = DatasetUtilities.findMaximumRangeValue(dataset);
259: assertTrue(n.doubleValue() > 101.0);
260: }
261:
262: /**
263: * A simple test for the cumulative range calculation. The sequence of
264: * "cumulative" values are considered to be { 0.0, 10.0, 25.0, 18.0 } so
265: * the range should be 0.0 -> 25.0.
266: */
267: public void testCumulativeRange1() {
268: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
269: dataset.addValue(10.0, "Series 1", "Start");
270: dataset.addValue(15.0, "Series 1", "Delta 1");
271: dataset.addValue(-7.0, "Series 1", "Delta 2");
272: Range range = DatasetUtilities
273: .findCumulativeRangeBounds(dataset);
274: assertEquals(0.0, range.getLowerBound(), 0.00000001);
275: assertEquals(25.0, range.getUpperBound(), 0.00000001);
276: }
277:
278: /**
279: * A further test for the cumulative range calculation.
280: */
281: public void testCumulativeRange2() {
282: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
283: dataset.addValue(-21.4, "Series 1", "Start Value");
284: dataset.addValue(11.57, "Series 1", "Delta 1");
285: dataset.addValue(3.51, "Series 1", "Delta 2");
286: dataset.addValue(-12.36, "Series 1", "Delta 3");
287: dataset.addValue(3.39, "Series 1", "Delta 4");
288: dataset.addValue(38.68, "Series 1", "Delta 5");
289: dataset.addValue(-43.31, "Series 1", "Delta 6");
290: dataset.addValue(-29.59, "Series 1", "Delta 7");
291: dataset.addValue(35.30, "Series 1", "Delta 8");
292: dataset.addValue(5.0, "Series 1", "Delta 9");
293: Range range = DatasetUtilities
294: .findCumulativeRangeBounds(dataset);
295: assertEquals(-49.51, range.getLowerBound(), 0.00000001);
296: assertEquals(23.39, range.getUpperBound(), 0.00000001);
297: }
298:
299: /**
300: * Test the creation of a dataset from an array.
301: */
302: public void testCreateCategoryDataset1() {
303: String[] rowKeys = { "R1", "R2", "R3" };
304: String[] columnKeys = { "C1", "C2" };
305: double[][] data = new double[3][];
306: data[0] = new double[] { 1.1, 1.2 };
307: data[1] = new double[] { 2.1, 2.2 };
308: data[2] = new double[] { 3.1, 3.2 };
309: CategoryDataset dataset = DatasetUtilities
310: .createCategoryDataset(rowKeys, columnKeys, data);
311: assertTrue(dataset.getRowCount() == 3);
312: assertTrue(dataset.getColumnCount() == 2);
313: }
314:
315: /**
316: * Test the creation of a dataset from an array. This time is should fail
317: * because the array dimensions are around the wrong way.
318: */
319: public void testCreateCategoryDataset2() {
320: boolean pass = false;
321: String[] rowKeys = { "R1", "R2", "R3" };
322: String[] columnKeys = { "C1", "C2" };
323: double[][] data = new double[2][];
324: data[0] = new double[] { 1.1, 1.2, 1.3 };
325: data[1] = new double[] { 2.1, 2.2, 2.3 };
326: CategoryDataset dataset = null;
327: try {
328: dataset = DatasetUtilities.createCategoryDataset(rowKeys,
329: columnKeys, data);
330: } catch (IllegalArgumentException e) {
331: pass = true; // got it!
332: }
333: assertTrue(pass);
334: assertTrue(dataset == null);
335: }
336:
337: /**
338: * Test for a bug reported in the forum:
339: *
340: * http://www.jfree.org/phpBB2/viewtopic.php?t=7903
341: */
342: public void testMaximumStackedRangeValue() {
343: double v1 = 24.3;
344: double v2 = 14.2;
345: double v3 = 33.2;
346: double v4 = 32.4;
347: double v5 = 26.3;
348: double v6 = 22.6;
349: Number answer = new Double(Math.max(v1 + v2 + v3, v4 + v5 + v6));
350: DefaultCategoryDataset d = new DefaultCategoryDataset();
351: d.addValue(v1, "Row 0", "Column 0");
352: d.addValue(v2, "Row 1", "Column 0");
353: d.addValue(v3, "Row 2", "Column 0");
354: d.addValue(v4, "Row 0", "Column 1");
355: d.addValue(v5, "Row 1", "Column 1");
356: d.addValue(v6, "Row 2", "Column 1");
357: Number max = DatasetUtilities.findMaximumStackedRangeValue(d);
358: assertTrue(max.equals(answer));
359: }
360:
361: /**
362: * Some checks for the findStackedRangeBounds() method.
363: */
364: public void testFindStackedRangeBoundsForCategoryDataset1() {
365: CategoryDataset d1 = createCategoryDataset1();
366: Range r = DatasetUtilities.findStackedRangeBounds(d1);
367: assertEquals(0.0, r.getLowerBound(), EPSILON);
368: assertEquals(15.0, r.getUpperBound(), EPSILON);
369:
370: d1 = createCategoryDataset2();
371: r = DatasetUtilities.findStackedRangeBounds(d1);
372: assertEquals(-2.0, r.getLowerBound(), EPSILON);
373: assertEquals(2.0, r.getUpperBound(), EPSILON);
374: }
375:
376: /**
377: * Some checks for the findStackedRangeBounds() method.
378: */
379: public void testFindStackedRangeBoundsForCategoryDataset2() {
380: CategoryDataset d1 = new DefaultCategoryDataset();
381: Range r = DatasetUtilities.findStackedRangeBounds(d1);
382: assertTrue(r == null);
383: }
384:
385: /**
386: * Some checks for the findStackedRangeBounds() method.
387: */
388: public void testFindStackedRangeBoundsForTableXYDataset1() {
389: TableXYDataset d2 = createTableXYDataset1();
390: Range r = DatasetUtilities.findStackedRangeBounds(d2);
391: assertEquals(-2.0, r.getLowerBound(), EPSILON);
392: assertEquals(2.0, r.getUpperBound(), EPSILON);
393: }
394:
395: /**
396: * Some checks for the findStackedRangeBounds() method.
397: */
398: public void testFindStackedRangeBoundsForTableXYDataset2() {
399: DefaultTableXYDataset d = new DefaultTableXYDataset();
400: Range r = DatasetUtilities.findStackedRangeBounds(d);
401: assertEquals(r, new Range(0.0, 0.0));
402: }
403:
404: /**
405: * Tests the stacked range extent calculation.
406: */
407: public void testStackedRangeWithMap() {
408: CategoryDataset d = createCategoryDataset1();
409: KeyToGroupMap map = new KeyToGroupMap("G0");
410: map.mapKeyToGroup("R2", "G1");
411: Range r = DatasetUtilities.findStackedRangeBounds(d, map);
412: assertEquals(0.0, r.getLowerBound(), EPSILON);
413: assertEquals(9.0, r.getUpperBound(), EPSILON);
414: }
415:
416: /**
417: * Some checks for the limitPieDataset() methods.
418: */
419: public void testLimitPieDataset() {
420:
421: // check that empty dataset is handled OK
422: DefaultPieDataset d1 = new DefaultPieDataset();
423: PieDataset d2 = DatasetUtilities.createConsolidatedPieDataset(
424: d1, "Other", 0.05);
425: assertEquals(0, d2.getItemCount());
426:
427: // check that minItem limit is observed
428: d1.setValue("Item 1", 1.0);
429: d1.setValue("Item 2", 49.50);
430: d1.setValue("Item 3", 49.50);
431: d2 = DatasetUtilities.createConsolidatedPieDataset(d1, "Other",
432: 0.05);
433: assertEquals(3, d2.getItemCount());
434: assertEquals("Item 1", d2.getKey(0));
435: assertEquals("Item 2", d2.getKey(1));
436: assertEquals("Item 3", d2.getKey(2));
437:
438: // check that minItem limit is observed
439: d1.setValue("Item 4", 1.0);
440: d2 = DatasetUtilities.createConsolidatedPieDataset(d1, "Other",
441: 0.05, 2);
442:
443: // and that simple aggregation works
444: assertEquals(3, d2.getItemCount());
445: assertEquals("Item 2", d2.getKey(0));
446: assertEquals("Item 3", d2.getKey(1));
447: assertEquals("Other", d2.getKey(2));
448: assertEquals(new Double(2.0), d2.getValue("Other"));
449:
450: }
451:
452: /**
453: * Creates a dataset for testing.
454: *
455: * @return A dataset.
456: */
457: private CategoryDataset createCategoryDataset1() {
458: DefaultCategoryDataset result = new DefaultCategoryDataset();
459: result.addValue(1.0, "R0", "C0");
460: result.addValue(1.0, "R1", "C0");
461: result.addValue(1.0, "R2", "C0");
462: result.addValue(4.0, "R0", "C1");
463: result.addValue(5.0, "R1", "C1");
464: result.addValue(6.0, "R2", "C1");
465: return result;
466: }
467:
468: /**
469: * Creates a dataset for testing.
470: *
471: * @return A dataset.
472: */
473: private CategoryDataset createCategoryDataset2() {
474: DefaultCategoryDataset result = new DefaultCategoryDataset();
475: result.addValue(1.0, "R0", "C0");
476: result.addValue(-2.0, "R1", "C0");
477: result.addValue(2.0, "R0", "C1");
478: result.addValue(-1.0, "R1", "C1");
479: return result;
480: }
481:
482: /**
483: * Creates a dataset for testing.
484: *
485: * @return A dataset.
486: */
487: private XYDataset createXYDataset1() {
488: XYSeries series1 = new XYSeries("S1");
489: series1.add(1.0, 100.0);
490: series1.add(2.0, 101.0);
491: series1.add(3.0, 102.0);
492: XYSeries series2 = new XYSeries("S2");
493: series2.add(1.0, 103.0);
494: series2.add(2.0, null);
495: series2.add(3.0, 105.0);
496: XYSeriesCollection result = new XYSeriesCollection();
497: result.addSeries(series1);
498: result.addSeries(series2);
499: result.setIntervalWidth(0.0);
500: return result;
501: }
502:
503: /**
504: * Creates a sample dataset for testing purposes.
505: *
506: * @return A sample dataset.
507: */
508: private TableXYDataset createTableXYDataset1() {
509: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
510:
511: XYSeries s1 = new XYSeries("Series 1", true, false);
512: s1.add(1.0, 1.0);
513: s1.add(2.0, 2.0);
514: dataset.addSeries(s1);
515:
516: XYSeries s2 = new XYSeries("Series 2", true, false);
517: s2.add(1.0, -2.0);
518: s2.add(2.0, -1.0);
519: dataset.addSeries(s2);
520:
521: return dataset;
522: }
523:
524: }
|