001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.util.Iterator;
022:
023: import org.apache.commons.math.TestUtils;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: /**
030: * Test cases for the {@link Frequency} class.
031: *
032: * @version $Revision: 240244 $ $Date: 2005-08-26 06:40:32 -0700 (Fri, 26 Aug 2005) $
033: */
034:
035: public final class FrequencyTest extends TestCase {
036: private long oneL = 1;
037: private long twoL = 2;
038: private long threeL = 3;
039: private int oneI = 1;
040: private int twoI = 2;
041: private int threeI = 3;
042: private String oneS = "1";
043: private String twoS = "2";
044: private double tolerance = 10E-15;
045: private Frequency f = null;
046:
047: public FrequencyTest(String name) {
048: super (name);
049: }
050:
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
076: .getCumFreq(new Integer(2)));
077: assertEquals("five cumulative frequency", 4, f.getCumFreq(5));
078: assertEquals("foo cumulative frequency", 0, f.getCumFreq("foo"));
079:
080: f.clear();
081: assertEquals("total count", 0, f.getSumFreq());
082:
083: // userguide examples -------------------------------------------------------------------
084: f.addValue("one");
085: f.addValue("One");
086: f.addValue("oNe");
087: f.addValue("Z");
088: assertEquals("one cumulative frequency", 1, f.getCount("one"));
089: assertEquals("Z cumulative pct", 0.5, f.getCumPct("Z"),
090: tolerance);
091: assertEquals("z cumulative pct", 1.0, f.getCumPct("z"),
092: tolerance);
093: assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"),
094: tolerance);
095: f.clear();
096:
097: f = null;
098: Frequency f = new Frequency();
099: f.addValue(1);
100: f.addValue(new Integer(1));
101: f.addValue(new Long(1));
102: f.addValue(2);
103: f.addValue(new Integer(-1));
104: assertEquals("1 count", 3, f.getCount(1));
105: assertEquals("1 count", 3, f.getCount(new Integer(1)));
106: assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance);
107: assertEquals("1 pct", 0.6, f.getPct(new Integer(1)), tolerance);
108: assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance);
109: assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance);
110:
111: f = null;
112: f = new Frequency(String.CASE_INSENSITIVE_ORDER);
113: f.addValue("one");
114: f.addValue("One");
115: f.addValue("oNe");
116: f.addValue("Z");
117: assertEquals("one count", 3, f.getCount("one"));
118: assertEquals("Z cumulative pct -- case insensitive", 1, f
119: .getCumPct("Z"), tolerance);
120: assertEquals("z cumulative pct -- case insensitive", 1, f
121: .getCumPct("z"), tolerance);
122:
123: f = null;
124: f = new Frequency();
125: assertEquals(0L, f.getCount('a'));
126: assertEquals(0L, f.getCumFreq('b'));
127: TestUtils.assertEquals(Double.NaN, f.getPct('a'), 0.0);
128: TestUtils.assertEquals(Double.NaN, f.getCumPct('b'), 0.0);
129: f.addValue('a');
130: f.addValue('b');
131: f.addValue('c');
132: f.addValue('d');
133: assertEquals(1L, f.getCount('a'));
134: assertEquals(2L, f.getCumFreq('b'));
135: assertEquals(0.25, f.getPct('a'), 0.0);
136: assertEquals(0.5, f.getCumPct('b'), 0.0);
137: assertEquals(1.0, f.getCumPct('e'), 0.0);
138: }
139:
140: /** test pcts */
141: public void testPcts() {
142: f.addValue(oneL);
143: f.addValue(twoL);
144: f.addValue(oneI);
145: f.addValue(twoI);
146: f.addValue(threeL);
147: f.addValue(threeL);
148: f.addValue(3);
149: f.addValue(threeI);
150: assertEquals("one pct", 0.25, f.getPct(1), tolerance);
151: assertEquals("two pct", 0.25, f.getPct(new Long(2)), tolerance);
152: assertEquals("three pct", 0.5, f.getPct(threeL), tolerance);
153: assertEquals("five pct", 0, f.getPct(5), tolerance);
154: assertEquals("foo pct", 0, f.getPct("foo"), tolerance);
155: assertEquals("one cum pct", 0.25, f.getCumPct(1), tolerance);
156: assertEquals("two cum pct", 0.50, f.getCumPct(new Long(2)),
157: tolerance);
158: assertEquals("Integer argument", 0.50, f.getCumPct(new Integer(
159: 2)), tolerance);
160: assertEquals("three cum pct", 1.0, f.getCumPct(threeL),
161: tolerance);
162: assertEquals("five cum pct", 1.0, f.getCumPct(5), tolerance);
163: assertEquals("zero cum pct", 0.0, f.getCumPct(0), tolerance);
164: assertEquals("foo cum pct", 0, f.getCumPct("foo"), tolerance);
165: }
166:
167: /** test adding incomparable values */
168: public void testAdd() {
169: char aChar = 'a';
170: char bChar = 'b';
171: String aString = "a";
172: f.addValue(aChar);
173: f.addValue(bChar);
174: try {
175: f.addValue(aString);
176: fail("Expecting IllegalArgumentException");
177: } catch (IllegalArgumentException ex) {
178: // expected
179: }
180: assertEquals("a pct", 0.5, f.getPct(aChar), tolerance);
181: assertEquals("b cum pct", 1.0, f.getCumPct(bChar), tolerance);
182: assertEquals("a string pct", 0.0, f.getPct(aString), tolerance);
183: assertEquals("a string cum pct", 0.0, f.getCumPct(aString),
184: tolerance);
185: }
186:
187: /** test empty table */
188: public void testEmptyTable() {
189: assertEquals("freq sum, empty table", 0, f.getSumFreq());
190: assertEquals("count, empty table", 0, f.getCount(0));
191: assertEquals("count, empty table", 0, f
192: .getCount(new Integer(0)));
193: assertEquals("cum freq, empty table", 0, f.getCumFreq(0));
194: assertEquals("cum freq, empty table", 0, f.getCumFreq("x"));
195: assertTrue("pct, empty table", Double.isNaN(f.getPct(0)));
196: assertTrue("pct, empty table", Double.isNaN(f
197: .getPct(new Integer(0))));
198: assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0)));
199: assertTrue("cum pct, empty table", Double.isNaN(f
200: .getCumPct(new Integer(0))));
201: }
202:
203: /**
204: * Tests toString()
205: */
206: public void testToString() {
207: f.addValue(oneL);
208: f.addValue(twoL);
209: f.addValue(oneI);
210: f.addValue(twoI);
211:
212: String s = f.toString();
213: //System.out.println(s);
214: assertNotNull(s);
215: BufferedReader reader = new BufferedReader(new StringReader(s));
216: try {
217: String line = reader.readLine(); // header line
218: assertNotNull(line);
219:
220: line = reader.readLine(); // one's or two's line
221: assertNotNull(line);
222:
223: line = reader.readLine(); // one's or two's line
224: assertNotNull(line);
225:
226: line = reader.readLine(); // no more elements
227: assertNull(line);
228: } catch (IOException ex) {
229: fail(ex.getMessage());
230: }
231: }
232:
233: public void testIntegerValues() {
234: Object obj1 = null;
235: obj1 = new Integer(1);
236: Integer int1 = new Integer(1);
237: f.addValue(obj1);
238: f.addValue(int1);
239: f.addValue(2);
240: f.addValue(new Long(2));
241: assertEquals("Integer 1 count", 2, f.getCount(1));
242: assertEquals("Integer 1 count", 2, f.getCount(new Integer(1)));
243: assertEquals("Integer 1 count", 2, f.getCount(new Long(1)));
244: assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance);
245: assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(new Long(1)),
246: tolerance);
247: assertEquals("Integer 1 cumPct", 0.5, f
248: .getCumPct(new Integer(1)), tolerance);
249: Iterator it = f.valuesIterator();
250: while (it.hasNext()) {
251: assertTrue(it.next() instanceof Long);
252: }
253: }
254: }
|