Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 89700 invoked from network); 12 Aug 2004 15:33:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 12 Aug 2004 15:33:45 -0000 Received: (qmail 75586 invoked by uid 500); 12 Aug 2004 15:33:42 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 75406 invoked by uid 500); 12 Aug 2004 15:33:40 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 75393 invoked by uid 500); 12 Aug 2004 15:33:40 -0000 Received: (qmail 75388 invoked by uid 99); 12 Aug 2004 15:33:40 -0000 X-ASF-Spam-Status: No, hits=-2.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Thu, 12 Aug 2004 08:33:40 -0700 Received: (qmail 89626 invoked by uid 1718); 12 Aug 2004 15:33:39 -0000 Date: 12 Aug 2004 15:33:39 -0000 Message-ID: <20040812153339.89625.qmail@minotaur.apache.org> From: psteitz@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/stat FrequencyTest.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N psteitz 2004/08/12 08:33:39 Modified: math/src/java/org/apache/commons/math/stat Frequency.java math/src/test/org/apache/commons/math/stat FrequencyTest.java Log: Fixed two errors reported on commons-user / commons-dev: 1. addValue(object) and getXxx methods failing or returning incorrect results for Integer arguments when the freq table is not empty 2. getXxx methods failing / returning inconsistent values when invoked on an empty table. Revision Changes Path 1.26 +27 -2 jakarta-commons/math/src/java/org/apache/commons/math/stat/Frequency.java Index: Frequency.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/Frequency.java,v retrieving revision 1.25 retrieving revision 1.26 diff -u -r1.25 -r1.26 --- Frequency.java 2 Jul 2004 05:27:41 -0000 1.25 +++ Frequency.java 12 Aug 2004 15:33:39 -0000 1.26 @@ -114,6 +114,15 @@ public void addValue(int v) { addValue(new Long(v)); } + + /** + * Adds 1 to the frequency count for v. + * + * @param v the value to add. + */ + public void addValue(Integer v) { + addValue(new Long(v.longValue())); + } /** * Adds 1 to the frequency count for v. @@ -170,6 +179,9 @@ * @return the frequency of v. */ public long getCount(Object v) { + if (v instanceof Integer) { + return getCount(((Integer) v).longValue()); + } long result = 0; try { Long count = (Long) freqTable.get(v); @@ -217,11 +229,16 @@ /** * Returns the percentage of values that are equal to v * (as a proportion between 0 and 1). + *

+ * Returns Double.NaN if no values have been added. * * @param v the value to lookup * @return the proportion of values equal to v */ public double getPct(Object v) { + if (getSumFreq() == 0) { + return Double.NaN; + } return (double) getCount(v) / (double) getSumFreq(); } @@ -269,6 +286,9 @@ * @return the proportion of values equal to v */ public long getCumFreq(Object v) { + if (getSumFreq() == 0) { + return 0; + } Comparator c = freqTable.comparator(); if (c == null) { c = new NaturalComparator(); @@ -346,12 +366,17 @@ * Returns the cumulative percentage of values less than or equal to v * (as a proportion between 0 and 1). *

- * Returns 0 if v is not comparable to the values set. + * Returns Double.NaN if no values have been added. + * Returns 0 if at least one value has been added, but v is not comparable + * to the values set. * * @param v the value to lookup * @return the proportion of values less than or equal to v */ public double getCumPct(Object v) { + if (getSumFreq() == 0) { + return Double.NaN; + } return (double) getCumFreq(v) / (double) getSumFreq(); } 1.13 +28 -1 jakarta-commons/math/src/test/org/apache/commons/math/stat/FrequencyTest.java Index: FrequencyTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/FrequencyTest.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- FrequencyTest.java 7 Mar 2004 00:57:11 -0000 1.12 +++ FrequencyTest.java 12 Aug 2004 15:33:39 -0000 1.13 @@ -87,6 +87,20 @@ f.clear(); f = null; + Frequency f = new Frequency(); + f.addValue(1); + f.addValue(new Integer(1)); + f.addValue(new Long(1)); + f.addValue(2); + f.addValue(new Integer(-1)); + assertEquals("1 count", 3, f.getCount(1)); + assertEquals("1 count", 3, f.getCount(new Integer(1))); + assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance); + assertEquals("1 pct", 0.6, f.getPct(new Integer(1)), tolerance); + assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance); + assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance); + + f = null; f = new Frequency(String.CASE_INSENSITIVE_ORDER); f.addValue("one"); f.addValue("One"); @@ -137,6 +151,19 @@ assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance); assertEquals("a string pct",0.0,f.getPct(aString),tolerance); assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance); + } + + /** test empty table */ + public void testEmptyTable() { + assertEquals("freq sum, empty table", 0, f.getSumFreq()); + assertEquals("count, empty table", 0, f.getCount(0)); + assertEquals("count, empty table",0, f.getCount(new Integer(0))); + assertEquals("cum freq, empty table", 0, f.getCumFreq(0)); + assertEquals("cum freq, empty table", 0, f.getCumFreq("x")); + assertTrue("pct, empty table", Double.isNaN(f.getPct(0))); + assertTrue("pct, empty table", Double.isNaN(f.getPct(new Integer(0)))); + assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0))); + assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(new Integer(0)))); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org