/
nummatrix2d.t
246 lines (213 loc) · 7.97 KB
/
nummatrix2d.t
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
class Test::NumMatrix2D is Pla::Matrix::NumericMatrixTest;
# Test boilerplate.
INIT {
use('UnitTest::Testcase');
use('UnitTest::Assertions');
}
MAIN();
sub MAIN() {
my $proto := Opcode::get_root_global(pir::get_namespace__P().get_name);
$proto.suite.run;
}
# Test class methods to help generalize some tests.
method matrix() {
my $m := Parrot::new("NumMatrix2D");
return ($m);
}
method defaultvalue() { 1.0; }
method nullvalue() { 0.0; }
method fancyvalue($idx) {
[5.1, 6.2, 7.3, 8.4][$idx];
}
# TODO: Need to add lots more tests for is_equal. It uses a new float
# comparison algorithm that I want to really exercise.
method test_VTABLE_get_string() {
my $m := self.matrix2x2(1.0, 2.0,
3.0, 4.0);
my $s := pir::set__SP($m);
my $t := pir::sprintf__SSP("\t%S\t%S\n\t%S\t%S\n", [1.0, 2.0, 3.0, 4.0]);
assert_equal($s, $t, "cannot get string");
}
# TODO: Tests for get/set_pmc to prove that we get a Float from it
# $P1 = $P0[0]
# assert_instance_of($P1, "Float", "got Number PMC from linear index")
# Addition Tests
method test_VTABLE_add_NUMMATRIX2D() {
my $m := self.matrix2x2(1.0, 3.0, 2.0, 4.0);
my $n := self.matrix2x2(5.0, 7.0, 6.0, 8.0);
my $o := self.matrix2x2(6.0, 10.0, 8.0, 12.0);
my $p := pir::add__PPP($m, $n);
assert_equal($p, $o, "can add two matrices together of the same size");
}
method test_VTABLE_add_NUMMATRIX2D_SIZEFAIL() {
assert_throws(Exception::OutOfBounds, "error on sizes not equal", {
my $m := self.matrix2x2(1.0, 3.0, 2.0, 4.0);
my $n := self.matrix();
my $o := pir::add__PPP($m, $n);
fail("addition worked, apparently");
});
}
method test_VTABLE_i_add_NUMMATRIX2D() {
my $m := self.matrix2x2(1.0, 3.0, 2.0, 4.0);
my $n := self.matrix2x2(5.0, 7.0, 6.0, 8.0);
my $o := self.matrix2x2(6.0, 10.0, 8.0, 12.0);
Q:PIR {
$P0 = find_lex "$m"
$P1 = find_lex "$n"
$P2 = find_lex "$o"
add $P0, $P1
assert_equal($P0, $P2, "can i_add two matrices together of the same size")
}
}
# Subtraction Tests
method test_VTABLE_subtract_NUMMATRIX2D() {
my $m := self.matrix2x2(1.0, 3.0, 2.0, 4.0);
my $n := self.matrix2x2(5.0, 7.0, 6.0, 8.0);
my $o := self.matrix2x2(-4.0, -4.0, -4.0, -4.0);
my $p := pir::sub__PPP($m, $n);
assert_equal($p, $o, "can add subtract matrices together of the same size");
}
method test_VTABLE_subtract_NUMMATRIX2D_SIZEFAIL() {
assert_throws(Exception::OutOfBounds, "error on sizes not equal", {
my $m := self.matrix2x2(1.0, 3.0, 2.0, 4.0);
my $n := self.matrix();
my $o := pir::sub__PPP($m, $n);
fail("subtraction worked, apparently");
});
}
method test_VTABLE_i_subtract_NUMMATRIX2D() {
my $m := self.matrix2x2(1.0, 3.0, 2.0, 4.0);
my $n := self.matrix2x2(5.0, 7.0, 6.0, 8.0);
my $o := self.matrix2x2(-4.0, -4.0, -4.0, -4.0);
Q:PIR {
$P0 = find_lex "$m"
$P1 = find_lex "$n"
$P2 = find_lex "$o"
sub $P0, $P1
assert_equal($P0, $P2, "can not i_subtract matrices together of the same size")
}
}
# Multiplication Tests
method test_VTABLE_multiply_NUMMATRIX2D() {
my $A := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $B := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $C := self.matrix3x3(30.0, 36.0, 42.0,
66.0, 81.0, 96.0,
102.0, 126.0, 150.0);
my $Y := $A * $B;
assert_equal($C, $Y, "matrix multiply does not do the right thing");
$Y := $B * $A;
assert_equal($C, $Y, "matrix multiply does not do the right thing again");
}
method test_VTABLE_multiply_NUMMATRIX2D_SIZEFAIL() {
assert_throws(Exception::OutOfBounds, "error on sizes not equal", {
my $A := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $B := self.matrix2x2(1.0, 2.0,
3.0, 4.0);
my $C := $A * $B;
});
}
method test_VTABLE_i_multiply_NUMMATRIX2D() {
my $A := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $B := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $C := self.matrix3x3(30.0, 36.0, 42.0,
66.0, 81.0, 96.0,
102.0, 126.0, 150.0);
Q:PIR {
$P0 = find_lex "$A"
$P1 = find_lex "$B"
$P2 = find_lex "$C"
mul $P0, $P1
assert_equal($P0, $P2, "matrix i_multiply does not do the right thing")
}
}
# Block Get/Set method tests
method test_METHOD_set_block() {
my $m := self.matrix2x2(1.0, 2.0,
3.0, 4.0);
my $n := self.matrix3x3(0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0);
my $o := self.matrix3x3(0.0, 1.0, 2.0,
0.0, 3.0, 4.0,
0.0, 0.0, 0.0);
$n.set_block(0, 1, $m);
assert_equal($n, $o, "cannot set block");
# TODO: More tests for this method and coordinate combinations, including
# boundary-checking issues
}
# GEMM tests
method test_METHOD_gemm_aA() {
my $A := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $B := self.matrix3x3(1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0);
my $C := self.matrix3x3(0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0);
my $Y := self.matrix3x3(3.0, 6.0, 9.0,
12.0, 15.0, 18.0,
21.0, 24.0, 27.0);
my $Z := $A.'gemm'(3.0, $A, $B, 0.0, $C);
assert_equal($Y, $Z, "gemm aA does not work");
}
method test_METHOD_gemm_AB() {
my $A := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $B := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $C := self.matrix3x3(0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0);
my $Y := self.matrix3x3(30.0, 36.0, 42.0,
66.0, 81.0, 96.0,
102.0, 126.0, 150.0);
my $Z := $A.'gemm'(1.0, $A, $B, 0.0, $C);
assert_equal($Y, $Z, "gemm AB does not work");
}
method test_METHOD_gemm_aAB() {
my $A := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $B := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $C := self.matrix3x3(0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0);
my $Y := self.matrix3x3(15.0, 18.0, 21.0,
33.0, 40.5, 48.0,
51.0, 63.0, 75.0);
my $Z := $A.'gemm'(0.5, $A, $B, 0.0, $C);
assert_equal($Y, $Z, "gemm aAB does not work");
}
method test_METHOD_gemm_aABbC() {
my $A := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $B := self.matrix3x3(1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0);
my $C := self.matrix3x3(1.0, 1.0, 1.0,
2.0, 2.0, 2.0,
3.0, 3.0, 3.0);
my $Y := self.matrix3x3(5.0, 8.0, 11.0,
13.0, 20.5, 28.0,
21.0, 33.0, 45.0);
my $Z := $A.'gemm'(0.5, $A, $B, -10, $C);
assert_equal($Y, $Z, "gemm aAB does not work");
}