1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.stat;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.StringReader;
22  import java.util.Iterator;
23  
24  import org.apache.commons.math.TestUtils;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  /**
31   * Test cases for the {@link Frequency} class.
32   *
33   * @version $Revision: 780541 $ $Date: 2009-05-31 20:47:02 -0400 (Sun, 31 May 2009) $
34   */
35  
36  public final class FrequencyTest extends TestCase {
37      private long oneL = 1;
38      private long twoL = 2;
39      private long threeL = 3;
40      private int oneI = 1;
41      private int twoI = 2;
42      private int threeI=3;
43      private double tolerance = 10E-15;
44      private Frequency f = null;
45      
46      public FrequencyTest(String name) {
47          super(name);
48      }
49  
50      @Override
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(Integer.valueOf(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(Integer.valueOf(1));
97          f.addValue(Long.valueOf(1));
98          f.addValue(2);
99          f.addValue(Integer.valueOf(-1));
100         assertEquals("1 count", 3, f.getCount(1));
101         assertEquals("1 count", 3, f.getCount(Integer.valueOf(1)));
102         assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance);
103         assertEquals("1 pct", 0.6, f.getPct(Integer.valueOf(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(Long.valueOf(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(Long.valueOf(2)),tolerance);
151         assertEquals("Integer argument",0.50,f.getCumPct(Integer.valueOf(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         try {
172             f.addValue(2);
173             fail("Expecting IllegalArgumentException");
174         } catch (IllegalArgumentException ex) {
175             // expected
176         }
177         assertEquals("a pct",0.5,f.getPct(aChar),tolerance);
178         assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance);
179         assertEquals("a string pct",0.0,f.getPct(aString),tolerance);
180         assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance);
181         
182         f = new Frequency();
183         f.addValue("One");
184         try {
185             f.addValue(new Integer("One")); 
186             fail("Expecting IllegalArgumentException");
187         } catch (IllegalArgumentException ex) {
188             // expected
189         }
190     }
191     
192     // Check what happens when non-Comparable objects are added
193     @SuppressWarnings("deprecation")
194     public void testAddNonComparable(){
195         try {
196             f.addValue(new Object()); // This was previously OK
197             fail("Expected IllegalArgumentException");
198         } catch (IllegalArgumentException expected) {
199         }
200         f.clear();
201         f.addValue(1);
202         try {
203             f.addValue(new Object());
204             fail("Expected IllegalArgumentException");
205         } catch (IllegalArgumentException expected) {
206         }
207     }
208 
209     /** test empty table */
210     public void testEmptyTable() {
211         assertEquals("freq sum, empty table", 0, f.getSumFreq());
212         assertEquals("count, empty table", 0, f.getCount(0));
213         assertEquals("count, empty table",0, f.getCount(Integer.valueOf(0)));
214         assertEquals("cum freq, empty table", 0, f.getCumFreq(0));
215         assertEquals("cum freq, empty table", 0, f.getCumFreq("x"));
216         assertTrue("pct, empty table", Double.isNaN(f.getPct(0)));
217         assertTrue("pct, empty table", Double.isNaN(f.getPct(Integer.valueOf(0))));
218         assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0)));
219         assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(Integer.valueOf(0))));   
220     }
221     
222     /**
223      * Tests toString() 
224      */
225     public void testToString(){
226         f.addValue(oneL);
227         f.addValue(twoL);
228         f.addValue(oneI);
229         f.addValue(twoI);
230         
231         String s = f.toString();
232         //System.out.println(s);
233         assertNotNull(s);
234         BufferedReader reader = new BufferedReader(new StringReader(s));
235         try {
236             String line = reader.readLine(); // header line
237             assertNotNull(line);
238             
239             line = reader.readLine(); // one's or two's line
240             assertNotNull(line);
241                         
242             line = reader.readLine(); // one's or two's line
243             assertNotNull(line);
244 
245             line = reader.readLine(); // no more elements
246             assertNull(line);
247         } catch(IOException ex){
248             fail(ex.getMessage());
249         }        
250     }
251     public void testIntegerValues() {
252         Comparable<?> obj1 = null;
253         obj1 = Integer.valueOf(1);
254         Integer int1 = Integer.valueOf(1);
255         f.addValue(obj1);
256         f.addValue(int1);
257         f.addValue(2);
258         f.addValue(Long.valueOf(2));
259         assertEquals("Integer 1 count", 2, f.getCount(1));
260         assertEquals("Integer 1 count", 2, f.getCount(Integer.valueOf(1)));
261         assertEquals("Integer 1 count", 2, f.getCount(Long.valueOf(1)));
262         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance);
263         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Long.valueOf(1)), tolerance);
264         assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Integer.valueOf(1)), tolerance);
265         Iterator<?> it = f.valuesIterator();
266         while (it.hasNext()) {
267             assertTrue(it.next() instanceof Long);
268         }     
269     }
270     
271     public void testSerial() {
272         f.addValue(oneL);
273         f.addValue(twoL);
274         f.addValue(oneI);
275         f.addValue(twoI);
276         assertEquals(f, TestUtils.serializeAndRecover(f));
277     }
278 }
279