From 6fecb32ad6fb69685dd7cbd3ed662ec7572d7f1c Mon Sep 17 00:00:00 2001 From: William Barker Date: Sun, 31 May 2009 22:18:18 +0000 Subject: [PATCH] Clean up linear serialization tests git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@780521 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/DenseFieldMatrixTest.java | 22 +++---------------- .../math/linear/DenseRealMatrixTest.java | 22 ++++--------------- .../math/linear/FieldMatrixImplTest.java | 20 ++--------------- .../math/linear/FieldVectorImplTest.java | 21 +++--------------- .../math/linear/RealMatrixImplTest.java | 19 ++-------------- .../math/linear/RealVectorImplTest.java | 22 +++++-------------- .../math/linear/SparseRealMatrixTest.java | 20 +++-------------- .../math/linear/SparseRealVectorTest.java | 22 +++++-------------- 8 files changed, 27 insertions(+), 141 deletions(-) diff --git a/src/test/org/apache/commons/math/linear/DenseFieldMatrixTest.java b/src/test/org/apache/commons/math/linear/DenseFieldMatrixTest.java index 97b34b4d4..24f044ed4 100644 --- a/src/test/org/apache/commons/math/linear/DenseFieldMatrixTest.java +++ b/src/test/org/apache/commons/math/linear/DenseFieldMatrixTest.java @@ -1252,27 +1252,11 @@ public final class DenseFieldMatrixTest extends TestCase { } - @SuppressWarnings("unchecked") public void testSerial() { - try { - File test = File.createTempFile("DFM",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - DenseFieldMatrix m = new DenseFieldMatrix(testData); - out.writeObject(m); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - DenseFieldMatrix nm = (DenseFieldMatrix)in.readObject(); - in.close(); - test.delete(); - assertEquals(m,nm); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + DenseFieldMatrix m = new DenseFieldMatrix(testData); + assertEquals(m,TestUtils.serializeAndRecover(m)); } - + private static class SetVisitor extends DefaultFieldMatrixChangingVisitor { public SetVisitor() { super(Fraction.ZERO); diff --git a/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java b/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java index 1af06c28b..78e53cff4 100644 --- a/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java +++ b/src/test/org/apache/commons/math/linear/DenseRealMatrixTest.java @@ -25,6 +25,7 @@ import java.io.ObjectOutputStream; import java.util.Arrays; import java.util.Random; +import org.apache.commons.math.TestUtils; import org.apache.commons.math.linear.decomposition.LUDecompositionImpl; import org.apache.commons.math.linear.decomposition.NonSquareMatrixException; @@ -1163,25 +1164,10 @@ public final class DenseRealMatrixTest extends TestCase { } public void testSerial() { - try { - File test = File.createTempFile("DRM",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - DenseRealMatrix m = new DenseRealMatrix(testData); - out.writeObject(m); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - DenseRealMatrix nm = (DenseRealMatrix)in.readObject(); - in.close(); - test.delete(); - assertEquals(m,nm); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + DenseRealMatrix m = new DenseRealMatrix(testData); + assertEquals(m,TestUtils.serializeAndRecover(m)); } - + private static class SetVisitor extends DefaultRealMatrixChangingVisitor { @Override public double visit(int i, int j, double value) { diff --git a/src/test/org/apache/commons/math/linear/FieldMatrixImplTest.java b/src/test/org/apache/commons/math/linear/FieldMatrixImplTest.java index 8fb7e43a9..6e42d7d73 100644 --- a/src/test/org/apache/commons/math/linear/FieldMatrixImplTest.java +++ b/src/test/org/apache/commons/math/linear/FieldMatrixImplTest.java @@ -939,25 +939,9 @@ public final class FieldMatrixImplTest extends TestCase { } - @SuppressWarnings("unchecked") public void testSerial() { - try { - File test = File.createTempFile("FMI",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - FieldMatrixImpl m = new FieldMatrixImpl(testData); - out.writeObject(m); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - FieldMatrixImpl nm = (FieldMatrixImpl)in.readObject(); - in.close(); - test.delete(); - assertEquals(m,nm); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + FieldMatrixImpl m = new FieldMatrixImpl(testData); + assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultFieldMatrixChangingVisitor { diff --git a/src/test/org/apache/commons/math/linear/FieldVectorImplTest.java b/src/test/org/apache/commons/math/linear/FieldVectorImplTest.java index d1b8dffdc..b6d57ebb3 100644 --- a/src/test/org/apache/commons/math/linear/FieldVectorImplTest.java +++ b/src/test/org/apache/commons/math/linear/FieldVectorImplTest.java @@ -31,6 +31,7 @@ import junit.framework.TestSuite; import org.apache.commons.math.Field; import org.apache.commons.math.FieldElement; +import org.apache.commons.math.TestUtils; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.fraction.FractionField; @@ -618,25 +619,9 @@ public class FieldVectorImplTest extends TestCase { } - @SuppressWarnings("unchecked") public void testSerial() { - try { - File test = File.createTempFile("FVI",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - FieldVectorImpl v = new FieldVectorImpl(vec1); - out.writeObject(v); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - FieldVectorImpl nv = (FieldVectorImpl)in.readObject(); - in.close(); - test.delete(); - assertEquals(v,nv); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + FieldVectorImpl v = new FieldVectorImpl(vec1); + assertEquals(v,TestUtils.serializeAndRecover(v)); } /** verifies that two vectors are equals */ diff --git a/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java b/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java index 6441bf637..df81c2ad1 100644 --- a/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java +++ b/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java @@ -942,23 +942,8 @@ public final class RealMatrixImplTest extends TestCase { } public void testSerial() { - try { - File test = File.createTempFile("RMI",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - RealMatrixImpl m = new RealMatrixImpl(testData); - out.writeObject(m); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - RealMatrixImpl nm = (RealMatrixImpl)in.readObject(); - in.close(); - test.delete(); - assertEquals(m,nm); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + RealMatrixImpl m = new RealMatrixImpl(testData); + assertEquals(m,TestUtils.serializeAndRecover(m)); } diff --git a/src/test/org/apache/commons/math/linear/RealVectorImplTest.java b/src/test/org/apache/commons/math/linear/RealVectorImplTest.java index 6b1f40a9a..8db19a426 100644 --- a/src/test/org/apache/commons/math/linear/RealVectorImplTest.java +++ b/src/test/org/apache/commons/math/linear/RealVectorImplTest.java @@ -23,6 +23,9 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; + +import org.apache.commons.math.TestUtils; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -1176,23 +1179,8 @@ public class RealVectorImplTest extends TestCase { } public void testSerial() { - try { - File test = File.createTempFile("RVI",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - RealVectorImpl v = new RealVectorImpl(new double[] { 0, 1, 2 }); - out.writeObject(v); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - RealVectorImpl nv = (RealVectorImpl)in.readObject(); - in.close(); - test.delete(); - assertEquals(v,nv); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + RealVectorImpl v = new RealVectorImpl(new double[] { 0, 1, 2 }); + assertEquals(v,TestUtils.serializeAndRecover(v)); } diff --git a/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java b/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java index 936d13fcb..01ea24752 100644 --- a/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java +++ b/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java @@ -27,6 +27,7 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.commons.math.TestUtils; import org.apache.commons.math.linear.decomposition.LUDecompositionImpl; import org.apache.commons.math.linear.decomposition.NonSquareMatrixException; @@ -649,23 +650,8 @@ public final class SparseRealMatrixTest extends TestCase { } public void testSerial() { - try { - File test = File.createTempFile("OMRM",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - OpenMapRealMatrix m = createSparseMatrix(testData); - out.writeObject(m); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - OpenMapRealMatrix nm = (OpenMapRealMatrix)in.readObject(); - in.close(); - test.delete(); - assertEquals(m,nm); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + OpenMapRealMatrix m = createSparseMatrix(testData); + assertEquals(m,TestUtils.serializeAndRecover(m)); } // --------------- -----------------Protected methods diff --git a/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java b/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java index 37854822b..e0b1e352b 100644 --- a/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java +++ b/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java @@ -23,6 +23,9 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; + +import org.apache.commons.math.TestUtils; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -1100,23 +1103,8 @@ public class SparseRealVectorTest extends TestCase { } public void testSerial() { - try { - File test = File.createTempFile("OMV",".ser"); - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(test)); - OpenMapRealVector v = new OpenMapRealVector(new double[] { 0, 1, 2 }); - out.writeObject(v); - out.close(); - ObjectInputStream in = new ObjectInputStream(new FileInputStream(test)); - OpenMapRealVector nv = (OpenMapRealVector)in.readObject(); - in.close(); - test.delete(); - assertEquals(v,nv); - - } catch (IOException e) { - fail("IOException: "+e); - } catch (ClassNotFoundException e) { - fail("Can't happen: "+e); - } + OpenMapRealVector v = new OpenMapRealVector(new double[] { 0, 1, 2 }); + assertEquals(v,TestUtils.serializeAndRecover(v)); } /** verifies that two vectors are close (sup norm) */