View Javadoc
1   /*
2    * Copyright 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 eu.simuline.octave.OctaveEngine;
19  import eu.simuline.octave.OctaveEngineFactory;
20  import eu.simuline.octave.type.OctaveComplex;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import org.junit.Ignore;
25  import org.junit.Test;
26  
27  /**
28   * Test read/write of {@link OctaveComplex}
29   */
30  public class TestIoOctaveComplex {
31  
32      /** Test */
33      @Test public void testGetScalar() {
34          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
35          octave.eval("y = 1.2 + 3.4i;");
36          final OctaveComplex c = octave.get(OctaveComplex.class, "y");
37          assertEquals(1.2, c.getReal(1, 1), 1e-10);
38          assertEquals(3.4, c.getImag(1, 1), 1e-10);
39          octave.close();
40      }
41  
42      /** Test */
43      @Test public void testGet2dMatrix() {
44          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
45          octave.eval("y = [ 1.1 1.1i ; 2.2 2.2i ];");
46          final OctaveComplex c = octave.get(OctaveComplex.class, "y");
47          assertEquals(1.1, c.getReal(1, 1), 1e-10);
48          assertEquals(0.0, c.getImag(1, 1), 1e-10);
49          assertEquals(0.0, c.getReal(1, 2), 1e-10);
50          assertEquals(1.1, c.getImag(1, 2), 1e-10);
51          assertEquals(2.2, c.getReal(2, 1), 1e-10);
52          assertEquals(0.0, c.getImag(2, 1), 1e-10);
53          assertEquals(0.0, c.getReal(2, 2), 1e-10);
54          assertEquals(2.2, c.getImag(2, 2), 1e-10);
55          octave.close();
56      }
57  
58      /** Test that a real number also works in the complex code */
59      @Test public void testGetReal() {
60          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
61          octave.eval("y = 1.2;");
62          final OctaveComplex c = octave.get(OctaveComplex.class, "y");
63          assertEquals(1.2, c.getReal(1, 1), 1e-10);
64          assertEquals(0, c.getImag(1, 1), 1e-10);
65          octave.close();
66      }
67  
68  }