001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.stat.descriptive;
018    
019    import junit.framework.Test;
020    import junit.framework.TestCase;
021    import junit.framework.TestSuite;
022    
023    import org.apache.commons.math.stat.descriptive.moment.Mean;
024    
025    /**
026     * Tests for AbstractUnivariateStatistic 
027     *
028     * @version $Revision: 670469 $ $Date: 2008-06-23 04:01:38 -0400 (Mon, 23 Jun 2008) $
029     */
030    public class AbstractUnivariateStatisticTest extends TestCase {
031        
032        public AbstractUnivariateStatisticTest(String name) {
033            super(name);
034        }
035        
036        public static Test suite() {
037            TestSuite suite = new TestSuite(AbstractUnivariateStatisticTest.class);
038            suite.setName("AbstractUnivariateStatistic Tests");
039            return suite;
040        }
041        
042        protected double[] testArray = {0, 1, 2, 3, 4, 5};
043        protected double[] nullArray = null;
044        protected double[] singletonArray = {0};
045        protected Mean testStatistic = new Mean();
046        
047        public void testTestPositive() {
048            for (int j = 0; j < 6; j++) {
049                for (int i = 1; i < (7 - j); i++) {
050                    assertTrue(testStatistic.test(testArray, 0, i));
051                }  
052            }
053            assertTrue(testStatistic.test(singletonArray, 0, 1));
054        }
055        
056        public void testTestNegative() {
057            assertFalse(testStatistic.test(singletonArray, 0, 0));
058            assertFalse(testStatistic.test(testArray, 0, 0));
059            try {
060                testStatistic.test(singletonArray, 2, 1);  // start past end
061                fail("Expecting IllegalArgumentException");
062            } catch (IllegalArgumentException ex) {
063                // expected
064            }
065            try {
066                testStatistic.test(testArray, 0, 7);  // end past end
067                fail("Expecting IllegalArgumentException");
068            } catch (IllegalArgumentException ex) {
069                // expected
070            }
071            try {
072                testStatistic.test(testArray, -1, 1);  // start negative
073                fail("Expecting IllegalArgumentException");
074            } catch (IllegalArgumentException ex) {
075                // expected
076            }
077            try {
078                testStatistic.test(testArray, 0, -1);  // length negative
079                fail("Expecting IllegalArgumentException");
080            } catch (IllegalArgumentException ex) {
081                // expected
082            }
083            try {
084                testStatistic.test(nullArray, 0, 1);  // null array
085                fail("Expecting IllegalArgumentException");
086            } catch (IllegalArgumentException ex) {
087                // expected
088            }      
089        } 
090    }