source: tests/array-container/array-sbscr-types.cfa @ 4eebbcc

ADTast-experimental
Last change on this file since 4eebbcc was a5e2682, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

Improve new-array subscripting to cover missing cases.

Missing cases include acknowledging a[0] and a[1] as required uses; therefore, give them
special overloads. Add comments to explain why.

Missing cases include test coverage of these overloads (where tests actually fail when an overload
is missing). Perhaps surprisingly, you need to use a lot of features at the same time for the need
for several overloads to obtain; make test coverage go there.

Also, switch thesis's demo of mutidimensional transposing/slicing to use (improved) libcfa ar
(pseudo-)trait, in place of former ix trait. Therefore, also port this demo to new array
syntax. (No changes to thesis discussion around this demo yet; these are still pending.) All
in, cause this thesis demo to be runnable again.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include <containers/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
31#define show( expr ) printf( "%.1f\n", expr )
32
33#define singleDimTestBody(testName) {                      \
34                                                           \
35    printf(testName "\n\n");                               \
36                                                           \
37    assert( 3 < N );                                       \
38                                                           \
39    show( a[i1] );                                         \
40    show( a[i2] );                                         \
41    printf("\n");                                          \
42                                                           \
43    for( i_dynbounded; N ) show( a[i_dynbounded] );        \
44    printf("\n");                                          \
45                                                           \
46    for( i_stabounded; 4 ) show( a[i_stabounded] );        \
47    printf("\n");                                          \
48}
49
50forall( [N] )
51void test_common_arg_types(array(float, N) & a, ptrdiff_t i1, size_t i2)
52    singleDimTestBody("Simple array")
53
54forall( [N], A& | ar(A, float, N) )
55void test_common_arg_types__via_trait(A & a, ptrdiff_t i1, size_t i2)
56    singleDimTestBody("Via trait")
57
58void do1dimTest() {
59    array(float, 5) a;
60    a[0] = 100.0;
61    a[1] = 100.1;
62    a[2] = 100.2;
63    a[3] = 100.3;
64    a[4] = 100.4;
65
66    test_common_arg_types(a, 3, 3);
67    test_common_arg_types__via_trait(a, 3, 3);
68}
69
70#define multiDimTestBody(testName) {                         \
71                                                             \
72    printf(testName "\n\n");                                 \
73                                                             \
74    assert( 3 < M );                                         \
75    assert( 3 < N );                                         \
76                                                             \
77    show(( a[x1,x1] ));                                      \
78    show(( a[x1,x2] ));                                      \
79    show(( a[x2,x1] ));                                      \
80    show(( a[x2,x2] ));                                      \
81    printf("\n");                                            \
82                                                             \
83    for( i_dynbounded; M ) show(( a[i_dynbounded, 3] ));     \
84    printf("\n");                                            \
85                                                             \
86    for( i_stabounded; 4 ) show(( a[i_stabounded, 3] ));     \
87    printf("\n");                                            \
88                                                             \
89    for( j_dynbounded; N ) show(( a[3, j_dynbounded] ));     \
90    printf("\n");                                            \
91                                                             \
92    for( j_stabounded; 4 ) show(( a[3, j_stabounded] ));     \
93    printf("\n");                                            \
94}
95
96forall( [M], [N] )
97void test_common_arg_types__md(array(float, M, N) & a, ptrdiff_t x1, size_t x2)
98    multiDimTestBody("Simple array, multidim")
99
100
101forall( [M], [N], A_outer &, A_inner & | ar(A_outer, A_inner, M) | ar(A_inner, float, N) )
102void test_common_arg_types__md__via_trait(A_outer & a, ptrdiff_t x1, size_t x2)
103    multiDimTestBody("Via trait, multidim")
104
105
106void doMdimTest() {
107
108    array(float, 5, 4) b;
109    b[ix0,ix0] = 0.0; b[ix0,1] = 0.1; b[ix0,2] = 0.2; b[ix0,3] = 0.3;
110    b[  1,ix0] = 1.0; b[  1,1] = 1.1; b[  1,2] = 1.2; b[  1,3] = 1.3;
111    b[  2,ix0] = 2.0; b[  2,1] = 2.1; b[  2,2] = 2.2; b[  2,3] = 2.3;
112    b[  3,ix0] = 3.0; b[  3,1] = 3.1; b[  3,2] = 3.2; b[  3,3] = 3.3;
113    b[  4,ix0] = 4.0; b[  4,1] = 4.1; b[  4,2] = 4.2; b[  4,3] = 4.3;
114
115    test_common_arg_types__md(b, 3, 3);
116    test_common_arg_types__md__via_trait(b, 3, 3);
117    printf("Transposed, ");
118    test_common_arg_types__md__via_trait(b[all], 3, 3);
119
120    printf("Slice giving ");
121    test_common_arg_types(b[2], 3, 3);
122
123    printf("Same slice ");
124    test_common_arg_types__via_trait(b[2], 3, 3);
125
126    printf("Strided slice ");
127    test_common_arg_types__via_trait(b[all,2], 3, 3);
128}
129
130int main() {
131
132    // can't be inlined in same func due to Trac #175.
133    do1dimTest();
134    doMdimTest();
135}
Note: See TracBrowser for help on using the repository browser.