View Javadoc
1   /*
2    * Copyright 2010 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.io.impl;
17  
18  //import        eu.simuline.testhelpers.Actions;
19  
20  import java.util.Map;
21  import java.util.TreeMap;
22  
23  import eu.simuline.octave.OctaveEngine;
24  import eu.simuline.octave.OctaveEngineFactory;
25  import eu.simuline.octave.io.OctaveIO;
26  import eu.simuline.octave.type.OctaveInt;
27  import eu.simuline.octave.type.OctaveObject;
28  
29  import static org.junit.Assert.assertEquals;
30  //import static org.junit.Assert.assertTrue;
31  
32  //import eu.simuline.testhelpers.Actions;
33  //import eu.simuline.testhelpers.Accessor;
34  //import static eu.simuline.testhelpers.Assert.assertEquals;
35  //import static eu.simuline.testhelpers.Assert.assertArraysEquals;
36  
37  //import static org.junit.Assert.assertEquals;
38  //import static org.junit.Assert.assertTrue;
39  //import static org.junit.Assert.assertNull;
40  //import static org.junit.Assert.fail;
41  
42  
43  import org.junit.Ignore;
44  import org.junit.Test;
45  // import org.junit.Before;
46  // import org.junit.runner.RunWith;
47  // import org.junit.runners.Suite;
48  // import org.junit.runners.Suite.SuiteClasses;
49  
50  
51  /**
52   * Test read/write of {@link OctaveInt}
53   */
54  //@RunWith(Suite.class)
55  //@SuiteClasses({TestIoOctaveInt.class})
56  public class TestIoOctaveInt {
57  
58      @Ignore @Test public void testScalarToText() {
59          final OctaveObject integer42 = new OctaveInt(new int[] {42}, 1, 1);
60          assertEquals("# name: ans\n" + // 
61  		     "# type: int32 scalar\n" + // 
62  		     "42\n", 
63  		     OctaveIO.toText(integer42));
64          final OctaveObject integer43 = new OctaveInt(new int[] {43}, 1, 1);
65          assertEquals("# name: integer43\n" +
66  		     "# type: int32 scalar\n" +
67  		     "43\n", 
68  		     OctaveIO.toText("integer43", integer43));
69      }
70  
71  
72      /**
73       * Test
74       */
75      @Ignore @Test public void test3dToText() {
76          final OctaveInt matrix = new OctaveInt(3, 4, 2);
77          assertEquals("# name: matrix3d\n" + //
78  		     "# type: int32 matrix\n" + //
79  		     "# ndims: 3\n" + //
80  		     " 3 4 2\n" + //
81  		     " 0\n 0\n 0\n" + //
82  		     " 0\n 0\n 0\n" + //
83  		     " 0\n 0\n 0\n" + //
84  		     " 0\n 0\n 0\n" + //
85  		     " 0\n 0\n 0\n" + //
86  		     " 0\n 0\n 0\n" + //
87  		     " 0\n 0\n 0\n" + //
88  		     " 0\n 0\n 0\n", 
89  		     OctaveIO.toText("matrix3d", matrix));
90          matrix.set(42, new int[] {1, 3, 2});
91          assertEquals("# name: matrix3d\n" + //
92  		     "# type: int32 matrix\n" + //
93  		     "# ndims: 3\n" + //
94  		     " 3 4 2\n" + //
95  		     " 0\n 0\n 0\n" + // matrix(x,x,1)
96  		     " 0\n 0\n 0\n" + //
97  		     " 0\n 0\n 0\n" + //
98  		     " 0\n 0\n 0\n" + //
99  
100 		     " 0\n 0\n 0\n" + // matrix(x,x,2)
101 		     " 0\n 0\n 0\n" + //
102 		     " 42\n 0\n 0\n" + // matrix(1,3,2)
103 		     " 0\n 0\n 0\n", 
104 		     OctaveIO.toText("matrix3d", matrix));
105     }
106 
107     /**
108      * Test
109      */
110     @Ignore @Test public void testEmpty3dToText() {
111         final OctaveInt matrix = new OctaveInt(0, 0, 0);
112         assertEquals("# name: matrix3d\n" + 
113 		     "# type: int32 matrix\n" + 
114 		     "# ndims: 3\n 0 0 0\n", 
115 		     OctaveIO.toText("matrix3d", matrix));
116     }
117 
118 
119     /**
120      * @throws Exception
121      */
122     @Ignore @Test public void testEmpty2dToText() throws Exception {
123         final OctaveInt matrix = new OctaveInt(0, 0);
124         assertEquals(0, matrix.getSize(1));
125         assertEquals(0, matrix.getSize(2));
126         assertEquals("# name: matrix\n" + //
127 		     "# type: int32 matrix\n" + //
128 		     "# rows: 0\n" + //
129 		     "# columns: 0\n", 
130 		     OctaveIO.toText("matrix", matrix));
131     }
132 
133     /**
134      * @throws Exception
135      */
136     @Ignore @Test public void test2dToText() throws Exception {
137         final int[] numbers = { 1, 2, 3, 4, 5, 6 };
138         final OctaveInt matrix = new OctaveInt(numbers, 2, 3);
139         assertEquals(2, matrix.getSize(1));
140         assertEquals(3, matrix.getSize(2));
141         assertEquals("# name: mymatrix\n" + //
142 		     "# type: int32 matrix\n" + //
143 		     "# rows: 2\n" + //
144 		     "# columns: 3\n" + //
145 		     " 1 3 5\n" + //
146 		     " 2 4 6\n", 
147 		     OctaveIO.toText("mymatrix", matrix));
148     }
149 
150 
151     /**
152      * @throws Exception
153      */
154     @Ignore @Test public void test2dToTextB() throws Exception {
155         final OctaveInt matrix = new OctaveInt(2, 3);
156         assertEquals(2, matrix.getSize(1));
157         assertEquals(3, matrix.getSize(2));
158         assertEquals("# name: matrix\n" + //
159 		     "# type: int32 matrix\n" + //
160 		     "# rows: 2\n" + //
161 		     "# columns: 3\n" + //
162 		     " 0 0 0\n" + //
163 		     " 0 0 0\n", 
164 		     OctaveIO.toText("matrix", matrix));
165         matrix.set(42, 1, 2);
166         assertEquals("# name: myother\n" + //
167 		     "# type: int32 matrix\n" + //
168 		     "# rows: 2\n" + //
169 		     "# columns: 3\n" + //
170 		     " 0 42 0\n" + //
171 		     " 0 0 0\n", 
172 		     OctaveIO.toText("myother", matrix));
173         matrix.set(2, 2, 1);
174         assertEquals("# name: myother\n" + //
175 		     "# type: int32 matrix\n" + //
176 		     "# rows: 2\n" + //
177 		     "# columns: 3\n" + //
178 		     " 0 42 0\n" + //
179 		     " 2 0 0\n", 
180 		     OctaveIO.toText("myother", matrix));
181         matrix.set(4, 2, 2);
182         assertEquals("# name: myother\n" + //
183 		     "# type: int32 matrix\n" + //
184 		     "# rows: 2\n" + //
185 		     "# columns: 3\n" + //
186 		     " 0 42 0\n" + //
187 		     " 2 4 0\n", 
188 		     OctaveIO.toText("myother", matrix));
189     }
190 
191 
192 
193     /**
194      * @throws Exception
195      */
196     @Test public void testOctaveIntScalar() throws Exception {
197 System.out.println("testOctaveIntScalar()...");
198 	
199         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
200         //final OctaveObject i1 = new OctaveInt(42, 1, 1);
201 	final OctaveInt i1 = new OctaveInt(1, 1);
202 	i1.set(42, 1, 1);
203         octave.put("i", i1);
204         final OctaveInt i2 = (OctaveInt)octave.get("i");	
205         assertEquals(i1, i2);
206         octave.close();
207 System.out.println("...testOctaveIntScalar()");
208     }
209 
210 
211     /**
212      * @throws Exception
213      */
214     @Ignore @Test public void testOctaveGet() throws Exception {
215         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
216         octave.eval("m=int32([1 2;3 4]);");
217         final OctaveInt m = octave.get(OctaveInt.class, "m");
218         assertEquals("# name: m\n" + //
219 		     "# type: int32 matrix\n" + //
220 		     "# rows: 2\n" + //
221 		     "# columns: 2\n" + //
222 		     " 1 2\n" + //
223 		     " 3 4\n",  //
224 		     OctaveIO.toText("m", m));
225         octave.close();
226     }
227 
228 
229 
230     /**
231      * @throws Exception
232      */
233     @Test public void testOctaveSetExecGet() throws Exception {
234 System.out.println("testOctaveSetExecGet()...");
235         final int[] numbers = { 1, 2, 3, 4, 5, 6 };//
236         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
237         final OctaveInt in = new OctaveInt(numbers, 2, 3);//2, 3
238         octave.put("in", in);
239         octave.eval("out=in;");
240         final OctaveInt out = octave.get(OctaveInt.class, "out");
241         assertEquals(OctaveIO.toText(in), OctaveIO.toText(out));
242         octave.eval("slicerow=in(2,:); slicecol=in(:,2);");
243         OctaveInt slicerow = octave.get(OctaveInt.class, "slicerow");
244         OctaveInt slicecol = octave.get(OctaveInt.class, "slicecol");
245         assertEquals(2, slicerow.get(1, 1));
246         assertEquals(4, slicerow.get(1, 2));
247         assertEquals(6, slicerow.get(1, 3));
248         assertEquals(3, slicecol.get(1, 1));
249         assertEquals(4, slicecol.get(2, 1));
250         octave.close();
251 System.out.println("...testOctaveSetExecGet()");
252     }
253 
254 
255     /**
256      * Test
257      */
258     @Ignore @Test public void testOctave3dMatrix() {
259         final OctaveInt matrix3d = new OctaveInt(3, 4, 2);
260         matrix3d.set(42, 1, 3, 2);
261         matrix3d.set(-1, 3, 1, 1);
262         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
263         octave.put("matrix3d", matrix3d);
264         octave.eval("x1 = matrix3d(:,:,1);");
265         octave.eval("x2 = matrix3d(:,:,2);");
266         octave.eval("x3 = matrix3d(:,3,:);");
267         octave.eval("x4 = matrix3d(3,:,:);");
268         final OctaveInt x1 = octave.get(OctaveInt.class, "x1");
269         final OctaveInt x2 = octave.get(OctaveInt.class, "x2");
270         final OctaveInt x3 = octave.get(OctaveInt.class, "x3");
271         final OctaveInt x4 = octave.get(OctaveInt.class, "x4");
272         octave.close();
273         assertEquals( 0, x1.get(1, 3));
274         assertEquals(-1, x1.get(3, 1));
275         assertEquals(42, x2.get(1, 3));
276         assertEquals( 0, x2.get(3, 1));
277         assertEquals(42, x3.get(1, 1, 2));
278         assertEquals(-1, x4.get(1, 1, 1));
279     }
280 
281     /** Test */
282     @Ignore @Test public void testOctaveNdMatrix() {
283         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
284         final Map<String, OctaveObject> vars = 
285 	    new TreeMap<String, OctaveObject>();
286         final int[] bigdata = new int[2 * 3 * 4];
287         for (int idx = 0; idx < bigdata.length; idx++) {
288             bigdata[idx] = idx + 1;
289         }
290         final int[] data2d = {1, 2, 3, 5, 8, 13};
291         final int[] datascalar = {42};
292         vars.put("bigmatrix",    new OctaveInt(bigdata, 1, 2, 3, 4));
293         vars.put("matrix2d",     new OctaveInt(data2d, 2, 3));
294         vars.put("matrixscalar", new OctaveInt(datascalar, 1, 1));
295         vars.put("matrixzero",   new OctaveInt(0, 0, 0, 0));
296         vars.put("matrixzero2d", new OctaveInt(0, 0));
297         octave.putAll(vars);
298         OctaveInt matrixzero   = octave.get(OctaveInt.class, "matrixzero");
299         OctaveInt matrix2d     = octave.get(OctaveInt.class, "matrix2d");
300         OctaveInt bigmatrix    = octave.get(OctaveInt.class, "bigmatrix");
301         OctaveInt matrixzero2d = octave.get(OctaveInt.class, "matrixzero2d");
302         OctaveInt matrixscalar = octave.get(OctaveInt.class, "matrixscalar");
303         assertEquals(matrixzero,   vars.get("matrixzero"));
304         assertEquals(matrixzero2d, vars.get("matrixzero2d"));
305         assertEquals(matrixscalar, vars.get("matrixscalar"));
306         assertEquals(matrix2d,     vars.get("matrix2d"));
307         assertEquals(bigmatrix,    vars.get("bigmatrix"));
308         octave.close();
309 
310         assertEquals("# name: matrixzero2d\n" + //
311 		     "# type: matrix\n" + //
312 		     "# rows: 0\n" + //
313 		     "# columns: 0\n", //
314 		     OctaveIO.toText("matrixzero2d", matrixzero2d));
315 
316         assertEquals("# name: matrixzero\n" + //
317 		     "# type: matrix\n" + //
318 		     "# ndims: 4\n" + //
319 		     " 0 0 0 0\n",  // 
320 		     OctaveIO.toText("matrixzero", matrixzero));
321 
322         assertEquals("# name: matrixscalar\n" + //
323 		     "# type: scalar\n" + //
324 		     "42\n",  //
325 		     OctaveIO.toText("matrixscalar", matrixscalar));
326 
327         assertEquals("# name: matrix2d\n" + //
328 		     "# type: matrix\n" + //
329 		     "# rows: 2\n" + //
330 		     "# columns: 3\n" + //
331 		     " 1 3 8\n" + //
332 		     " 2 5 13\n",  //
333 		     OctaveIO.toText("matrix2d", matrix2d));
334 
335         assertEquals("# name: bigmatrix\n" + //
336 		     "# type: matrix\n" + //
337 		     "# ndims: 4\n" + //
338 		     " 1 2 3 4\n" + //
339 		     " 1\n" + //
340 		     " 2\n" + //
341 		     " 3\n" + //
342 		     " 4\n" + //
343 		     " 5\n" + //
344 		     " 6\n" + //
345 		     " 7\n" + //
346 		     " 8\n" + //
347 		     " 9\n" + //
348 		     " 10\n" + //
349 		     " 11\n" + //
350 		     " 12\n" + //
351 		     " 13\n" + //
352 		     " 14\n" + //
353 		     " 15\n" + //
354 		     " 16\n" + //
355 		     " 17\n" + //
356 		     " 18\n" + //
357 		     " 19\n" + //
358 		     " 20\n" + //
359 		     " 21\n" + //
360 		     " 22\n" + //
361 		     " 23\n" + //
362 		     " 24\n",  //
363 		     OctaveIO.toText("bigmatrix", bigmatrix));
364     }
365 
366 
367     /* -------------------------------------------------------------------- *
368      * framework.                                                           *
369      * -------------------------------------------------------------------- */
370 
371 
372     /**
373      * Runs the test case.
374      *
375      * Uncomment either the textual UI, Swing UI, or AWT UI.
376      */
377     public static void main(String[] args) {
378 	//Actions.runFromMain();
379     }
380 }