source: tests/designations.cfa

Last change on this file was 85a3806, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

change from printf to sout

  • Property mode set to 100644
File size: 6.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// designations.c --
8//
9// Author           : Rob Schluntz
10// Created On       : Thu Jun 29 15:26:36 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jun 29 11:31:21 2023
13// Update Count     : 28
14//
15
16#include <fstream.hfa>
17
18// Note: this test case has been crafted so that it compiles with both cfa and with gcc without any modifications.
19// In particular, since the syntax for designations in Cforall differs from that of C, preprocessor substitution
20// is used for the designation syntax
21#ifdef __cforall
22#define _ :
23#define AT @
24#else
25#define _ =
26#define AT
27#endif
28
29const int indentAmt = 2;
30void indent( int level ) {
31        sout | wd( level, "" ) | nonl;
32}
33
34// A contains fields with different types (int vs. int *)
35struct A {
36        int x, y;
37        int * ptr;
38};
39void printA( struct A a, int level ) {
40        indent( level );
41        sout | "(A){ " | a.x | a.y | a.ptr | " }";
42}
43
44// B contains struct members
45struct B {
46        struct A a0, a1;
47};
48void printB( struct B b, int level ) {
49        indent( level );
50        sout | "(B){";
51        printA( b.a0, level+indentAmt );
52        printA( b.a1, level+indentAmt );
53        indent( level );
54        sout | "}";
55}
56
57// C contains an array - tests that after 3 ints, the members of B are initialized.
58struct C {
59        int arr[3];
60        struct B b;
61};
62void printC( struct C c, int level ) {
63        indent( level );
64        sout | "(C){";
65        indent( level+indentAmt );
66        sout | "(int[]{ " | c.arr[0] | c.arr[1] | c.arr[2] | " }";
67        printB( c.b, level+indentAmt );
68        indent( level );
69        sout | "}";
70}
71
72// D contains an unnamed aggregate - tests that this doesn't interfere with initialization.
73struct D {
74        struct {
75                int x;
76        };
77};
78void printD( struct D d, int level ) {
79        indent( level);
80        sout | "(D){ " | d.x | "}";
81}
82
83// E tests unions
84union E {
85        struct A a;
86        struct B b;
87        struct C c;
88        struct D d;
89        int i;
90};
91
92struct Fred {
93        double i[3];
94        int j;
95        struct Mary {
96                struct Jane {
97                        double j;
98                } j;
99                double i;
100        } m;
101};
102struct Fred s1 AT= { .m.j _ 3 };
103struct Fred s2 AT= { .i _ { [2] _ 2 } };
104
105int main() {
106        // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero)
107        struct A y0 = {
108                .x _ 2,
109                .y _ 3
110        };
111
112        // simple initializaiton case - initialize all elements explicitly with no designations
113        struct A y1 = {
114                2, 3, 0
115        };
116
117
118        // use designation to move to member y, leaving x default-initialized (zero)
119        struct A y2 = {
120                .y _ 3,
121                0
122        };
123
124#if ERROR
125        struct A yErr0 = {
126                {} // error - empty scalar initializer is illegal
127        };
128#endif
129
130        sout | "=====A=====";
131        printA( y0, 0 );
132        printA( y1, 0 );
133        printA( y2, 0 );
134        sout | "=====A=====" | nl | nl;
135
136        // initialize only first element (z0.a.x), leaving everything else default-initialized (zero), no nested curly-braces
137        struct B z0 = { 5 };
138
139        // some nested curly braces, use designation to 'jump around' within structure, leaving some members default-initialized
140        struct B z1 = {
141                { 3 }, // z1.a0
142                { 4 }, // z1.a1
143                .a0 _ { 5 }, // z1.a0
144                { 6 }, // z1.a1
145                .a0.y _ 2, // z1.a0.y
146                0, // z1.a0.ptr
147        };
148
149        // z2.a0.y and z2.a0.ptr default-initialized, everything else explicit
150        struct B z2 = {
151                { 1 },
152                { 2, 3, 0 }
153        };
154
155        // initialize every member, omitting nested curly braces
156        struct B z3 = {
157                1, 2, 0, 4, 5, 0
158        };
159
160        // no initializer - legal C, but garbage values - don't print this one
161        struct B z4;
162
163        // no curly braces - initialize with object of same type
164        struct B z5 = z2;
165
166        // z6.a0.y and z6.a0.ptr default-initialized, everything else explicit.
167        // no curly braces on z6.a1 initializers
168        struct B z6 = {
169                { 1 },
170                2, 3, 0
171        };
172
173        sout | "=====B=====";
174        printB( z0, 0 );
175        printB( z1, 0 );
176        printB( z2, 0 );
177        printB( z3, 0 );
178        printB( z5, 0 );
179        printB( z6, 0 );
180        sout | "=====B=====" | nl | nl;
181
182        // TODO: what about extra things in a nested init? are empty structs skipped??
183
184        // test that initializing 'past array bound' correctly moves to next member.
185        struct C c1 = {
186                2, 3, 4,  // arr
187                5, 6, 0,  // b.a0
188                7, 8, 0,  // b.a1
189        };
190
191        sout | "=====C=====";
192        printC( c1, 0 );
193        sout | "=====C=====" | nl | nl;
194
195#if ERROR
196        // nested initializer can't refer to same type in C
197        struct C cErr0 = { c1 };
198
199        // must use curly braces to initialize members
200        struct C cErr1 = 2;
201
202        // can't initialize with array compound literal
203        struct C cErr2 = {
204                (int[3]) { 1, 2, 3 }  // error: array initialized from non-constant array expression
205        };
206#endif
207
208#if WARNING
209        // can't initialize array with array - converts to int*
210        int cWarn0_arr[3] = { 1, 2, 3 };
211        struct C cWarn0 = {
212                cWarn0_arr  // warning: initialization makes integer from ptr without cast
213        };
214#endif
215        // array designation
216        int i[2] = { [1] _ 3 };
217        // allowed to have 'too many' initialized lists - essentially they are ignored.
218        int i1 = { 3 };
219
220        // doesn't work yet.
221        // designate unnamed object's members
222        // struct D d = { .x _ 3 };
223#if ERROR
224        struct D d1 = { .y _ 3 };
225#endif
226
227        // simple union initialization - initialized first member (e0.a)
228        union E e0 = {
229                y0
230        };
231
232        // simple union initialization - initializes first member (e1.a) - with nested initializer list
233        union E e1 = {
234                { 2, 3, 0 }
235        };
236
237        // simple union initialization - initializes first member (e2.a) - without nested initializer list
238        union E e2 = {
239                2, 3, 0
240        };
241
242        // move cursor to e4.b.a0.x and initialize until e3.b.a1.ptr inclusive
243        union E e3 = {
244                .b.a0.x _ 2, 3, 0, 5, 6, 0
245        };
246
247        sout | "=====E=====";
248        printA( e0.a, 0 );
249        printA( e1.a, 0 );
250        printA( e2.a, 0 );
251        printB( e3.b, 0 );
252        sout | "=====E=====" | nl | nl;
253
254        // special case of initialization: char[] can be initialized with a string literal
255        const char * str0 = "hello";
256        char str1[] = "hello";
257        const char c2[] = "abc";
258        const char c3[] = { 'a', 'b', 'c' };
259        const char c4[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} };
260
261        // more cases
262
263//      int widths[] = { [3 ... 9] _ 1, [10 ... 99] _ 2, [100] _ 3 };
264//      int widths[] = { [3 ~ 9] _ 1, [10 ~ 99] _ 2, [100] _ 3 };
265        struct point { int x, y; };
266        struct point p = { .y _ 5, .x _ 7 };
267        union foo { int i; double d; };
268        union foo f = { .d _ 4 };
269        int v1, v2, v4;
270        int w[6] = { [1] _ v1, v2, [4] _ v4 };
271        int whitespace[256] = { [' '] _ 1, ['\t'] _ 1, ['\v'] _ 1, ['\f'] _ 1, ['\n'] _ 1, ['\r'] _ 1 };
272        struct point ptarray[10] = { [2].y _ 34, [2].x _ 35, [0].x _ 36 };
273}
274
275// Local Variables: //
276// tab-width: 4 //
277// End: //
Note: See TracBrowser for help on using the repository browser.