📄 statisticstests.java
字号:
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * -------------------- * StatisticsTests.java * -------------------- * (C) Copyright 2004-2007, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * Changes * ------- * 25-Mar-2004 : Version 1 (DG); * 04-Oct-2004 : Eliminated NumberUtils usage (DG); * */package org.jfree.data.statistics.junit;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.List;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import org.jfree.data.statistics.Statistics;/** * Tests for the {@link Statistics} class. */public class StatisticsTests extends TestCase { /** * Returns the tests as a test suite. * * @return The test suite. */ public static Test suite() { return new TestSuite(StatisticsTests.class); } /** * Constructs a new set of tests. * * @param name the name of the tests. */ public StatisticsTests(String name) { super(name); } /** * Some checks for the calculateMean(Number[]) and * calculateMean(Number[], boolean) methods. */ public void testCalculateMean_Array() { // try null array boolean pass = false; try { Statistics.calculateMean((Number[]) null); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); pass = false; try { Statistics.calculateMean((Number[]) null, false); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); // try an array containing no items assertTrue(Double.isNaN(Statistics.calculateMean(new Number[0]))); assertTrue(Double.isNaN(Statistics.calculateMean(new Number[0], false))); // try an array containing a single Number Number[] values = new Number[] { new Double(1.0) }; assertEquals(1.0, Statistics.calculateMean(values), EPSILON); assertEquals(1.0, Statistics.calculateMean(values, true), EPSILON); assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON); // try an array containing a single Number and a null values = new Number[] { new Double(1.0), null }; assertTrue(Double.isNaN(Statistics.calculateMean(values))); assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON); // try an array containing a single Number and a NaN values = new Number[] { new Double(1.0), new Double(Double.NaN) }; assertTrue(Double.isNaN(Statistics.calculateMean(values))); assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON); } /** * Some checks for the calculateMean(Collection) and * calculateMean(Collection, boolean) methods. */ public void testCalculateMean_Collection() { // try a null collection boolean pass = false; try { Statistics.calculateMean((Collection) null); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); pass = false; try { Statistics.calculateMean((Collection) null, false); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); // try an empty collection List values = new ArrayList(); assertTrue(Double.isNaN(Statistics.calculateMean(values))); assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); assertTrue(Double.isNaN(Statistics.calculateMean(values, false))); // try a collection with a single number values.add(new Double(9.0)); assertEquals(9.0, Statistics.calculateMean(values), EPSILON); assertEquals(9.0, Statistics.calculateMean(values, true), EPSILON); assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON); // try a collection with a single number plus a null values.add(null); assertTrue(Double.isNaN(Statistics.calculateMean(values))); assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON); // try a collection with a single number plus a NaN values.clear(); values.add(new Double(9.0)); values.add(new Double(Double.NaN)); assertTrue(Double.isNaN(Statistics.calculateMean(values))); assertTrue(Double.isNaN(Statistics.calculateMean(values, true))); assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON); // try a collection with several numbers values = new ArrayList(); values.add(new Double(9.0)); values.add(new Double(3.0)); values.add(new Double(2.0)); values.add(new Double(2.0)); double mean = Statistics.calculateMean(values); assertEquals(4.0, mean, EPSILON); // a Collection containing a NaN will return Double.NaN for the result values.add(new Double(Double.NaN)); assertTrue(Double.isNaN(Statistics.calculateMean(values))); } static final double EPSILON = 0.0000000001; /** * Some checks for the calculateMedian(List, boolean) method. */ public void testCalculateMedian() { // check null list assertTrue(Double.isNaN(Statistics.calculateMedian(null, false))); assertTrue(Double.isNaN(Statistics.calculateMedian(null, true))); // check empty list List list = new ArrayList(); assertTrue(Double.isNaN(Statistics.calculateMedian(list, false))); assertTrue(Double.isNaN(Statistics.calculateMedian(list, true))); // check list containing null list.add(null); boolean pass = false; try { Statistics.calculateMedian(list, false); } catch (NullPointerException e) { pass = true; } assertTrue(pass); pass = false; try { Statistics.calculateMedian(list, true); } catch (NullPointerException e) { pass = true; } assertTrue(pass); // check a list containing a non-Number object list.clear(); list.add("Not a number"); pass = false; try { Statistics.calculateMedian(list, false); } catch (ClassCastException e) { pass = true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -