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;
018    
019    import java.io.BufferedReader;
020    import java.io.IOException;
021    import java.io.StringReader;
022    import java.util.Iterator;
023    
024    import org.apache.commons.math.TestUtils;
025    
026    import junit.framework.Test;
027    import junit.framework.TestCase;
028    import junit.framework.TestSuite;
029    
030    /**
031     * Test cases for the {@link Frequency} class.
032     *
033     * @version $Revision: 780541 $ $Date: 2009-05-31 20:47:02 -0400 (Sun, 31 May 2009) $
034     */
035    
036    public final class FrequencyTest extends TestCase {
037        private long oneL = 1;
038        private long twoL = 2;
039        private long threeL = 3;
040        private int oneI = 1;
041        private int twoI = 2;
042        private int threeI=3;
043        private double tolerance = 10E-15;
044        private Frequency f = null;
045        
046        public FrequencyTest(String name) {
047            super(name);
048        }
049    
050        @Override
051        public void setUp() {  
052            f = new Frequency();
053        }
054        
055        public static Test suite() {
056            TestSuite suite = new TestSuite(FrequencyTest.class);
057            suite.setName("Frequency Tests");
058            return suite;
059        }
060        
061        /** test freq counts */
062        public void testCounts() {
063            assertEquals("total count",0,f.getSumFreq());
064            f.addValue(oneL);
065            f.addValue(twoL);
066            f.addValue(1);
067            f.addValue(oneI);
068            assertEquals("one frequency count",3,f.getCount(1));
069            assertEquals("two frequency count",1,f.getCount(2));
070            assertEquals("three frequency count",0,f.getCount(3));
071            assertEquals("total count",4,f.getSumFreq());
072            assertEquals("zero cumulative frequency", 0, f.getCumFreq(0));
073            assertEquals("one cumulative frequency", 3,  f.getCumFreq(1));
074            assertEquals("two cumulative frequency", 4,  f.getCumFreq(2));
075            assertEquals("Integer argument cum freq",4, f.getCumFreq(Integer.valueOf(2)));
076            assertEquals("five cumulative frequency", 4,  f.getCumFreq(5));
077            assertEquals("foo cumulative frequency", 0,  f.getCumFreq("foo"));
078            
079            f.clear();
080            assertEquals("total count",0,f.getSumFreq());
081            
082            // userguide examples -------------------------------------------------------------------
083            f.addValue("one");
084            f.addValue("One");
085            f.addValue("oNe");
086            f.addValue("Z");
087            assertEquals("one cumulative frequency", 1 ,  f.getCount("one"));
088            assertEquals("Z cumulative pct", 0.5,  f.getCumPct("Z"), tolerance);
089            assertEquals("z cumulative pct", 1.0,  f.getCumPct("z"), tolerance);
090            assertEquals("Ot cumulative pct", 0.25,  f.getCumPct("Ot"), tolerance);
091            f.clear();
092            
093            f = null;
094            Frequency f = new Frequency();
095            f.addValue(1);
096            f.addValue(Integer.valueOf(1));
097            f.addValue(Long.valueOf(1));
098            f.addValue(2);
099            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