-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_mdarray.c
83 lines (51 loc) · 1.66 KB
/
test_mdarray.c
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
/* Copyright 2022-2023. Martin Uecker
* All rights reserved. Use of this source code is governed by
* a BSD-style license which can be found in the LICENSE file.
* */
#include <assert.h>
#include "mdarray.h"
mdarray_decl(3, int);
mdarray_decl(3, float);
void fill(mdarray(3, float) arg, float val)
{
auto x = &mdarray_array(arg);
for (size_t i = 0; i < array_lengthof((*x)); i++)
for (size_t j = 0; j < array_lengthof((*x)[0]); j++)
for (size_t k = 0; k < array_lengthof((*x)[0][0]); k++)
(*x)[i][j][k] = val;
}
int main()
{
mdarray(3, int) x = mdarray_init(3, int, (int[4][3][2]){ 0 });
// mdarray_array provides an lvalue with a conventional array type
int (*y)[4][3][2] = &mdarray_array(x);
assert(4 == array_lengthof(*y));
assert(3 == array_lengthof((*y)[0]));
assert(2 == array_lengthof((*y)[0][0]));
mdarray_array(x)[0][1][1] = 3;
(*y)[1][0][1] = 1;
// array_rank retuns the rank of a multi-dimensional array
float u3[3][2][1];
float u2[3][3];
float u1[2];
extern float v;
_Static_assert(3 == array_rank(u3), "");
_Static_assert(2 == array_rank(u2), "");
_Static_assert(1 == array_rank(u1), "");
_Static_assert(0 == array_rank(v), "");
// array_dims retuns the dimensions as a comma-separated list
size_t dims[3] = { array_dims(3, u3) };
assert(dims[2] == 3);
assert(dims[1] == 2);
assert(dims[0] == 1);
extern array_eltype(u1) v;
extern array_nested_eltype(u1) v;
extern array_nested_eltype(u2) v;
extern array_nested_eltype(u3) v;
u3[1][1][0] = 1.;
auto z = mdarray_init(3, float, u3);
mdarray(3, float)* zp = &z;
assert(1. == mdarray_array(*zp)[1][1][0]);
fill(z, 2.);
assert(2. == mdarray_array(*zp)[1][1][0]);
}