1 42 43 package org.jfree.data.xy.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.xy.XIntervalSeries; 57 58 61 public class XIntervalSeriesTests extends TestCase { 62 63 68 public static Test suite() { 69 return new TestSuite(XIntervalSeriesTests.class); 70 } 71 72 77 public XIntervalSeriesTests(String name) { 78 super(name); 79 } 80 81 84 public void testEquals() { 85 86 XIntervalSeries s1 = new XIntervalSeries("s1"); 87 XIntervalSeries s2 = new XIntervalSeries("s1"); 88 assertTrue(s1.equals(s2)); 89 90 s1 = new XIntervalSeries("s2"); 92 assertFalse(s1.equals(s2)); 93 s2 = new XIntervalSeries("s2"); 94 assertTrue(s1.equals(s2)); 95 96 s1 = new XIntervalSeries("s2", false, true); 98 assertFalse(s1.equals(s2)); 99 s2 = new XIntervalSeries("s2", false, true); 100 assertTrue(s1.equals(s2)); 101 102 s1 = new XIntervalSeries("s2", false, false); 104 assertFalse(s1.equals(s2)); 105 s2 = new XIntervalSeries("s2", false, false); 106 assertTrue(s1.equals(s2)); 107 108 s1.add(1.0, 0.5, 1.5, 2.0); 110 assertFalse(s1.equals(s2)); 111 s2.add(1.0, 0.5, 1.5, 2.0); 112 assertTrue(s2.equals(s1)); 113 114 s1.add(2.0, 0.5, 1.5, 2.0); 116 assertFalse(s1.equals(s2)); 117 s2.add(2.0, 0.5, 1.5, 2.0); 118 assertTrue(s2.equals(s1)); 119 120 s1.remove(new Double (1.0)); 122 assertFalse(s1.equals(s2)); 123 s2.remove(new Double (1.0)); 124 assertTrue(s2.equals(s1)); 125 126 } 127 128 131 public void testCloning() { 132 XIntervalSeries s1 = new XIntervalSeries("s1"); 133 s1.add(1.0, 0.5, 1.5, 2.0); 134 XIntervalSeries s2 = null; 135 try { 136 s2 = (XIntervalSeries) s1.clone(); 137 } 138 catch (CloneNotSupportedException e) { 139 e.printStackTrace(); 140 } 141 assertTrue(s1 != s2); 142 assertTrue(s1.getClass() == s2.getClass()); 143 assertTrue(s1.equals(s2)); 144 } 145 146 149 public void testSerialization() { 150 151 XIntervalSeries s1 = new XIntervalSeries("s1"); 152 s1.add(1.0, 0.5, 1.5, 2.0); 153 XIntervalSeries s2 = null; 154 155 try { 156 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 157 ObjectOutput out = new ObjectOutputStream (buffer); 158 out.writeObject(s1); 159 out.close(); 160 161 ObjectInput in = new ObjectInputStream ( 162 new ByteArrayInputStream (buffer.toByteArray())); 163 s2 = (XIntervalSeries) in.readObject(); 164 in.close(); 165 } 166 catch (Exception e) { 167 e.printStackTrace(); 168 } 169 assertEquals(s1, s2); 170 171 } 172 173 176 public void testIndexOf() { 177 XIntervalSeries s1 = new XIntervalSeries("Series 1"); 178 s1.add(1.0, 1.0, 1.0, 2.0); 179 s1.add(2.0, 2.0, 2.0, 3.0); 180 s1.add(3.0, 3.0, 3.0, 4.0); 181 assertEquals(0, s1.indexOf(new Double (1.0))); 182 } 183 184 187 public void testIndexOf2() { 188 XIntervalSeries s1 = new XIntervalSeries("Series 1", false, true); 189 s1.add(1.0, 1.0, 1.0, 2.0); 190 s1.add(3.0, 3.0, 3.0, 3.0); 191 s1.add(2.0, 2.0, 2.0, 2.0); 192 assertEquals(0, s1.indexOf(new Double (1.0))); 193 assertEquals(1, s1.indexOf(new Double (3.0))); 194 assertEquals(2, s1.indexOf(new Double (2.0))); 195 } 196 197 200 public void testRemove() { 201 XIntervalSeries s1 = new XIntervalSeries("Series 1"); 202 s1.add(1.0, 1.0, 1.0, 2.0); 203 s1.add(2.0, 2.0, 2.0, 2.0); 204 s1.add(3.0, 3.0, 3.0, 3.0); 205 assertEquals(3, s1.getItemCount()); 206 207 s1.remove(new Double (2.0)); 208 assertEquals(new Double (3.0), s1.getX(1)); 209 210 s1.remove(new Double (1.0)); 211 assertEquals(new Double (3.0), s1.getX(0)); 212 } 213 214 private static final double EPSILON = 0.0000000001; 215 216 220 public void testAdditionOfDuplicateXValues() { 221 XIntervalSeries s1 = new XIntervalSeries("Series 1"); 222 s1.add(1.0, 1.0, 1.0, 1.0); 223 s1.add(2.0, 2.0, 2.0, 2.0); 224 s1.add(2.0, 3.0, 3.0, 3.0); 225 s1.add(2.0, 4.0, 4.0, 4.0); 226 s1.add(3.0, 5.0, 5.0, 5.0); 227 assertEquals(1.0, s1.getYValue(0), EPSILON); 228 assertEquals(2.0, s1.getYValue(1), EPSILON); 229 assertEquals(3.0, s1.getYValue(2), EPSILON); 230 assertEquals(4.0, s1.getYValue(3), EPSILON); 231 assertEquals(5.0, s1.getYValue(4), EPSILON); 232 } 233 234 237 public void testAdd() { 238 XIntervalSeries series = new XIntervalSeries("Series", false, true); 239 series.add(5.0, 5.50, 5.50, 5.50); 240 series.add(5.1, 5.51, 5.51, 5.51); 241 series.add(6.0, 6.6, 6.6, 6.6); 242 series.add(3.0, 3.3, 3.3, 3.3); 243 series.add(4.0, 4.4, 4.4, 4.4); 244 series.add(2.0, 2.2, 2.2, 2.2); 245 series.add(1.0, 1.1, 1.1, 1.1); 246 assertEquals(5.5, series.getYValue(0), EPSILON); 247 assertEquals(5.51, series.getYValue(1), EPSILON); 248 assertEquals(6.6, series.getYValue(2), EPSILON); 249 assertEquals(3.3, series.getYValue(3), EPSILON); 250 assertEquals(4.4, series.getYValue(4), EPSILON); 251 assertEquals(2.2, series.getYValue(5), EPSILON); 252 assertEquals(1.1, series.getYValue(6), EPSILON); 253 } 254 255 258 public void testSetMaximumItemCount() { 259 XIntervalSeries s1 = new XIntervalSeries("S1"); 260 assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount()); 261 s1.setMaximumItemCount(2); 262 assertEquals(2, s1.getMaximumItemCount()); 263 s1.add(1.0, 1.1, 1.1, 1.1); 264 s1.add(2.0, 2.2, 2.2, 2.2); 265 s1.add(3.0, 3.3, 3.3, 3.3); 266 assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON); 267 assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON); 268 } 269 270 273 public void testSetMaximumItemCount2() { 274 XIntervalSeries s1 = new XIntervalSeries("S1"); 275 s1.add(1.0, 1.1, 1.1, 1.1); 276 s1.add(2.0, 2.2, 2.2, 2.2); 277 s1.add(3.0, 3.3, 3.3, 3.3); 278 s1.setMaximumItemCount(2); 279 assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON); 280 assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON); 281 } 282 283 } 284 | Popular Tags |