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  
20  import eu.simuline.octave.OctaveEngine;
21  import eu.simuline.octave.OctaveEngineFactory;
22  import eu.simuline.octave.io.OctaveIO;
23  import eu.simuline.octave.type.OctaveBoolean;
24  import eu.simuline.octave.type.OctaveObject;
25  import eu.simuline.octave.type.OctaveSparseBoolean;
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 TestIoOctaveSparseBoolean {
37  
38      private static final String TEXT_TRUE = 
39  	"# name: x\n" + //
40  	"# type: sparse bool matrix\n" + //
41  	"# nnz: 1\n" + //
42  	"# rows: 1\n" + //
43  	"# columns: 1\n" + //
44  	"1 1 1\n";
45  
46      private static final String TEXT_EMPTY = 
47  	"# name: x\n" + //
48  	"# type: sparse bool matrix\n" + //
49  	"# nnz: 0\n" + //
50  	"# rows: 0\n" + //
51  	"# columns: 0\n";
52  
53      /** */
54      @Test public void testReadFalse() {
55          final Map<String, OctaveObject> read = 
56  	    OctaveIO.readWithName("# name: x\n" + //
57  				  "# type: sparse bool matrix\n" + //
58  				  "# nnz: 0\n" + //
59  				  "# rows: 1\n" + //
60  				  "# columns: 1\n");
61          assertTrue(read.toString(), read.containsKey("x"));
62      }
63  
64      /** */
65      @Test public void testReadTrue() {
66          final Map<String, OctaveObject> read = OctaveIO.readWithName(TEXT_TRUE);
67          assertTrue(read.toString(), read.containsKey("x"));
68      }
69  
70      /** */
71      @Test public void testWriteTrue() {
72          final OctaveSparseBoolean o = new OctaveSparseBoolean(1, 1, 1);
73          o.set(true, 1, 1);
74          assertEquals(TEXT_TRUE, OctaveIO.toText("x", o));
75      }
76  
77      /** */
78      @Test public void testReadEmpty() {
79          final Map<String, OctaveObject> read = 
80  	    OctaveIO.readWithName(TEXT_EMPTY);
81          assertTrue(read.toString(), read.containsKey("x"));
82          assertEquals(new OctaveSparseBoolean(0, 0, 0), read.get("x"));
83      }
84  
85      /** */
86      @Test public void testWriteEmpty() {
87          final OctaveSparseBoolean o = new OctaveSparseBoolean(0, 0, 0);
88          assertEquals(TEXT_EMPTY, OctaveIO.toText("x", o));
89      }
90  
91      /** */
92      @Test public void testReadWrite() {
93          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
94  
95          if (octave.getOctaveVersion().equals("3.0.5")) {
96              return; // Skip test on octave 3.0.5
97          }
98  
99          OctaveObject s;
100 
101         octave.eval("x = true(0, 0);");
102         octave.eval("s = sparse(x);");
103         s = octave.get("s");
104         octave.put("c", s);
105         octave.eval("assert(c, s);");
106 
107         octave.eval("x = true;");
108         octave.eval("s = sparse(x);");
109         s = octave.get("s");
110         octave.put("c", s);
111         octave.eval("assert(c, s);");
112 
113         octave.eval("x(3, 1) = true;");
114         octave.eval("s = sparse(x);");
115         s = octave.get("s");
116         octave.put("c", s);
117         octave.eval("assert(c, s);");
118 
119         octave.eval("x(2, 2) = true;");
120         octave.eval("s = sparse(x);");
121         s = octave.get("s");
122         octave.put("c", s);
123         octave.eval("assert(c, s);");
124 
125         octave.close();
126     }
127 
128 }