source: tests/designations.cfa@ 8705a11

Last change on this file since 8705a11 was a465d751, checked in by Andrew Beach <ajbeach@…>, 8 months ago

In the 'designations': Instead of leaving some variables unused, print more of them for further checking. (All passed.)

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