1 46 47 package org.jfree.data.general.junit; 48 49 import junit.framework.Test; 50 import junit.framework.TestCase; 51 import junit.framework.TestSuite; 52 53 import org.jfree.data.KeyToGroupMap; 54 import org.jfree.data.Range; 55 import org.jfree.data.category.CategoryDataset; 56 import org.jfree.data.category.DefaultCategoryDataset; 57 import org.jfree.data.general.DatasetUtilities; 58 import org.jfree.data.general.DefaultPieDataset; 59 import org.jfree.data.general.PieDataset; 60 import org.jfree.data.xy.DefaultTableXYDataset; 61 import org.jfree.data.xy.TableXYDataset; 62 import org.jfree.data.xy.XYDataset; 63 import org.jfree.data.xy.XYSeries; 64 import org.jfree.data.xy.XYSeriesCollection; 65 66 69 public class DatasetUtilitiesTests extends TestCase { 70 71 private static final double EPSILON = 0.0000000001; 72 73 78 public static Test suite() { 79 return new TestSuite(DatasetUtilitiesTests.class); 80 } 81 82 87 public DatasetUtilitiesTests(String name) { 88 super(name); 89 } 90 91 94 public void testJava() { 95 assertTrue(Double.isNaN(Math.min(1.0, Double.NaN))); 96 assertTrue(Double.isNaN(Math.max(1.0, Double.NaN))); 97 } 98 99 102 public void testCalculatePieDatasetTotal() { 103 DefaultPieDataset d = new DefaultPieDataset(); 104 assertEquals( 105 0.0, DatasetUtilities.calculatePieDatasetTotal(d), EPSILON 106 ); 107 d.setValue("A", 1.0); 108 assertEquals( 109 1.0, DatasetUtilities.calculatePieDatasetTotal(d), EPSILON 110 ); 111 d.setValue("B", 3.0); 112 assertEquals( 113 4.0, DatasetUtilities.calculatePieDatasetTotal(d), EPSILON 114 ); 115 } 116 117 120 public void testFindDomainBounds() { 121 XYDataset dataset = createXYDataset1(); 122 Range r = DatasetUtilities.findDomainBounds(dataset); 123 assertEquals(1.0, r.getLowerBound(), EPSILON); 124 assertEquals(3.0, r.getUpperBound(), EPSILON); 125 } 126 127 130 public void testIterateDomainBounds() { 131 XYDataset dataset = createXYDataset1(); 132 Range r = DatasetUtilities.iterateDomainBounds(dataset); 133 assertEquals(1.0, r.getLowerBound(), EPSILON); 134 assertEquals(3.0, r.getUpperBound(), EPSILON); 135 } 136 137 140 public void testFindRangeBounds1() { 141 CategoryDataset dataset = createCategoryDataset1(); 142 Range r = DatasetUtilities.findRangeBounds(dataset); 143 assertEquals(1.0, r.getLowerBound(), EPSILON); 144 assertEquals(6.0, r.getUpperBound(), EPSILON); 145 } 146 147 150 public void testFindRangeBounds2() { 151 XYDataset dataset = createXYDataset1(); 152 Range r = DatasetUtilities.findRangeBounds(dataset); 153 assertEquals(100.0, r.getLowerBound(), EPSILON); 154 assertEquals(105.0, r.getUpperBound(), EPSILON); 155 } 156 157 160 public void testIterateCategoryRangeBounds() { 161 CategoryDataset dataset = createCategoryDataset1(); 162 Range r = DatasetUtilities.iterateCategoryRangeBounds(dataset, false); 163 assertEquals(1.0, r.getLowerBound(), EPSILON); 164 assertEquals(6.0, r.getUpperBound(), EPSILON); 165 } 166 167 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 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 194 public void testFindMinimumDomainValue() { 195 XYDataset dataset = createXYDataset1(); 196 Number minimum = DatasetUtilities.findMinimumDomainValue(dataset); 197 assertEquals(new Double (1.0), minimum); 198 } 199 200 203 public void testFindMaximumDomainValue() { 204 XYDataset dataset = createXYDataset1(); 205 Number maximum = DatasetUtilities.findMaximumDomainValue(dataset); 206 assertEquals(new Double (3.0), maximum); 207 } 208 209 212 public void testFindMinimumRangeValue() { 213 CategoryDataset d1 = createCategoryDataset1(); 214 Number min1 = DatasetUtilities.findMinimumRangeValue(d1); 215 assertEquals(new Double (1.0), min1); 216 217 XYDataset d2 = createXYDataset1(); 218 Number min2 = DatasetUtilities.findMinimumRangeValue(d2); 219 assertEquals(new Double (100.0), min2); 220 } 221 222 225 public void testFindMaximumRangeValue() { 226 CategoryDataset d1 = createCategoryDataset1(); 227 Number max1 = DatasetUtilities.findMaximumRangeValue(d1); 228 assertEquals(new Double (6.0), max1); 229 230 XYDataset dataset = createXYDataset1(); 231 Number maximum = DatasetUtilities.findMaximumRangeValue(dataset); 232 assertEquals(new Double (105.0), maximum); 233 } 234 235 238 public void testMinMaxRange() { 239 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 240 dataset.addValue(100.0, "Series 1", "Type 1"); 241 dataset.addValue(101.1, "Series 1", "Type 2"); 242 Number min = DatasetUtilities.findMinimumRangeValue(dataset); 243 assertTrue(min.doubleValue() < 100.1); 244 Number max = DatasetUtilities.findMaximumRangeValue(dataset); 245 assertTrue(max.doubleValue() > 101.0); 246 } 247 248 251 public void test803660() { 252 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 253 dataset.addValue(100.0, "Series 1", "Type 1"); 254 dataset.addValue(101.1, "Series 1", "Type 2"); 255 Number n = DatasetUtilities.findMaximumRangeValue(dataset); 256 assertTrue(n.doubleValue() > 101.0); 257 } 258 259 264 public void testCumulativeRange1() { 265 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 266 dataset.addValue(10.0, "Series 1", "Start"); 267 dataset.addValue(15.0, "Series 1", "Delta 1"); 268 dataset.addValue(-7.0, "Series 1", "Delta 2"); 269 Range range = DatasetUtilities.findCumulativeRangeBounds(dataset); 270 assertEquals(0.0, range.getLowerBound(), 0.00000001); 271 assertEquals(25.0, range.getUpperBound(), 0.00000001); 272 } 273 274 277 public void testCumulativeRange2() { 278 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 279 dataset.addValue(-21.4, "Series 1", "Start Value"); 280 dataset.addValue(11.57, "Series 1", "Delta 1"); 281 dataset.addValue(3.51, "Series 1", "Delta 2"); 282 dataset.addValue(-12.36, "Series 1", "Delta 3"); 283 dataset.addValue(3.39, "Series 1", "Delta 4"); 284 dataset.addValue(38.68, "Series 1", "Delta 5"); 285 dataset.addValue(-43.31, "Series 1", "Delta 6"); 286 dataset.addValue(-29.59, "Series 1", "Delta 7"); 287 dataset.addValue(35.30, "Series 1", "Delta 8"); 288 dataset.addValue(5.0, "Series 1", "Delta 9"); 289 Range range = DatasetUtilities.findCumulativeRangeBounds(dataset); 290 assertEquals(-49.51, range.getLowerBound(), 0.00000001); 291 assertEquals(23.39, range.getUpperBound(), 0.00000001); 292 } 293 294 297 public void testCreateCategoryDataset1() { 298 String [] rowKeys = {"R1", "R2", "R3"}; 299 String [] columnKeys = {"C1", "C2"}; 300 double[][] data = new double[3][]; 301 data[0] = new double[] {1.1, 1.2}; 302 data[1] = new double[] {2.1, 2.2}; 303 data[2] = new double[] {3.1, 3.2}; 304 CategoryDataset dataset = DatasetUtilities.createCategoryDataset( 305 rowKeys, columnKeys, data 306 ); 307 assertTrue(dataset.getRowCount() == 3); 308 assertTrue(dataset.getColumnCount() == 2); 309 } 310 311 315 public void testCreateCategoryDataset2() { 316 boolean pass = false; 317 String [] rowKeys = {"R1", "R2", "R3"}; 318 String [] columnKeys = {"C1", "C2"}; 319 double[][] data = new double[2][]; 320 data[0] = new double[] {1.1, 1.2, 1.3}; 321 data[1] = new double[] {2.1, 2.2, 2.3}; 322 CategoryDataset dataset = null; 323 try { 324 dataset = DatasetUtilities.createCategoryDataset( 325 rowKeys, columnKeys, data 326 ); 327 } 328 catch (IllegalArgumentException e) { 329 pass = true; } 331 assertTrue(pass); 332 assertTrue(dataset == null); 333 } 334 335 340 public void testMaximumStackedRangeValue() { 341 double v1 = 24.3; 342 double v2 = 14.2; 343 double v3 = 33.2; 344 double v4 = 32.4; 345 double v5 = 26.3; 346 double v6 = 22.6; 347 Number answer = new Double (Math.max(v1 + v2 + v3, v4 + v5 + v6)); 348 DefaultCategoryDataset d = new DefaultCategoryDataset(); 349 d.addValue(v1, "Row 0", "Column 0"); 350 d.addValue(v2, "Row 1", "Column 0"); 351 d.addValue(v3, "Row 2", "Column 0"); 352 d.addValue(v4, "Row 0", "Column 1"); 353 d.addValue(v5, "Row 1", "Column 1"); 354 d.addValue(v6, "Row 2", "Column 1"); 355 Number max = DatasetUtilities.findMaximumStackedRangeValue(d); 356 assertTrue(max.equals(answer)); 357 } 358 359 362 public void testFindStackedRangeBoundsForCategoryDataset1() { 363 CategoryDataset d1 = createCategoryDataset1(); 364 Range r = DatasetUtilities.findStackedRangeBounds(d1); 365 assertEquals(0.0, r.getLowerBound(), EPSILON); 366 assertEquals(15.0, r.getUpperBound(), EPSILON); 367 368 d1 = createCategoryDataset2(); 369 r = DatasetUtilities.findStackedRangeBounds(d1); 370 assertEquals(-2.0, r.getLowerBound(), EPSILON); 371 assertEquals(2.0, r.getUpperBound(), EPSILON); 372 } 373 374 377 public void testFindStackedRangeBoundsForCategoryDataset2() { 378 CategoryDataset d1 = new DefaultCategoryDataset(); 379 Range r = DatasetUtilities.findStackedRangeBounds(d1); 380 assertTrue(r == null); 381 } 382 383 386 public void testFindStackedRangeBoundsForTableXYDataset1() { 387 TableXYDataset d2 = createTableXYDataset1(); 388 Range r = DatasetUtilities.findStackedRangeBounds(d2); 389 assertEquals(-2.0, r.getLowerBound(), EPSILON); 390 assertEquals(2.0, r.getUpperBound(), EPSILON); 391 } 392 393 396 public void testFindStackedRangeBoundsForTableXYDataset2() { 397 DefaultTableXYDataset d = new DefaultTableXYDataset(); 398 Range r = DatasetUtilities.findStackedRangeBounds(d); 399 assertEquals(r, new Range(0.0, 0.0)); 400 } 401 402 405 public void testStackedRangeWithMap() { 406 CategoryDataset d = createCategoryDataset1(); 407 KeyToGroupMap map = new KeyToGroupMap("G0"); 408 map.mapKeyToGroup("R2", "G1"); 409 Range r = DatasetUtilities.findStackedRangeBounds(d, map); 410 assertEquals(0.0, r.getLowerBound(), EPSILON); 411 assertEquals(9.0, r.getUpperBound(), EPSILON); 412 } 413 414 417 public void testLimitPieDataset() { 418 419 DefaultPieDataset d1 = new DefaultPieDataset(); 421 PieDataset d2 = DatasetUtilities.createConsolidatedPieDataset( 422 d1, "Other", 0.05 423 ); 424 assertEquals(0, d2.getItemCount()); 425 426 d1.setValue("Item 1", 1.0); 428 d1.setValue("Item 2", 49.50); 429 d1.setValue("Item 3", 49.50); 430 d2 = DatasetUtilities.createConsolidatedPieDataset(d1, "Other", 0.05); 431 assertEquals(3, d2.getItemCount()); 432 assertEquals("Item 1", d2.getKey(0)); 433 assertEquals("Item 2", d2.getKey(1)); 434 assertEquals("Item 3", d2.getKey(2)); 435 436 d1.setValue("Item 4", 1.0); 438 d2 = DatasetUtilities.createConsolidatedPieDataset( 439 d1, "Other", 0.05, 2 440 ); 441 442 assertEquals(3, d2.getItemCount()); 444 assertEquals("Item 2", d2.getKey(0)); 445 assertEquals("Item 3", d2.getKey(1)); 446 assertEquals("Other", d2.getKey(2)); 447 assertEquals(new Double (2.0), d2.getValue("Other")); 448 449 } 450 451 456 private CategoryDataset createCategoryDataset1() { 457 DefaultCategoryDataset result = new DefaultCategoryDataset(); 458 result.addValue(1.0, "R0", "C0"); 459 result.addValue(1.0, "R1", "C0"); 460 result.addValue(1.0, "R2", "C0"); 461 result.addValue(4.0, "R0", "C1"); 462 result.addValue(5.0, "R1", "C1"); 463 result.addValue(6.0, "R2", "C1"); 464 return result; 465 } 466 467 472 private CategoryDataset createCategoryDataset2() { 473 DefaultCategoryDataset result = new DefaultCategoryDataset(); 474 result.addValue(1.0, "R0", "C0"); 475 result.addValue(-2.0, "R1", "C0"); 476 result.addValue(2.0, "R0", "C1"); 477 result.addValue(-1.0, "R1", "C1"); 478 return result; 479 } 480 481 482 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 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 } 525 | Popular Tags |