BoxAndWhiskerCalculatorTest.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * --------------------------------
  28. * BoxAndWhiskerCalculatorTest.java
  29. * --------------------------------
  30. * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 28-Aug-2003 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.data.statistics;
  41. import static org.junit.Assert.assertTrue;
  42. import static org.junit.Assert.assertEquals;
  43. import java.util.ArrayList;
  44. import java.util.List;
  45. import org.junit.Test;
  46. /**
  47. * Tests for the {@link BoxAndWhiskerCalculator} class.
  48. */
  49. public class BoxAndWhiskerCalculatorTest {
  50. /**
  51. * Some checks for the calculateBoxAndWhiskerStatistics() method.
  52. */
  53. @Test
  54. public void testCalculateBoxAndWhiskerStatistics() {
  55. // try null list
  56. boolean pass = false;
  57. try {
  58. BoxAndWhiskerCalculator.calculateBoxAndWhiskerStatistics(null);
  59. }
  60. catch (IllegalArgumentException e) {
  61. pass = true;
  62. }
  63. assertTrue(pass);
  64. // try a list containing a single value
  65. List values = new ArrayList();
  66. values.add(new Double(1.1));
  67. BoxAndWhiskerItem item
  68. = BoxAndWhiskerCalculator.calculateBoxAndWhiskerStatistics(values);
  69. assertEquals(1.1, item.getMean().doubleValue(), EPSILON);
  70. assertEquals(1.1, item.getMedian().doubleValue(), EPSILON);
  71. assertEquals(1.1, item.getQ1().doubleValue(), EPSILON);
  72. assertEquals(1.1, item.getQ3().doubleValue(), EPSILON);
  73. }
  74. private static final double EPSILON = 0.000000001;
  75. /**
  76. * Tests the Q1 calculation.
  77. */
  78. @Test
  79. public void testCalculateQ1() {
  80. // try null argument
  81. boolean pass = false;
  82. try {
  83. BoxAndWhiskerCalculator.calculateQ1(null);
  84. }
  85. catch (IllegalArgumentException e) {
  86. pass = true;
  87. }
  88. assertTrue(pass);
  89. List values = new ArrayList();
  90. double q1 = BoxAndWhiskerCalculator.calculateQ1(values);
  91. assertTrue(Double.isNaN(q1));
  92. values.add(new Double(1.0));
  93. q1 = BoxAndWhiskerCalculator.calculateQ1(values);
  94. assertEquals(q1, 1.0, EPSILON);
  95. values.add(new Double(2.0));
  96. q1 = BoxAndWhiskerCalculator.calculateQ1(values);
  97. assertEquals(q1, 1.0, EPSILON);
  98. values.add(new Double(3.0));
  99. q1 = BoxAndWhiskerCalculator.calculateQ1(values);
  100. assertEquals(q1, 1.5, EPSILON);
  101. values.add(new Double(4.0));
  102. q1 = BoxAndWhiskerCalculator.calculateQ1(values);
  103. assertEquals(q1, 1.5, EPSILON);
  104. }
  105. /**
  106. * Tests the Q3 calculation.
  107. */
  108. @Test
  109. public void testCalculateQ3() {
  110. // try null argument
  111. boolean pass = false;
  112. try {
  113. BoxAndWhiskerCalculator.calculateQ3(null);
  114. }
  115. catch (IllegalArgumentException e) {
  116. pass = true;
  117. }
  118. assertTrue(pass);
  119. List values = new ArrayList();
  120. double q3 = BoxAndWhiskerCalculator.calculateQ3(values);
  121. assertTrue(Double.isNaN(q3));
  122. values.add(new Double(1.0));
  123. q3 = BoxAndWhiskerCalculator.calculateQ3(values);
  124. assertEquals(q3, 1.0, EPSILON);
  125. values.add(new Double(2.0));
  126. q3 = BoxAndWhiskerCalculator.calculateQ3(values);
  127. assertEquals(q3, 2.0, EPSILON);
  128. values.add(new Double(3.0));
  129. q3 = BoxAndWhiskerCalculator.calculateQ3(values);
  130. assertEquals(q3, 2.5, EPSILON);
  131. values.add(new Double(4.0));
  132. q3 = BoxAndWhiskerCalculator.calculateQ3(values);
  133. assertEquals(q3, 3.5, EPSILON);
  134. }
  135. /**
  136. * The test case included in bug report 1593149.
  137. */
  138. @Test
  139. public void test1593149() {
  140. ArrayList theList = new ArrayList(5);
  141. theList.add(0, new Double(1.0));
  142. theList.add(1, new Double(2.0));
  143. theList.add(2, new Double(Double.NaN));
  144. theList.add(3, new Double(3.0));
  145. theList.add(4, new Double(4.0));
  146. BoxAndWhiskerItem theItem =
  147. BoxAndWhiskerCalculator.calculateBoxAndWhiskerStatistics(theList);
  148. assertEquals(1.0, theItem.getMinRegularValue().doubleValue(), EPSILON);
  149. assertEquals(4.0, theItem.getMaxRegularValue().doubleValue(), EPSILON);
  150. }
  151. }