View Javadoc
1   /*
2    * Copyright 2007, 2008 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.testhelpers.Actions;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotSame;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.fail;
25  
26  import org.junit.Ignore;
27  import org.junit.Test;
28  
29  import java.util.Arrays;
30  
31  //import net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;
32  
33  /**
34   * Test {@link OctaveDouble}
35   */
36  public class TestOctaveDouble {
37  
38      /**
39       * Test
40       */
41      @Test public void testScalar() {
42          final OctaveDouble s1a = Octave.scalar(1);
43          final OctaveDouble s1b = Octave.scalar(1);
44          final OctaveDouble s1c = Octave.scalar(0);
45          s1c.set(1, 1, 1);
46  
47          assertEquals(s1a, s1b);
48          assertEquals(s1a, s1c);
49          assertEquals(s1b, s1c);
50          assertNotSame(s1a, s1b);
51          assertNotSame(s1a, s1c);
52          assertNotSame(s1b, s1c);
53  
54          final OctaveDouble s0 = Octave.scalar(0);
55          final OctaveDouble s2 = Octave.scalar(2);
56  
57          assertTrue(!s1a.equals(s0));
58          assertTrue(!s1a.equals(s2));
59      }
60  
61      /**
62       */
63      @Test public void testGetAndSet() {
64          final OctaveDouble matrix = new OctaveDouble(3, 6, 5, 4);
65          matrix.set(2.0, 2, 5, 2, 3);
66          for (int row = 1; row <= 3; row++) {
67              for (int column = 1; column <= 6; column++) {
68                  for (int depth = 1; depth <= 5; depth++) {
69                      for (int coffee = 1; coffee <= 4; coffee++) {
70                          if (row == 2 && column == 5 && 
71  			    depth == 2 && coffee == 3) {
72                              assertEquals(2.0, 
73  					 matrix.get(row, column, depth, coffee),
74  					 0.0);
75                          } else {
76                              assertEquals(0.0, 
77  					 matrix.get(row, column, depth, coffee),
78  					 0.0);
79                          }
80                      }
81                  }
82              }
83          }
84          try {
85              matrix.get(2, 3, 1, 0);
86              fail("Attempt to get with a position " + 
87  		 "that includes a 0 should fail");
88          } catch (final IndexOutOfBoundsException e) {
89              // ok
90          }
91          try {
92              matrix.get(2, 3, 10, 3);
93              fail("Attempt to get with a position " + 
94  		 "that exceeds range should fail");
95          } catch (final IndexOutOfBoundsException e) {
96              // ok
97          }
98          try {
99              matrix.get(2, 3, 2, 3, 4);
100             fail("Attempt to get with a position " + 
101 		 "that exceeds dimensions should fail");
102         } catch (final IndexOutOfBoundsException e) {
103             // ok
104         }
105 
106     }
107 
108     /**
109      * Test that the Matrix is not modified when the size matrix is changed.
110      */
111     @Test public void testSizeConstructorModify() {
112         final int[] size = new int[] { 2, 2 };
113         final OctaveDouble matrix = new OctaveDouble(size);
114         assertEquals(2, matrix.getSize(1));
115         assertEquals(2, matrix.getSize(2));
116         size[1] = 3;
117         assertEquals(2, matrix.getSize(1));
118         assertEquals(2, matrix.getSize(2));
119     }
120 
121     /**
122      */
123     @Test public void testSizeConstructor() {
124         final OctaveDouble matrix = new OctaveDouble(3, 6, 5, 4);
125         assertEquals(4, matrix.getSizeLength());
126         assertEquals(3, matrix.getSize(1));
127         assertEquals(6, matrix.getSize(2));
128         assertEquals(5, matrix.getSize(3));
129         assertEquals(4, matrix.getSize(4));
130 
131         final OctaveDouble matrixEmpty = new OctaveDouble(0, 0);
132 	assertEquals(matrixEmpty.dataSize(), 0);
133 
134         try {
135             new OctaveDouble(1);
136             fail("OctaveMatrix should not support one dimensional matrices");
137         } catch (final IllegalArgumentException e) {
138             // OK
139         }
140     }
141 
142     /**
143      */
144     @Test public void testDataSizeConstructor() {
145         final double[] data = new double[2 * 3 * 4];
146         for (int idx = 0; idx < data.length; idx++) {
147             data[idx] = idx + 1.0;
148         }
149         final OctaveDouble matrix = new OctaveDouble(data, 2, 3, 4);
150         double d = 1.0;
151         for (int depth = 1; depth <= 4; depth++) {
152             for (int column = 1; column <= 3; column++) {
153                 for (int row = 1; row <= 2; row++) {
154                     assertEquals(d, matrix.get(row, column, depth), 0.0);
155                     d++;
156                 }
157             }
158         }
159 
160         // a larger data array is ok
161         new OctaveDouble(data, 2, 2, 4);
162 
163         try {
164             new OctaveDouble(data, 2, 4, 4);
165             fail("should throw IllegalArgumentException");
166         } catch (final IllegalArgumentException e) {
167             assertEquals("length of data(24) is smaller than size([2, 4, 4])",
168 			 e.getMessage());
169         }
170     }
171 
172     /**
173      * Test that shallowCopy makes an identical copy 
174      * that does not change when the original is changed.
175      */
176     @Test public void testShallowCopy() {
177         final OctaveDouble a = new OctaveDouble(1, 1);
178         a.set(22, 1, 1);
179         final OctaveDouble b = a.shallowCopy();
180         assertEquals(a, b);
181         assertEquals(22.0, a.get(1, 1), 0.0);
182         assertEquals(22.0, b.get(1, 1), 0.0);
183         a.set(33, 1, 1);
184         assertEquals(33.0, a.get(1, 1), 0.0);
185         assertEquals(22.0, b.get(1, 1), 0.0);
186         b.set(44, 1, 1);
187         assertEquals(33.0, a.get(1, 1), 0.0);
188         assertEquals(44.0, b.get(1, 1), 0.0);
189     }
190 
191     /**
192      * Test that shallowCopy makes an identical copy 
193      * that does not change size when the original is changed.
194      */
195     @Test public void testShallowCopySize() {
196         final OctaveDouble a = new OctaveDouble(2, 2);
197         final OctaveDouble b = a.shallowCopy();
198         assertEquals(a, b);
199         assertEquals(2, a.getSize(1));
200         assertEquals(2, a.getSize(2));
201         assertEquals(2, b.getSize(1));
202         assertEquals(2, b.getSize(2));
203         a.set(33, 2, 3);
204         assertEquals(2, a.getSize(1));
205         assertEquals(3, a.getSize(2));
206         assertEquals(2, b.getSize(1));
207         assertEquals(2, b.getSize(2));
208         b.set(44, 3, 2);
209         assertEquals(2, a.getSize(1));
210         assertEquals(3, a.getSize(2));
211         assertEquals(3, b.getSize(1));
212         assertEquals(2, b.getSize(2));
213     }
214 
215     /**
216      */
217     @Test public void testMakeCopy() {
218         final double[] data = new double[2 * 3 * 4];
219         for (int idx = 0; idx < data.length; idx++) {
220             data[idx] = idx + 1.0;
221         }
222         final OctaveDouble matrix = new OctaveDouble(data, 2, 3, 4)
223 	    .shallowCopy();
224         double d = 1.0;
225         for (int depth = 1; depth <= 4; depth++) {
226             for (int column = 1; column <= 3; column++) {
227                 for (int row = 1; row <= 2; row++) {
228                     assertEquals(matrix.get(row, column, depth), d, 0.0);
229                     d++;
230                 }
231             }
232         }
233 
234     }
235 
236     /** */
237     @Test public void testGrowth() {
238         final OctaveDouble matrix = new OctaveDouble(3, 3, 3, 3);
239 	// test set and get without resize 
240         matrix.set(42.0, 2, 2, 2, 2);
241         matrix.set( 1.0, 3, 2, 2, 2);
242         matrix.set( 2.0, 2, 3, 2, 2);
243         matrix.set( 3.0, 2, 2, 3, 2);
244         matrix.set( 4.0, 2, 2, 2, 3);
245         assertEquals(42.0, matrix.get(2, 2, 2, 2), 0.0);
246         assertEquals( 1.0, matrix.get(3, 2, 2, 2), 0.0);
247         assertEquals( 2.0, matrix.get(2, 3, 2, 2), 0.0);
248         assertEquals( 3.0, matrix.get(2, 2, 3, 2), 0.0);
249         assertEquals( 4.0, matrix.get(2, 2, 2, 3), 0.0);
250 
251 	// test set and get with resize 
252         matrix.set(Math.PI, 4, 5, 7, 6);
253         // assertEquals(42.0, matrix.get(2, 2, 2, 2), 0.0);
254         // assertEquals( 1.0, matrix.get(3, 2, 2, 2), 0.0);
255         // assertEquals( 2.0, matrix.get(2, 3, 2, 2), 0.0);
256         // assertEquals( 3.0, matrix.get(2, 2, 3, 2), 0.0);
257         // assertEquals( 4.0, matrix.get(2, 2, 2, 3), 0.0);
258         // assertEquals(Math.PI, matrix.get(4, 5, 7, 6), 0.0);
259 
260 
261 	final OctaveDouble matrixB = new OctaveDouble(4, 5, 7, 6);
262         matrixB.set(42.0, 2, 2, 2, 2);
263         matrixB.set( 1.0, 3, 2, 2, 2);
264         matrixB.set( 2.0, 2, 3, 2, 2);
265         matrixB.set( 3.0, 2, 2, 3, 2);
266         matrixB.set( 4.0, 2, 2, 2, 3);
267         matrixB.set(Math.PI, 4, 5, 7, 6);
268  	assertEquals(matrixB, matrix);
269     }
270 
271     /** */
272     @Test public void testResize() {
273         final OctaveDouble matrix = new OctaveDouble(0, 4);
274         assertEquals(2, matrix.getSizeLength());
275         assertEquals(0, matrix.getSize(1));
276         assertEquals(4, matrix.getSize(2));
277         // assertEquals(0, matrix.dataLength()); is 0
278         assertTrue(matrix.dataSize() >= 0);
279 
280         matrix.set(42.0, 1, 1);
281         assertEquals(42.0, matrix.get(1, 1), 0.0);
282         assertEquals(2, matrix.getSizeLength());
283         assertEquals(1, matrix.getSize(1));
284         assertEquals(4, matrix.getSize(2));
285         // assertEquals(4, matrix.dataLength()); is 8
286         assertTrue(matrix.dataSize() >= 4);
287     }
288 
289     /** Test Performance of Resize */
290     @Test public void testPerformance() {
291         OctaveDouble matrix = new OctaveDouble(30, 0);
292         final long allowedTime;
293 //        if (matrix instanceof HasBeenInstrumented) {
294 	allowedTime = 1800;//1800;
295 //        } else {
296 //            allowedTime = 300;
297 //        }
298         long t = System.currentTimeMillis();
299         // 4125 was the number of containers in a real job.
300         for (int pos = 1; pos <= 4125; ++pos) {
301             matrix.set(4.2, 1, pos);
302             matrix.set(4.2, 30, pos);
303         }
304         long timeused = System.currentTimeMillis() - t;
305         if (timeused > allowedTime) {
306             fail("Performance test didn't finish in " + allowedTime + 
307 		 "ms (used " + timeused + "ms)");
308         }
309 
310         matrix = new OctaveDouble(0, 30);
311         t = System.currentTimeMillis();
312         // 1000 is just some random number
313         for (int pos = 1; pos <= 1000; ++pos) {
314             matrix.set(4.2, pos, 1);
315             matrix.set(4.2, pos, 30);
316         }
317         timeused = System.currentTimeMillis() - t;
318         if (timeused > allowedTime) {
319             fail("Performance test didn't finish in " + allowedTime + 
320 		 "ms (used " + timeused + "ms)");
321         }
322     }
323 
324     /**
325      * Test {@link OctaveComplex} resizes correctly
326      */
327     @Test public void testResize2() {
328         final OctaveDouble d = new OctaveDouble(1, 1);
329         d.set(22, 1, 2);
330         d.set(33, 2, 2);
331         assertEquals( 0.0, d.get(1, 1), 0.0);
332         assertEquals(22.0, d.get(1, 2), 0.0);
333         assertEquals( 0.0, d.get(2, 1), 0.0);
334         assertEquals(33.0, d.get(2, 2), 0.0);
335     }
336 
337     /* -------------------------------------------------------------------- *
338      * framework.                                                           *
339      * -------------------------------------------------------------------- */
340 
341 
342     /**
343      * Runs the test case.
344      *
345      * Uncomment either the textual UI, Swing UI, or AWT UI.
346      */
347     public static void main(String[] args) {
348 	Actions.runFromMain();
349     }
350 }