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.OctaveLong;
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 OctaveLong}
53   */
54  //@RunWith(Suite.class)
55  //@SuiteClasses({TestIoOctaveLong.class})
56  public class TestIoOctaveLong {
57  
58      @Ignore @Test public void testScalarToText() {
59          final OctaveObject integer42 = new OctaveLong(new long[] {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 OctaveLong(new long[] {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 OctaveLong matrix = new OctaveLong(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 OctaveLong matrix = new OctaveLong(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 OctaveLong matrix = new OctaveLong(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 long[] numbers = { 1, 2, 3, 4, 5, 6 };
138         final OctaveLong matrix = new OctaveLong(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 OctaveLong matrix = new OctaveLong(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 testOctaveLongScalar() throws Exception {
197         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
198         //final OctaveObject i1 = new OctaveLong(42, 1, 1);
199 	final OctaveLong i1 = new OctaveLong(1, 1);
200 	i1.set(42, 1, 1);
201         octave.put("i", i1);
202         final OctaveLong i2 = (OctaveLong)octave.get("i");	
203         assertEquals(i1, i2);
204         octave.close();
205     }
206 
207 
208     /**
209      * @throws Exception
210      */
211     @Ignore @Test public void testOctaveGet() throws Exception {
212         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
213         octave.eval("m=int32([1 2;3 4]);");
214         final OctaveLong m = octave.get(OctaveLong.class, "m");
215         assertEquals("# name: m\n" + //
216 		     "# type: int32 matrix\n" + //
217 		     "# rows: 2\n" + //
218 		     "# columns: 2\n" + //
219 		     " 1 2\n" + //
220 		     " 3 4\n",  //
221 		     OctaveIO.toText("m", m));
222         octave.close();
223     }
224 
225 
226 
227     /**
228      * @throws Exception
229      */
230     @Test public void testOctaveSetExecGet() throws Exception {
231         final long[] numbers = { 1, 2, 3, 4, 5, 6 };//
232         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
233         final OctaveLong in = new OctaveLong(numbers, 2, 3);//2, 3
234         octave.put("in", in);
235         octave.eval("out=in;");
236         final OctaveLong out = octave.get(OctaveLong.class, "out");
237         assertEquals(OctaveIO.toText(in), OctaveIO.toText(out));
238         octave.eval("slicerow=in(2,:); slicecol=in(:,2);");
239         OctaveLong slicerow = octave.get(OctaveLong.class, "slicerow");
240         OctaveLong slicecol = octave.get(OctaveLong.class, "slicecol");
241         assertEquals(2, slicerow.get(1, 1));
242         assertEquals(4, slicerow.get(1, 2));
243         assertEquals(6, slicerow.get(1, 3));
244         assertEquals(3, slicecol.get(1, 1));
245         assertEquals(4, slicecol.get(2, 1));
246         octave.close();
247     }
248 
249 
250     /**
251      * Test
252      */
253     @Ignore @Test public void testOctave3dMatrix() {
254         final OctaveLong matrix3d = new OctaveLong(3, 4, 2);
255         matrix3d.set(42, 1, 3, 2);
256         matrix3d.set(-1, 3, 1, 1);
257         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
258         octave.put("matrix3d", matrix3d);
259         octave.eval("x1 = matrix3d(:,:,1);");
260         octave.eval("x2 = matrix3d(:,:,2);");
261         octave.eval("x3 = matrix3d(:,3,:);");
262         octave.eval("x4 = matrix3d(3,:,:);");
263         final OctaveLong x1 = octave.get(OctaveLong.class, "x1");
264         final OctaveLong x2 = octave.get(OctaveLong.class, "x2");
265         final OctaveLong x3 = octave.get(OctaveLong.class, "x3");
266         final OctaveLong x4 = octave.get(OctaveLong.class, "x4");
267         octave.close();
268         assertEquals( 0, x1.get(1, 3));
269         assertEquals(-1, x1.get(3, 1));
270         assertEquals(42, x2.get(1, 3));
271         assertEquals( 0, x2.get(3, 1));
272         assertEquals(42, x3.get(1, 1, 2));
273         assertEquals(-1, x4.get(1, 1, 1));
274     }
275 
276     /** Test */
277     @Ignore @Test public void testOctaveNdMatrix() {
278         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
279         final Map<String, OctaveObject> vars = 
280 	    new TreeMap<String, OctaveObject>();
281         final long[] bigdata = new long[2 * 3 * 4];
282         for (int idx = 0; idx < bigdata.length; idx++) {
283             bigdata[idx] = idx + 1;
284         }
285         final long[] data2d = {1, 2, 3, 5, 8, 13};
286         final long[] datascalar = {42};
287         vars.put("bigmatrix",    new OctaveLong(bigdata, 1, 2, 3, 4));
288         vars.put("matrix2d",     new OctaveLong(data2d, 2, 3));
289         vars.put("matrixscalar", new OctaveLong(datascalar, 1, 1));
290         vars.put("matrixzero",   new OctaveLong(0, 0, 0, 0));
291         vars.put("matrixzero2d", new OctaveLong(0, 0));
292         octave.putAll(vars);
293         OctaveLong matrixzero   = octave.get(OctaveLong.class, "matrixzero");
294         OctaveLong matrix2d     = octave.get(OctaveLong.class, "matrix2d");
295         OctaveLong bigmatrix    = octave.get(OctaveLong.class, "bigmatrix");
296         OctaveLong matrixzero2d = octave.get(OctaveLong.class, "matrixzero2d");
297         OctaveLong matrixscalar = octave.get(OctaveLong.class, "matrixscalar");
298         assertEquals(matrixzero,   vars.get("matrixzero"));
299         assertEquals(matrixzero2d, vars.get("matrixzero2d"));
300         assertEquals(matrixscalar, vars.get("matrixscalar"));
301         assertEquals(matrix2d,     vars.get("matrix2d"));
302         assertEquals(bigmatrix,    vars.get("bigmatrix"));
303         octave.close();
304 
305         assertEquals("# name: matrixzero2d\n" + //
306 		     "# type: matrix\n" + //
307 		     "# rows: 0\n" + //
308 		     "# columns: 0\n", //
309 		     OctaveIO.toText("matrixzero2d", matrixzero2d));
310 
311         assertEquals("# name: matrixzero\n" + //
312 		     "# type: matrix\n" + //
313 		     "# ndims: 4\n" + //
314 		     " 0 0 0 0\n",  // 
315 		     OctaveIO.toText("matrixzero", matrixzero));
316 
317         assertEquals("# name: matrixscalar\n" + //
318 		     "# type: scalar\n" + //
319 		     "42\n",  //
320 		     OctaveIO.toText("matrixscalar", matrixscalar));
321 
322         assertEquals("# name: matrix2d\n" + //
323 		     "# type: matrix\n" + //
324 		     "# rows: 2\n" + //
325 		     "# columns: 3\n" + //
326 		     " 1 3 8\n" + //
327 		     " 2 5 13\n",  //
328 		     OctaveIO.toText("matrix2d", matrix2d));
329 
330         assertEquals("# name: bigmatrix\n" + //
331 		     "# type: matrix\n" + //
332 		     "# ndims: 4\n" + //
333 		     " 1 2 3 4\n" + //
334 		     " 1\n" + //
335 		     " 2\n" + //
336 		     " 3\n" + //
337 		     " 4\n" + //
338 		     " 5\n" + //
339 		     " 6\n" + //
340 		     " 7\n" + //
341 		     " 8\n" + //
342 		     " 9\n" + //
343 		     " 10\n" + //
344 		     " 11\n" + //
345 		     " 12\n" + //
346 		     " 13\n" + //
347 		     " 14\n" + //
348 		     " 15\n" + //
349 		     " 16\n" + //
350 		     " 17\n" + //
351 		     " 18\n" + //
352 		     " 19\n" + //
353 		     " 20\n" + //
354 		     " 21\n" + //
355 		     " 22\n" + //
356 		     " 23\n" + //
357 		     " 24\n",  //
358 		     OctaveIO.toText("bigmatrix", bigmatrix));
359     }
360 
361 
362     /* -------------------------------------------------------------------- *
363      * framework.                                                           *
364      * -------------------------------------------------------------------- */
365 
366 
367     /**
368      * Runs the test case.
369      *
370      * Uncomment either the textual UI, Swing UI, or AWT UI.
371      */
372     public static void main(String[] args) {
373 	//Actions.runFromMain();
374     }
375 }