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