KickJava   Java API By Example, From Geeks To Geeks.

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


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

37
38 package org.jfree.data.junit;
39
40 import junit.framework.Test;
41 import junit.framework.TestCase;
42 import junit.framework.TestSuite;
43
44 import org.jfree.data.MovingAverage;
45 import org.jfree.data.time.Day;
46 import org.jfree.data.time.TimeSeries;
47 import org.jfree.date.SerialDate;
48 import org.jfree.util.NumberUtils;
49
50 /**
51  * Tests for the {@link MovingAverage} class.
52  *
53  * @author David Gilbert
54  */

55 public class MovingAverageTests extends TestCase {
56
57     /**
58      * Returns the tests as a test suite.
59      *
60      * @return the test suite.
61      */

62     public static Test suite() {
63         return new TestSuite(MovingAverageTests.class);
64     }
65
66     /**
67      * Constructs a new set of tests.
68      *
69      * @param name the name of the tests.
70      */

71     public MovingAverageTests(String JavaDoc name) {
72         super(name);
73     }
74
75     /**
76      * A test for the values calculated from a time series.
77      */

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

106     private TimeSeries createDailyTimeSeries1() {
107         
108         TimeSeries series = new TimeSeries("Series 1", Day.class);
109         series.add(new Day(11, SerialDate.AUGUST, 2003), 11.2);
110         series.add(new Day(13, SerialDate.AUGUST, 2003), 13.8);
111         series.add(new Day(17, SerialDate.AUGUST, 2003), 14.1);
112         series.add(new Day(18, SerialDate.AUGUST, 2003), 12.7);
113         series.add(new Day(19, SerialDate.AUGUST, 2003), 16.5);
114         series.add(new Day(20, SerialDate.AUGUST, 2003), 15.6);
115         series.add(new Day(25, SerialDate.AUGUST, 2003), 19.8);
116         series.add(new Day(27, SerialDate.AUGUST, 2003), 10.7);
117         series.add(new Day(28, SerialDate.AUGUST, 2003), 14.3);
118         return series;
119             
120     }
121     
122 }
123
Popular Tags