/
Testcase.nqp
455 lines (385 loc) · 14.8 KB
/
Testcase.nqp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# Copyright (C) 2010, Andrew Whitworth. See accompanying LICENSE file, or
# http://www.opensource.org/licenses/artistic-license-2.0.php for license.
INIT {
use('UnitTest::Testcase');
use('UnitTest::Assertions');
}
class Pla::Testcase is UnitTest::Testcase {
method default_loader() {
Pla::Loader.new;
}
# A default value which can be set at a particular location and tested
method defaultvalue() {
return (1);
}
# The null value which is auto-inserted into the matrix on resize.
method nullvalue() {
return (0);
}
# A novel value which can be used to flag interesting changes in tests.
method fancyvalue($idx) {
return ([5, 6, 7, 8][$idx]);
}
# Create an empty matrix of the given type
method matrix() {
Exception::MethodNotFound.new(
:message("Must subclass matrix in your test class")
).throw;
}
# Create a 2x2 matrix of the type with given values row-first
method matrix2x2($aa, $ab, $ba, $bb) {
my $m := self.matrix();
$m{Key.new(0,0)} := $aa;
$m{Key.new(0,1)} := $ab;
$m{Key.new(1,0)} := $ba;
$m{Key.new(1,1)} := $bb;
return ($m);
}
method defaultmatrix2x2() {
return self.matrix2x2(
self.defaultvalue(),
self.defaultvalue(),
self.defaultvalue(),
self.defaultvalue()
);
}
method fancymatrix2x2() {
return self.matrix2x2(
self.fancyvalue(0),
self.fancyvalue(1),
self.fancyvalue(2),
self.fancyvalue(3)
);
}
# Create a 3x3 matrix of the type with given values row-first
method matrix3x3($aa, $ab, $ac, $ba, $bb, $bc, $ca, $cb, $cc) {
my $m := self.matrix();
$m{Key.new(0,0)} := $aa;
$m{Key.new(0,1)} := $ab;
$m{Key.new(0,2)} := $ac;
$m{Key.new(1,0)} := $ba;
$m{Key.new(1,1)} := $bb;
$m{Key.new(1,2)} := $bc;
$m{Key.new(2,0)} := $ca;
$m{Key.new(2,1)} := $cb;
$m{Key.new(2,2)} := $cc;
return ($m);
}
### COMMON TESTS ###
# TODO: For some of the stub tests below, we should fail() if a test isn't
# provided. This means we're going to have to actually implement these
# methods in the various types and enforce the behavior.
method test_OP_new() {
assert_throws_nothing("Cannot create ComplexMatrix2D", {
my $m := self.matrix();
assert_not_null($m, "Could not create a ComplexMatrix2D");
});
}
method test_OP_does() {
my $m := self.matrix();
assert_true(pir::does($m, "matrix"), "Does not do matrix");
assert_false(pir::does($m, "gobbledegak"), "Does gobbledegak");
}
method test_VTABLE_get_pmc_keyed() {
my $m := self.matrix();
my $a := self.defaultvalue();
$m{Key.new(0,0)} := $a;
my $b := $m{Key.new(0,0)};
assert_equal($a, $b, "get_pmc_keyed doesn't work");
}
method test_VTABLE_set_pmc_keyed() {
assert_throws_nothing("Cannot set_pmc_keyed", {
my $m := self.matrix();
my $a := self.defaultvalue();
$m{Key.new(0,0)} := $a;
});
}
method test_VTABLE_clone() {
my $m := self.defaultmatrix2x2();
my $n := pir::clone($m);
assert_equal($m, $n, "clones are not equal");
assert_not_same($m, $n, "clones are the same PMC!");
}
method test_VTABLE_is_equal() {
my $m := self.defaultmatrix2x2();
my $n := self.defaultmatrix2x2();
assert_equal($m, $n, "equal matrices are not equal");
}
method test_VTABLE_is_equal_SIZEFAIL() {
my $m := self.defaultmatrix2x2();
my $n := self.defaultmatrix2x2();
$n{Key.new(2, 2)} := self.nullvalue();
assert_not_equal($m, $n, "different sized matrices are equal");
}
method test_VTABLE_is_equal_ELEMSFAIL() {
my $m := self.defaultmatrix2x2();
my $n := self.defaultmatrix2x2();
$n{Key.new(1,1)} := self.fancyvalue(0);
assert_not_equal($m, $n, "non-equal matrices are equal");
}
method test_VTABLE_get_attr_str() {
my $m := self.matrix();
$m{Key.new(5,7)} := self.defaultvalue;
assert_equal(pir::getattribute__PPS($m, "rows"), 6, "matrix does not have right size");
assert_equal(pir::getattribute__PPS($m, "cols"), 8, "matrix does not have right size");
}
method test_VTABLE_get_attr_str_EMPTY() {
my $m := self.matrix();
assert_equal(pir::getattribute__PPS($m, "rows"), 0, "empty matrix has non-zero row count");
assert_equal(pir::getattribute__PPS($m, "cols"), 0, "empty matrix has non-zero col count");
}
method test_VTABLE_freeze() {
todo("Tests Needed!");
}
method test_VTABLE_thaw() {
todo("Tests Needed!");
}
# TODO: Add tests that get_pmc_keyed_int and set_pmc_keyed_int share a
# correct linear relationship with get_pmc_keyed and set_pmc_keyed.
# Test to show that autoresizing behavior of the type is consistent.
method test_autoresizing() {
todo("Tests Needed!");
}
method test_METHOD_resize() {
my $m := self.matrix();
$m.resize(3,3);
assert_equal(pir::getattribute__PPS($m, "rows"), 3, "matrix does not have right size");
assert_equal(pir::getattribute__PPS($m, "cols"), 3, "matrix does not have right size");
}
method test_METHOD_resize_SHRINK() {
my $m := self.matrix();
$m.resize(3,3);
$m.resize(1,1);
assert_equal(pir::getattribute__PPS($m, "rows"), 3, "matrix does not have right size");
assert_equal(pir::getattribute__PPS($m, "cols"), 3, "matrix does not have right size");
}
method test_METHOD_resize_NEGATIVEINDICES() {
my $m := self.matrix();
$m.resize(-1, -1);
assert_equal(pir::getattribute__PPS($m, "cols"), 0, "negative indices silently ignored");
assert_equal(pir::getattribute__PPS($m, "rows"), 0, "negative indices silently ignored");
}
method test_METHOD_fill_RESIZE() {
my $m := self.defaultmatrix2x2();
my $n := self.matrix();
$n.fill(self.defaultvalue(), 2, 2);
assert_equal($n, $m, "Cannot fill+Resize");
}
method test_METHOD_fill() {
my $m := self.defaultmatrix2x2();
my $n := self.matrix2x2(
self.fancyvalue(0),
self.fancyvalue(0),
self.fancyvalue(0),
self.fancyvalue(0)
);
$m.fill(self.fancyvalue(0));
assert_equal($n, $m, "Cannot fill");
}
# Test transposing square matrices
method test_METHOD_transpose() {
my $m := self.matrix2x2(
self.fancyvalue(0),
self.fancyvalue(1),
self.fancyvalue(2),
self.fancyvalue(3)
);
my $n := self.matrix2x2(
self.fancyvalue(0),
self.fancyvalue(2),
self.fancyvalue(1),
self.fancyvalue(3)
);
$m.transpose();
assert_equal($n, $m, "cannot transpose matrix");
}
# Test transposing non-square matrices
method test_METHOD_transpose_DIMCHANGE() {
my $m := self.matrix();
$m{Key.new(0,0)} := self.fancyvalue(0);
$m{Key.new(0,1)} := self.fancyvalue(1);
$m{Key.new(0,2)} := self.fancyvalue(2);
$m{Key.new(0,3)} := self.fancyvalue(3);
my $n := self.matrix();
$n{Key.new(0,0)} := self.fancyvalue(0);
$n{Key.new(1,0)} := self.fancyvalue(1);
$n{Key.new(2,0)} := self.fancyvalue(2);
$n{Key.new(3,0)} := self.fancyvalue(3);
$m.transpose();
assert_equal($m, $n, "cannot transpose with non-square dimensions");
}
# Test transposing square matrices
method test_METHOD_mem_transpose() {
my $m := self.matrix2x2(
self.fancyvalue(0),
self.fancyvalue(1),
self.fancyvalue(2),
self.fancyvalue(3)
);
my $n := self.matrix2x2(
self.fancyvalue(0),
self.fancyvalue(2),
self.fancyvalue(1),
self.fancyvalue(3)
);
$m.mem_transpose();
assert_equal($n, $m, "cannot mem_transpose matrix");
}
# Test transposing non-square matrices
method test_METHOD_mem_transpose_DIMCHANGE() {
my $m := self.matrix();
$m{Key.new(0,0)} := self.fancyvalue(0);
$m{Key.new(0,1)} := self.fancyvalue(1);
$m{Key.new(0,2)} := self.fancyvalue(2);
$m{Key.new(0,3)} := self.fancyvalue(3);
my $n := self.matrix();
$n{Key.new(0,0)} := self.fancyvalue(0);
$n{Key.new(1,0)} := self.fancyvalue(1);
$n{Key.new(2,0)} := self.fancyvalue(2);
$n{Key.new(3,0)} := self.fancyvalue(3);
$m.mem_transpose();
assert_equal($m, $n, "cannot mem_transpose with non-square dimensions");
}
method test_METHOD_iterate_function_inplace() {
todo("Tests Needed!");
}
# TODO: Come up with a good consistant way to test that we are hitting all
# the coords, and hitting each only once.
method test_METHOD_iterate_function_inplace_COORDS() {
todo("Tests Needed!");
#my $m := self.defaultmatrix2x2();
#my $n := matrix2x2(0.0, 1.0, 1.0, 2.0);
#$m.iterate_function_inplace(-> $matrix, $value, $x, $y{
# return ($x + $y);
#});
#assert_equal($m, $n, "Cannot iterate with args");
}
# TODO: Come up with a good consistant way to test that we are hitting all
# the coords, and can pass constant args to each.
method test_METHOD_iterate_function_inplace_ARGS() {
todo("Tests Needed!");
#my $m := matrix2x2(1.0, 2.0, 3.0, 4.0);
#my $n := matrix2x2(1.0, 4.0, 9.0, 16.0);
#$m.iterate_function_inplace(-> $matrix, $value, $x, $y, $a, $b {
# return ($value * $value);
#}, 5, 2);
#assert_equal($m, $n, "Cannot iterate with args");
}
method test_METHOD_iterate_function_external() {
todo("Tests Needed!");
}
method test_METHOD_initialize_from_array() {
my $a := [self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3)];
my $m := self.matrix2x2(self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3));
my $n := self.matrix();
$n.initialize_from_array(2, 2, $a);
assert_equal($n, $m, "cannot initialize_from_array");
}
method test_METHOD_initialize_from_array_ZEROPAD() {
my $a := [self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3)];
my $m := self.matrix3x3(self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2),
self.fancyvalue(3), 0, 0,
0, 0, 0);
my $n := self.matrix();
$n.initialize_from_array(3, 3, $a);
assert_equal($n, $m, "cannot initalize from array with zero padding");
}
method test_METHOD_initialize_from_array_UNDERSIZE() {
my $a := [self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3)];
my $m := self.matrix();
$m{Key.new(0,0)} := self.fancyvalue(0);
my $n := self.matrix();
$n.initialize_from_array(1, 1, $a);
assert_equal($n, $m, "cannot initialize from array undersized");
}
method test_METHOD_initialize_from_args() {
my $m := self.matrix2x2(self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3));
my $n := self.matrix();
$n.initialize_from_array(2, 2, self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3));
assert_equal($n, $m, "cannot initialize_from_args");
}
method test_METHOD_initialize_from_args_ZEROPAD() {
my $m := self.matrix3x3(self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2),
self.fancyvalue(3), 0, 0,
0, 0, 0);
my $n := self.matrix();
$n.initialize_from_array(3, 3, self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3));
assert_equal($n, $m, "cannot initalize from args with zero padding");
}
method test_METHOD_initialize_from_args_UNDERSIZE() {
my $m := self.matrix();
$m{Key.new(0,0)} := self.fancyvalue(0);
my $n := self.matrix();
$n.initialize_from_array(1, 1, self.fancyvalue(0), self.fancyvalue(1), self.fancyvalue(2), self.fancyvalue(3));
assert_equal($n, $m, "cannot initialize from args undersized");
}
method test_METHOD_get_block() {
todo("Tests Needed!");
}
# Test that get_block(0,0,0,0) returns a zero-size matrix
method test_METHOD_get_block_ZEROSIZE() {
todo("Tests Needed!");
}
# Test that get_block(-1,-1,0,0) throws the proper exception
method test_METHOD_get_block_NEGINDICES() {
todo("Tests Needed!");
}
# Test that get_block(0,0,-1,-1) throws the proper exception
method test_METHOD_get_block_NEGSIZES() {
todo("Tests Needed!");
}
# Test the behavior of get_block when we request a block crossing or outside
# the boundaries of the matrix
method test_METHOD_get_block_OVERFLOW() {
todo("Tests Needed!");
}
method test_METHOD_set_block() {
todo("Tests Needed!");
}
method test_METHOD_set_block_ZEROSIZE() {
todo("Tests Needed!");
}
method test_METHOD_set_block_NEGINDICES() {
todo("Tests Needed!");
}
method test_METHOD_set_block_NEGSIZE() {
todo("Tests Needed!");
}
method test_METHOD_set_block_OVERFLOW() {
todo("Tests Needed!");
}
# TODO: Setup a test here to check the case where we set_block with a "block"
# that is not a matrix.
method test_METHOD_set_block_SCALAR() {
todo("Tests Needed!");
}
# TODO: We should probably create a few tests to check set_block when using
# various matrix types. For instance,
# NumMatrix2d.set_block(PMCMatrix2D) should work, and vice-versa. We
# can test [almost] all combinations.
}
class Pla::Loader is UnitTest::Loader ;
method order_tests(@tests) {
my $test_method := 'test_METHOD';
my $test_op := 'test_OP';
my $test_vtable := 'test_VTABLE';
my $len := $test_op.length; # The shortest
my %partition;
for <test_METHOD test_OP test_VTABLE MISC> {
%partition{$_} := [ ];
}
for @tests -> $name {
my $name_lc := $name.downcase.substr(0, $len);
if %partition.contains( $name_lc ) {
%partition{$name_lc}.push: $name;
}
else {
%partition<MISC>.push: $name;
}
}
my @result;
for <test_OP test_VTABLE test_METHOD MISC> {
@result.append: %partition{$_}.unsort;
}
@result;
}