1 44 45 package org.jfree.data.statistics.junit; 46 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectOutputStream ; 53 54 import junit.framework.Test; 55 import junit.framework.TestCase; 56 import junit.framework.TestSuite; 57 58 import org.jfree.data.statistics.HistogramDataset; 59 60 63 public class HistogramDatasetTests extends TestCase { 64 65 70 public static Test suite() { 71 return new TestSuite(HistogramDatasetTests.class); 72 } 73 74 79 public HistogramDatasetTests(String name) { 80 super(name); 81 } 82 83 private static final double EPSILON = 0.0000000001; 84 85 88 public void testBins() { 89 double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5}; 90 HistogramDataset hd = new HistogramDataset(); 91 hd.addSeries("Series 1", values, 5); 92 assertEquals(hd.getYValue(0, 0), 3.0, EPSILON); 93 assertEquals(hd.getYValue(0, 1), 3.0, EPSILON); 94 assertEquals(hd.getYValue(0, 2), 2.0, EPSILON); 95 assertEquals(hd.getYValue(0, 3), 0.0, EPSILON); 96 assertEquals(hd.getYValue(0, 4), 1.0, EPSILON); 97 } 98 99 102 public void testEquals() { 103 104 double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5}; 105 HistogramDataset d1 = new HistogramDataset(); 106 d1.addSeries("Series 1", values, 5); 107 HistogramDataset d2 = new HistogramDataset(); 108 d2.addSeries("Series 1", values, 5); 109 110 assertTrue(d1.equals(d2)); 111 assertTrue(d2.equals(d1)); 112 113 } 114 115 118 public void testCloning() { 119 double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5}; 120 HistogramDataset d1 = new HistogramDataset(); 121 d1.addSeries("Series 1", values, 5); 122 HistogramDataset d2 = null; 123 try { 124 d2 = (HistogramDataset) d1.clone(); 125 } 126 catch (CloneNotSupportedException e) { 127 System.err.println("Failed to clone."); 128 } 129 assertTrue(d1 != d2); 130 assertTrue(d1.getClass() == d2.getClass()); 131 assertTrue(d1.equals(d2)); 132 } 133 134 137 public void testSerialization() { 138 double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5}; 139 HistogramDataset d1 = new HistogramDataset(); 140 d1.addSeries("Series 1", values, 5); 141 HistogramDataset d2 = null; 142 143 try { 144 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 145 ObjectOutput out = new ObjectOutputStream (buffer); 146 out.writeObject(d1); 147 out.close(); 148 149 ObjectInput in = new ObjectInputStream ( 150 new ByteArrayInputStream (buffer.toByteArray()) 151 ); 152 d2 = (HistogramDataset) in.readObject(); 153 in.close(); 154 } 155 catch (Exception e) { 156 System.out.println(e.toString()); 157 } 158 assertEquals(d1, d2); 159 } 160 161 165 public void testGetSeriesKey() { 166 double[] values = {1.0, 2.0, 3.0, 4.0, 6.0, 12.0, 5.0, 6.3, 4.5}; 167 HistogramDataset d1 = new HistogramDataset(); 168 d1.addSeries("Series 1", values, 5); 169 assertEquals("Series 1", d1.getSeriesKey(0)); 170 } 171 172 175 public void testAddSeries() { 176 double[] values = {-1.0, 0.0, 0.1, 0.9, 1.0, 1.1, 1.9, 2.0, 3.0}; 177 HistogramDataset d = new HistogramDataset(); 178 d.addSeries("S1", values, 2, 0.0, 2.0); 179 assertEquals(0.0, d.getStartXValue(0, 0), EPSILON); 180 assertEquals(1.0, d.getEndXValue(0, 0), EPSILON); 181 assertEquals(4.0, d.getYValue(0, 0), EPSILON); 182 183 assertEquals(1.0, d.getStartXValue(0, 1), EPSILON); 184 assertEquals(2.0, d.getEndXValue(0, 1), EPSILON); 185 assertEquals(5.0, d.getYValue(0, 1), EPSILON); 186 } 187 188 191 public void testBinBoundaries() { 192 double[] values = { -5.000000000000286E-5 }; 193 int bins = 1260; 194 double minimum = -0.06307522528160199; 195 double maximum = 0.06297522528160199; 196 HistogramDataset d = new HistogramDataset(); 197 d.addSeries("S1", values, bins, minimum, maximum); 198 assertEquals(0.0, d.getYValue(0, 629), EPSILON); 199 assertEquals(1.0, d.getYValue(0, 630), EPSILON); 200 assertEquals(0.0, d.getYValue(0, 631), EPSILON); 201 assertTrue(values[0] > d.getStartXValue(0, 630)); 202 assertTrue(values[0] < d.getEndXValue(0, 630)); 203 } 204 205 209 public void test1553088() { 210 double[] values = {-1.0, 0.0, -Double.MIN_VALUE, 3.0}; 211 HistogramDataset d = new HistogramDataset(); 212 d.addSeries("S1", values, 2, -1.0, 0.0); 213 assertEquals(-1.0, d.getStartXValue(0, 0), EPSILON); 214 assertEquals(-0.5, d.getEndXValue(0, 0), EPSILON); 215 assertEquals(1.0, d.getYValue(0, 0), EPSILON); 216 217 assertEquals(-0.5, d.getStartXValue(0, 1), EPSILON); 218 assertEquals(0.0, d.getEndXValue(0, 1), EPSILON); 219 assertEquals(3.0, d.getYValue(0, 1), EPSILON); 220 } 221 222 } 223 | Popular Tags |