1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ---------------
  25. * RangeTests.java
  26. * ---------------
  27. * (C) Copyright 2003 by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: MovingAverageTests.java,v 1.4 2004/11/19 11:39:56 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 14-Aug-2003 : Version 1 (DG);
  37. * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
  38. *
  39. */
  40. package org.jfree.data.time.junit;
  41. import junit.framework.Test;
  42. import junit.framework.TestCase;
  43. import junit.framework.TestSuite;
  44. import org.jfree.data.time.Day;
  45. import org.jfree.data.time.MovingAverage;
  46. import org.jfree.data.time.TimeSeries;
  47. import org.jfree.date.MonthConstants;
  48. /**
  49. * Tests for the {@link MovingAverage} class.
  50. */
  51. public class MovingAverageTests extends TestCase {
  52. private static final double EPSILON = 0.0000000001;
  53. /**
  54. * Returns the tests as a test suite.
  55. *
  56. * @return The test suite.
  57. */
  58. public static Test suite() {
  59. return new TestSuite(MovingAverageTests.class);
  60. }
  61. /**
  62. * Constructs a new set of tests.
  63. *
  64. * @param name the name of the tests.
  65. */
  66. public MovingAverageTests(String name) {
  67. super(name);
  68. }
  69. /**
  70. * A test for the values calculated from a time series.
  71. */
  72. public void test1() {
  73. TimeSeries source = createDailyTimeSeries1();
  74. TimeSeries maverage = MovingAverage.createMovingAverage(
  75. source, "Moving Average", 3, 3
  76. );
  77. // the moving average series has 7 items, the first three
  78. // days (11, 12, 13 August are skipped)
  79. assertEquals(7, maverage.getItemCount());
  80. double value = maverage.getValue(0).doubleValue();
  81. assertEquals(14.1, value, EPSILON);
  82. value = maverage.getValue(1).doubleValue();
  83. assertEquals(13.4, value, EPSILON);
  84. value = maverage.getValue(2).doubleValue();
  85. assertEquals(14.433333333333, value, EPSILON);
  86. value = maverage.getValue(3).doubleValue();
  87. assertEquals(14.933333333333, value, EPSILON);
  88. value = maverage.getValue(4).doubleValue();
  89. assertEquals(19.8, value, EPSILON);
  90. value = maverage.getValue(5).doubleValue();
  91. assertEquals(15.25, value, EPSILON);
  92. value = maverage.getValue(6).doubleValue();
  93. assertEquals(12.5, value, EPSILON);
  94. }
  95. /**
  96. * Creates a sample series.
  97. *
  98. * @return A sample series.
  99. */
  100. private TimeSeries createDailyTimeSeries1() {
  101. TimeSeries series = new TimeSeries("Series 1", Day.class);
  102. series.add(new Day(11, MonthConstants.AUGUST, 2003), 11.2);
  103. series.add(new Day(13, MonthConstants.AUGUST, 2003), 13.8);
  104. series.add(new Day(17, MonthConstants.AUGUST, 2003), 14.1);
  105. series.add(new Day(18, MonthConstants.AUGUST, 2003), 12.7);
  106. series.add(new Day(19, MonthConstants.AUGUST, 2003), 16.5);
  107. series.add(new Day(20, MonthConstants.AUGUST, 2003), 15.6);
  108. series.add(new Day(25, MonthConstants.AUGUST, 2003), 19.8);
  109. series.add(new Day(27, MonthConstants.AUGUST, 2003), 10.7);
  110. series.add(new Day(28, MonthConstants.AUGUST, 2003), 14.3);
  111. return series;
  112. }
  113. }