HistogramDatasetTest.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * HistogramDatasetTest.java
  29. * -------------------------
  30. * (C) Copyright 2004-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 01-Mar-2004 : Version 1 (DG);
  38. * 08-Jun-2005 : Added test for getSeriesKey(int) bug (DG);
  39. * 03-Aug-2006 : Added testAddSeries() and testBinBoundaries() method (DG);
  40. * 22-May-2008 : Added testAddSeries2() and enhanced testCloning() (DG);
  41. * 08-Dec-2009 : Added test2902842() for patch at SourceForge (DG);
  42. *
  43. */
  44. package org.jfree.data.statistics;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertNotNull;
  49. import org.jfree.chart.TestUtilities;
  50. import org.jfree.data.general.DatasetChangeEvent;
  51. import org.jfree.data.general.DatasetChangeListener;
  52. import org.junit.Test;
  53. /**
  54. * Tests for the {@link HistogramDataset} class.
  55. */
  56. public class HistogramDatasetTest implements DatasetChangeListener {
  57. private static final double EPSILON = 0.0000000001;
  58. /**
  59. * Some checks that the correct values are assigned to bins.
  60. */
  61. @Test
  62. public void testBins() {
  63. double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
  64. HistogramDataset hd = new HistogramDataset();
  65. hd.addSeries("Series 1", values, 5);
  66. assertEquals(hd.getYValue(0, 0), 3.0, EPSILON);
  67. assertEquals(hd.getYValue(0, 1), 3.0, EPSILON);
  68. assertEquals(hd.getYValue(0, 2), 2.0, EPSILON);
  69. assertEquals(hd.getYValue(0, 3), 0.0, EPSILON);
  70. assertEquals(hd.getYValue(0, 4), 1.0, EPSILON);
  71. }
  72. /**
  73. * Confirm that the equals method can distinguish all the required fields.
  74. */
  75. @Test
  76. public void testEquals() {
  77. double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
  78. HistogramDataset d1 = new HistogramDataset();
  79. d1.addSeries("Series 1", values, 5);
  80. HistogramDataset d2 = new HistogramDataset();
  81. d2.addSeries("Series 1", values, 5);
  82. assertTrue(d1.equals(d2));
  83. assertTrue(d2.equals(d1));
  84. d1.addSeries("Series 2", new double[] {1.0, 2.0, 3.0}, 2);
  85. assertFalse(d1.equals(d2));
  86. d2.addSeries("Series 2", new double[] {1.0, 2.0, 3.0}, 2);
  87. assertTrue(d1.equals(d2));
  88. }
  89. /**
  90. * Confirm that cloning works.
  91. */
  92. @Test
  93. public void testCloning() throws CloneNotSupportedException {
  94. double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
  95. HistogramDataset d1 = new HistogramDataset();
  96. d1.addSeries("Series 1", values, 5);
  97. HistogramDataset d2 = (HistogramDataset) d1.clone();
  98. assertTrue(d1 != d2);
  99. assertTrue(d1.getClass() == d2.getClass());
  100. assertTrue(d1.equals(d2));
  101. // simple check for independence
  102. d1.addSeries("Series 2", new double[] {1.0, 2.0, 3.0}, 2);
  103. assertFalse(d1.equals(d2));
  104. d2.addSeries("Series 2", new double[] {1.0, 2.0, 3.0}, 2);
  105. assertTrue(d1.equals(d2));
  106. }
  107. /**
  108. * Serialize an instance, restore it, and check for equality.
  109. */
  110. @Test
  111. public void testSerialization() {
  112. double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
  113. HistogramDataset d1 = new HistogramDataset();
  114. d1.addSeries("Series 1", values, 5);
  115. HistogramDataset d2 = (HistogramDataset) TestUtilities.serialised(d1);
  116. assertEquals(d1, d2);
  117. // simple check for independence
  118. d1.addSeries("Series 2", new double[] {1.0, 2.0, 3.0}, 2);
  119. assertFalse(d1.equals(d2));
  120. d2.addSeries("Series 2", new double[] {1.0, 2.0, 3.0}, 2);
  121. assertTrue(d1.equals(d2));
  122. }
  123. /**
  124. * A test for a bug reported in the forum where the series name isn't being
  125. * returned correctly.
  126. */
  127. @Test
  128. public void testGetSeriesKey() {
  129. double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5};
  130. HistogramDataset d1 = new HistogramDataset();
  131. d1.addSeries("Series 1", values, 5);
  132. assertEquals("Series 1", d1.getSeriesKey(0));
  133. }
  134. /**
  135. * Some checks for the addSeries() method.
  136. */
  137. @Test
  138. public void testAddSeries() {
  139. double[] values = {-1.0, 0.0, 0.1, 0.9, 1.0, 1.1, 1.9, 2.0, 3.0};
  140. HistogramDataset d = new HistogramDataset();
  141. d.addSeries("S1", values, 2, 0.0, 2.0);
  142. assertEquals(0.0, d.getStartXValue(0, 0), EPSILON);
  143. assertEquals(1.0, d.getEndXValue(0, 0), EPSILON);
  144. assertEquals(4.0, d.getYValue(0, 0), EPSILON);
  145. assertEquals(1.0, d.getStartXValue(0, 1), EPSILON);
  146. assertEquals(2.0, d.getEndXValue(0, 1), EPSILON);
  147. assertEquals(5.0, d.getYValue(0, 1), EPSILON);
  148. }
  149. /**
  150. * Another check for the addSeries() method.
  151. */
  152. @Test
  153. public void testAddSeries2() {
  154. double[] values = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
  155. HistogramDataset hd = new HistogramDataset();
  156. hd.addSeries("S1", values, 5);
  157. assertEquals(0.0, hd.getStartXValue(0, 0), EPSILON);
  158. assertEquals(1.0, hd.getEndXValue(0, 0), EPSILON);
  159. assertEquals(1.0, hd.getYValue(0, 0), EPSILON);
  160. assertEquals(1.0, hd.getStartXValue(0, 1), EPSILON);
  161. assertEquals(2.0, hd.getEndXValue(0, 1), EPSILON);
  162. assertEquals(1.0, hd.getYValue(0, 1), EPSILON);
  163. assertEquals(2.0, hd.getStartXValue(0, 2), EPSILON);
  164. assertEquals(3.0, hd.getEndXValue(0, 2), EPSILON);
  165. assertEquals(1.0, hd.getYValue(0, 2), EPSILON);
  166. assertEquals(3.0, hd.getStartXValue(0, 3), EPSILON);
  167. assertEquals(4.0, hd.getEndXValue(0, 3), EPSILON);
  168. assertEquals(1.0, hd.getYValue(0, 3), EPSILON);
  169. assertEquals(4.0, hd.getStartXValue(0, 4), EPSILON);
  170. assertEquals(5.0, hd.getEndXValue(0, 4), EPSILON);
  171. assertEquals(2.0, hd.getYValue(0, 4), EPSILON);
  172. }
  173. /**
  174. * This test is derived from a reported bug.
  175. */
  176. @Test
  177. public void testBinBoundaries() {
  178. double[] values = {-5.000000000000286E-5};
  179. int bins = 1260;
  180. double minimum = -0.06307522528160199;
  181. double maximum = 0.06297522528160199;
  182. HistogramDataset d = new HistogramDataset();
  183. d.addSeries("S1", values, bins, minimum, maximum);
  184. assertEquals(0.0, d.getYValue(0, 629), EPSILON);
  185. assertEquals(1.0, d.getYValue(0, 630), EPSILON);
  186. assertEquals(0.0, d.getYValue(0, 631), EPSILON);
  187. assertTrue(values[0] > d.getStartXValue(0, 630));
  188. assertTrue(values[0] < d.getEndXValue(0, 630));
  189. }
  190. /**
  191. * Some checks for bug 1553088. An IndexOutOfBoundsException is thrown
  192. * when a data value is *very* close to the upper limit of the last bin.
  193. */
  194. @Test
  195. public void test1553088() {
  196. double[] values = {-1.0, 0.0, -Double.MIN_VALUE, 3.0};
  197. HistogramDataset d = new HistogramDataset();
  198. d.addSeries("S1", values, 2, -1.0, 0.0);
  199. assertEquals(-1.0, d.getStartXValue(0, 0), EPSILON);
  200. assertEquals(-0.5, d.getEndXValue(0, 0), EPSILON);
  201. assertEquals(1.0, d.getYValue(0, 0), EPSILON);
  202. assertEquals(-0.5, d.getStartXValue(0, 1), EPSILON);
  203. assertEquals(0.0, d.getEndXValue(0, 1), EPSILON);
  204. assertEquals(3.0, d.getYValue(0, 1), EPSILON);
  205. }
  206. /**
  207. * A test to show the limitation addressed by patch 2902842.
  208. */
  209. @Test
  210. public void test2902842() {
  211. this.lastEvent = null;
  212. double[] values = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
  213. HistogramDataset hd = new HistogramDataset();
  214. hd.addChangeListener(this);
  215. hd.addSeries("S1", values, 5);
  216. assertNotNull(this.lastEvent);
  217. }
  218. /**
  219. * A reference to the last event received by the datasetChanged() method.
  220. */
  221. private DatasetChangeEvent lastEvent;
  222. /**
  223. * Receives event notification.
  224. *
  225. * @param event the event.
  226. */
  227. @Override
  228. public void datasetChanged(DatasetChangeEvent event) {
  229. this.lastEvent = event;
  230. }
  231. }