KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > junit > RangeTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------
27  * RangeTests.java
28  * ---------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: RangeTests.java,v 1.3 2005/03/29 12:56:53 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 14-Aug-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.data.junit;
43
44 import java.io.ByteArrayInputStream JavaDoc;
45 import java.io.ByteArrayOutputStream JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.io.ObjectOutput JavaDoc;
49 import java.io.ObjectOutputStream JavaDoc;
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 /**
58  * Tests for the {@link Range} class.
59  */

60 public class RangeTests extends TestCase {
61
62     /**
63      * Returns the tests as a test suite.
64      *
65      * @return The test suite.
66      */

67     public static Test suite() {
68         return new TestSuite(RangeTests.class);
69     }
70
71     /**
72      * Constructs a new set of tests.
73      *
74      * @param name the name of the tests.
75      */

76     public RangeTests(String JavaDoc name) {
77         super(name);
78     }
79
80     /**
81      * Confirm that the equals method can distinguish all the required fields.
82      */

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     /**
101      * Simple tests for the contains() method.
102      */

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     /**
116      * Tests the constrain() method for various values.
117      */

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     /**
147      * Simple tests for the intersects() method.
148      */

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     /**
167      * A simple test for the expand() method.
168      */

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     /**
177      * Serialize an instance, restore it, and check for equality.
178      */

179     public void testSerialization() {
180
181         Range r1 = new Range(25.0, 133.42);
182         Range r2 = null;
183
184         try {
185             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
186             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
187             out.writeObject(r1);
188             out.close();
189
190             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
191                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
192             );
193             r2 = (Range) in.readObject();
194             in.close();
195         }
196         catch (Exception JavaDoc e) {
197             System.out.println(e.toString());
198         }
199         assertEquals(r1, r2);
200
201     }
202
203 }
204
Popular Tags