KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > junit > MovingAverageTests


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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------------
28  * MovingAverageTests.java
29  * -----------------------
30  * (C) Copyright 2003-2005 by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: MovingAverageTests.java,v 1.1.2.1 2006/10/03 15:41:40 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 14-Aug-2003 : Version 1 (DG);
40  * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
41  *
42  */

43
44 package org.jfree.data.time.junit;
45
46 import junit.framework.Test;
47 import junit.framework.TestCase;
48 import junit.framework.TestSuite;
49
50 import org.jfree.data.time.Day;
51 import org.jfree.data.time.MovingAverage;
52 import org.jfree.data.time.TimeSeries;
53 import org.jfree.date.MonthConstants;
54
55 /**
56  * Tests for the {@link MovingAverage} class.
57  */

58 public class MovingAverageTests extends TestCase {
59
60     private static final double EPSILON = 0.0000000001;
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(MovingAverageTests.class);
69     }
70
71     /**
72      * Constructs a new set of tests.
73      *
74      * @param name the name of the tests.
75      */

76     public MovingAverageTests(String JavaDoc name) {
77         super(name);
78     }
79
80     /**
81      * A test for the values calculated from a time series.
82      */

83     public void test1() {
84         TimeSeries source = createDailyTimeSeries1();
85         TimeSeries maverage = MovingAverage.createMovingAverage(
86             source, "Moving Average", 3, 3
87         );
88         
89         // the moving average series has 7 items, the first three
90
// days (11, 12, 13 August are skipped)
91
assertEquals(7, maverage.getItemCount());
92         double value = maverage.getValue(0).doubleValue();
93         assertEquals(14.1, value, EPSILON);
94         value = maverage.getValue(1).doubleValue();
95         assertEquals(13.4, value, EPSILON);
96         value = maverage.getValue(2).doubleValue();
97         assertEquals(14.433333333333, value, EPSILON);
98         value = maverage.getValue(3).doubleValue();
99         assertEquals(14.933333333333, value, EPSILON);
100         value = maverage.getValue(4).doubleValue();
101         assertEquals(19.8, value, EPSILON);
102         value = maverage.getValue(5).doubleValue();
103         assertEquals(15.25, value, EPSILON);
104         value = maverage.getValue(6).doubleValue();
105         assertEquals(12.5, value, EPSILON);
106     }
107     
108     /**
109      * Creates a sample series.
110      *
111      * @return A sample series.
112      */

113     private TimeSeries createDailyTimeSeries1() {
114         
115         TimeSeries series = new TimeSeries("Series 1", Day.class);
116         series.add(new Day(11, MonthConstants.AUGUST, 2003), 11.2);
117         series.add(new Day(13, MonthConstants.AUGUST, 2003), 13.8);
118         series.add(new Day(17, MonthConstants.AUGUST, 2003), 14.1);
119         series.add(new Day(18, MonthConstants.AUGUST, 2003), 12.7);
120         series.add(new Day(19, MonthConstants.AUGUST, 2003), 16.5);
121         series.add(new Day(20, MonthConstants.AUGUST, 2003), 15.6);
122         series.add(new Day(25, MonthConstants.AUGUST, 2003), 19.8);
123         series.add(new Day(27, MonthConstants.AUGUST, 2003), 10.7);
124         series.add(new Day(28, MonthConstants.AUGUST, 2003), 14.3);
125         return series;
126             
127     }
128     
129 }
130
Popular Tags