Index: lutinmatrix/src/test/org/codelutin/math/matrix/BasicMatrixTest.java diff -u lutinmatrix/src/test/org/codelutin/math/matrix/BasicMatrixTest.java:1.6 lutinmatrix/src/test/org/codelutin/math/matrix/BasicMatrixTest.java:1.7 --- lutinmatrix/src/test/org/codelutin/math/matrix/BasicMatrixTest.java:1.6 Mon Jan 23 13:50:06 2006 +++ lutinmatrix/src/test/org/codelutin/math/matrix/BasicMatrixTest.java Thu Jun 1 17:38:56 2006 @@ -23,10 +23,10 @@ * Created: 27 oct. 2004 * * @author Benjamin Poussin - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Mise a jour: $Date: 2006/01/23 13:50:06 $ - * par : $Author: bpoussin $ + * Mise a jour: $Date: 2006/06/01 17:38:56 $ + * par : $Author: ruchaud $ */ package org.codelutin.math.matrix; @@ -137,7 +137,7 @@ assertEquals(98, mat.getValue(new int[]{0, 9, 4}), 0); assertEquals(97, mat.getValue(new int[]{0, 4, 2}), 0); -// System.out.println(mat.toString()); + //System.out.println(mat.toString()); // acces a un element qui n'existe pas try{ Index: lutinmatrix/src/test/org/codelutin/math/matrix/ImportExportMatrixTest.java diff -u /dev/null lutinmatrix/src/test/org/codelutin/math/matrix/ImportExportMatrixTest.java:1.1 --- /dev/null Thu Jun 1 17:39:01 2006 +++ lutinmatrix/src/test/org/codelutin/math/matrix/ImportExportMatrixTest.java Thu Jun 1 17:38:56 2006 @@ -0,0 +1,119 @@ +/* + * *##% Copyright (C) 2002, 2003, 2004 Code Lutin, Cédric Pineau, Benjamin + * Poussin + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307, USA. ##% + */ +package org.codelutin.math.matrix; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Arrays; +import java.util.List; + +import junit.framework.TestCase; + +/** + * Test de l'import et export CSV + * + * @author ruchaud + * + */ +public class ImportExportMatrixTest extends TestCase { + + private MatrixND mat1D; + private MatrixND mat2D; + private MatrixND mat2DSemantics; + + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(); + } + + @Override + protected void setUp() throws Exception { + mat1D = new MatrixNDImpl(getFactory(), new int[]{10}); + mat1D.setValue(new int[]{0}, 0); + mat1D.setValue(new int[]{1}, 1); + mat1D.setValue(new int[]{2}, 2); + mat1D.setValue(new int[]{3}, 3); + mat1D.setValue(new int[]{4}, 4); + + mat2D = new MatrixNDImpl(getFactory(), new int[]{20,10}); + mat2D.setValue(new int[]{0, 0}, 0); + mat2D.setValue(new int[]{0, 1}, 1); + mat2D.setValue(new int[]{0, 2}, 2); + mat2D.setValue(new int[]{0, 3}, 3); + mat2D.setValue(new int[]{0, 4}, 4); + mat2D.setValue(new int[]{0, 5}, 4); + mat2D.setValue(new int[]{0, 6}, 4); + mat2D.setValue(new int[]{0, 7}, 4); + mat2D.setValue(new int[]{1, 8}, 4); + mat2D.setValue(new int[]{2, 9}, 4); + + List sem1 = Arrays.asList(new String[] { "toto", "titi", "tutu", "trtr" }); + List sem2 = Arrays.asList(new String[] { "tata", "tete", "tyty" }); + mat2DSemantics = new MatrixNDImpl(getFactory(), "name", + new List[] { sem1, sem2 }, new String[]{"dim1", "dim2"}); + } + + public void testImport() throws IOException { + String test = "0.0,1.0,2.0,3.0,4.0,4.0,4.0,4.0,0.3,0.0\n" + + "0.0,0.0,7.0,0.0,0.0,2.0,0.0,0.0,4.0,0.0\n" + + "0.0,0.0,8.0,1.0,0.0,0.0,0.0,0.0,0.0,4.0\n"; + StringReader reader = new StringReader(test); + + mat2D.importCSV(reader, new int[]{0,0}); + assertEquals(mat2D.getValue(1,2), 7.0); + assertEquals(mat2D.getValue(2,2), 8.0); + assertEquals(mat2D.getValue(1,8), 4.0); + + reader = new StringReader(test); + mat2D.importCSV(reader, new int[]{1,1}); + assertEquals(mat2D.getValue(2,3), 7.0); + assertEquals(mat2D.getValue(3,3), 8.0); + assertEquals(mat2D.getValue(2,9), 4.0); + } + + public void testExport() throws IOException { + testExport(mat1D, false); + testExport(mat2D, false); + + testExport(mat1D, true); + testExport(mat2D, true); + + testExport(mat2DSemantics, false); + testExport(mat2DSemantics, true); + } + + private void testExport(MatrixND matrixND, boolean withSemantics) throws IOException { + StringWriter writer = new StringWriter(); + matrixND.exportCSV(writer, withSemantics); + + StringReader writerToReader = new StringReader(writer.toString()); + matrixND.importCSV(writerToReader, new int[]{0,0}); + + StringWriter readerToWriter = new StringWriter(); + matrixND.exportCSV(readerToWriter, withSemantics); + + assertEquals(writer.toString(), readerToWriter.toString()); + } + + public void testSupport() throws Exception { + assertEquals(new MatrixNDImpl(getFactory(), new int[]{1}).isSupportedCSV(), true); + assertEquals(new MatrixNDImpl(getFactory(), new int[]{2,2}).isSupportedCSV(), true); + assertEquals(new MatrixNDImpl(getFactory(), new int[]{3,3,3}).isSupportedCSV(), false); + } +}