    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 org.apache.commons.math.TestUtils;
    24
    25  import junit.framework.Test;
    26  import junit.framework.TestCase;
    27  import junit.framework.TestSuite;
    28
    29  /***
    30   * Test cases for the {@link Frequency} class.
    31   *
    32   * @version $Revision: 240244 $ $Date: 2005-08-26 06:40:32 -0700 (Fri, 26 Aug 2005) $
    33   */
    34
    35  public final class FrequencyTest extends TestCase {
    36      private long oneL = 1;
    37      private long twoL = 2;
    38      private long threeL = 3;
    39      private int oneI = 1;
    40      private int twoI = 2;
    41      private int threeI=3;
    42      private String oneS = "1";
    43      private String twoS = "2";
    44      private double tolerance = 10E-15;
    45      private Frequency f = null;
    46
    47      public FrequencyTest(String name) {
    48          super(name);
    49      }
    50
    51      public void setUp() {
    52          f = new Frequency();
    53      }
    54
    55      public static Test suite() {
    56          TestSuite suite = new TestSuite(FrequencyTest.class);
    57          suite.setName("Frequency Tests");
    58          return suite;
    59      }
    60
    61      /*** test freq counts */
    62      public void testCounts() {
    63          assertEquals("total count",0,f.getSumFreq());
    64          f.addValue(oneL);
    65          f.addValue(twoL);
    66          f.addValue(1);
    67          f.addValue(oneI);
    68          assertEquals("one frequency count",3,f.getCount(1));
    69          assertEquals("two frequency count",1,f.getCount(2));
    70          assertEquals("three frequency count",0,f.getCount(3));
    71          assertEquals("total count",4,f.getSumFreq());
    72          assertEquals("zero cumulative frequency", 0, f.getCumFreq(0));
    73          assertEquals("one cumulative frequency", 3,  f.getCumFreq(1));
    74          assertEquals("two cumulative frequency", 4,  f.getCumFreq(2));
    75          assertEquals("Integer argument cum freq",4, f.getCumFreq(new Integer(2)));
    76          assertEquals("five cumulative frequency", 4,  f.getCumFreq(5));
    77          assertEquals("foo cumulative frequency", 0,  f.getCumFreq("foo"));
    78
    79          f.clear();
    80          assertEquals("total count",0,f.getSumFreq());
    81
    82          // userguide examples -------------------------------------------------------------------
    83          f.addValue("one");
    84          f.addValue("One");
    85          f.addValue("oNe");
    86          f.addValue("Z");
    87          assertEquals("one cumulative frequency", 1 ,  f.getCount("one"));
    88          assertEquals("Z cumulative pct", 0.5,  f.getCumPct("Z"), tolerance);
    89          assertEquals("z cumulative pct", 1.0,  f.getCumPct("z"), tolerance);
    90          assertEquals("Ot cumulative pct", 0.25,  f.getCumPct("Ot"), tolerance);
    91          f.clear();
    92
    93          f = null;
    94          Frequency f = new Frequency();
    95          f.addValue(1);
    96          f.addValue(new Integer(1));
    97          f.addValue(new Long(1));
    98          f.addValue(2);
    99          f.addValue(new Integer(-1));
    100         assertEquals("1 count", 3, f.getCount(1));
    101         assertEquals("1 count", 3, f.getCount(new Integer(1)));
    102         assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance);
    103         assertEquals("1 pct", 0.6, f.getPct(new Integer(1)), tolerance);
    104         assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance);
    105         assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance);
    106
    107         f = null;
    108         f = new Frequency(String.CASE_INSENSITIVE_ORDER);
    109         f.addValue("one");
    110         f.addValue("One");
    111         f.addValue("oNe");
    112         f.addValue("Z");
    113         assertEquals("one count", 3 ,  f.getCount("one"));
    114         assertEquals("Z cumulative pct -- case insensitive", 1 ,  f.getCumPct("Z"), tolerance);
    115         assertEquals("z cumulative pct -- case insensitive", 1 ,  f.getCumPct("z"), tolerance);
    116
    117         f = null;
    118         f = new Frequency();
    119         assertEquals(0L, f.getCount('a'));
    120         assertEquals(0L, f.getCumFreq('b'));
    121         TestUtils.assertEquals(Double.NaN, f.getPct('a'), 0.0);
    122         TestUtils.assertEquals(Double.NaN, f.getCumPct('b'), 0.0);
    123         f.addValue('a');
    124         f.addValue('b');
    125         f.addValue('c');
    126         f.addValue('d');
    127         assertEquals(1L, f.getCount('a'));
    128         assertEquals(2L, f.getCumFreq('b'));
    129         assertEquals(0.25, f.getPct('a'), 0.0);
    130         assertEquals(0.5, f.getCumPct('b'), 0.0);
    131         assertEquals(1.0, f.getCumPct('e'), 0.0);
    132     }
    133
    134     /*** test pcts */
    135     public void testPcts() {
    136         f.addValue(oneL);
    137         f.addValue(twoL);
    138         f.addValue(oneI);
    139         f.addValue(twoI);
    140         f.addValue(threeL);
    141         f.addValue(threeL);
    142         f.addValue(3);
    143         f.addValue(threeI);
    144         assertEquals("one pct",0.25,f.getPct(1),tolerance);
    145         assertEquals("two pct",0.25,f.getPct(new Long(2)),tolerance);
    146         assertEquals("three pct",0.5,f.getPct(threeL),tolerance);
    147         assertEquals("five pct",0,f.getPct(5),tolerance);
    148         assertEquals("foo pct",0,f.getPct("foo"),tolerance);
    149         assertEquals("one cum pct",0.25,f.getCumPct(1),tolerance);
    150         assertEquals("two cum pct",0.50,f.getCumPct(new Long(2)),tolerance);
    151         assertEquals("Integer argument",0.50,f.getCumPct(new Integer(2)),tolerance);
    152         assertEquals("three cum pct",1.0,f.getCumPct(threeL),tolerance);
    153         assertEquals("five cum pct",1.0,f.getCumPct(5),tolerance);
    154         assertEquals("zero cum pct",0.0,f.getCumPct(0),tolerance);
    155         assertEquals("foo cum pct",0,f.getCumPct("foo"),tolerance);
    156     }
    157
    158     /*** test adding incomparable values */
    159     public void testAdd() {
    160         char aChar = 'a';
    161         char bChar = 'b';
    162         String aString = "a";
    163         f.addValue(aChar);
    164         f.addValue(bChar);
    165         try {
    166             f.addValue(aString);
    167             fail("Expecting IllegalArgumentException");
    168         } catch (IllegalArgumentException ex) {
    169             // expected
    170         }
    171         assertEquals("a pct",0.5,f.getPct(aChar),tolerance);
    172         assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance);
    173         assertEquals("a string pct",0.0,f.getPct(aString),tolerance);
    174         assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance);
    175     }
    176
    177     /*** test empty table */
    178     public void testEmptyTable() {
    179         assertEquals("freq sum, empty table", 0, f.getSumFreq());
    180         assertEquals("count, empty table", 0, f.getCount(0));
    181         assertEquals("count, empty table",0, f.getCount(new Integer(0)));
    182         assertEquals("cum freq, empty table", 0, f.getCumFreq(0));
    183         assertEquals("cum freq, empty table", 0, f.getCumFreq("x"));
    184         assertTrue("pct, empty table", Double.isNaN(f.getPct(0)));
    185         assertTrue("pct, empty table", Double.isNaN(f.getPct(new Integer(0))));
    186         assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0)));
    187         assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(new Integer(0))));
    188     }
    189
    190     /***
    191      * Tests toString()
    192      */
    193     public void testToString(){
    194         f.addValue(oneL);
    195         f.addValue(twoL);
    196         f.addValue(oneI);
    197         f.addValue(twoI);
    198
    199         String s = f.toString();
    200         //System.out.println(s);
    201         assertNotNull(s);
    202         BufferedReader reader = new BufferedReader(new StringReader(s));
    203         try {
    204             String line = reader.readLine(); // header line
    205             assertNotNull(line);
    206
    207             line = reader.readLine(); // one's or two's line
    208             assertNotNull(line);
    209
    210             line = reader.readLine(); // one's or two's line
    211             assertNotNull(line);
    212
    213             line = reader.readLine(); // no more elements
    214             assertNull(line);
    215         } catch(IOException ex){
    216             fail(ex.getMessage());
    217         }
    218     }
    219     public void testIntegerValues() {
    220         Object obj1 = null;
    221         obj1 = new Integer(1);
    222         Integer int1 = new Integer(1);
    223         f.addValue(obj1);
    224         f.addValue(int1);
    225         f.addValue(2);
    226         f.addValue(new Long(2));
    227         assertEquals("Integer 1 count", 2, f.getCount(1));
    228         assertEquals("Integer 1 count", 2, f.getCount(new Integer(1)));
    229         assertEquals("Integer 1 count", 2, f.getCount(new Long(1)));
    230         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance);
    231         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(new Long(1)), tolerance);
    232         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(new Integer(1)), tolerance);
    233         Iterator it = f.valuesIterator();
    234         while (it.hasNext()) {
    235             assertTrue(it.next() instanceof Long);
    236         }
    237     }
    238 }
    239