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.type;
17  
18  import eu.simuline.octave.type.cast.Cast;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  import org.junit.Ignore;
24  import org.junit.Test;
25  
26  /**
27   * Test OctaveComplex
28   */
29  public class TestOctaveComplex {
30  
31      /**
32       * Test that an OctaveDouble can be viewed as an OctaveComplex
33       */
34      @Test public void testCast() {
35          Cast.cast(OctaveComplex.class, new OctaveDouble(1, 1));
36      }
37  
38      /**
39       * Test OctaveComplex resizes correctly
40       */
41      @Test public void testResize() {
42          final OctaveComplex complex = new OctaveComplex(1, 1);
43  
44          checkSize(complex, 1, 1);
45          assertEquals(0.0, complex.getReal(1, 1), 0.0);
46          assertEquals(0.0, complex.getImag(1, 1), 0.0);
47  
48          complex.setReal(22, 1, 2);
49          checkSize(complex, 1, 2);
50          assertEquals( 0.0, complex.getReal(1, 1), 0.0);
51          assertEquals( 0.0, complex.getImag(1, 1), 0.0);
52          assertEquals(22.0, complex.getReal(1, 2), 0.0);
53          assertEquals( 0.0, complex.getImag(1, 2), 0.0);
54  
55          complex.setImag(33, 2, 1);
56          checkSize(complex, 2, 2);
57          assertEquals( 0.0, complex.getReal(1, 1), 0.0);
58          assertEquals( 0.0, complex.getImag(1, 1), 0.0);
59          assertEquals(22.0, complex.getReal(1, 2), 0.0);
60          assertEquals( 0.0, complex.getImag(1, 2), 0.0);
61          assertEquals( 0.0, complex.getReal(2, 1), 0.0);
62          assertEquals(33.0, complex.getImag(2, 1), 0.0);
63          assertEquals( 0.0, complex.getReal(2, 2), 0.0);
64          assertEquals( 0.0, complex.getImag(2, 2), 0.0);
65  
66          // assertEquals(22.0, complex.getReal()[complex.pos2ind(1, 2)], 0.0);
67          // assertEquals(33.0, complex.getImag()[complex.pos2ind(2, 1)], 0.0);
68      }
69  
70      private void checkSize(final OctaveComplex complex, 
71  			   final int i, 
72  			   final int j) {
73          assertEquals(i, complex.getSize(1));
74          assertEquals(j, complex.getSize(2));
75          complex.getReal(i, j);
76          complex.getImag(i, j);
77          failGet(complex, i + 1, j);
78          failGet(complex, i, j + 1);
79      }
80  
81      private void failGet(final OctaveComplex complex, 
82  			 final int i, 
83  			 final int j) {
84          try {
85              complex.getReal(i, j);
86              fail();
87          } catch (final IndexOutOfBoundsException e) {
88              // Expect this
89          }
90          try {
91              complex.getImag(i, j);
92              fail();
93          } catch (final IndexOutOfBoundsException e) {
94              // Expect this
95          }
96      }
97  
98  }