-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.odin
298 lines (266 loc) · 7.59 KB
/
tests.odin
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
//+private=file
package mdspan
import test "core:testing"
import "core:math/rand"
import "core:math"
import "core:slice"
import "core:fmt"
import "core:mem"
@test
scalar_construction :: proc (t: ^test.T) {
foo := 10
s := scalar(&foo)
test.expect_value(t, typeid_of(type_of(s)), typeid_of(Span(int, 0)))
test.expect_value(t, s.ravel, &foo)
}
@test
array_construction :: proc (t: ^test.T) {
foos := []int{1, 2, 3, 4, 5}
s := array(foos)
test.expect_value(t, typeid_of(type_of(s)), typeid_of(Span(int, 1)))
test.expect_value(t, s.shape, [1]int{5})
test.expect_value(t, s.ravel, raw_data(foos))
}
@test
create_span_from_slice_exact :: proc(t: ^test.T) {
data := [30]int{}
s := from_slice(data[:], [?]int{2, 3, 5})
test.expect_value(t, s.ravel, raw_data(data[:]))
test.expect_value(t, s.shape, [3]int{2, 3, 5})
}
@test
create_span_from_slice_with_fill :: proc(t: ^test.T) {
data := [30]int{}
s := from_slice(data[:], [?]int{2, -1, 5})
test.expect_value(t, s.ravel, raw_data(data[:]))
test.expect_value(t, s.shape, [3]int{2, 3, 5})
}
@test
to_slice_is_inverse_of_from_slice :: proc(t: ^test.T) {
data := [30]int{}
s := from_slice(data[:], [?]int{2, -1, 5})
recovered := to_slice(s)
test.expect_value(t, raw_data(recovered), raw_data(data[:]))
test.expect_value(t, len(recovered), len(data[:]))
}
@test
out_of_place_transposition_works :: proc (t: ^test.T) {
data := [4 * 5 * 7]int{}
for it, i in &data {it = i}
s := from_slice(data[:], [?]int{4, 5, 7})
// axis reversal
f := transpose(s, context.temp_allocator)
for i in 0 ..< 4 do for j in 0 ..< 5 do for k in 0 ..< 7 {
idx := [?]int{i, j, k}
test.expect(
t,
index(s, idx)^ == index(f, idx.zyx)^,
)
}
// a custom permutation
gperm := [?]int{0, 2, 1}
g := transpose(s, gperm, context.temp_allocator)
for i in 0 ..< 4 do for j in 0 ..< 5 do for k in 0 ..< 7 {
idx := [?]int{i, j, k}
test.expect(
t,
index(s, idx)^ == index(
g,
[?]int{idx[gperm[0]], idx[gperm[1]], idx[gperm[2]]},
)^,
)
}
}
@test
matrix_product_identity_is_neutral :: proc(t: ^test.T) {
id_data := [3*3]int{1, 0, 0, 0, 1, 0, 0, 0, 1}
id := from_slice(id_data[:], [?]int{3, 3})
{
r_data := [3*400]int{}
for it in &r_data { it = rand.int_max(1000) }
r := from_slice(r_data[:], [?]int{3, -1})
s := to_slice(matrix_matrix_product(id, r, context.temp_allocator))
for i in 0 ..< len(s) {
test.expect_value(t, s[i], r_data[i])
}
}
{
r_data := [3]int{}
for it in &r_data { it = rand.int_max(1000) }
r := from_slice(r_data[:], [?]int{3})
s := to_slice(matrix_vector_product(id, r, context.temp_allocator))
for i in 0 ..< len(s) {
test.expect_value(t, s[i], r_data[i])
}
}
{
r_data := [3]int{}
for it in &r_data { it = rand.int_max(1000) }
r := from_slice(r_data[:], [?]int{3})
s := to_slice(vector_matrix_product(r, id, context.temp_allocator))
for i in 0 ..< len(s) {
test.expect_value(t, s[i], r_data[i])
}
}
}
@test
rotations_are_permutations :: proc (t: ^test.T) {
data := [4 * 7 * 10]int{}
for it, i in &data { it = i }
s := from_slice(data[:], [?]int{4, 7, 10})
rotate(&s, array([]int{1, 2, 3, 4, 5, 6, 7}), 0)
// test that all elements are still present
for i in 0 ..< 4 * 7 * 10 {
_, ok := slice.linear_search(data[:], i)
test.expect(t, ok)
}
}
@test
rotate_with_bias :: proc (t: ^test.T) {
initial_ravel := []int{
0, 1,
2, 3,
// ------
4, 5,
6, 7,
}
s := from_slice(initial_ravel, [?]int{2, 2, 2})
{
// rotates cells (axis = 1), the leading axis of `shifts` is matched to the
// second free axis (which is the third axis) of `span` (bias = 1)
f, ok := rotate(span = s, shifts = array([]int{0, 1}), axis = 1, bias = 1)
test.expect(t, ok)
defer destroy(f)
elems := to_slice(f)
expected_ravel := []int{
0, 3,
2, 1,
// ------
4, 7,
6, 5,
}
for it, i in elems {
test.expect_value(t, it, expected_ravel[i])
}
}
{
// rotates rows (axis = 2), the leading axis of `shifts` is matched to the
// second free axis (which is the second axis) of `span` (bias = 1)
f, ok := rotate(span = s, shifts = array([]int{0, 1}), axis = 2, bias = 1)
test.expect(t, ok)
defer destroy(f)
elems := to_slice(f)
expected_ravel := []int{
0, 1,
3, 2,
// ------
4, 5,
7, 6,
}
for it, i in elems {
test.expect_value(t, it, expected_ravel[i])
}
}
{
// rotates cubes (axis = 0), the leading axis of `shifts` is matched to the
// first free axis (which is the second axis) of `span` (bias = 0)
f, ok := rotate(span = s, shifts = array([]int{0, 1}), axis = 0, bias = 0)
defer destroy(f)
test.expect(t, ok)
elems := to_slice(f)
expected_ravel := []int{
0, 1,
6, 7,
// ------
4, 5,
2, 3,
}
for it, i in elems {
test.expect_value(t, it, expected_ravel[i])
}
}
}
@test
transpositions_are_permutations :: proc (t: ^test.T) {
data := [4 * 7 * 10]int{}
for it, i in &data { it = i }
s := from_slice(data[:], [?]int{4, 7, 10})
transpose(&s, [?]int{1, 2, 0})
// test that all elements are still present
for i in 0 ..< 4 * 7 * 10 {
_, ok := slice.linear_search(data[:], i)
test.expect(t, ok)
}
}
@test
basic_reshape_functionality :: proc (t: ^test.T) {
data := [5 * 14] int {}
for it, i in &data { it = i }
s := from_slice(data[:], [?]int{5, 14})
{
f, ok := reshape(s, [?]int{5, 2, -1}, context.temp_allocator)
test.expect(t, ok, "out of place reshape should succeed with compatible fill dimension")
test.expect(t, f.ravel != s.ravel, "out of place reshape makes a new allocation")
for it, i in to_slice(f) {
test.expect(t, it == i, "elements should match")
}
}
{
f, ok := reshape(s, [?]int{5, 3, -1}, context.temp_allocator)
test.expect(t, !ok, "out of place reshape should fail with incompatible fill dimension")
}
{
f, ok := reshape(s, [?]int{3, 7, 6}, context.temp_allocator)
test.expect(t, ok, "out of place reshape should always succeed with fixed shape")
test.expect(t, f.ravel != s.ravel, "out of place reshape makes a new allocation")
for it, i in to_slice(f) {
test.expect(t, it == i % (5 * 14), "elements should repeat")
}
}
{
f, ok := reshape(&s, [?]int{5, 2, -1})
test.expect(t, ok, "in place reshape should succeed with compatible fill dimension")
test.expect(t, f.ravel == s.ravel, "in place reshape shares the allocation")
for it, i in to_slice(f) {
test.expect(t, it == i, "elements should match")
}
}
{
f, ok := reshape(&s, [?]int{3, 7, 6})
test.expect(t, !ok, "in place reshape should fail with larger shape")
}
{
f, ok := reshape(&s, [?]int{3, 7, 3})
test.expect(t, ok, "in place reshape should succeed with smaller shape")
test.expect(t, f.ravel == s.ravel, "in place reshape shares the allocation")
for it, i in to_slice(f) {
test.expect(t, it == i, "elements should match")
}
}
{
empty, ok1 := reshape(&s, [?]int{0, 0})
test.expect(t, ok1, "can reshape into empty array")
f, ok2 := reshape(empty, [?]int {5, 4}, context.temp_allocator)
test.expect(t, ok2, "can reshape from empty array")
for it, i in to_slice(f) {
test.expect(t, it == 0, "elements are zero-initialized if no prototype is available")
}
}
}
@test
sum_reductions_along_axes_commute :: proc (t: ^test.T) {
test_size := 1024
for cols in 1 ..< 1024 {
rows := test_size / cols
data := make([]int, cols * rows); defer delete(data)
for elem in &data { elem = rand.int_max(1024) }
expect := math.sum(data)
arr := from_slice(data, [?]int{rows, cols})
first := reduce_add(arr, 0); defer destroy(first)
second := reduce_add(arr, 1); defer destroy(second)
first_second := reduce_add(first)
second_first := reduce_add(second)
test.expect(t, first_second == expect)
test.expect(t, second_first == expect)
}
}