1 41 42 package org.jfree.data.junit; 43 44 import java.io.ByteArrayInputStream ; 45 import java.io.ByteArrayOutputStream ; 46 import java.io.ObjectInput ; 47 import java.io.ObjectInputStream ; 48 import java.io.ObjectOutput ; 49 import java.io.ObjectOutputStream ; 50 51 import junit.framework.Test; 52 import junit.framework.TestCase; 53 import junit.framework.TestSuite; 54 55 import org.jfree.data.Range; 56 57 60 public class RangeTests extends TestCase { 61 62 67 public static Test suite() { 68 return new TestSuite(RangeTests.class); 69 } 70 71 76 public RangeTests(String name) { 77 super(name); 78 } 79 80 83 public void testEquals() { 84 85 Range r1 = new Range(0.0, 1.0); 86 Range r2 = new Range(0.0, 1.0); 87 assertEquals(r1, r2); 88 assertEquals(r2, r1); 89 90 r1 = new Range(0.0, 1.0); 91 r2 = new Range(0.5, 1.0); 92 assertFalse(r1.equals(r2)); 93 94 r1 = new Range(0.0, 1.0); 95 r2 = new Range(0.0, 2.0); 96 assertFalse(r1.equals(r2)); 97 98 } 99 100 103 public void testContains() { 104 Range r1 = new Range(0.0, 1.0); 105 assertFalse(r1.contains(Double.NaN)); 106 assertFalse(r1.contains(Double.NEGATIVE_INFINITY)); 107 assertFalse(r1.contains(-1.0)); 108 assertTrue(r1.contains(0.0)); 109 assertTrue(r1.contains(0.5)); 110 assertTrue(r1.contains(1.0)); 111 assertFalse(r1.contains(2.0)); 112 assertFalse(r1.contains(Double.POSITIVE_INFINITY)); 113 } 114 115 118 public void testConstrain() { 119 Range r1 = new Range(0.0, 1.0); 120 121 double d = r1.constrain(0.5); 122 assertEquals(0.5, d, 0.0000001); 123 124 d = r1.constrain(0.0); 125 assertEquals(0.0, d, 0.0000001); 126 127 d = r1.constrain(1.0); 128 assertEquals(1.0, d, 0.0000001); 129 130 d = r1.constrain(-1.0); 131 assertEquals(0.0, d, 0.0000001); 132 133 d = r1.constrain(2.0); 134 assertEquals(1.0, d, 0.0000001); 135 136 d = r1.constrain(Double.POSITIVE_INFINITY); 137 assertEquals(1.0, d, 0.0000001); 138 139 d = r1.constrain(Double.NEGATIVE_INFINITY); 140 assertEquals(0.0, d, 0.0000001); 141 142 d = r1.constrain(Double.NaN); 143 assertTrue(Double.isNaN(d)); 144 } 145 146 149 public void testIntersects() { 150 Range r1 = new Range(0.0, 1.0); 151 assertFalse(r1.intersects(-2.0, -1.0)); 152 assertFalse(r1.intersects(-2.0, 0.0)); 153 assertTrue(r1.intersects(-2.0, 0.5)); 154 assertTrue(r1.intersects(-2.0, 1.0)); 155 assertTrue(r1.intersects(-2.0, 1.5)); 156 assertTrue(r1.intersects(0.0, 0.5)); 157 assertTrue(r1.intersects(0.0, 1.0)); 158 assertTrue(r1.intersects(0.0, 1.5)); 159 assertTrue(r1.intersects(0.5, 0.6)); 160 assertTrue(r1.intersects(0.5, 1.0)); 161 assertTrue(r1.intersects(0.5, 1.5)); 162 assertFalse(r1.intersects(1.0, 1.1)); 163 assertFalse(r1.intersects(1.5, 2.0)); 164 } 165 166 169 public void testExpand() { 170 Range r1 = new Range(0.0, 100.0); 171 Range r2 = Range.expand(r1, 0.10, 0.10); 172 assertEquals(-10.0, r2.getLowerBound(), 0.001); 173 assertEquals(110.0, r2.getUpperBound(), 0.001); 174 } 175 176 179 public void testSerialization() { 180 181 Range r1 = new Range(25.0, 133.42); 182 Range r2 = null; 183 184 try { 185 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 186 ObjectOutput out = new ObjectOutputStream (buffer); 187 out.writeObject(r1); 188 out.close(); 189 190 ObjectInput in = new ObjectInputStream ( 191 new ByteArrayInputStream (buffer.toByteArray()) 192 ); 193 r2 = (Range) in.readObject(); 194 in.close(); 195 } 196 catch (Exception e) { 197 System.out.println(e.toString()); 198 } 199 assertEquals(r1, r2); 200 201 } 202 203 } 204 | Popular Tags |