1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
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 public void setUp() {
51 f = new Frequency();
52 }
53
54 public static Test suite() {
55 TestSuite suite = new TestSuite(FrequencyTest.class);
56 suite.setName("Frequency Tests");
57 return suite;
58 }
59
60
61 public void testCounts() {
62 assertEquals("total count",0,f.getSumFreq());
63 f.addValue(oneL);
64 f.addValue(twoL);
65 f.addValue(1);
66 f.addValue(oneI);
67 assertEquals("one frequency count",3,f.getCount(1));
68 assertEquals("two frequency count",1,f.getCount(2));
69 assertEquals("three frequency count",0,f.getCount(3));
70 assertEquals("total count",4,f.getSumFreq());
71 assertEquals("zero cumulative frequency", 0, f.getCumFreq(0));
72 assertEquals("one cumulative frequency", 3, f.getCumFreq(1));
73 assertEquals("two cumulative frequency", 4, f.getCumFreq(2));
74 assertEquals("Integer argument cum freq",4, f.getCumFreq(new Integer(2)));
75 assertEquals("five cumulative frequency", 4, f.getCumFreq(5));
76 assertEquals("foo cumulative frequency", 0, f.getCumFreq("foo"));
77
78 f.clear();
79 assertEquals("total count",0,f.getSumFreq());
80
81
82 f.addValue("one");
83 f.addValue("One");
84 f.addValue("oNe");
85 f.addValue("Z");
86 assertEquals("one cumulative frequency", 1 , f.getCount("one"));
87 assertEquals("Z cumulative pct", 0.5, f.getCumPct("Z"), tolerance);
88 assertEquals("z cumulative pct", 1.0, f.getCumPct("z"), tolerance);
89 assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"), tolerance);
90 f.clear();
91
92 f = null;
93 Frequency f = new Frequency();
94 f.addValue(1);
95 f.addValue(new Integer(1));
96 f.addValue(new Long(1));
97 f.addValue(2);
98 f.addValue(new Integer(-1));
99 assertEquals("1 count", 3, f.getCount(1));
100 assertEquals("1 count", 3, f.getCount(new Integer(1)));
101 assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance);
102 assertEquals("1 pct", 0.6, f.getPct(new Integer(1)), tolerance);
103 assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance);
104 assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance);
105
106 f = null;
107 f = new Frequency(String.CASE_INSENSITIVE_ORDER);
108 f.addValue("one");
109 f.addValue("One");
110 f.addValue("oNe");
111 f.addValue("Z");
112 assertEquals("one count", 3 , f.getCount("one"));
113 assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), tolerance);
114 assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), tolerance);
115
116 f = null;
117 f = new Frequency();
118 assertEquals(0L, f.getCount('a'));
119 assertEquals(0L, f.getCumFreq('b'));
120 TestUtils.assertEquals(Double.NaN, f.getPct('a'), 0.0);
121 TestUtils.assertEquals(Double.NaN, f.getCumPct('b'), 0.0);
122 f.addValue('a');
123 f.addValue('b');
124 f.addValue('c');
125 f.addValue('d');
126 assertEquals(1L, f.getCount('a'));
127 assertEquals(2L, f.getCumFreq('b'));
128 assertEquals(0.25, f.getPct('a'), 0.0);
129 assertEquals(0.5, f.getCumPct('b'), 0.0);
130 assertEquals(1.0, f.getCumPct('e'), 0.0);
131 }
132
133
134 public void testPcts() {
135 f.addValue(oneL);
136 f.addValue(twoL);
137 f.addValue(oneI);
138 f.addValue(twoI);
139 f.addValue(threeL);
140 f.addValue(threeL);
141 f.addValue(3);
142 f.addValue(threeI);
143 assertEquals("one pct",0.25,f.getPct(1),tolerance);
144 assertEquals("two pct",0.25,f.getPct(new Long(2)),tolerance);
145 assertEquals("three pct",0.5,f.getPct(threeL),tolerance);
146 assertEquals("five pct",0,f.getPct(5),tolerance);
147 assertEquals("foo pct",0,f.getPct("foo"),tolerance);
148 assertEquals("one cum pct",0.25,f.getCumPct(1),tolerance);
149 assertEquals("two cum pct",0.50,f.getCumPct(new Long(2)),tolerance);
150 assertEquals("Integer argument",0.50,f.getCumPct(new Integer(2)),tolerance);
151 assertEquals("three cum pct",1.0,f.getCumPct(threeL),tolerance);
152 assertEquals("five cum pct",1.0,f.getCumPct(5),tolerance);
153 assertEquals("zero cum pct",0.0,f.getCumPct(0),tolerance);
154 assertEquals("foo cum pct",0,f.getCumPct("foo"),tolerance);
155 }
156
157
158 public void testAdd() {
159 char aChar = 'a';
160 char bChar = 'b';
161 String aString = "a";
162 f.addValue(aChar);
163 f.addValue(bChar);
164 try {
165 f.addValue(aString);
166 fail("Expecting IllegalArgumentException");
167 } catch (IllegalArgumentException ex) {
168
169 }
170 assertEquals("a pct",0.5,f.getPct(aChar),tolerance);
171 assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance);
172 assertEquals("a string pct",0.0,f.getPct(aString),tolerance);
173 assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance);
174 }
175
176
177 public void testEmptyTable() {
178 assertEquals("freq sum, empty table", 0, f.getSumFreq());
179 assertEquals("count, empty table", 0, f.getCount(0));
180 assertEquals("count, empty table",0, f.getCount(new Integer(0)));
181 assertEquals("cum freq, empty table", 0, f.getCumFreq(0));
182 assertEquals("cum freq, empty table", 0, f.getCumFreq("x"));
183 assertTrue("pct, empty table", Double.isNaN(f.getPct(0)));
184 assertTrue("pct, empty table", Double.isNaN(f.getPct(new Integer(0))));
185 assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0)));
186 assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(new Integer(0))));
187 }
188
189
190
191
192 public void testToString(){
193 f.addValue(oneL);
194 f.addValue(twoL);
195 f.addValue(oneI);
196 f.addValue(twoI);
197
198 String s = f.toString();
199
200 assertNotNull(s);
201 BufferedReader reader = new BufferedReader(new StringReader(s));
202 try {
203 String line = reader.readLine();
204 assertNotNull(line);
205
206 line = reader.readLine();
207 assertNotNull(line);
208
209 line = reader.readLine();
210 assertNotNull(line);
211
212 line = reader.readLine();
213 assertNull(line);
214 } catch(IOException ex){
215 fail(ex.getMessage());
216 }
217 }
218 public void testIntegerValues() {
219 Object obj1 = null;
220 obj1 = new Integer(1);
221 Integer int1 = new Integer(1);
222 f.addValue(obj1);
223 f.addValue(int1);
224 f.addValue(2);
225 f.addValue(new Long(2));
226 assertEquals("Integer 1 count", 2, f.getCount(1));
227 assertEquals("Integer 1 count", 2, f.getCount(new Integer(1)));
228 assertEquals("Integer 1 count", 2, f.getCount(new Long(1)));
229 assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance);
230 assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(new Long(1)), tolerance);
231 assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(new Integer(1)), tolerance);
232 Iterator it = f.valuesIterator();
233 while (it.hasNext()) {
234 assertTrue(it.next() instanceof Long);
235 }
236 }
237 }
238