1 42 43 package org.jfree.data.statistics.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.data.statistics.SimpleHistogramBin; 57 58 61 public class SimpleHistogramBinTests extends TestCase { 62 63 68 public static Test suite() { 69 return new TestSuite(SimpleHistogramBinTests.class); 70 } 71 72 77 public SimpleHistogramBinTests(String name) { 78 super(name); 79 } 80 81 84 public void testAccepts() { 85 SimpleHistogramBin bin1 = new SimpleHistogramBin(1.0, 2.0); 86 assertFalse(bin1.accepts(0.0)); 87 assertTrue(bin1.accepts(1.0)); 88 assertTrue(bin1.accepts(1.5)); 89 assertTrue(bin1.accepts(2.0)); 90 assertFalse(bin1.accepts(2.1)); 91 assertFalse(bin1.accepts(Double.NaN)); 92 93 SimpleHistogramBin bin2 94 = new SimpleHistogramBin(1.0, 2.0, false, false); 95 assertFalse(bin2.accepts(0.0)); 96 assertFalse(bin2.accepts(1.0)); 97 assertTrue(bin2.accepts(1.5)); 98 assertFalse(bin2.accepts(2.0)); 99 assertFalse(bin2.accepts(2.1)); 100 assertFalse(bin2.accepts(Double.NaN)); 101 } 102 103 106 public void testOverlapsWidth() { 107 SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0); 108 SimpleHistogramBin b2 = new SimpleHistogramBin(2.0, 3.0); 109 SimpleHistogramBin b3 = new SimpleHistogramBin(3.0, 4.0); 110 SimpleHistogramBin b4 = new SimpleHistogramBin(0.0, 5.0); 111 SimpleHistogramBin b5 = new SimpleHistogramBin(2.0, 3.0, false, true); 112 SimpleHistogramBin b6 = new SimpleHistogramBin(2.0, 3.0, true, false); 113 assertTrue(b1.overlapsWith(b2)); 114 assertTrue(b2.overlapsWith(b1)); 115 assertFalse(b1.overlapsWith(b3)); 116 assertFalse(b3.overlapsWith(b1)); 117 assertTrue(b1.overlapsWith(b4)); 118 assertTrue(b4.overlapsWith(b1)); 119 assertFalse(b1.overlapsWith(b5)); 120 assertFalse(b5.overlapsWith(b1)); 121 assertTrue(b1.overlapsWith(b6)); 122 assertTrue(b6.overlapsWith(b1)); 123 } 124 125 128 public void testEquals() { 129 SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0); 130 SimpleHistogramBin b2 = new SimpleHistogramBin(1.0, 2.0); 131 assertTrue(b1.equals(b2)); 132 assertTrue(b2.equals(b1)); 133 134 b1 = new SimpleHistogramBin(1.1, 2.0, true, true); 135 assertFalse(b1.equals(b2)); 136 b2 = new SimpleHistogramBin(1.1, 2.0, true, true); 137 assertTrue(b1.equals(b2)); 138 139 b1 = new SimpleHistogramBin(1.1, 2.2, true, true); 140 assertFalse(b1.equals(b2)); 141 b2 = new SimpleHistogramBin(1.1, 2.2, true, true); 142 assertTrue(b1.equals(b2)); 143 144 b1 = new SimpleHistogramBin(1.1, 2.2, false, true); 145 assertFalse(b1.equals(b2)); 146 b2 = new SimpleHistogramBin(1.1, 2.2, false, true); 147 assertTrue(b1.equals(b2)); 148 149 b1 = new SimpleHistogramBin(1.1, 2.2, false, false); 150 assertFalse(b1.equals(b2)); 151 b2 = new SimpleHistogramBin(1.1, 2.2, false, false); 152 assertTrue(b1.equals(b2)); 153 154 b1.setItemCount(99); 155 assertFalse(b1.equals(b2)); 156 b2.setItemCount(99); 157 assertTrue(b1.equals(b2)); 158 } 159 160 163 public void testCloning() { 164 SimpleHistogramBin b1 = new SimpleHistogramBin(1.1, 2.2, false, true); 165 b1.setItemCount(99); 166 SimpleHistogramBin b2 = null; 167 try { 168 b2 = (SimpleHistogramBin) b1.clone(); 169 } 170 catch (CloneNotSupportedException e) { 171 System.err.println("Failed to clone."); 172 } 173 assertTrue(b1 != b2); 174 assertTrue(b1.getClass() == b2.getClass()); 175 assertTrue(b1.equals(b2)); 176 177 b2.setItemCount(111); 179 assertFalse(b1.equals(b2)); 180 } 181 182 185 public void testSerialization() { 186 187 SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0, false, true); 188 b1.setItemCount(123); 189 SimpleHistogramBin b2 = null; 190 try { 191 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 192 ObjectOutput out = new ObjectOutputStream (buffer); 193 out.writeObject(b1); 194 out.close(); 195 ObjectInput in = new ObjectInputStream ( 196 new ByteArrayInputStream (buffer.toByteArray()) 197 ); 198 b2 = (SimpleHistogramBin) in.readObject(); 199 in.close(); 200 } 201 catch (Exception e) { 202 System.out.println(e.toString()); 203 } 204 assertEquals(b1, b2); 205 } 206 207 } 208 | Popular Tags |