1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package eu.simuline.octave.type;
17
18
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
29
30 public class TestOctaveBoolean {
31
32
33
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
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
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
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
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
92 }
93 }
94
95
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
117
118 @Test public void testGrowth() {
119 final OctaveBoolean matrix = new OctaveBoolean(3, 3, 3, 3);
120
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
133 matrix.set(true, 4, 5, 7, 6);
134
135
136
137
138
139
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
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
167 assertTrue(matrix.dataSize() >= 4);
168 }
169
170
171 @Test public void testPerformance() {
172 OctaveBoolean matrix = new OctaveBoolean(30, 0);
173 final long allowedTime;
174
175 allowedTime = 1800;
176
177
178
179 long t = System.currentTimeMillis();
180
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
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 }