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 net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import org.junit.Ignore;
25  import org.junit.Test;
26  
27  /**
28   * Test OctaveBoolean
29   */
30  public class TestOctaveBoolean {
31  
32      /**
33       * Test 
34       */
35      @Test public void testGetAndSet() {
36          final OctaveBoolean matrix = new OctaveBoolean(3, 6, 5, 4);
37          matrix.set(true, 2, 5, 2, 3);
38          for (int row = 1; row <= 3; row++) {
39              for (int column = 1; column <= 6; column++) {
40                  for (int depth = 1; depth <= 5; depth++) {
41                      for (int coffee = 1; coffee <= 4; coffee++) {
42                          if (row == 2 && column == 5 
43  			    && depth == 2 && coffee == 3) {
44  			    // true for exactly the entry explicitly set above 
45                              assertTrue( matrix.get(row, column, depth, coffee));
46                          } else {
47                              assertTrue(!matrix.get(row, column, depth, coffee));
48                          }
49                      }
50                  }
51              }
52          }
53          try {
54              matrix.get(2, 3, 1, 0);
55              fail("Attempt to get with a position " + 
56  		 "that includes a 0 should fail");
57          } catch (final IndexOutOfBoundsException e) {
58              // ok
59          }
60          try {
61              matrix.get(2, 3, 10, 3);
62              fail("Attempt to get with a position " + 
63  		 "that exceeds range should fail");
64          } catch (final IndexOutOfBoundsException e) {
65              // ok
66          }
67          try {
68              matrix.get(2, 3, 2, 3, 4);
69              fail("Attempt to get with a position " + 
70  		 "that exceeds dimensions should fail");
71          } catch (final IndexOutOfBoundsException e) {
72              // ok
73          }
74  
75      }
76  
77      /**
78       */
79      @Test public void testSizeConstructor() {
80          final OctaveBoolean matrix = new OctaveBoolean(3, 6, 5, 4);
81          assertEquals(4, matrix.getSizeLength());
82          assertEquals(3, matrix.getSize(1));
83          assertEquals(6, matrix.getSize(2));
84          assertEquals(5, matrix.getSize(3));
85          assertEquals(4, matrix.getSize(4));
86  
87          try {
88              new OctaveBoolean(1);
89              fail("OctaveMatrix should not support one dimensional matrices");
90          } catch (final IllegalArgumentException e) {
91              // OK
92          }
93      }
94  
95      /** Test */
96      @Test public void testMakeCopy() {
97          final boolean[] data = new boolean[2 * 3 * 4];
98          for (int idx = 0; idx < data.length; idx++) {
99              data[idx] = idx % 2 == 0;
100         }
101         final OctaveBoolean matrix = new OctaveBoolean(data, 2, 3, 4)
102 	    .shallowCopy();
103         boolean b = true;
104         for (int depth = 1; depth <= 4; depth++) {
105             for (int column = 1; column <= 3; column++) {
106                 for (int row = 1; row <= 2; row++) {
107                     assertEquals(matrix.get(row, column, depth), b);
108                     b = !b;
109                 }
110             }
111         }
112 
113     }
114 
115     /**
116      * matrixzero doesn't work because of bug in octave
117      */
118     @Test public void testGrowth() {
119         final OctaveBoolean matrix = new OctaveBoolean(3, 3, 3, 3);
120 	// test set and get without resize 
121         matrix.set(true, 2, 2, 2, 2);
122         matrix.set(true, 3, 2, 2, 2);
123         matrix.set(true, 2, 3, 2, 2);
124         matrix.set(true, 2, 2, 3, 2);
125         matrix.set(true, 2, 2, 2, 3);
126         assertEquals(true, matrix.get(2, 2, 2, 2));
127         assertEquals(true, matrix.get(3, 2, 2, 2));
128         assertEquals(true, matrix.get(2, 3, 2, 2));
129         assertEquals(true, matrix.get(2, 2, 3, 2));
130         assertEquals(true, matrix.get(2, 2, 2, 3));
131 
132 	// test set and get with resize 
133         matrix.set(true, 4, 5, 7, 6);
134         // assertEquals(true, matrix.get(2, 2, 2, 2));
135         // assertEquals(true, matrix.get(3, 2, 2, 2));
136         // assertEquals(true, matrix.get(2, 3, 2, 2));
137         // assertEquals(true, matrix.get(2, 2, 3, 2));
138         // assertEquals(true, matrix.get(2, 2, 2, 3));
139         // assertEquals(true, matrix.get(4, 5, 7, 6));
140 
141 
142 	final OctaveBoolean matrixB = new OctaveBoolean(4, 5, 7, 6);
143         matrixB.set(true, 2, 2, 2, 2);
144         matrixB.set(true, 3, 2, 2, 2);
145         matrixB.set(true, 2, 3, 2, 2);
146         matrixB.set(true, 2, 2, 3, 2);
147         matrixB.set(true, 2, 2, 2, 3);
148         matrixB.set(true, 4, 5, 7, 6);
149 	assertEquals(matrixB, matrix);
150     }
151 
152     /** */
153     @Test public void testResize() {
154         final OctaveBoolean matrix = new OctaveBoolean(0, 4);
155         assertEquals(2, matrix.getSizeLength());
156         assertEquals(0, matrix.getSize(1));
157         assertEquals(4, matrix.getSize(2));
158         // assertEquals(0, matrix.getData().length); is 0
159 	assertTrue(matrix.dataSize() >= 0);
160 
161         matrix.set(true, 1, 1);
162         assertEquals(true, matrix.get(1, 1));
163         assertEquals(2, matrix.getSizeLength());
164         assertEquals(1, matrix.getSize(1));
165         assertEquals(4, matrix.getSize(2));
166         // assertEquals(4, matrix.getData().length); is 8
167         assertTrue(matrix.dataSize() >= 4);
168     }
169 
170     /** Test Performance of Resize */
171     @Test public void testPerformance() {
172         OctaveBoolean matrix = new OctaveBoolean(30, 0);
173         final long allowedTime;
174 //        if (matrix instanceof HasBeenInstrumented) {
175             allowedTime = 1800;
176         // } else {
177         //     allowedTime = 300;
178         // }
179         long t = System.currentTimeMillis();
180         // 4125 was the number of containers in a real job.
181         for (int pos = 1; pos <= 4125; ++pos) {
182             matrix.set(true, 1, pos);
183             matrix.set(true, 30, pos);
184         }
185         long timeused = System.currentTimeMillis() - t;
186         if (timeused > allowedTime) {
187             fail("Performance test didn't finish in " + allowedTime + 
188 		 "ms (used " + timeused + "ms)");
189         }
190 
191         matrix = new OctaveBoolean(0, 30);
192         t = System.currentTimeMillis();
193         // 1000 is just some random number
194         for (int pos = 1; pos <= 1000; ++pos) {
195             matrix.set(true, pos, 1);
196             matrix.set(true, pos, 30);
197         }
198         timeused = System.currentTimeMillis() - t;
199         if (timeused > allowedTime) {
200             fail("Performance test didn't finish in " + allowedTime + 
201 		 "ms (used " + timeused + "ms)");
202         }
203     }
204 
205 }