commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdigg...@apache.org
Subject cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/stat DescriptiveStatisticsTest.java
Date Wed, 19 Nov 2003 13:26:42 GMT
mdiggory    2003/11/19 05:26:42

  Modified:    math/src/test/org/apache/commons/math TestUtils.java
               math/src/test/org/apache/commons/math/stat
                        DescriptiveStatisticsTest.java
  Log:
  Additional tooling for simple JUnit testing of serialization.
  
  Revision  Changes    Path
  1.8       +37 -1     jakarta-commons/math/src/test/org/apache/commons/math/TestUtils.java
  
  Index: TestUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/TestUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestUtils.java	15 Nov 2003 18:52:31 -0000	1.7
  +++ TestUtils.java	19 Nov 2003 13:26:42 -0000	1.8
  @@ -54,6 +54,13 @@
   
   package org.apache.commons.math;
   
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +import java.io.IOException;
  +import java.io.ObjectInputStream;
  +import java.io.ObjectOutputStream;
  +
   import org.apache.commons.math.complex.Complex;
   
   import junit.framework.Assert;
  @@ -84,5 +91,34 @@
       public static void assertEquals(Complex expected, Complex actual, double delta) {
           assertEquals(expected.getReal(), actual.getReal(), delta);
           assertEquals(expected.getImaginary(), actual.getImaginary(), delta);
  +    }
  +    
  +    public static Object serializeAndRecover(Object o){
  +        
  +        Object result = null;
  +        
  +        File tmp = null;
  +        
  +        try {
  +            
  +            // serialize the Object
  +            tmp = File.createTempFile("test",".ser");
  +            FileOutputStream fo = new FileOutputStream(tmp);
  +            ObjectOutputStream so = new ObjectOutputStream(fo);
  +            so.writeObject(o);
  +            so.flush();
  +
  +            // deserialize the Book
  +            FileInputStream fi = new FileInputStream(tmp);
  +            ObjectInputStream si = new ObjectInputStream(fi);  
  +            result = si.readObject();
  +            
  +        }catch (Exception e) {
  +            e.printStackTrace();
  +        }finally{
  +            if(tmp != null) tmp.delete();
  +        }
  +        
  +        return result;
       }
   }
  
  
  
  1.2       +28 -2     jakarta-commons/math/src/test/org/apache/commons/math/stat/DescriptiveStatisticsTest.java
  
  Index: DescriptiveStatisticsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/DescriptiveStatisticsTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DescriptiveStatisticsTest.java	15 Nov 2003 16:01:41 -0000	1.1
  +++ DescriptiveStatisticsTest.java	19 Nov 2003 13:26:42 -0000	1.2
  @@ -57,6 +57,7 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.math.TestUtils;
   import org.apache.commons.math.random.RandomData;
   import org.apache.commons.math.random.RandomDataImpl;
   
  @@ -340,6 +341,31 @@
           assertTrue("empty value set should return NaN",
               Double.isNaN(u.getPercentile(50)));
       }
  -                                     
  +                      
  +    /** test stats */
  +    public void testSerialization() {
  +        DescriptiveStatistics u = DescriptiveStatistics.newInstance(); 
  +        assertEquals("total count",0,u.getN(),tolerance);
  +        u.addValue(one);
  +        u.addValue(two);
  +        
  +        DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);

  + 
  +        u2.addValue(two);
  +        u2.addValue(three);
  +        
  +        assertEquals("N",n,u2.getN(),tolerance);
  +        assertEquals("sum",sum,u2.getSum(),tolerance);
  +        assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
  +        assertEquals("var",var,u2.getVariance(),tolerance);
  +        assertEquals("std",std,u2.getStandardDeviation(),tolerance);
  +        assertEquals("mean",mean,u2.getMean(),tolerance);
  +        assertEquals("min",min,u2.getMin(),tolerance);
  +        assertEquals("max",max,u2.getMax(),tolerance);
  +
  +        u2.clear();
  +        assertEquals("total count",0,u2.getN(),tolerance);    
  +    }       
  +                                   
   }
   
  
  
  

---------------------------------------------------------------------
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