source: tests/designations.cfa @ 075c6d5

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

change from printf to sout

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[bb1cd95]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
[a04ce4d]11// Last Modified By : Peter A. Buhr
[85a3806]12// Last Modified On : Thu Jun 29 11:31:21 2023
13// Update Count     : 28
[bb1cd95]14//
15
[85a3806]16#include <fstream.hfa>
17
[bb1cd95]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
[0cf5b79]21#ifdef __cforall
[8f2f185]22#define _ :
23#define AT @
[bb1cd95]24#else
[8f2f185]25#define _ =
26#define AT
[bb1cd95]27#endif
28
29const int indentAmt = 2;
[8f2f185]30void indent( int level ) {
[85a3806]31        sout | wd( level, "" ) | nonl;
[bb1cd95]32}
33
34// A contains fields with different types (int vs. int *)
35struct A {
[62423350]36        int x, y;
37        int * ptr;
[bb1cd95]38};
[8f2f185]39void printA( struct A a, int level ) {
40        indent( level );
[85a3806]41        sout | "(A){ " | a.x | a.y | a.ptr | " }";
[bb1cd95]42}
43
44// B contains struct members
45struct B {
[62423350]46        struct A a0, a1;
[bb1cd95]47};
[8f2f185]48void printB( struct B b, int level ) {
49        indent( level );
[85a3806]50        sout | "(B){";
[8f2f185]51        printA( b.a0, level+indentAmt );
52        printA( b.a1, level+indentAmt );
53        indent( level );
[85a3806]54        sout | "}";
[bb1cd95]55}
56
57// C contains an array - tests that after 3 ints, the members of B are initialized.
58struct C {
[62423350]59        int arr[3];
60        struct B b;
[bb1cd95]61};
[8f2f185]62void printC( struct C c, int level ) {
63        indent( level );
[85a3806]64        sout | "(C){";
[8f2f185]65        indent( level+indentAmt );
[85a3806]66        sout | "(int[]{ " | c.arr[0] | c.arr[1] | c.arr[2] | " }";
[8f2f185]67        printB( c.b, level+indentAmt );
68        indent( level );
[85a3806]69        sout | "}";
[bb1cd95]70}
71
72// D contains an unnamed aggregate - tests that this doesn't interfere with initialization.
73struct D {
[62423350]74        struct {
75                int x;
76        };
[bb1cd95]77};
[8f2f185]78void printD( struct D d, int level ) {
79        indent( level);
[85a3806]80        sout | "(D){ " | d.x | "}";
[bb1cd95]81}
82
83// E tests unions
84union E {
[62423350]85        struct A a;
86        struct B b;
87        struct C c;
88        struct D d;
89        int i;
[bb1cd95]90};
91
[a04ce4d]92struct Fred {
[85a3806]93        double i[3];
94        int j;
95        struct Mary {
96                struct Jane {
97                        double j;
98                } j;
99                double i;
100        } m;
[a04ce4d]101};
[8f2f185]102struct Fred s1 AT= { .m.j _ 3 };
103struct Fred s2 AT= { .i _ { [2] _ 2 } };
[a04ce4d]104
[bb1cd95]105int main() {
[62423350]106        // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero)
107        struct A y0 = {
[8f2f185]108                .x _ 2,
109                .y _ 3
[62423350]110        };
[bb1cd95]111
[62423350]112        // simple initializaiton case - initialize all elements explicitly with no designations
113        struct A y1 = {
114                2, 3, 0
115        };
[bb1cd95]116
117
[62423350]118        // use designation to move to member y, leaving x default-initialized (zero)
119        struct A y2 = {
[8f2f185]120                .y _ 3,
[62423350]121                0
122        };
[bb1cd95]123
124#if ERROR
[62423350]125        struct A yErr0 = {
126                {} // error - empty scalar initializer is illegal
127        };
[bb1cd95]128#endif
129
[85a3806]130        sout | "=====A=====";
[8f2f185]131        printA( y0, 0 );
132        printA( y1, 0 );
133        printA( y2, 0 );
[85a3806]134        sout | "=====A=====" | nl | nl;
[62423350]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
[8f2f185]143                .a0 _ { 5 }, // z1.a0
[62423350]144                { 6 }, // z1.a1
[8f2f185]145                .a0.y _ 2, // z1.a0.y
[62423350]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
[85a3806]173        sout | "=====B=====";
[8f2f185]174        printB( z0, 0 );
175        printB( z1, 0 );
176        printB( z2, 0 );
177        printB( z3, 0 );
178        printB( z5, 0 );
179        printB( z6, 0 );
[85a3806]180        sout | "=====B=====" | nl | nl;
[62423350]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
[85a3806]191        sout | "=====C=====";
[8f2f185]192        printC( c1, 0 );
[85a3806]193        sout | "=====C=====" | nl | nl;
[bb1cd95]194
195#if ERROR
[62423350]196        // nested initializer can't refer to same type in C
197        struct C cErr0 = { c1 };
[bb1cd95]198
[62423350]199        // must use curly braces to initialize members
200        struct C cErr1 = 2;
[bb1cd95]201
[62423350]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        };
[bb1cd95]206#endif
207
208#if WARNING
[62423350]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        };
[bb1cd95]214#endif
[a04ce4d]215        // array designation
[8f2f185]216        int i[2] = { [1] _ 3 };
[62423350]217        // allowed to have 'too many' initialized lists - essentially they are ignored.
218        int i1 = { 3 };
[bb1cd95]219
[62423350]220        // doesn't work yet.
221        // designate unnamed object's members
[8f2f185]222        // struct D d = { .x _ 3 };
[bb1cd95]223#if ERROR
[8f2f185]224        struct D d1 = { .y _ 3 };
[bb1cd95]225#endif
226
[62423350]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 = {
[8f2f185]244                .b.a0.x _ 2, 3, 0, 5, 6, 0
[62423350]245        };
246
[85a3806]247        sout | "=====E=====";
[8f2f185]248        printA( e0.a, 0 );
249        printA( e1.a, 0 );
250        printA( e2.a, 0 );
251        printB( e3.b, 0 );
[85a3806]252        sout | "=====E=====" | nl | nl;
[62423350]253
254        // special case of initialization: char[] can be initialized with a string literal
255        const char * str0 = "hello";
256        char str1[] = "hello";
[8f2f185]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 };
[bb1cd95]273}
274
275// Local Variables: //
276// tab-width: 4 //
277// End: //
Note: See TracBrowser for help on using the repository browser.