source: tests/array-collections/array-sbscr-types.cfa@ e0330d2c

Last change on this file since e0330d2c was 82ff201a, checked in by Peter A. Buhr <pabuhr@…>, 14 months ago

add #include <assert.h>, as no longer in array.hfa

  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include <collections/array.hfa>
2
3// Shows support for many required ways a user can index into a new array.
4//
5// A successful run of this test on 32 bit is necessary, before concluding
6// that a relevant change has good quality. Even though the test has no
7// differentiated 64/32-bit versions.
8//
9// Repetition, within this test, beween indexing directly into an `array(...)`
10// and indexing into a `A`, as in `forall(A...|ar(A...))`, represents indexing
11// into a (statically programmer-known) contiguous view, and a (potentially)
12// noncontiguous view, respectively. Users obtain noncontiguous views by
13// slicing or transposing higher-dimensional arrays. The limited uses of
14// `a[..., all, ...]` within this test create such situations. Working via
15// the `ar` trait is the first of two ways that users depend on the array
16// implementation tunneling subscript operators through the CFA assertion
17// system.
18//
19// This test uses the `a[i,j]` form for subscriping higher-dimensional arrays,
20// which is the "new" form, compared with the C-style `a[i][j]` form. The
21// "new" subscripting form is the second of two ways that users depend on the
22// array implementation tunneling subscript operators through the CFA
23// assertion system.
24//
25// This test covers types and syntactic forms that can convey a numeric value
26// to `a[-]` or `a[-,-,-]`. The array-md-sbscr-cases test covers combinations
27// of `a[i][j,k]` vs `a[i,j,k]` and `a[all,3][42]` vs `a[42,3]`, though
28// generally using ptrdiff_t-typed variables to convey numeric values.
29
30#include <assert.h>
31
32#define show( expr ) printf( "%.1f\n", expr )
33
34#define singleDimTestBody(testName) { \
35 \
36 printf(testName "\n\n"); \
37 \
38 assert( 3 < N ); \
39 \
40 show( a[i1] ); \
41 show( a[i2] ); \
42 printf("\n"); \
43 \
44 for( i_dynbounded; N ) show( a[i_dynbounded] ); \
45 printf("\n"); \
46 \
47 for( i_stabounded; 4 ) show( a[i_stabounded] ); \
48 printf("\n"); \
49}
50
51forall( [N] )
52void test_common_arg_types(array(float, N) & a, ptrdiff_t i1, size_t i2)
53 singleDimTestBody("Simple array")
54
55forall( [N], A& | ar(A, float, N) )
56void test_common_arg_types__via_trait(A & a, ptrdiff_t i1, size_t i2)
57 singleDimTestBody("Via trait")
58
59void do1dimTest() {
60 array(float, 5) a;
61 a[0] = 100.0;
62 a[1] = 100.1;
63 a[2] = 100.2;
64 a[3] = 100.3;
65 a[4] = 100.4;
66
67 test_common_arg_types(a, 3, 3);
68 test_common_arg_types__via_trait(a, 3, 3);
69}
70
71#define multiDimTestBody(testName) { \
72 \
73 printf(testName "\n\n"); \
74 \
75 assert( 3 < M ); \
76 assert( 3 < N ); \
77 \
78 show(( a[x1,x1] )); \
79 show(( a[x1,x2] )); \
80 show(( a[x2,x1] )); \
81 show(( a[x2,x2] )); \
82 printf("\n"); \
83 \
84 for( i_dynbounded; M ) show(( a[i_dynbounded, 3] )); \
85 printf("\n"); \
86 \
87 for( i_stabounded; 4 ) show(( a[i_stabounded, 3] )); \
88 printf("\n"); \
89 \
90 for( j_dynbounded; N ) show(( a[3, j_dynbounded] )); \
91 printf("\n"); \
92 \
93 for( j_stabounded; 4 ) show(( a[3, j_stabounded] )); \
94 printf("\n"); \
95}
96
97forall( [M], [N] )
98void test_common_arg_types__md(array(float, M, N) & a, ptrdiff_t x1, size_t x2)
99 multiDimTestBody("Simple array, multidim")
100
101
102forall( [M], [N], A_outer &, A_inner & | ar(A_outer, A_inner, M) | ar(A_inner, float, N) )
103void test_common_arg_types__md__via_trait(A_outer & a, ptrdiff_t x1, size_t x2)
104 multiDimTestBody("Via trait, multidim")
105
106
107void doMdimTest() {
108
109 array(float, 5, 4) b;
110 b[ix0,ix0] = 0.0; b[ix0,1] = 0.1; b[ix0,2] = 0.2; b[ix0,3] = 0.3;
111 b[ 1,ix0] = 1.0; b[ 1,1] = 1.1; b[ 1,2] = 1.2; b[ 1,3] = 1.3;
112 b[ 2,ix0] = 2.0; b[ 2,1] = 2.1; b[ 2,2] = 2.2; b[ 2,3] = 2.3;
113 b[ 3,ix0] = 3.0; b[ 3,1] = 3.1; b[ 3,2] = 3.2; b[ 3,3] = 3.3;
114 b[ 4,ix0] = 4.0; b[ 4,1] = 4.1; b[ 4,2] = 4.2; b[ 4,3] = 4.3;
115
116 test_common_arg_types__md(b, 3, 3);
117 test_common_arg_types__md__via_trait(b, 3, 3);
118 printf("Transposed, ");
119 test_common_arg_types__md__via_trait(b[all], 3, 3);
120
121 printf("Slice giving ");
122 test_common_arg_types(b[2], 3, 3);
123
124 printf("Same slice ");
125 test_common_arg_types__via_trait(b[2], 3, 3);
126
127 printf("Strided slice ");
128 test_common_arg_types__via_trait(b[all,2], 3, 3);
129}
130
131int main() {
132
133 // can't be inlined in same func due to Trac #175.
134 do1dimTest();
135 doMdimTest();
136}
Note: See TracBrowser for help on using the repository browser.