]> pd.if.org Git - lice/blob - tests/struct.c
autocommit for files dated 2014-11-17 20:15:26
[lice] / tests / struct.c
1 // structures
2
3 struct A {
4     int a;
5     struct {
6         char b;
7         int  c;
8     } y;
9 } A;
10
11 struct B {
12     int a;
13 } B;
14
15 struct C {
16     int a;
17     int b;
18 } C;
19
20 int main(void) {
21     struct {
22         int a;
23     } a;
24     a.a = 1024;
25     expecti(a.a, 1024);
26
27     struct {
28         int a;
29         int b;
30     } b;
31     b.a = 1024;
32     b.b = 2048;
33     expecti(b.a, 1024);
34     expecti(b.b, 2048);
35
36     struct {
37         int a;
38         struct {
39             char b;
40             int  c;
41         } b;
42     } c;
43     c.a   = 1024;
44     c.b.b = 32;
45     c.b.c = 2048;
46     expecti(c.a,   1024);
47     expecti(c.b.b, 32);
48     expecti(c.b.c, 2048);
49
50     struct name1 {
51         int a;
52         struct {
53             char b;
54             int  c;
55         } b;
56     };
57     struct name1 d;
58     d.a   = 16;
59     d.b.b = 32;
60     d.b.c = 64;
61     expecti(d.a,   16);
62     expecti(d.b.b, 32);
63     expecti(d.b.c, 64);
64
65     struct name2 {
66         int a;
67         int b;
68     } e;
69     struct name2 *f = &e;
70
71     // obj->ptr
72     e.a = 128;
73     expecti((*f).a, 128);
74
75     // ptr->obj
76     (*f).a = 256;
77     expecti(e.a, 256);
78
79     // again
80     e.b = 64;
81     expecti((*f).b, 64);
82
83     (*f).b = 128;
84     expecti(e.b, 128);
85
86     // over read?
87     struct {
88         int a[3];
89         int b[3];
90     } g;
91     g.a[0] = 1024;
92     g.b[1] = 2048;
93     expecti(g.a[0], 1024);
94     expecti(g.b[1], 2048);
95     expecti(g.a[4], 2048); // &g.a[4] == &g.b[0]
96
97     A.a   = 64;
98     A.y.b = 65;
99     A.y.c = 256;
100     expecti(A.a,   64);
101     expecti(A.y.b, 65);
102     expecti(A.y.c, 256);
103
104     struct B *h = &B;
105     B.a = 128;
106     expecti((*h).a, 128);
107     expecti(B.a,    128);
108     expecti(h->a,   128);
109
110     h->a = 256;
111     expecti((*h).a, 256);
112     expecti(B.a,    256);
113     expecti(h->a,   256);
114
115     struct C i[3];
116     i[0].a = 32;
117     expecti(i[0].a, 32);
118     i[0].b = 64;
119     expecti(i[0].b, 64);
120     i[1].b = 128;
121     expecti(i[1].b, 128);
122     int *j = i;
123     expecti(j[3], 128); // &j[3] == &i[1].b
124
125     struct { char c; } k = { 'a' };
126     expecti(k.c, 'a');
127
128     struct { int a[3]; } l = { { 1, 2, 3 } };
129     expecti(l.a[0], 1);
130     expecti(l.a[1], 2);
131     expecti(l.a[2], 3);
132
133     // unnamed shit
134     struct {
135         union {
136             struct {
137                 int x;
138                 int y;
139             };
140             struct {
141                 char c[8];
142             };
143         };
144     } m;
145     m.x = 10;
146     m.y = 20;
147     expecti(m.c[0], 10);
148     expecti(m.c[4], 20);
149
150     // structure copy via assignment
151     struct {
152         int a;
153         int b;
154         int c;
155     } X, Y, Z;
156     X.a = 8;
157     X.b = 16;
158     X.c = 32;
159     Y = X;
160     Z.a = 64;
161     Z.b = 128;
162     Z.c = 256;
163     expecti(Y.a, 8);
164     expecti(Y.b, 16);
165     expecti(Y.c, 32);
166     Y = Z;
167     expecti(Y.a, 64);
168     expecti(Y.b, 128);
169     expecti(Y.c, 256);
170
171     // arrows
172     struct cell {
173         int value;
174         struct cell *next;
175     };
176     struct cell aa = { 10, 0 };
177     struct cell bb = { 20, &aa };
178     struct cell cc = { 30, &bb };
179     struct cell *dd = &cc;
180
181     expecti(cc.value,              30);
182     expecti(dd->value,             30);
183     expecti(dd->next->value,       20);
184     expecti(dd->next->next->value, 10);
185
186     dd->value             = 16;
187     dd->next->value       = 32;
188     dd->next->next->value = 64;
189     expecti(dd->value,             16);
190     expecti(dd->next->value,       32);
191     expecti(dd->next->next->value, 64);
192
193     // addressing
194     struct super {
195         int a;
196         struct {
197             int b;
198         } y;
199     } poke = { 99, 100 };
200
201     int *poke1 = &poke.a;
202     int *poke2 = &poke.y.b;
203
204     expecti(*poke1,     99);
205     expecti(*poke2,     100);
206     expecti(*&poke.a,   99);
207     expecti(*&poke.y.b, 100);
208
209     struct super *inward = &poke;
210     int *poke3 = &inward->a;
211     int *poke4 = &inward->y.b;
212
213     expecti(*poke3,        99);
214     expecti(*poke4,        100);
215     expecti(*&inward->a,   99);
216     expecti(*&inward->y.b, 100);
217
218     // incomplete in the opposite direction
219     struct incomplete1;
220     struct incomplete2 {
221         struct incomplete1 *next;
222     };
223     struct incomplete1 {
224         int value;
225     };
226
227     struct incomplete1 in1 = { 3 };
228     struct incomplete2 in2 = { &in1 };
229
230     expecti(in2.next->value, 3);
231
232     struct {
233         int x;
234     } __a = {
235         1024
236     }, *__b = &__a;
237     int *__c = &__b->x;
238
239     expecti(*__c, 1024);
240
241
242     // bit fields
243     union {
244         int i;
245         struct {
246             int a: 5;
247             int b: 5;
248         };
249     } bitfield;
250     bitfield.i = 0;
251     bitfield.a = 20;
252     bitfield.b = 15;
253     expecti(bitfield.i, 500); // (15 << 5) + 20 == 500
254
255     struct {
256         char a:4;
257         char b:4;
258     } bitfield2 = { 5, 10 };
259
260     expecti(bitfield2.a, 5);
261     expecti(bitfield2.b, 10);
262
263     union {
264         int  a: 10;
265         char b: 5;
266         char c: 5;
267     } bitfield3;
268
269     bitfield3.a = 2;
270     expecti(bitfield3.a, 2);
271     expecti(bitfield3.b, 2);
272     expecti(bitfield3.c, 2);
273
274     struct __empty {};
275     expecti(sizeof(struct __empty), 0);
276
277     // FAMS
278     struct __fam0 { int a, b[]; };
279     struct __fam1 { int a, b[0]; };
280
281     expecti(sizeof(struct __fam0), 4);
282     expecti(sizeof(struct __fam1), 4);
283
284     struct __gccfam { int a[0]; };
285     expecti(sizeof(struct __gccfam), 0);
286
287     struct __famoverwrite { int a, b[]; };
288     struct __famoverwrite OV = { 1, 2, 3, 4, 5 };
289
290     expecti(OV.b[0], 2);
291     expecti(OV.b[1], 3);
292     expecti(OV.b[2], 4);
293     expecti(OV.b[3], 5);
294
295     return 0;
296 }