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: * MovingAverageTests.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: MovingAverageTests.java,v 1.1.2.1 2006/10/03 15:41:40 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 14-Aug-2003 : Version 1 (DG);
040: * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
041: *
042: */
043:
044: package org.jfree.data.time.junit;
045:
046: import junit.framework.Test;
047: import junit.framework.TestCase;
048: import junit.framework.TestSuite;
049:
050: import org.jfree.data.time.Day;
051: import org.jfree.data.time.MovingAverage;
052: import org.jfree.data.time.TimeSeries;
053: import org.jfree.date.MonthConstants;
054:
055: /**
056: * Tests for the {@link MovingAverage} class.
057: */
058: public class MovingAverageTests extends TestCase {
059:
060: private static final double EPSILON = 0.0000000001;
061:
062: /**
063: * Returns the tests as a test suite.
064: *
065: * @return The test suite.
066: */
067: public static Test suite() {
068: return new TestSuite(MovingAverageTests.class);
069: }
070:
071: /**
072: * Constructs a new set of tests.
073: *
074: * @param name the name of the tests.
075: */
076: public MovingAverageTests(String name) {
077: super (name);
078: }
079:
080: /**
081: * A test for the values calculated from a time series.
082: */
083: public void test1() {
084: TimeSeries source = createDailyTimeSeries1();
085: TimeSeries maverage = MovingAverage.createMovingAverage(source,
086: "Moving Average", 3, 3);
087:
088: // the moving average series has 7 items, the first three
089: // days (11, 12, 13 August are skipped)
090: assertEquals(7, maverage.getItemCount());
091: double value = maverage.getValue(0).doubleValue();
092: assertEquals(14.1, value, EPSILON);
093: value = maverage.getValue(1).doubleValue();
094: assertEquals(13.4, value, EPSILON);
095: value = maverage.getValue(2).doubleValue();
096: assertEquals(14.433333333333, value, EPSILON);
097: value = maverage.getValue(3).doubleValue();
098: assertEquals(14.933333333333, value, EPSILON);
099: value = maverage.getValue(4).doubleValue();
100: assertEquals(19.8, value, EPSILON);
101: value = maverage.getValue(5).doubleValue();
102: assertEquals(15.25, value, EPSILON);
103: value = maverage.getValue(6).doubleValue();
104: assertEquals(12.5, value, EPSILON);
105: }
106:
107: /**
108: * Creates a sample series.
109: *
110: * @return A sample series.
111: */
112: private TimeSeries createDailyTimeSeries1() {
113:
114: TimeSeries series = new TimeSeries("Series 1", Day.class);
115: series.add(new Day(11, MonthConstants.AUGUST, 2003), 11.2);
116: series.add(new Day(13, MonthConstants.AUGUST, 2003), 13.8);
117: series.add(new Day(17, MonthConstants.AUGUST, 2003), 14.1);
118: series.add(new Day(18, MonthConstants.AUGUST, 2003), 12.7);
119: series.add(new Day(19, MonthConstants.AUGUST, 2003), 16.5);
120: series.add(new Day(20, MonthConstants.AUGUST, 2003), 15.6);
121: series.add(new Day(25, MonthConstants.AUGUST, 2003), 19.8);
122: series.add(new Day(27, MonthConstants.AUGUST, 2003), 10.7);
123: series.add(new Day(28, MonthConstants.AUGUST, 2003), 14.3);
124: return series;
125:
126: }
127:
128: }
|