DefaultBoxAndWhiskerXYDatasetTest.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * --------------------------------------
  28. * DefaultBoxAndWhiskerXYDatasetTest.java
  29. * --------------------------------------
  30. * (C) Copyright 2007-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 12-Nov-2007 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.data.statistics;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.junit.Assert.assertFalse;
  44. import java.util.ArrayList;
  45. import java.util.Date;
  46. import org.jfree.chart.TestUtilities;
  47. import org.jfree.data.Range;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link DefaultBoxAndWhiskerXYDataset} class.
  51. */
  52. public class DefaultBoxAndWhiskerXYDatasetTest {
  53. /**
  54. * Confirm that the equals method can distinguish all the required fields.
  55. */
  56. @Test
  57. public void testEquals() {
  58. DefaultBoxAndWhiskerXYDataset d1 = new DefaultBoxAndWhiskerXYDataset(
  59. "Series");
  60. DefaultBoxAndWhiskerXYDataset d2 = new DefaultBoxAndWhiskerXYDataset(
  61. "Series");
  62. assertTrue(d1.equals(d2));
  63. d1.add(new Date(1L), new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0, 5.0,
  64. 6.0, 7.0, 8.0, new ArrayList()));
  65. assertFalse(d1.equals(d2));
  66. d2.add(new Date(1L), new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0, 5.0,
  67. 6.0, 7.0, 8.0, new ArrayList()));
  68. assertTrue(d1.equals(d2));
  69. }
  70. /**
  71. * Serialize an instance, restore it, and check for equality.
  72. */
  73. @Test
  74. public void testSerialization() {
  75. DefaultBoxAndWhiskerXYDataset d1 = new DefaultBoxAndWhiskerXYDataset(
  76. "Series");
  77. d1.add(new Date(1L), new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0, 5.0,
  78. 6.0, 7.0, 8.0, new ArrayList()));
  79. DefaultBoxAndWhiskerXYDataset d2 = (DefaultBoxAndWhiskerXYDataset)
  80. TestUtilities.serialised(d1);
  81. assertEquals(d1, d2);
  82. // test independence
  83. d1.add(new Date(2L), new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0, 5.0,
  84. 6.0, 7.0, 8.0, new ArrayList()));
  85. assertFalse(d1.equals(d2));
  86. }
  87. /**
  88. * Confirm that cloning works.
  89. */
  90. @Test
  91. public void testCloning() throws CloneNotSupportedException {
  92. DefaultBoxAndWhiskerXYDataset d1 = new DefaultBoxAndWhiskerXYDataset(
  93. "Series");
  94. d1.add(new Date(1L), new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0, 5.0,
  95. 6.0, 7.0, 8.0, new ArrayList()));
  96. DefaultBoxAndWhiskerXYDataset d2 = (DefaultBoxAndWhiskerXYDataset)
  97. d1.clone();
  98. assertTrue(d1 != d2);
  99. assertTrue(d1.getClass() == d2.getClass());
  100. assertTrue(d1.equals(d2));
  101. // test independence
  102. d1.add(new Date(2L), new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0, 5.0,
  103. 6.0, 7.0, 8.0, new ArrayList()));
  104. assertFalse(d1.equals(d2));
  105. }
  106. private static final double EPSILON = 0.0000000001;
  107. /**
  108. * Some checks for the add() method.
  109. */
  110. @Test
  111. public void testAdd() {
  112. DefaultBoxAndWhiskerXYDataset dataset
  113. = new DefaultBoxAndWhiskerXYDataset("S1");
  114. BoxAndWhiskerItem item1 = new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0,
  115. 5.0, 6.0, 7.0, 8.0, new ArrayList());
  116. dataset.add(new Date(33L), item1);
  117. assertEquals(1.0, dataset.getY(0, 0).doubleValue(), EPSILON);
  118. assertEquals(1.0, dataset.getMeanValue(0, 0).doubleValue(), EPSILON);
  119. assertEquals(2.0, dataset.getMedianValue(0, 0).doubleValue(), EPSILON);
  120. assertEquals(3.0, dataset.getQ1Value(0, 0).doubleValue(), EPSILON);
  121. assertEquals(4.0, dataset.getQ3Value(0, 0).doubleValue(), EPSILON);
  122. assertEquals(5.0, dataset.getMinRegularValue(0, 0).doubleValue(),
  123. EPSILON);
  124. assertEquals(6.0, dataset.getMaxRegularValue(0, 0).doubleValue(),
  125. EPSILON);
  126. assertEquals(7.0, dataset.getMinOutlier(0, 0).doubleValue(), EPSILON);
  127. assertEquals(8.0, dataset.getMaxOutlier(0, 0).doubleValue(), EPSILON);
  128. assertEquals(new Range(5.0, 6.0), dataset.getRangeBounds(false));
  129. }
  130. /**
  131. * Some basic checks for the constructor.
  132. */
  133. @Test
  134. public void testConstructor() {
  135. DefaultBoxAndWhiskerXYDataset dataset
  136. = new DefaultBoxAndWhiskerXYDataset("S1");
  137. assertEquals(1, dataset.getSeriesCount());
  138. assertEquals(0, dataset.getItemCount(0));
  139. assertTrue(Double.isNaN(dataset.getRangeLowerBound(false)));
  140. assertTrue(Double.isNaN(dataset.getRangeUpperBound(false)));
  141. }
  142. /**
  143. * Some checks for the getRangeBounds() method.
  144. */
  145. @Test
  146. public void testGetRangeBounds() {
  147. DefaultBoxAndWhiskerXYDataset d1
  148. = new DefaultBoxAndWhiskerXYDataset("S");
  149. d1.add(new Date(1L), new BoxAndWhiskerItem(1.0, 2.0, 3.0, 4.0, 5.0,
  150. 6.0, 7.0, 8.0, new ArrayList()));
  151. assertEquals(new Range(5.0, 6.0), d1.getRangeBounds(false));
  152. assertEquals(new Range(5.0, 6.0), d1.getRangeBounds(true));
  153. d1.add(new Date(1L), new BoxAndWhiskerItem(1.5, 2.5, 3.5, 4.5, 5.5,
  154. 6.5, 7.5, 8.5, new ArrayList()));
  155. assertEquals(new Range(5.0, 6.5), d1.getRangeBounds(false));
  156. assertEquals(new Range(5.0, 6.5), d1.getRangeBounds(true));
  157. d1.add(new Date(2L), new BoxAndWhiskerItem(2.5, 3.5, 4.5, 5.5, 6.5,
  158. 7.5, 8.5, 9.5, new ArrayList()));
  159. assertEquals(new Range(5.0, 7.5), d1.getRangeBounds(false));
  160. assertEquals(new Range(5.0, 7.5), d1.getRangeBounds(true));
  161. }
  162. }