View Javadoc
1   /*
2    * Copyright 2007, 2008, 2009 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 java.util.Map;
19  import java.util.TreeMap;
20  
21  import eu.simuline.octave.OctaveEngine;
22  import eu.simuline.octave.OctaveEngineFactory;
23  import eu.simuline.octave.io.OctaveIO;
24  import eu.simuline.octave.type.OctaveBoolean;
25  import eu.simuline.octave.type.OctaveObject;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertTrue;
29  
30  import org.junit.Ignore;
31  import org.junit.Test;
32  
33  /**
34   * Test read/write of {@link OctaveBoolean}
35   */
36  public class TestIoOctaveBoolean {
37  
38      /**
39       * Test
40       */
41      @Test public void testConstructorIntIntInt() {
42          final OctaveBoolean matrix = new OctaveBoolean(3, 4, 2);
43          assertEquals("# name: matrix3d\n" + //
44  		     "# type: bool matrix\n" + //
45  		     "# ndims: 3\n" + //
46  		     " 3 4 2\n" + //
47  		     " 0\n 0\n 0\n" + //
48  		     " 0\n 0\n 0\n" + //
49  		     " 0\n 0\n 0\n" + //
50  		     " 0\n 0\n 0\n" + //
51  		     " 0\n 0\n 0\n" + //
52  		     " 0\n 0\n 0\n" + //
53  		     " 0\n 0\n 0\n" + //
54  		     " 0\n 0\n 0\n", 
55  		     OctaveIO.toText("matrix3d", matrix));
56          matrix.set(true, 1, 3, 2);
57          assertEquals("# name: matrix3d\n" + //
58  		     "# type: bool matrix\n" + //
59  		     "# ndims: 3\n" + //
60  		     " 3 4 2\n" + //
61  		     " 0\n 0\n 0\n" + //
62  		     " 0\n 0\n 0\n" + //
63  		     " 0\n 0\n 0\n" + //
64  		     " 0\n 0\n 0\n" + //
65  		     " 0\n 0\n 0\n" + //
66  		     " 0\n 0\n 0\n" + //
67  		     " 1\n 0\n 0\n" + //
68  		     " 0\n 0\n 0\n", 
69  		     OctaveIO.toText("matrix3d", matrix));
70      }
71  
72      /**
73       * Test
74       */
75      @Test public void testConstructor1() {
76          final OctaveBoolean matrix = new OctaveBoolean(0, 0, 0);
77          assertEquals("# name: matrix3d\n" + 
78  		     "# type: bool matrix\n" + 
79  		     "# ndims: 3\n 0 0 0\n", 
80  		     OctaveIO.toText("matrix3d", matrix));
81      }
82  
83      /**
84       * @throws Exception
85       */
86      @Test public void testConstructor2() throws Exception {
87          final OctaveBoolean matrix = new OctaveBoolean(0, 0);
88          assertEquals(0, matrix.getSize(1));
89          assertEquals(0, matrix.getSize(2));
90          assertEquals("# name: matrix\n" + //
91  		     "# type: bool matrix\n" + //
92  		     "# rows: 0\n" + //
93  		     "# columns: 0\n", 
94  		     OctaveIO.toText("matrix", matrix));
95      }
96  
97      /**
98       * @throws Exception
99       */
100     @Test public void testConstructorMatrix() throws Exception {
101         final boolean[] numbers = { true, true, false, false, true, true };
102         final OctaveBoolean matrix = new OctaveBoolean(numbers, 2, 3);
103         assertEquals(2, matrix.getSize(1));
104         assertEquals(3, matrix.getSize(2));
105         assertEquals("# name: mymatrix\n" + //
106 		     "# type: bool matrix\n" + //
107 		     "# rows: 2\n" + //
108 		     "# columns: 3\n" + //
109 		     " 1 0 1\n" + //
110 		     " 1 0 1\n",  //
111 		     OctaveIO.toText("mymatrix", matrix));
112     }
113 
114     /**
115      * @throws Exception
116      */
117     @Test public void testConstructorIntInt() throws Exception {
118         final OctaveBoolean matrix = new OctaveBoolean(2, 3);
119         assertEquals(2, matrix.getSize(1));
120         assertEquals(3, matrix.getSize(2));
121         assertEquals("# name: matrix\n" + //
122 		     "# type: bool matrix\n" + //
123 		     "# rows: 2\n" + //
124 		     "# columns: 3\n" + //
125 		     " 0 0 0\n" + //
126 		     " 0 0 0\n", //
127 		     OctaveIO.toText("matrix", matrix));
128         matrix.set(true, 1, 2);
129         assertEquals("# name: myother\n" + //
130 		     "# type: bool matrix\n" + //
131 		     "# rows: 2\n" + //
132 		     "# columns: 3\n" + //
133 		     " 0 1 0\n" + //
134 		     " 0 0 0\n",  //
135 		     OctaveIO.toText("myother", matrix));
136         matrix.set(true, 2, 1);
137         assertEquals("# name: myother\n" + //
138 		     "# type: bool matrix\n" + //
139 		     "# rows: 2\n" + //
140 		     "# columns: 3\n" + //
141 		     " 0 1 0\n" + //
142 		     " 1 0 0\n",  //
143 		     OctaveIO.toText("myother", matrix));
144         matrix.set(true, 2, 2);
145         assertEquals("# name: myother\n" + //
146 		     "# type: bool matrix\n" + //
147 		     "# rows: 2\n" + //
148 		     "# columns: 3\n" + //
149 		     " 0 1 0\n" + //
150 		     " 1 1 0\n",  //
151 		     OctaveIO.toText("myother", matrix));
152     }
153 
154     /**
155      * @throws Exception
156      */
157     @Test public void testGrowth() throws Exception {
158         final OctaveBoolean matrix = new OctaveBoolean(0, 0);
159         assertEquals(0, matrix.getSize(1));
160         assertEquals(0, matrix.getSize(2));
161         assertEquals("# name: matrix\n" + //
162 		     "# type: bool matrix\n" + //
163 		     "# rows: 0\n" + //
164 		     "# columns: 0\n",  //
165 		     OctaveIO.toText("matrix", matrix));
166         matrix.set(true, 1, 1);
167         assertEquals(1, matrix.getSize(1));
168         assertEquals(1, matrix.getSize(2));
169         assertEquals("# name: matrix\n" + //
170 		     "# type: bool\n" + //
171 		     "1\n",  //
172 		     OctaveIO.toText("matrix", matrix));
173         matrix.set(true, 3, 1);
174         assertEquals(3, matrix.getSize(1));
175         assertEquals(1, matrix.getSize(2));
176         assertEquals("# name: matrix\n" + //
177 		     "# type: bool matrix\n" + //
178 		     "# rows: 3\n" + //
179 		     "# columns: 1\n" + //
180 		     " 1\n" + //
181 		     " 0\n" + //
182 		     " 1\n",  //
183 		     OctaveIO.toText("matrix", matrix));
184 
185         final OctaveBoolean matrix2 = new OctaveBoolean(0, 0);
186         matrix2.set(true, 1, 3);
187         assertEquals("# name: matrix\n" + //
188 		     "# type: bool matrix\n" + //
189 		     "# rows: 1\n" + //
190 		     "# columns: 3\n" + //
191 		     " 0 0 1\n", 
192 		     OctaveIO.toText("matrix", matrix2));
193     }
194 
195     /**
196      * @throws Exception
197      */
198     @Test public void testOctaveGet() throws Exception {
199         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
200         octave.eval("m=[true false;false true];");
201         final OctaveBoolean m = octave.get(OctaveBoolean.class, "m");
202         assertEquals(2, m.getSize(1));
203         assertEquals(2, m.getSize(2));
204         assertEquals(true, m.get(1, 1));
205         assertEquals(false, m.get(1, 2));
206         assertEquals(false, m.get(2, 1));
207         assertEquals(true, m.get(2, 2));
208         octave.close();
209     }
210 
211     /**
212      * @throws Exception
213      */
214     @Test public void testOctaveSetExecGet() throws Exception {
215         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
216         final boolean[] numbers = { true, false, true, false, true, false };
217         final OctaveBoolean in = new OctaveBoolean(numbers, 2, 3);
218         octave.put("in", in);
219         octave.eval("out=in;");
220         final OctaveBoolean out = octave.get(OctaveBoolean.class, "out");
221         assertEquals(OctaveIO.toText(in), OctaveIO.toText(out));
222         octave.eval("slicerow=in(2,:); slicecol=in(:,2);");
223         final OctaveBoolean slicerow = octave.get(OctaveBoolean.class, 
224 						  "slicerow");
225         final OctaveBoolean slicecol = octave.get(OctaveBoolean.class, 
226 						  "slicecol");
227         assertEquals(false, slicerow.get(1, 1));
228         assertEquals(false, slicerow.get(1, 2));
229         assertEquals(false, slicerow.get(1, 3));
230         assertEquals(true,  slicecol.get(1, 1));
231         assertEquals(false, slicecol.get(2, 1));
232         octave.close();
233     }
234 
235     /**
236      * Test
237      */
238     @Test public void test3dMatrix() {
239         final OctaveBoolean matrix3d = new OctaveBoolean(3, 4, 2);
240         matrix3d.set(true, 1, 3, 2);
241         matrix3d.set(true, 3, 1, 1);
242         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
243         octave.put("matrix3d", matrix3d);
244         octave.eval("x1 = matrix3d(:,:,1);");
245         octave.eval("x2 = matrix3d(:,:,2);");
246         octave.eval("x3 = matrix3d(:,3,:);");
247         octave.eval("x4 = matrix3d(3,:,:);");
248         final OctaveBoolean x1 = octave.get(OctaveBoolean.class, "x1");
249         final OctaveBoolean x2 = octave.get(OctaveBoolean.class, "x2");
250         final OctaveBoolean x3 = octave.get(OctaveBoolean.class, "x3");
251         final OctaveBoolean x4 = octave.get(OctaveBoolean.class, "x4");
252         octave.close();
253         assertEquals(false, x1.get(1, 3));
254         assertEquals(true,  x1.get(3, 1));
255         assertEquals(true,  x2.get(1, 3));
256         assertEquals(false, x2.get(3, 1));
257         assertEquals(true,  x3.get(1, 1, 2));
258         assertEquals(true,  x4.get(1, 1, 1));
259     }
260 
261     /** Test */
262     @Test public void testNdMatrix() {
263         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
264         final TreeMap<String, OctaveObject> vars = 
265 	    new TreeMap<String, OctaveObject>();
266         final boolean[] bigdata = new boolean[2 * 3 * 4];
267         for (int idx = 0; idx < bigdata.length; idx++) {
268             bigdata[idx] = idx % 2 == 0;
269         }
270         final boolean[] data2d = { true, false, true, false, true, false };
271         final boolean[] datascalar = { true };
272         vars.put("bigmatrix", new OctaveBoolean(bigdata, 1, 2, 3, 4));
273         vars.put("matrix2d", new OctaveBoolean(data2d, 2, 3));
274         vars.put("matrixscalar", new OctaveBoolean(datascalar, 1, 1));
275         vars.put("matrixzero", new OctaveBoolean(0, 0, 0, 0));
276         vars.put("matrixzero2d", new OctaveBoolean(0, 0));
277         octave.putAll(vars);
278         final OctaveBoolean matrixzero = octave.get(OctaveBoolean.class, 
279 						    "matrixzero");
280         final OctaveBoolean matrix2d = octave.get(OctaveBoolean.class, 
281 						  "matrix2d");
282         final OctaveBoolean bigmatrix = octave.get(OctaveBoolean.class, 
283 						   "bigmatrix");
284         final OctaveBoolean matrixzero2d = octave.get(OctaveBoolean.class, 
285 						      "matrixzero2d");
286         final OctaveBoolean matrixscalar = octave.get(OctaveBoolean.class, 
287 						      "matrixscalar");
288         assertEquals(matrixzero,   vars.get("matrixzero"));
289         assertEquals(matrixzero2d, vars.get("matrixzero2d"));
290         assertEquals(matrixscalar, vars.get("matrixscalar"));
291         assertEquals(matrix2d,     vars.get("matrix2d"));
292         assertEquals(bigmatrix,    vars.get("bigmatrix"));
293         octave.close();
294 
295         assertEquals("# name: matrixzero2d\n" + //
296 		     "# type: bool matrix\n" + //
297 		     "# rows: 0\n" + //
298 		     "# columns: 0\n",  //
299 		     OctaveIO.toText("matrixzero2d", matrixzero2d));
300 
301         assertEquals("# name: matrixzero\n" + //
302 		     "# type: bool matrix\n" + //
303 		     "# ndims: 4\n" + //
304 		     " 0 0 0 0\n",  // 
305 		     OctaveIO.toText("matrixzero", matrixzero));
306 
307         assertEquals("# name: matrixscalar\n" + //
308 		     "# type: bool\n" + //
309 		     "1\n",  //
310 		     OctaveIO.toText("matrixscalar", matrixscalar));
311 
312         assertEquals("# name: matrix2d\n" + //
313 		     "# type: bool matrix\n" + //
314 		     "# rows: 2\n" + //
315 		     "# columns: 3\n" + //
316 		     " 1 1 1\n" + //
317 		     " 0 0 0\n",  //
318 		     OctaveIO.toText("matrix2d", matrix2d));
319 
320         assertEquals("# name: bigmatrix\n" + //
321 		     "# type: bool matrix\n" + //
322 		     "# ndims: 4\n" + //
323 		     " 1 2 3 4\n" + //
324 		     " 1\n" + //
325 		     " 0\n" + //
326 		     " 1\n" + //
327 		     " 0\n" + //
328 		     " 1\n" + //
329 		     " 0\n" + //
330 		     " 1\n" + //
331 		     " 0\n" + //
332 		     " 1\n" + //
333 		     " 0\n" + //
334 		     " 1\n" + //
335 		     " 0\n" + //
336 		     " 1\n" + //
337 		     " 0\n" + //
338 		     " 1\n" + //
339 		     " 0\n" + //
340 		     " 1\n" + //
341 		     " 0\n" + //
342 		     " 1\n" + //
343 		     " 0\n" + //
344 		     " 1\n" + //
345 		     " 0\n" + //
346 		     " 1\n" + //
347 		     " 0\n",  //
348 		     OctaveIO.toText("bigmatrix", bigmatrix));
349     }
350 
351     /** */
352     @Test public void testWriteRead() {
353         final OctaveBoolean boolean1 = new OctaveBoolean(0, 0);
354         boolean1.set(true, 2, 2);
355 
356         final String text = OctaveIO.toText(boolean1);
357 
358         final Map<String, OctaveObject> read = OctaveIO.readWithName(text);
359         assertTrue(read.containsKey("ans"));
360         assertEquals(boolean1, read.get("ans"));
361     }
362 
363     /** */
364     @Test public void testRead() {
365         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
366 
367         final OctaveBoolean bool = new OctaveBoolean(0, 0);
368         octave.eval("x = true(0, 0);");
369         assertEquals(bool, octave.get("x"));
370 
371         bool.set(true, 1, 1);
372         octave.eval("x = true;");
373         assertEquals(bool, octave.get("x"));
374 
375         bool.set(true, 3, 1);
376         octave.eval("x(3, 1) = true;");
377         assertEquals(bool, octave.get("x"));
378 
379         bool.set(true, 2, 2);
380         octave.eval("x(2, 2) = true;");
381         assertEquals(bool, octave.get("x"));
382 
383         final OctaveBoolean bool3 = new OctaveBoolean(0, 0, 0);
384         bool3.set(true, 2, 2, 2);
385         octave.eval("x3(2, 2, 2) = true;");
386         assertEquals(bool3, octave.get("x3"));
387 
388         octave.close();
389     }
390 
391     /**
392      * Test that we handle the special single boolean case. 
393      * Type will be "bool", not "bool matrix".
394      */
395     @Test public void testReadSingle() {
396         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
397 
398         final OctaveBoolean bool = new OctaveBoolean(0, 0);
399         bool.set(true, 1, 1);
400         octave.eval("x = true;");
401         assertEquals(bool, octave.get("x"));
402 
403         octave.close();
404     }
405 
406     public void main(String[] args) {
407     }
408 
409 }