DefaultHeatMapDatasetTest.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * DefaultHeatMapDatasetTest.java
  29. * ------------------------------
  30. * (C) Copyright 2009-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes:
  36. * --------
  37. * 28-Jan-2009 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.data.general;
  41. import org.jfree.chart.TestUtilities;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNull;
  46. import static org.junit.Assert.assertNotNull;
  47. import org.junit.Test;
  48. /**
  49. * Somes tests for the {@link DefaultHeatMapDataset} class.
  50. *
  51. * @since 1.0.13
  52. */
  53. public class DefaultHeatMapDatasetTest implements DatasetChangeListener {
  54. /** The last event received. */
  55. private DatasetChangeEvent lastEvent;
  56. /**
  57. * Records the last event.
  58. *
  59. * @param event the last event.
  60. */
  61. @Override
  62. public void datasetChanged(DatasetChangeEvent event) {
  63. this.lastEvent = event;
  64. }
  65. private static final double EPSILON = 0.0000000001;
  66. /**
  67. * Some general tests.
  68. */
  69. @Test
  70. public void testGeneral() {
  71. DefaultHeatMapDataset d = new DefaultHeatMapDataset(10, 5, 0.0, 9.0,
  72. 0.0, 5.0);
  73. assertEquals(10, d.getXSampleCount());
  74. assertEquals(5, d.getYSampleCount());
  75. assertEquals(0.0, d.getMinimumXValue(), EPSILON);
  76. assertEquals(9.0, d.getMaximumXValue(), EPSILON);
  77. assertEquals(0.0, d.getMinimumYValue(), EPSILON);
  78. assertEquals(5.0, d.getMaximumYValue(), EPSILON);
  79. assertEquals(0.0, d.getZValue(0, 0), EPSILON);
  80. d.addChangeListener(this);
  81. d.setZValue(0, 0, 1.0, false);
  82. assertEquals(1.0, d.getZValue(0, 0), EPSILON);
  83. assertNull(this.lastEvent);
  84. d.setZValue(1, 2, 2.0);
  85. assertEquals(2.0, d.getZValue(1, 2), EPSILON);
  86. assertNotNull(this.lastEvent);
  87. }
  88. /**
  89. * Some tests for the equals() method.
  90. */
  91. @Test
  92. public void testEquals() {
  93. DefaultHeatMapDataset d1 = new DefaultHeatMapDataset(5, 10, 1.0, 2.0,
  94. 3.0, 4.0);
  95. DefaultHeatMapDataset d2 = new DefaultHeatMapDataset(5, 10, 1.0, 2.0,
  96. 3.0, 4.0);
  97. assertEquals(d1, d2);
  98. d1 = new DefaultHeatMapDataset(6, 10, 1.0, 2.0, 3.0, 4.0);
  99. assertFalse(d1.equals(d2));
  100. d2 = new DefaultHeatMapDataset(6, 10, 1.0, 2.0, 3.0, 4.0);
  101. assertTrue(d1.equals(d2));
  102. d1 = new DefaultHeatMapDataset(6, 11, 1.0, 2.0, 3.0, 4.0);
  103. assertFalse(d1.equals(d2));
  104. d2 = new DefaultHeatMapDataset(6, 11, 1.0, 2.0, 3.0, 4.0);
  105. assertTrue(d1.equals(d2));
  106. d1 = new DefaultHeatMapDataset(6, 11, 2.0, 2.0, 3.0, 4.0);
  107. assertFalse(d1.equals(d2));
  108. d2 = new DefaultHeatMapDataset(6, 11, 2.0, 2.0, 3.0, 4.0);
  109. assertTrue(d1.equals(d2));
  110. d1 = new DefaultHeatMapDataset(6, 11, 2.0, 3.0, 3.0, 4.0);
  111. assertFalse(d1.equals(d2));
  112. d2 = new DefaultHeatMapDataset(6, 11, 2.0, 3.0, 3.0, 4.0);
  113. assertTrue(d1.equals(d2));
  114. d1 = new DefaultHeatMapDataset(6, 11, 2.0, 3.0, 4.0, 4.0);
  115. assertFalse(d1.equals(d2));
  116. d2 = new DefaultHeatMapDataset(6, 11, 2.0, 3.0, 4.0, 4.0);
  117. assertTrue(d1.equals(d2));
  118. d1 = new DefaultHeatMapDataset(6, 11, 2.0, 3.0, 4.0, 5.0);
  119. assertFalse(d1.equals(d2));
  120. d2 = new DefaultHeatMapDataset(6, 11, 2.0, 3.0, 4.0, 5.0);
  121. assertTrue(d1.equals(d2));
  122. d1.setZValue(1, 2, 3.0);
  123. assertFalse(d1.equals(d2));
  124. d2.setZValue(1, 2, 3.0);
  125. assertTrue(d1.equals(d2));
  126. d1.setZValue(0, 0, Double.NEGATIVE_INFINITY);
  127. assertFalse(d1.equals(d2));
  128. d2.setZValue(0, 0, Double.NEGATIVE_INFINITY);
  129. assertTrue(d1.equals(d2));
  130. d1.setZValue(0, 1, Double.POSITIVE_INFINITY);
  131. assertFalse(d1.equals(d2));
  132. d2.setZValue(0, 1, Double.POSITIVE_INFINITY);
  133. assertTrue(d1.equals(d2));
  134. d1.setZValue(0, 2, Double.NaN);
  135. assertFalse(d1.equals(d2));
  136. d2.setZValue(0, 2, Double.NaN);
  137. assertTrue(d1.equals(d2));
  138. }
  139. /**
  140. * Confirm that cloning works.
  141. */
  142. @Test
  143. public void testCloning() throws CloneNotSupportedException {
  144. DefaultHeatMapDataset d1 = new DefaultHeatMapDataset(2, 3, -1.0, 4.0,
  145. -2.0, 5.0);
  146. d1.setZValue(0, 0, 10.0);
  147. d1.setZValue(0, 1, Double.NEGATIVE_INFINITY);
  148. d1.setZValue(0, 2, Double.POSITIVE_INFINITY);
  149. d1.setZValue(1, 0, Double.NaN);
  150. DefaultHeatMapDataset d2 = (DefaultHeatMapDataset) d1.clone();
  151. assertTrue(d1 != d2);
  152. assertTrue(d1.getClass() == d2.getClass());
  153. assertTrue(d1.equals(d2));
  154. // simple check for independence
  155. d1.setZValue(0, 0, 11.0);
  156. assertFalse(d1.equals(d2));
  157. d2.setZValue(0, 0, 11.0);
  158. assertTrue(d1.equals(d2));
  159. }
  160. /**
  161. * Serialize an instance, restore it, and check for equality.
  162. */
  163. @Test
  164. public void testSerialization() {
  165. DefaultHeatMapDataset d1 = new DefaultHeatMapDataset(2, 3, -1.0, 4.0,
  166. -2.0, 5.0);
  167. d1.setZValue(0, 0, 10.0);
  168. d1.setZValue(0, 1, Double.NEGATIVE_INFINITY);
  169. d1.setZValue(0, 2, Double.POSITIVE_INFINITY);
  170. d1.setZValue(1, 0, Double.NaN);
  171. DefaultHeatMapDataset d2 = (DefaultHeatMapDataset)
  172. TestUtilities.serialised(d1);
  173. assertEquals(d1, d2);
  174. }
  175. }