    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.stat;
    17
    18  import java.io.BufferedReader;
    19  import java.io.IOException;
    20  import java.io.StringReader;
    21  import java.util.Iterator;
    22
    23  import junit.framework.Test;
    24  import junit.framework.TestCase;
    25  import junit.framework.TestSuite;
    26
    27  /***
    28   * Test cases for the {@link Frequency} class.
    29   *
    30   * @version $Revision: 1.15 $ $Date: 2004/09/21 03:03:54 $
    31   */
    32
    33  public final class FrequencyTest extends TestCase {
    34      private long oneL = 1;
    35      private long twoL = 2;
    36      private long threeL = 3;
    37      private int oneI = 1;
    38      private int twoI = 2;
    39      private int threeI=3;
    40      private String oneS = "1";
    41      private String twoS = "2";
    42      private double tolerance = 10E-15;
    43      private Frequency f = null;
    44
    45      public FrequencyTest(String name) {
    46          super(name);
    47      }
    48
    49      public void setUp() {
    50      	f = new Frequency();
    51      }
    52
    53      public static Test suite() {
    54          TestSuite suite = new TestSuite(FrequencyTest.class);
    55          suite.setName("Frequency Tests");
    56          return suite;
    57      }
    58
    59      /*** test freq counts */
    60      public void testCounts() {
    61          assertEquals("total count",0,f.getSumFreq());
    62          f.addValue(oneL);
    63          f.addValue(twoL);
    64          f.addValue(1);
    65          f.addValue(oneI);
    66          assertEquals("one frequency count",3,f.getCount(1));
    67          assertEquals("two frequency count",1,f.getCount(2));
    68          assertEquals("three frequency count",0,f.getCount(3));
    69          assertEquals("total count",4,f.getSumFreq());
    70          assertEquals("zero cumulative frequency", 0, f.getCumFreq(0));
    71          assertEquals("one cumulative frequency", 3,  f.getCumFreq(1));
    72          assertEquals("two cumulative frequency", 4,  f.getCumFreq(2));
    73          assertEquals("Integer argument cum freq",4, f.getCumFreq(new Integer(2)));
    74          assertEquals("five cumulative frequency", 4,  f.getCumFreq(5));
    75          assertEquals("foo cumulative frequency", 0,  f.getCumFreq("foo"));
    76
    77          f.clear();
    78          assertEquals("total count",0,f.getSumFreq());
    79
    80          // userguide examples -------------------------------------------------------------------
    81          f.addValue("one");
    82          f.addValue("One");
    83          f.addValue("oNe");
    84          f.addValue("Z");
    85          assertEquals("one cumulative frequency", 1 ,  f.getCount("one"));
    86          assertEquals("Z cumulative pct", 0.5,  f.getCumPct("Z"), tolerance);
    87          assertEquals("z cumulative pct", 1.0,  f.getCumPct("z"), tolerance);
    88          assertEquals("Ot cumulative pct", 0.25,  f.getCumPct("Ot"), tolerance);
    89          f.clear();
    90
    91          f = null;
    92          Frequency f = new Frequency();
    93          f.addValue(1);
    94          f.addValue(new Integer(1));
    95          f.addValue(new Long(1));
    96          f.addValue(2);
    97          f.addValue(new Integer(-1));
    98          assertEquals("1 count", 3, f.getCount(1));
    99          assertEquals("1 count", 3, f.getCount(new Integer(1)));
    100         assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance);
    101         assertEquals("1 pct", 0.6, f.getPct(new Integer(1)), tolerance);
    102         assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance);
    103         assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance);
    104
    105         f = null;
    106         f = new Frequency(String.CASE_INSENSITIVE_ORDER);
    107         f.addValue("one");
    108         f.addValue("One");
    109         f.addValue("oNe");
    110         f.addValue("Z");
    111         assertEquals("one count", 3 ,  f.getCount("one"));
    112         assertEquals("Z cumulative pct -- case insensitive", 1 ,  f.getCumPct("Z"), tolerance);
    113         assertEquals("z cumulative pct -- case insensitive", 1 ,  f.getCumPct("z"), tolerance);
    114     }
    115
    116     /*** test pcts */
    117     public void testPcts() {
    118         f.addValue(oneL);
    119         f.addValue(twoL);
    120         f.addValue(oneI);
    121         f.addValue(twoI);
    122         f.addValue(threeL);
    123         f.addValue(threeL);
    124         f.addValue(3);
    125         f.addValue(threeI);
    126         assertEquals("one pct",0.25,f.getPct(1),tolerance);
    127         assertEquals("two pct",0.25,f.getPct(new Long(2)),tolerance);
    128         assertEquals("three pct",0.5,f.getPct(threeL),tolerance);
    129         assertEquals("five pct",0,f.getPct(5),tolerance);
    130         assertEquals("foo pct",0,f.getPct("foo"),tolerance);
    131         assertEquals("one cum pct",0.25,f.getCumPct(1),tolerance);
    132         assertEquals("two cum pct",0.50,f.getCumPct(new Long(2)),tolerance);
    133         assertEquals("Integer argument",0.50,f.getCumPct(new Integer(2)),tolerance);
    134         assertEquals("three cum pct",1.0,f.getCumPct(threeL),tolerance);
    135         assertEquals("five cum pct",1.0,f.getCumPct(5),tolerance);
    136         assertEquals("zero cum pct",0.0,f.getCumPct(0),tolerance);
    137         assertEquals("foo cum pct",0,f.getCumPct("foo"),tolerance);
    138     }
    139
    140     /*** test adding incomparable values */
    141     public void testAdd() {
    142     	char aChar = 'a';
    143     	char bChar = 'b';
    144     	String aString = "a";
    145     	f.addValue(aChar);
    146     	f.addValue(bChar);
    147     	try {
    148     		f.addValue(aString);
    149     		fail("Expecting IllegalArgumentException");
    150     	} catch (IllegalArgumentException ex) {
    151     		// expected
    152     	}
    153     	assertEquals("a pct",0.5,f.getPct(aChar),tolerance);
    154     	assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance);
    155     	assertEquals("a string pct",0.0,f.getPct(aString),tolerance);
    156     	assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance);
    157     }
    158
    159     /*** test empty table */
    160     public void testEmptyTable() {
    161         assertEquals("freq sum, empty table", 0, f.getSumFreq());
    162         assertEquals("count, empty table", 0, f.getCount(0));
    163         assertEquals("count, empty table",0, f.getCount(new Integer(0)));
    164         assertEquals("cum freq, empty table", 0, f.getCumFreq(0));
    165         assertEquals("cum freq, empty table", 0, f.getCumFreq("x"));
    166         assertTrue("pct, empty table", Double.isNaN(f.getPct(0)));
    167         assertTrue("pct, empty table", Double.isNaN(f.getPct(new Integer(0))));
    168         assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0)));
    169         assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(new Integer(0))));
    170     }
    171
    172     /***
    173      * Tests toString()
    174      */
    175     public void testToString(){
    176         f.addValue(oneL);
    177         f.addValue(twoL);
    178         f.addValue(oneI);
    179         f.addValue(twoI);
    180
    181         String s = f.toString();
    182         //System.out.println(s);
    183         assertNotNull(s);
    184         BufferedReader reader = new BufferedReader(new StringReader(s));
    185         try {
    186             String line = reader.readLine(); // header line
    187             assertNotNull(line);
    188
    189             line = reader.readLine(); // one's or two's line
    190             assertNotNull(line);
    191
    192             line = reader.readLine(); // one's or two's line
    193             assertNotNull(line);
    194
    195             line = reader.readLine(); // no more elements
    196             assertNull(line);
    197         } catch(IOException ex){
    198             fail(ex.getMessage());
    199         }
    200     }
    201     public void testIntegerValues() {
    202         Object obj1 = null;
    203         obj1 = new Integer(1);
    204         Integer int1 = new Integer(1);
    205         f.addValue(obj1);
    206         f.addValue(int1);
    207         f.addValue(2);
    208         f.addValue(new Long(2));
    209         assertEquals("Integer 1 count", 2, f.getCount(1));
    210         assertEquals("Integer 1 count", 2, f.getCount(new Integer(1)));
    211         assertEquals("Integer 1 count", 2, f.getCount(new Long(1)));
    212         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance);
    213         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(new Long(1)), tolerance);
    214         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(new Integer(1)), tolerance);
    215         Iterator it = f.valuesIterator();
    216         while (it.hasNext()) {
    217             assertTrue(it.next() instanceof Long);
    218         }
    219     }
    220 }
    221