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.io.BufferedReader;
19  import java.io.StringReader;
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.Octave;
25  import eu.simuline.octave.type.OctaveCell;
26  import eu.simuline.octave.type.OctaveDouble;
27  import eu.simuline.octave.type.OctaveString;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  import org.junit.Ignore;
32  import org.junit.Test;
33  
34  /**
35   * Test read/write of {@link OctaveCell}
36   */
37  public class TestIoOctaveCell {
38  
39      /**
40       */
41      @Test public void testConstructor() {
42          final OctaveCell cell = new OctaveCell(0, 0);
43          assertEquals(0, cell.getSize(1));
44          assertEquals(0, cell.getSize(2));
45          assertEquals("# name: ans\n" + //
46  		     "# type: cell\n" + //
47  		     "# rows: 0\n" + //
48  		     "# columns: 0\n", 
49  		     OctaveIO.toText("ans", cell));
50      }
51  
52      /**
53       */
54      @Test public void testConstructorValue() {
55          final OctaveCell cell = new OctaveCell(0, 0);
56          cell.set(Octave.scalar(42), 1, 1);
57          assertEquals(1, cell.getSize(1));
58          assertEquals(1, cell.getSize(2));
59          assertEquals("# name: mycell2\n" + 
60  		     "# type: cell\n" + 
61  		     "# rows: 1\n" + 
62  		     "# columns: 1\n" + 
63  		     "# name: <cell-element>\n" + 
64  		     "# type: scalar\n" + 
65  		     "42.0\n\n", 
66  		     OctaveIO.toText("mycell2", cell));
67      }
68  
69      /**
70       */
71      @Test public void testConstructorIntInt() {
72          final OctaveCell cell = new OctaveCell(2, 2);
73          assertEquals(2, cell.getSize(1));
74          assertEquals(2, cell.getSize(2));
75          assertEquals("# name: mycell22\n" + 
76  		     "# type: cell\n" + 
77  		     "# rows: 2\n" + 
78  		     "# columns: 2\n" + //
79  
80  		     "# name: <cell-element>\n" + 
81  		     "# type: matrix\n" + 
82  		     "# rows: 0\n" + 
83  		     "# columns: 0\n" + //
84  
85  		     "# name: <cell-element>\n" + 
86  		     "# type: matrix\n" + 
87  		     "# rows: 0\n" + 
88  		     "# columns: 0\n" + //
89  		     "\n" + //
90  		     "# name: <cell-element>\n" + 
91  		     "# type: matrix\n" + 
92  		     "# rows: 0\n" + 
93  		     "# columns: 0\n" + //
94  
95  		     "# name: <cell-element>\n" + 
96  		     "# type: matrix\n" + 
97  		     "# rows: 0\n" + 
98  		     "# columns: 0\n" + //
99  		     "\n",
100 		     OctaveIO.toText("mycell22", cell));
101     }
102 
103     /**
104      */
105     @Test public void testSetIntInt() {
106         final OctaveCell cell = new OctaveCell(0, 0);
107         assertEquals(0, cell.getSize(1));
108         assertEquals(0, cell.getSize(2));
109         cell.set(Octave.scalar(42), 3, 4);
110         assertEquals(3, cell.getSize(1));
111         assertEquals(4, cell.getSize(2));
112         assertEquals("# name: mycell\n# type: cell\n" + 
113 		     "# rows: 3\n# columns: 4\n" + //
114 		     "# name: <cell-element>\n# type: matrix\n" + 
115 		     "# rows: 0\n# columns: 0\n" + //
116 		     "# name: <cell-element>\n# type: matrix\n" + 
117 		     "# rows: 0\n# columns: 0\n" + //
118 		     "# name: <cell-element>\n# type: matrix\n" + 
119 		     "# rows: 0\n# columns: 0\n" + //
120 		     "\n" + //
121 		     "# name: <cell-element>\n# type: matrix\n" + 
122 		     "# rows: 0\n# columns: 0\n" + //
123 		     "# name: <cell-element>\n# type: matrix\n" + 
124 		     "# rows: 0\n# columns: 0\n" + //
125 		     "# name: <cell-element>\n# type: matrix\n" + 
126 		     "# rows: 0\n# columns: 0\n" + //
127 		     "\n" + //
128 		     "# name: <cell-element>\n# type: matrix\n" + 
129 		     "# rows: 0\n# columns: 0\n" + //
130 		     "# name: <cell-element>\n# type: matrix\n" + 
131 		     "# rows: 0\n# columns: 0\n" + //
132 		     "# name: <cell-element>\n# type: matrix\n" + 
133 		     "# rows: 0\n# columns: 0\n" + //
134 		     "\n" + //
135 		     "# name: <cell-element>\n# type: matrix\n" + 
136 		     "# rows: 0\n# columns: 0\n" + //
137 		     "# name: <cell-element>\n# type: matrix\n" + 
138 		     "# rows: 0\n# columns: 0\n" + //
139 		     "# name: <cell-element>\n# type: scalar\n42.0\n" + //
140 		     "\n", 
141 		     OctaveIO.toText("mycell", cell));
142     }
143 
144     /**
145      * @throws Exception
146      */
147     @Test public void testOctaveConnection() throws Exception {
148         final OctaveCell cell = new OctaveCell(0, 0);
149         cell.set(Octave.scalar(42), 1, 1);
150         final OctaveCell cell2 = new OctaveCell(0, 0);
151         cell2.set(new OctaveString("mystring"), 1, 1);
152         cell.set(cell2, 3, 2);
153 
154         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
155         octave.put("mycell", cell);
156         final OctaveCell mycell_copy = octave.get(OctaveCell.class, "mycell");
157         assertEquals(cell, mycell_copy);
158         octave.close();
159     }
160 
161     /**
162      * @throws Exception
163      */
164     @Test public void testSameInOctave() throws Exception {
165         final OctaveCell cell = new OctaveCell(2, 3);
166         for (int r = 1; r <= 2; ++r) {
167             for (int c = 1; c <= 3; ++c) {
168                 cell.set(Octave.scalar(r + 0.1 * c), r, c);
169             }
170         }
171         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
172         octave.put("cell_java", cell);
173         // Assert it is the same in Octave and Java
174         octave.eval("cell_octave=cell(); " + 
175 		    "for r=1:2 for c=1:3 cell_octave{r,c}=r+0.1*c; end end");
176         octave.eval("assert(cell_octave, cell_java);");
177         // Assert that the returned value is the same as the original
178         assertEquals(cell, octave.get("cell_java"));
179         octave.close();
180     }
181 
182     /**
183      */
184     @Test public void testMatrixInCell() {
185         final OctaveDouble octaveMatrix = new OctaveDouble(2, 3);
186         octaveMatrix.set(42, 1, 1);
187         final OctaveCell cell = new OctaveCell(2, 2);
188         cell.set(Octave.scalar(42), 1, 1);
189         cell.set(octaveMatrix, 1, 2);
190         assertEquals(2, cell.getSize(1));
191         assertEquals(2, cell.getSize(2));
192         assertEquals("# name: mycell2\n" + //
193 		     "# type: cell\n" + //
194 		     "# rows: 2\n" + //
195 		     "# columns: 2\n" + //
196 
197 		     "# name: <cell-element>\n" + //
198 		     "# type: scalar\n" + //
199 		     "42.0\n" + //
200 
201 		     "# name: <cell-element>\n" + //
202 		     "# type: matrix\n" + //
203 		     "# rows: 0\n" + //
204 		     "# columns: 0\n" + //
205 
206 		     "\n" + //
207 
208 		     "# name: <cell-element>\n" + //
209 		     "# type: matrix\n" + //
210 		     "# rows: 2\n" + //
211 		     "# columns: 3\n" + //
212 		     " 42.0 0.0 0.0\n" + //
213 		     " 0.0 0.0 0.0\n" + //
214 
215 		     "# name: <cell-element>\n" + //
216 		     "# type: matrix\n" + //
217 		     "# rows: 0\n" + //
218 		     "# columns: 0\n" + //
219 
220 		     "\n",
221 		     OctaveIO.toText("mycell2", cell));
222     }
223 
224     /** */
225     @Test public void testCell12() {
226         final OctaveCell cell = new OctaveCell(0, 0);
227         cell.set(Octave.scalar(42), 1, 2);
228         assertEquals(1, cell.getSize(1));
229         assertEquals(2, cell.getSize(2));
230         assertEquals("# name: cell12\n" + //
231 		     "# type: cell\n" + //
232 		     "# rows: 1\n" + //
233 		     "# columns: 2\n" + //
234 
235 		     "# name: <cell-element>\n" + //
236 		     "# type: matrix\n" + //
237 		     "# rows: 0\n" + //
238 		     "# columns: 0\n" + //
239 
240 		     "\n" + //
241 		     "# name: <cell-element>\n" + //
242 		     "# type: scalar\n" + //
243 		     "42.0\n" + //
244 
245 		     "\n", 
246 		     OctaveIO.toText("cell12", cell));
247     }
248 
249     /** */
250     @Test public void testCell22() {
251         final OctaveCell cell = new OctaveCell(0, 0);
252         cell.set(Octave.scalar(12), 1, 2);
253         cell.set(Octave.scalar(21), 2, 1);
254         assertEquals(2, cell.getSize(1));
255         assertEquals(2, cell.getSize(2));
256         assertEquals("# name: cell22\n" + //
257 		     "# type: cell\n" + //
258 		     "# rows: 2\n" + //
259 		     "# columns: 2\n" + //
260 
261 		     "# name: <cell-element>\n" + //
262 		     "# type: matrix\n" + //
263 		     "# rows: 0\n" + //
264 		     "# columns: 0\n" + //
265 
266 		     "# name: <cell-element>\n" + //
267 		     "# type: scalar\n" + //
268 		     "21.0\n" + //
269 
270 		     "\n" + //
271 
272 		     "# name: <cell-element>\n" + //
273 		     "# type: scalar\n" + //
274 		     "12.0\n" + //
275 
276 		     "# name: <cell-element>\n" + //
277 		     "# type: matrix\n" + //
278 		     "# rows: 0\n" + //
279 		     "# columns: 0\n" + //
280 
281 		     "\n", 
282 		     OctaveIO.toText("cell22", cell));
283     }
284 
285     /**
286      * @throws Exception
287      */
288     @Test public void testWriteRead() throws Exception {
289         final OctaveCell cell = new OctaveCell(0, 0);
290         cell.set(Octave.scalar(12), 1, 2);
291         cell.set(Octave.scalar(21), 2, 1);
292         assertEquals(2, cell.getSize(1));
293         assertEquals(2, cell.getSize(2));
294 
295         final String text = OctaveIO.toText(cell);
296         final BufferedReader bufferedReader = 
297 	    new BufferedReader(new StringReader(text));
298 
299         assertEquals("# name: ans", bufferedReader.readLine());
300         assertEquals(cell, OctaveIO.read(bufferedReader));
301         assertEquals(-1, bufferedReader.read()); // Check end of file
302     }
303 
304 }