Index: lutinmatrix/src/test/org/codelutin/math/matrix/MatrixNDTest.java diff -u lutinmatrix/src/test/org/codelutin/math/matrix/MatrixNDTest.java:1.5 lutinmatrix/src/test/org/codelutin/math/matrix/MatrixNDTest.java:1.6 --- lutinmatrix/src/test/org/codelutin/math/matrix/MatrixNDTest.java:1.5 Fri Oct 21 14:27:23 2005 +++ lutinmatrix/src/test/org/codelutin/math/matrix/MatrixNDTest.java Fri Jan 13 16:55:30 2006 @@ -21,16 +21,22 @@ * * Created: 10 mai 2004 * -* @version $Revision: 1.5 $ +* @version $Revision: 1.6 $ * -* Mise a jour: $Date: 2005/10/21 14:27:23 $ +* Mise a jour: $Date: 2006/01/13 16:55:30 $ * par : $Author: bpoussin $ */ package org.codelutin.math.matrix; +import java.io.IOException; +import java.io.StreamTokenizer; +import java.io.StringReader; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Stack; + import junit.framework.TestCase; public class MatrixNDTest extends TestCase { @@ -494,5 +500,55 @@ } + + public void testToList() throws Exception { + MatrixND mat1 = getFactory().create(new int[]{3, 2, 1}); + mat1.setValue(0, 0, 0, 1); + mat1.setValue(1, 1, 0, 2); + mat1.setValue(2, 1, 0, 3); + List l = mat1.toList(); + System.out.println(l); + + MatrixND mat2 = getFactory().create(new int[]{3, 2, 1}); + mat2.fromList(l); + + System.out.println(mat1); + System.out.println(mat2); + + assertEquals(mat1, mat2); + + String s = String.valueOf(l); + List l2 = convertStringToList(s); + + System.out.println(l); + System.out.println(l2); + + assertEquals(l, l2); + } + + protected List convertStringToList(String s) throws Exception { + List result = null; + Stack stack = new Stack(); + double value = 0; + StreamTokenizer tok = new StreamTokenizer(new StringReader(s)); + int v = tok.nextToken(); + while (v != StreamTokenizer.TT_EOF) { + if (v == '[') { + stack.push(new ArrayList()); + } else if (v == ']'){ + List current = stack.pop(); + if (stack.empty()) { + result = current; + } else { + stack.peek().add(current); + } + } else if (v == StreamTokenizer.TT_NUMBER) { + value = tok.nval; + stack.peek().add(value); + } + v = tok.nextToken(); + } + return result; + } }