1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ----------------------------
  25. * SimpleHistogramBinTests.java
  26. * ----------------------------
  27. * (C) Copyright 2005 by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: SimpleHistogramBinTests.java,v 1.1 2005/01/10 14:35:58 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 10-Jan-2005 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.statistics.junit;
  40. import java.io.ByteArrayInputStream;
  41. import java.io.ByteArrayOutputStream;
  42. import java.io.ObjectInput;
  43. import java.io.ObjectInputStream;
  44. import java.io.ObjectOutput;
  45. import java.io.ObjectOutputStream;
  46. import junit.framework.Test;
  47. import junit.framework.TestCase;
  48. import junit.framework.TestSuite;
  49. import org.jfree.data.statistics.SimpleHistogramBin;
  50. /**
  51. * Tests for the {@link SimpleHistogramBin} class.
  52. */
  53. public class SimpleHistogramBinTests extends TestCase {
  54. /**
  55. * Returns the tests as a test suite.
  56. *
  57. * @return The test suite.
  58. */
  59. public static Test suite() {
  60. return new TestSuite(SimpleHistogramBinTests.class);
  61. }
  62. /**
  63. * Constructs a new set of tests.
  64. *
  65. * @param name the name of the tests.
  66. */
  67. public SimpleHistogramBinTests(String name) {
  68. super(name);
  69. }
  70. /**
  71. * Some checks for the accepts() method.
  72. */
  73. public void testAccepts() {
  74. SimpleHistogramBin bin1 = new SimpleHistogramBin(1.0, 2.0);
  75. assertFalse(bin1.accepts(0.0));
  76. assertTrue(bin1.accepts(1.0));
  77. assertTrue(bin1.accepts(1.5));
  78. assertTrue(bin1.accepts(2.0));
  79. assertFalse(bin1.accepts(2.1));
  80. assertFalse(bin1.accepts(Double.NaN));
  81. SimpleHistogramBin bin2 = new SimpleHistogramBin(1.0, 2.0, false, false);
  82. assertFalse(bin2.accepts(0.0));
  83. assertFalse(bin2.accepts(1.0));
  84. assertTrue(bin2.accepts(1.5));
  85. assertFalse(bin2.accepts(2.0));
  86. assertFalse(bin2.accepts(2.1));
  87. assertFalse(bin2.accepts(Double.NaN));
  88. }
  89. /**
  90. * Some checks for the overlapsWith() method.
  91. */
  92. public void testOverlapsWidth() {
  93. SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0);
  94. SimpleHistogramBin b2 = new SimpleHistogramBin(2.0, 3.0);
  95. SimpleHistogramBin b3 = new SimpleHistogramBin(3.0, 4.0);
  96. SimpleHistogramBin b4 = new SimpleHistogramBin(0.0, 5.0);
  97. SimpleHistogramBin b5 = new SimpleHistogramBin(2.0, 3.0, false, true);
  98. SimpleHistogramBin b6 = new SimpleHistogramBin(2.0, 3.0, true, false);
  99. assertTrue(b1.overlapsWith(b2));
  100. assertTrue(b2.overlapsWith(b1));
  101. assertFalse(b1.overlapsWith(b3));
  102. assertFalse(b3.overlapsWith(b1));
  103. assertTrue(b1.overlapsWith(b4));
  104. assertTrue(b4.overlapsWith(b1));
  105. assertFalse(b1.overlapsWith(b5));
  106. assertFalse(b5.overlapsWith(b1));
  107. assertTrue(b1.overlapsWith(b6));
  108. assertTrue(b6.overlapsWith(b1));
  109. }
  110. /**
  111. * Ensure that the equals() method can distinguish all fields.
  112. */
  113. public void testEquals() {
  114. SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0);
  115. SimpleHistogramBin b2 = new SimpleHistogramBin(1.0, 2.0);
  116. assertTrue(b1.equals(b2));
  117. assertTrue(b2.equals(b1));
  118. b1 = new SimpleHistogramBin(1.1, 2.0, true, true);
  119. assertFalse(b1.equals(b2));
  120. b2 = new SimpleHistogramBin(1.1, 2.0, true, true);
  121. assertTrue(b1.equals(b2));
  122. b1 = new SimpleHistogramBin(1.1, 2.2, true, true);
  123. assertFalse(b1.equals(b2));
  124. b2 = new SimpleHistogramBin(1.1, 2.2, true, true);
  125. assertTrue(b1.equals(b2));
  126. b1 = new SimpleHistogramBin(1.1, 2.2, false, true);
  127. assertFalse(b1.equals(b2));
  128. b2 = new SimpleHistogramBin(1.1, 2.2, false, true);
  129. assertTrue(b1.equals(b2));
  130. b1 = new SimpleHistogramBin(1.1, 2.2, false, false);
  131. assertFalse(b1.equals(b2));
  132. b2 = new SimpleHistogramBin(1.1, 2.2, false, false);
  133. assertTrue(b1.equals(b2));
  134. b1.setItemCount(99);
  135. assertFalse(b1.equals(b2));
  136. b2.setItemCount(99);
  137. assertTrue(b1.equals(b2));
  138. }
  139. /**
  140. * Some checks for the clone() method.
  141. */
  142. public void testCloning() {
  143. SimpleHistogramBin b1 = new SimpleHistogramBin(1.1, 2.2, false, true);
  144. b1.setItemCount(99);
  145. SimpleHistogramBin b2 = null;
  146. try {
  147. b2 = (SimpleHistogramBin) b1.clone();
  148. }
  149. catch (CloneNotSupportedException e) {
  150. System.err.println("Failed to clone.");
  151. }
  152. assertTrue(b1 != b2);
  153. assertTrue(b1.getClass() == b2.getClass());
  154. assertTrue(b1.equals(b2));
  155. // check that clone is independent of the original
  156. b2.setItemCount(111);
  157. assertFalse(b1.equals(b2));
  158. }
  159. /**
  160. * Serialize an instance, restore it, and check for equality.
  161. */
  162. public void testSerialization() {
  163. SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0, false, true);
  164. b1.setItemCount(123);
  165. SimpleHistogramBin b2 = null;
  166. try {
  167. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  168. ObjectOutput out = new ObjectOutputStream(buffer);
  169. out.writeObject(b1);
  170. out.close();
  171. ObjectInput in = new ObjectInputStream(
  172. new ByteArrayInputStream(buffer.toByteArray())
  173. );
  174. b2 = (SimpleHistogramBin) in.readObject();
  175. in.close();
  176. }
  177. catch (Exception e) {
  178. System.out.println(e.toString());
  179. }
  180. assertEquals(b1, b2);
  181. }
  182. }