001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------
028: * RangeTests.java
029: * ---------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: RangeTests.java,v 1.1.2.1 2006/10/03 15:41:43 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 14-Aug-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.data.Range;
057:
058: /**
059: * Tests for the {@link Range} class.
060: */
061: public class RangeTests extends TestCase {
062:
063: /**
064: * Returns the tests as a test suite.
065: *
066: * @return The test suite.
067: */
068: public static Test suite() {
069: return new TestSuite(RangeTests.class);
070: }
071:
072: /**
073: * Constructs a new set of tests.
074: *
075: * @param name the name of the tests.
076: */
077: public RangeTests(String name) {
078: super (name);
079: }
080:
081: /**
082: * Confirm that the equals method can distinguish all the required fields.
083: */
084: public void testEquals() {
085:
086: Range r1 = new Range(0.0, 1.0);
087: Range r2 = new Range(0.0, 1.0);
088: assertEquals(r1, r2);
089: assertEquals(r2, r1);
090:
091: r1 = new Range(0.0, 1.0);
092: r2 = new Range(0.5, 1.0);
093: assertFalse(r1.equals(r2));
094:
095: r1 = new Range(0.0, 1.0);
096: r2 = new Range(0.0, 2.0);
097: assertFalse(r1.equals(r2));
098:
099: }
100:
101: /**
102: * Simple tests for the contains() method.
103: */
104: public void testContains() {
105: Range r1 = new Range(0.0, 1.0);
106: assertFalse(r1.contains(Double.NaN));
107: assertFalse(r1.contains(Double.NEGATIVE_INFINITY));
108: assertFalse(r1.contains(-1.0));
109: assertTrue(r1.contains(0.0));
110: assertTrue(r1.contains(0.5));
111: assertTrue(r1.contains(1.0));
112: assertFalse(r1.contains(2.0));
113: assertFalse(r1.contains(Double.POSITIVE_INFINITY));
114: }
115:
116: /**
117: * Tests the constrain() method for various values.
118: */
119: public void testConstrain() {
120: Range r1 = new Range(0.0, 1.0);
121:
122: double d = r1.constrain(0.5);
123: assertEquals(0.5, d, 0.0000001);
124:
125: d = r1.constrain(0.0);
126: assertEquals(0.0, d, 0.0000001);
127:
128: d = r1.constrain(1.0);
129: assertEquals(1.0, d, 0.0000001);
130:
131: d = r1.constrain(-1.0);
132: assertEquals(0.0, d, 0.0000001);
133:
134: d = r1.constrain(2.0);
135: assertEquals(1.0, d, 0.0000001);
136:
137: d = r1.constrain(Double.POSITIVE_INFINITY);
138: assertEquals(1.0, d, 0.0000001);
139:
140: d = r1.constrain(Double.NEGATIVE_INFINITY);
141: assertEquals(0.0, d, 0.0000001);
142:
143: d = r1.constrain(Double.NaN);
144: assertTrue(Double.isNaN(d));
145: }
146:
147: /**
148: * Simple tests for the intersects() method.
149: */
150: public void testIntersects() {
151: Range r1 = new Range(0.0, 1.0);
152: assertFalse(r1.intersects(-2.0, -1.0));
153: assertFalse(r1.intersects(-2.0, 0.0));
154: assertTrue(r1.intersects(-2.0, 0.5));
155: assertTrue(r1.intersects(-2.0, 1.0));
156: assertTrue(r1.intersects(-2.0, 1.5));
157: assertTrue(r1.intersects(0.0, 0.5));
158: assertTrue(r1.intersects(0.0, 1.0));
159: assertTrue(r1.intersects(0.0, 1.5));
160: assertTrue(r1.intersects(0.5, 0.6));
161: assertTrue(r1.intersects(0.5, 1.0));
162: assertTrue(r1.intersects(0.5, 1.5));
163: assertFalse(r1.intersects(1.0, 1.1));
164: assertFalse(r1.intersects(1.5, 2.0));
165: }
166:
167: /**
168: * A simple test for the expand() method.
169: */
170: public void testExpand() {
171: Range r1 = new Range(0.0, 100.0);
172: Range r2 = Range.expand(r1, 0.10, 0.10);
173: assertEquals(-10.0, r2.getLowerBound(), 0.001);
174: assertEquals(110.0, r2.getUpperBound(), 0.001);
175: }
176:
177: /**
178: * Serialize an instance, restore it, and check for equality.
179: */
180: public void testSerialization() {
181:
182: Range r1 = new Range(25.0, 133.42);
183: Range r2 = null;
184:
185: try {
186: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
187: ObjectOutput out = new ObjectOutputStream(buffer);
188: out.writeObject(r1);
189: out.close();
190:
191: ObjectInput in = new ObjectInputStream(
192: new ByteArrayInputStream(buffer.toByteArray()));
193: r2 = (Range) in.readObject();
194: in.close();
195: } catch (Exception e) {
196: System.out.println(e.toString());
197: }
198: assertEquals(r1, r2);
199:
200: }
201:
202: }
|