source: tests/designations.cfa@ 3bd9508

Last change on this file since 3bd9508 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
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
[2e0bb92]12// Last Modified On : Tue Jan 28 21:49:50 2025
13// Update Count : 82
[bb1cd95]14//
15
16// Note: this test case has been crafted so that it compiles with both cfa and with gcc without any modifications.
[2e0bb92]17#include <stdio.h>
[a465d751]18
[0cf5b79]19#ifdef __cforall
[8f2f185]20#define AT @
[bb1cd95]21#else
[8f2f185]22#define AT
[bb1cd95]23#endif
24
[2e0bb92]25#pragma GCC diagnostic ignored "-Wmissing-braces"
26#pragma GCC diagnostic ignored "-Woverride-init"
27#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
28
29
[bb1cd95]30const int indentAmt = 2;
[8f2f185]31void indent( int level ) {
[2e0bb92]32 printf( "%*s", level, "" );
[bb1cd95]33}
34
35// A contains fields with different types (int vs. int *)
36struct A {
[62423350]37 int x, y;
38 int * ptr;
[bb1cd95]39};
[8f2f185]40void printA( struct A a, int level ) {
41 indent( level );
[2e0bb92]42 printf( "(A){ %d %d %p }\n", a.x, a.y, a.ptr );
[bb1cd95]43}
44
45// B contains struct members
46struct B {
[62423350]47 struct A a0, a1;
[bb1cd95]48};
[8f2f185]49void printB( struct B b, int level ) {
50 indent( level );
[2e0bb92]51 printf( "(B){\n" );
[8f2f185]52 printA( b.a0, level+indentAmt );
53 printA( b.a1, level+indentAmt );
54 indent( level );
[2e0bb92]55 printf( "}\n" );
[bb1cd95]56}
57
58// C contains an array - tests that after 3 ints, the members of B are initialized.
59struct C {
[62423350]60 int arr[3];
61 struct B b;
[bb1cd95]62};
[8f2f185]63void printC( struct C c, int level ) {
64 indent( level );
[2e0bb92]65 printf( "(C){\n" );
[8f2f185]66 indent( level+indentAmt );
[2e0bb92]67 printf( "(int[]{ %d %d %d }\n", c.arr[0], c.arr[1], c.arr[2] );
[8f2f185]68 printB( c.b, level+indentAmt );
69 indent( level );
[2e0bb92]70 printf( "}\n" );
[bb1cd95]71}
72
73// D contains an unnamed aggregate - tests that this doesn't interfere with initialization.
74struct D {
[62423350]75 struct {
76 int x;
77 };
[bb1cd95]78};
[8f2f185]79void printD( struct D d, int level ) {
80 indent( level);
[2e0bb92]81 printf( "(D){ %d }\n", d.x );
[bb1cd95]82}
83
84// E tests unions
85union E {
[62423350]86 struct A a;
87 struct B b;
88 struct C c;
89 struct D d;
90 int i;
[bb1cd95]91};
92
[a04ce4d]93struct Fred {
[85a3806]94 double i[3];
95 int j;
96 struct Mary {
97 struct Jane {
98 double j;
99 } j;
100 double i;
101 } m;
[a04ce4d]102};
[2e0bb92]103struct Fred s1 AT= { .m.j = { 3 } };
[c565d68]104struct Fred s2 AT= { .i = { [2] = 2 } };
[a04ce4d]105
[bb1cd95]106int main() {
[62423350]107 // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero)
108 struct A y0 = {
[c565d68]109 .x = 2,
110 .y = 3
[62423350]111 };
[bb1cd95]112
[62423350]113 // simple initializaiton case - initialize all elements explicitly with no designations
114 struct A y1 = {
115 2, 3, 0
116 };
[bb1cd95]117
118
[62423350]119 // use designation to move to member y, leaving x default-initialized (zero)
120 struct A y2 = {
[c565d68]121 .y = 3,
[62423350]122 0
123 };
[bb1cd95]124
125#if ERROR
[62423350]126 struct A yErr0 = {
127 {} // error - empty scalar initializer is illegal
128 };
[bb1cd95]129#endif
130
[2e0bb92]131 printf( "=====A=====\n" );
[8f2f185]132 printA( y0, 0 );
133 printA( y1, 0 );
134 printA( y2, 0 );
[2e0bb92]135 printf( "=====A=====\n\n" );
[62423350]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
[c565d68]144 .a0 = { 5 }, // z1.a0
[62423350]145 { 6 }, // z1.a1
[c565d68]146 .a0.y = 2, // z1.a0.y
[62423350]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;
[a465d751]163 // Mark this as unused since it can't be printed.
164 (void)z4;
[62423350]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
[2e0bb92]176 printf( "=====B=====\n" );
[8f2f185]177 printB( z0, 0 );
178 printB( z1, 0 );
179 printB( z2, 0 );
180 printB( z3, 0 );
181 printB( z5, 0 );
182 printB( z6, 0 );
[2e0bb92]183 printf( "=====B=====\n\n" );
[62423350]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
[2e0bb92]194 printf( "=====C=====\n" );
[8f2f185]195 printC( c1, 0 );
[2e0bb92]196 printf( "=====C=====\n\n" );
[bb1cd95]197
198#if ERROR
[62423350]199 // nested initializer can't refer to same type in C
200 struct C cErr0 = { c1 };
[bb1cd95]201
[62423350]202 // must use curly braces to initialize members
203 struct C cErr1 = 2;
[bb1cd95]204
[62423350]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 };
[bb1cd95]209#endif
210
211#if WARNING
[62423350]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 };
[bb1cd95]217#endif
[a04ce4d]218 // array designation
[c565d68]219 int i[2] = { [1] = 3 };
[2e0bb92]220
[62423350]221 // allowed to have 'too many' initialized lists - essentially they are ignored.
222 int i1 = { 3 };
[bb1cd95]223
[a465d751]224 printf( "====ARR====\n" );
225 printf( "[%d, %d]\n", i[0], i[1] );
226 printf( "%d\n", i1 );
227 printf( "====ARR====\n\n" );
228
[62423350]229 // doesn't work yet.
230 // designate unnamed object's members
[c565d68]231 // struct D d = { .x = 3 };
[bb1cd95]232#if ERROR
[c565d68]233 struct D d1 = { .y = 3 };
[bb1cd95]234#endif
235
[62423350]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 = {
[c565d68]253 .b.a0.x = 2, 3, 0, 5, 6, 0
[62423350]254 };
255
[2e0bb92]256 printf( "=====E=====\n" );
[8f2f185]257 printA( e0.a, 0 );
258 printA( e1.a, 0 );
259 printA( e2.a, 0 );
260 printB( e3.b, 0 );
[2e0bb92]261 printf( "=====E=====\n\n" );
[62423350]262
263 // special case of initialization: char[] can be initialized with a string literal
264 const char * str0 = "hello";
265 char str1[] = "hello";
[8f2f185]266 const char c2[] = "abc";
267 const char c3[] = { 'a', 'b', 'c' };
[a465d751]268 const char c4[][2] = { { 'a', 'b' }, { 'c', 'd' }, { 'c', 'd' } };
[2e0bb92]269 const char ch = c4[0][0];
[8f2f185]270
[a465d751]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
[8f2f185]282 // more cases
283
[c565d68]284// int widths[] = { [3 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
285// int widths[] = { [3 ~ 9] = 1, [10 ~ 99] = 2, [100] = 3 };
[8f2f185]286 struct point { int x, y; };
[c565d68]287 struct point p = { .y = 5, .x = 7 };
[8f2f185]288 union foo { int i; double d; };
[c565d68]289 union foo f = { .d = 4 };
[a465d751]290 int v1 = 1, v2 = 2, v4 = 4;
[c565d68]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 };
[a465d751]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" );
[bb1cd95]302}
303
304// Local Variables: //
305// tab-width: 4 //
306// End: //
Note: See TracBrowser for help on using the repository browser.