]> git.sur5r.net Git - cc65/blob - test/val/anon-struct2.c
Adjusted to the current multiline-comment style.
[cc65] / test / val / anon-struct2.c
1 /*
2   !!DESCRIPTION!! Make sure that the fields of anonymous structs/unions can be reached properly.
3   !!ORIGIN!!      cc65 regression tests
4   !!LICENCE!!     Public Domain
5   !!AUTHOR!!      Greg King
6 */
7
8 #include <stddef.h>
9 #include <stdio.h>
10
11 static unsigned char fails = 0;
12
13 typedef struct {
14     short s1;
15     struct {
16         char c1;
17         int i1;
18         long l1;
19     };
20     char c2;
21 } s1_t;
22
23 typedef struct {
24     char c2;
25     union {
26         int i1;
27         char c1;
28         long l1;
29     };
30     short s1;
31 } s2_t;
32
33 typedef union {
34     short s1;
35     struct {
36         int i1;
37         long l1;
38         char c1;
39     };
40     char c2;
41 } u1_t;
42
43 typedef union {
44     short s1;
45     union {
46         long l1;
47         char c1;
48         int i1;
49     };
50     char c2;
51 } u2_t;
52
53 typedef struct {
54     union {
55         short s1;
56         struct {
57             int i1;
58             long l1;
59             char c1;
60         };
61         char c2;
62     };
63     short s2;
64 } s3_t;
65
66 static s1_t s1;
67 static s2_t s2;
68 static u1_t u1;
69 static u2_t u2;
70
71 static long l2;
72 static int i2;
73
74 /* We use "variables" in the comparisons, so that we can avoid "constant
75 ** comparison" and "Unreachable code" warnings (the second one currently
76 ** can't be suppressed).
77 */
78
79 static size_t const zero = 0;
80 static size_t const one = 1;
81 static size_t const three = 3;
82 static size_t const five = 5;
83 static size_t const six = 6;
84 static size_t const seven = 7;
85 static size_t const nine = 9;
86
87 int main(void)
88 {
89     /* Can cc65 see the names of members of anonymous structs/unions? */
90
91     l2 = s1.l1;
92     l2 = s2.l1;
93     l2 = u1.l1;
94     l2 = u2.l1;
95
96     i2 = s1.c1;
97     i2 = s1.c2;
98     i2 = s2.c1;
99     i2 = s2.c2;
100     i2 = u1.c1;
101     i2 = u1.c2;
102     i2 = u2.c1;
103     i2 = u2.c2;
104
105     /* Does cc65 use the correct offsets of
106     ** the members of anonymous structs/unions?
107     */
108
109     if (offsetof(s1_t, i1) != three) {
110         printf("The offset of s1.i1 is %u; it should be 3.\n", offsetof(s1_t, i1));
111         ++fails;
112     }
113     if (offsetof(s2_t, l1) != one) {
114         printf("The offset of s2.l1 is %u; it should be 1.\n", offsetof(s2_t, l1));
115         ++fails;
116     }
117     if (offsetof(u1_t, c1) != six) {
118         printf("The offset of u1.c1 is %u; it should be 6.\n", offsetof(u1_t, c1));
119         ++fails;
120     }
121     if (offsetof(u2_t, i1) != zero) {
122         printf("The offset of u2.i1 is %u; it should be 0.\n", offsetof(u2_t, i1));
123         ++fails;
124     }
125
126     /* Does cc65 use the correct offset of a member
127     ** that's later than an anonymous struct/union?
128     */
129
130     if (offsetof(s1_t, c2) != nine) {
131         printf("The offset of s1.c2 is %u; it should be 9.\n", offsetof(s1_t, c2));
132         ++fails;
133     }
134     if (offsetof(s2_t, s1) != five) {
135         printf("The offset of s2.s1 is %u; it should be 5.\n", offsetof(s2_t, s1));
136         ++fails;
137     }
138     if (offsetof(u1_t, c2) != zero) {
139         printf("The offset of u1.c2 is %u; it should be 0.\n", offsetof(u1_t, c2));
140         ++fails;
141     }
142     if (offsetof(u2_t, c2) != zero) {
143         printf("The offset of u2.c2 is %u; it should be 0.\n", offsetof(u2_t, c2));
144         ++fails;
145     }
146     if (offsetof(s3_t, s2) != seven) {
147         printf("The offset of s3.s2 is %u; it should be 7.\n", offsetof(s3_t, s2));
148         ++fails;
149     }
150
151     return fails;
152 }