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.type;
17  
18  import eu.simuline.octave.exception.OctaveClassCastException;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.fail;
23  
24  import org.junit.Ignore;
25  import org.junit.Test;
26  
27  /**
28   * Test OctaveStruct object
29   */
30  public class TestOctaveStruct {
31  
32      /**
33       * Test that a copy is taken when getting from struct
34       */
35      @Test public void testOctaveGetCopy() {
36          final OctaveStruct struct = new OctaveStruct();
37          struct.set("scalar", Octave.scalar(42));
38          final OctaveDouble scalar = struct.get(OctaveDouble.class, "scalar");
39          scalar.set(10, 1, 1);
40          assertEquals(10.0,
41  		     scalar.get(1, 1), 
42  		     0.0);
43          assertEquals(42.0, 
44  		     struct.get(OctaveDouble.class, "scalar").get(1, 1), 
45  		     0.0);
46      }
47  
48      /**
49       * Test that the correct exception is thrown from OctaveStruct.get()
50       */
51      @Test public void testClassCast() {
52          final OctaveStruct struct = new OctaveStruct();
53          final OctaveDouble scalar = Octave.scalar(42);
54          struct.set("scalar", scalar);
55          try {
56              final OctaveCell cell = struct.get(OctaveCell.class, "scalar");
57              fail(cell.toString());
58          } catch (final OctaveClassCastException e) {
59              assertEquals(scalar, e.getOctaveObject());
60          }
61      }
62  
63      /**
64       * Test that we can get unknown values
65       */
66      @Test public void testGetUnknown() {
67          final OctaveStruct struct = new OctaveStruct();
68          assertNull(struct.get("unknown"));
69      }
70  
71  }