Additional tooling for simple JUnit testing of serialization.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141038 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-11-19 13:26:42 +00:00
parent 85a8b141a3
commit 29aba171ed
2 changed files with 65 additions and 3 deletions

View File

@ -54,12 +54,19 @@
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;
/**
* @version $Revision: 1.7 $ $Date: 2003/11/15 18:52:31 $
* @version $Revision: 1.8 $ $Date: 2003/11/19 13:26:42 $
*/
public class TestUtils {
/**
@ -85,4 +92,33 @@ public class TestUtils {
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;
}
}

View File

@ -57,13 +57,14 @@ import junit.framework.Test;
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;
/**
* Test cases for the {@link Univariate} class.
*
* @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:41 $
* @version $Revision: 1.2 $ $Date: 2003/11/19 13:26:42 $
*/
public final class DescriptiveStatisticsTest extends TestCase {
@ -340,6 +341,31 @@ public final class DescriptiveStatisticsTest extends TestCase {
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);
}
}