source: tests/designations.cfa@ 0bf03ba2

Last change on this file since 0bf03ba2 was c565d68, checked in by Peter A. Buhr <pabuhr@…>, 11 months ago

change CFA tests to use C designator syntax

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