001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * RendererUtilitiesTests.java
029: * ---------------------------
030: * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: RendererUtilitiesTests.java,v 1.1.2.1 2007/05/04 11:12:17 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-Apr-2007 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.renderer.junit;
044:
045: import junit.framework.Test;
046: import junit.framework.TestCase;
047: import junit.framework.TestSuite;
048:
049: import org.jfree.chart.renderer.RendererUtilities;
050: import org.jfree.data.DomainOrder;
051: import org.jfree.data.xy.DefaultXYDataset;
052:
053: /**
054: * Some checks for the {@link RendererUtilities} class.
055: */
056: public class RendererUtilitiesTests extends TestCase {
057:
058: /**
059: * Returns the tests as a test suite.
060: *
061: * @return The test suite.
062: */
063: public static Test suite() {
064: return new TestSuite(RendererUtilitiesTests.class);
065: }
066:
067: /**
068: * Constructs a new set of tests.
069: *
070: * @param name the name of the tests.
071: */
072: public RendererUtilitiesTests(String name) {
073: super (name);
074: }
075:
076: /**
077: * Some checks for the findLiveItemsLowerBound() method when the dataset is
078: * unordered.
079: */
080: public void testFindLiveItemsLowerBoundUnordered() {
081: DefaultXYDataset d = new DefaultXYDataset();
082:
083: // check a series with no items
084: d.addSeries("S1", new double[][] { {}, {} });
085: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 0,
086: 10.0, 11.0));
087:
088: // check a series with one item
089: d.addSeries("S2", new double[][] { { 1.0 }, { 2.0 } });
090: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 1,
091: 0.0, 1.1));
092: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 1,
093: 2.0, 3.3));
094:
095: // check a series with two items
096: d
097: .addSeries("S3", new double[][] { { 1.0, 2.0 },
098: { 2.0, 2.0 } });
099: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
100: 0.0, 1.1));
101: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
102: 1.0, 2.2));
103: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
104: 2.0, 3.3));
105: assertEquals(1, RendererUtilities.findLiveItemsLowerBound(d, 2,
106: 3.0, 4.4));
107:
108: // check a series with three items
109: d.addSeries("S4", new double[][] { { 1.0, 2.0, 1.5 },
110: { 2.0, 2.0, 2.0 } });
111: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
112: 0.0, 1.1));
113: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
114: 1.0, 2.2));
115: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
116: 2.0, 3.3));
117: assertEquals(2, RendererUtilities.findLiveItemsLowerBound(d, 3,
118: 3.0, 4.4));
119:
120: // check a series with four items
121: d.addSeries("S5", new double[][] { { 1.0, 2.0, 1.5, 1.8 },
122: { 2.0, 2.0, 2.0, 2.0 } });
123: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
124: 0.0, 1.1));
125: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
126: 1.0, 2.2));
127: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
128: 2.0, 3.3));
129: assertEquals(3, RendererUtilities.findLiveItemsLowerBound(d, 4,
130: 3.0, 4.4));
131: assertEquals(3, RendererUtilities.findLiveItemsLowerBound(d, 4,
132: 4.0, 5.5));
133: }
134:
135: /**
136: * Some checks for the findLiveItemsLowerBound() method when the dataset is
137: * ASCENDING.
138: */
139: public void testFindLiveItemsLowerBoundAscending() {
140: DefaultXYDataset d = new DefaultXYDataset() {
141: public DomainOrder getDomainOrder() {
142: // we're doing this for testing only, and make sure that we
143: // only add data in ascending order by x-value
144: return DomainOrder.ASCENDING;
145: }
146: };
147: // check a series with no items
148: d.addSeries("S1", new double[][] { {}, {} });
149: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 0,
150: 10.0, 11.1));
151:
152: // check a series with one item
153: d.addSeries("S2", new double[][] { { 1.0 }, { 2.0 } });
154: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 1,
155: 0.0, 1.1));
156: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 1,
157: 2.0, 2.2));
158:
159: // check a series with two items
160: d
161: .addSeries("S3", new double[][] { { 1.0, 2.0 },
162: { 2.0, 2.0 } });
163: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
164: 0.0, 1.1));
165: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
166: 1.0, 2.2));
167: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
168: 2.0, 3.3));
169: assertEquals(1, RendererUtilities.findLiveItemsLowerBound(d, 2,
170: 3.0, 4.4));
171:
172: // check a series with three items
173: d.addSeries("S4", new double[][] { { 1.0, 2.0, 3.0 },
174: { 2.0, 2.0, 2.0 } });
175: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
176: 0.0, 1.1));
177: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
178: 1.0, 2.2));
179: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
180: 2.0, 3.3));
181: assertEquals(1, RendererUtilities.findLiveItemsLowerBound(d, 3,
182: 3.0, 4.4));
183:
184: // check a series with four items
185: d.addSeries("S5", new double[][] { { 1.0, 2.0, 3.0, 4.0 },
186: { 2.0, 2.0, 2.0, 2.0 } });
187: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
188: 0.0, 1.1));
189: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
190: 1.0, 2.2));
191: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
192: 2.0, 3.3));
193: assertEquals(1, RendererUtilities.findLiveItemsLowerBound(d, 4,
194: 3.0, 4.4));
195: assertEquals(2, RendererUtilities.findLiveItemsLowerBound(d, 4,
196: 4.0, 5.5));
197:
198: }
199:
200: /**
201: * Some checks for the findLiveItemsLowerBound() method when the dataset is
202: * DESCENDING.
203: */
204: public void testFindLiveItemsLowerBoundDescending() {
205: DefaultXYDataset d = new DefaultXYDataset() {
206: public DomainOrder getDomainOrder() {
207: // we're doing this for testing only, and make sure that we
208: // only add data in ascending order by x-value
209: return DomainOrder.DESCENDING;
210: }
211: };
212: // check a series with no items
213: d.addSeries("S1", new double[][] { {}, {} });
214: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 0,
215: 10.0, 11.0));
216:
217: // check a series with one item
218: d.addSeries("S2", new double[][] { { 1.0 }, { 2.0 } });
219: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 1,
220: 0.0, 1.0));
221: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 1,
222: 1.1, 2.0));
223:
224: // check a series with two items
225: d
226: .addSeries("S3", new double[][] { { 2.0, 1.0 },
227: { 2.0, 2.0 } });
228: assertEquals(1, RendererUtilities.findLiveItemsLowerBound(d, 2,
229: 0.1, 0.5));
230: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
231: 0.1, 1.0));
232: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
233: 1.1, 2.0));
234: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
235: 2.2, 3.0));
236: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 2,
237: 3.3, 4.0));
238:
239: // check a series with three items
240: d.addSeries("S4", new double[][] { { 3.0, 2.0, 1.0 },
241: { 2.0, 2.0, 2.0 } });
242: assertEquals(1, RendererUtilities.findLiveItemsLowerBound(d, 3,
243: 0.0, 1.0));
244: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
245: 1.0, 2.0));
246: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
247: 2.0, 3.0));
248: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 3,
249: 3.0, 4.0));
250:
251: // check a series with four items
252: d.addSeries("S5", new double[][] { { 4.0, 3.0, 2.0, 1.0 },
253: { 2.0, 2.0, 2.0, 2.0 } });
254: assertEquals(3, RendererUtilities.findLiveItemsLowerBound(d, 4,
255: 0.1, 0.5));
256: assertEquals(2, RendererUtilities.findLiveItemsLowerBound(d, 4,
257: 0.1, 1.0));
258: assertEquals(1, RendererUtilities.findLiveItemsLowerBound(d, 4,
259: 1.1, 2.0));
260: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
261: 2.2, 3.0));
262: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
263: 3.3, 4.0));
264: assertEquals(0, RendererUtilities.findLiveItemsLowerBound(d, 4,
265: 4.4, 5.0));
266: }
267:
268: /**
269: * Some checks for the findLiveItemsUpperBound() method when the dataset is
270: * unordered.
271: */
272: public void testFindLiveItemsUpperBoundUnordered() {
273: DefaultXYDataset d = new DefaultXYDataset();
274:
275: // check a series with no items
276: d.addSeries("S1", new double[][] { {}, {} });
277: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 0,
278: 10.0, 11.0));
279:
280: // check a series with one item
281: d.addSeries("S2", new double[][] { { 1.0 }, { 2.0 } });
282: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 1,
283: 0.0, 1.1));
284: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 1,
285: 2.0, 3.3));
286:
287: // check a series with two items
288: d
289: .addSeries("S3", new double[][] { { 1.0, 2.0 },
290: { 2.0, 2.0 } });
291: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
292: 0.0, 1.1));
293: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
294: 1.0, 2.2));
295: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
296: 2.0, 3.3));
297: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
298: 3.0, 4.4));
299:
300: // check a series with three items
301: d.addSeries("S4", new double[][] { { 1.0, 2.0, 1.5 },
302: { 2.0, 2.0, 2.0 } });
303: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 3,
304: 0.0, 1.1));
305: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
306: 1.0, 2.2));
307: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
308: 2.0, 3.3));
309: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
310: 3.0, 4.4));
311:
312: // check a series with four items
313: d.addSeries("S5", new double[][] { { 1.0, 2.0, 1.5, 1.8 },
314: { 2.0, 2.0, 2.0, 2.0 } });
315: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 4,
316: 0.0, 1.1));
317: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
318: 1.0, 2.2));
319: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
320: 2.0, 3.3));
321: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
322: 3.0, 4.4));
323: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
324: 4.0, 5.5));
325: }
326:
327: /**
328: * Some checks for the findLiveItemsUpperBound() method when the dataset is
329: * ASCENDING.
330: */
331: public void testFindLiveItemsUpperBoundAscending() {
332: DefaultXYDataset d = new DefaultXYDataset() {
333: public DomainOrder getDomainOrder() {
334: // we're doing this for testing only, and make sure that we
335: // only add data in ascending order by x-value
336: return DomainOrder.ASCENDING;
337: }
338: };
339: // check a series with no items
340: d.addSeries("S1", new double[][] { {}, {} });
341: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 0,
342: 10.0, 11.1));
343:
344: // check a series with one item
345: d.addSeries("S2", new double[][] { { 1.0 }, { 2.0 } });
346: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 1,
347: 0.0, 1.1));
348: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 1,
349: 2.0, 2.2));
350:
351: // check a series with two items
352: d
353: .addSeries("S3", new double[][] { { 1.0, 2.0 },
354: { 2.0, 2.0 } });
355: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
356: 0.0, 1.0));
357: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
358: 1.0, 2.2));
359: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
360: 2.0, 3.3));
361: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
362: 3.0, 4.4));
363:
364: // check a series with three items
365: d.addSeries("S4", new double[][] { { 1.0, 2.0, 3.0 },
366: { 2.0, 2.0, 2.0 } });
367: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 3,
368: 0.0, 1.1));
369: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
370: 1.0, 2.2));
371: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
372: 2.0, 3.3));
373: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
374: 3.0, 4.4));
375:
376: // check a series with four items
377: d.addSeries("S5", new double[][] { { 1.0, 2.0, 3.0, 4.0 },
378: { 2.0, 2.0, 2.0, 2.0 } });
379: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 4,
380: 0.0, 1.1));
381: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 4,
382: 1.0, 2.2));
383: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
384: 2.0, 3.3));
385: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
386: 3.0, 4.4));
387: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
388: 4.0, 5.5));
389:
390: }
391:
392: /**
393: * Some checks for the findLiveItemsUpperBound() method when the dataset is
394: * DESCENDING.
395: */
396: public void testFindLiveItemsUpperBoundDescending() {
397: DefaultXYDataset d = new DefaultXYDataset() {
398: public DomainOrder getDomainOrder() {
399: // we're doing this for testing only, and make sure that we
400: // only add data in ascending order by x-value
401: return DomainOrder.DESCENDING;
402: }
403: };
404: // check a series with no items
405: d.addSeries("S1", new double[][] { {}, {} });
406: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 0,
407: 10.0, 11.0));
408:
409: // check a series with one item
410: d.addSeries("S2", new double[][] { { 1.0 }, { 2.0 } });
411: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 1,
412: 0.0, 1.0));
413: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 1,
414: 1.1, 2.0));
415:
416: // check a series with two items
417: d
418: .addSeries("S3", new double[][] { { 2.0, 1.0 },
419: { 2.0, 2.0 } });
420: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
421: 0.1, 0.5));
422: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 2,
423: 0.1, 1.0));
424: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 2,
425: 1.1, 2.0));
426: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 2,
427: 2.2, 3.0));
428: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 2,
429: 3.3, 4.0));
430:
431: // check a series with three items
432: d.addSeries("S4", new double[][] { { 3.0, 2.0, 1.0 },
433: { 2.0, 2.0, 2.0 } });
434: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
435: 0.0, 1.0));
436: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 3,
437: 1.0, 2.0));
438: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 3,
439: 2.0, 3.0));
440: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 3,
441: 3.0, 4.0));
442:
443: // check a series with four items
444: d.addSeries("S5", new double[][] { { 4.0, 3.0, 2.0, 1.0 },
445: { 2.0, 2.0, 2.0, 2.0 } });
446: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
447: 0.1, 0.5));
448: assertEquals(3, RendererUtilities.findLiveItemsUpperBound(d, 4,
449: 0.1, 1.0));
450: assertEquals(2, RendererUtilities.findLiveItemsUpperBound(d, 4,
451: 1.1, 2.0));
452: assertEquals(1, RendererUtilities.findLiveItemsUpperBound(d, 4,
453: 2.2, 3.0));
454: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 4,
455: 3.3, 4.0));
456: assertEquals(0, RendererUtilities.findLiveItemsUpperBound(d, 4,
457: 4.4, 5.0));
458: }
459:
460: }
|