commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/stat FrequencyTest.java
Date Thu, 12 Aug 2004 15:33:39 GMT
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).
  +     * <p>
  +     * Returns <code>Double.NaN</code> 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).
        * <p>
  -     * Returns 0 if v is not comparable to the values set.
  +     * Returns <code>Double.NaN</code> 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


Mime
View raw message