]> git.sur5r.net Git - cc65/blob - test/val/cq85.c
Merge https://github.com/cc65/cc65 into c1p
[cc65] / test / val / cq85.c
1 /*
2   !!DESCRIPTION!! C-Manual Chapter 8.5: Structure and Union declarations
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 struct defs {
8      int cbits;          /* No. of bits per char           */
9      int ibits;          /*                 int            */
10      int sbits;          /*                 short          */
11      int lbits;          /*                 long           */
12      int ubits;          /*                 unsigned       */
13      int fbits;          /*                 float          */
14      int dbits;          /*                 double         */
15      #ifndef NO_FLOATS
16         float fprec;        /* Smallest number that can be    */
17         float dprec;        /* significantly added to 1.      */
18      #endif
19      int flgs;           /* Print return codes, by section */
20      int flgm;           /* Announce machine dependencies  */
21      int flgd;           /* give explicit diagnostics      */
22      int flgl;           /* Report local return codes.     */
23      int rrc;            /* recent return code             */
24      int crc;            /* Cumulative return code         */
25      char rfs[8];        /* Return from section            */
26 };
27
28      int lbits;          /*                 long           */
29      int ubits;          /*                 unsigned       */
30      int fbits;          /*                 float          */
31      int dbits;          /*                 double         */
32      #ifndef NO_FLOATS
33         float fprec;        /* Smallest number that can be    */
34         float dprec;        /* significantly added to 1.      */
35      #endif
36      int flgs;           /* Print return codes, by section */
37      int flgm;           /* Announce machine dependencies  */
38      int flgd;           /* give explicit diagnostics      */
39      int flgl;           /* Report local return codes.     */
40      int rrc;            /* recent return code             */
41      int crc;            /* Cumulative return code         */
42      char rfs[8];        /* Return from section            */
43
44 #ifndef NO_OLD_FUNC_DECL
45 s85(pd0)          /*  8.5 Structure and union declarations   */
46 struct defs *pd0;
47 {
48 #else
49 int s85(struct defs *pd0){
50 #endif
51    static char s85er[] = "s85,er%d\n";
52    static char qs85[8] = "s85    ";
53    int rc;
54    char *ps, *pt;
55
56    struct tnode {
57      char tword[20];
58      int count;
59      struct tnode *left;
60      struct tnode *right;
61    };
62
63    struct tnode s1, s2, *sp;
64
65    struct{
66      char cdummy;
67      char c;
68    } sc;
69
70    struct{
71      char cdummy;
72      short s;
73    } ss;
74
75    struct{
76      char cdummy;
77      int i;
78    } si;
79
80    struct{
81      char cdummy;
82      long l;
83    } sl;
84
85    struct{
86      char cdummy;
87      unsigned u;
88    } su;
89
90    struct{
91      char cdummy;
92      #ifndef NO_FLOATS
93      float f;
94      #else
95      signed f;
96      #endif
97    } sf;
98
99    struct{
100      char cdummy;
101      #ifndef NO_FLOATS
102      double d;
103      #else
104      signed d;
105      #endif
106    } sd;
107
108    int diff[7], j;
109
110    static char *type[] = {
111      "char",
112      "short",
113      "int",
114      "long",
115      "unsigned",
116      #ifdef NO_FLOATS
117      "signed",
118      "signed",
119          #else
120      "float",
121      "double"
122          #endif
123    };
124
125    static char aln[] = " alignment: ";
126
127    #ifndef NO_BITFIELDS
128    struct{
129      int twobit:2;
130      int       :1;
131      int threebit:3;
132      int onebit:1;
133    } s3;
134    #else
135    struct{
136      unsigned char twobit;
137      unsigned char threebit;
138      unsigned char onebit;
139    } s3;
140    #endif
141
142    union{
143      char u1[30];
144      short u2[30];
145      int u3[30];
146      long u4[30];
147      unsigned u5[30];
148      #ifndef NO_FLOATS
149      float u6[30];
150      double u7[30];
151      #else
152      signed u6[30];
153      signed u7[30];
154      #endif
155    } u0;
156
157    ps = qs85;
158    pt = pd0->rfs;
159    rc = 0;
160    while (*pt++ = *ps++);
161
162         /* Within a structure, the objects declared have
163         addresses which increase as their declarations are
164         read left to right.
165                                                                 */
166
167    if( (char *)&s1.count - &s1.tword[0] <= 0
168      ||(char *)&s1.left - (char *)&s1.count <= 0
169      ||(char *)&s1.right - (char *)&s1.left <= 0){
170      if(pd0->flgd != 0) printf(s85er,1);
171      rc = rc+1;
172    }
173
174         /* Each non-field member of a structure begins on an
175         addressing boundary appropriate to its type.
176                                                                 */
177
178    diff[0] = &sc.c - &sc.cdummy;
179    diff[1] = (char *)&ss.s - &ss.cdummy;
180    diff[2] = (char *)&si.i - &si.cdummy;
181    diff[3] = (char *)&sl.l - &sl.cdummy;
182    diff[4] = (char *)&su.u - &su.cdummy;
183    diff[5] = (char *)&sf.f - &sf.cdummy;
184    diff[6] = (char *)&sd.d - &sd.cdummy;
185
186    if(pd0->flgm != 0)
187     for(j=0; j<7; j++)
188      printf("%s%s%d\n",type[j],aln,diff[j]);
189
190         /* Field specifications are highly implementation de-
191         pendent. About the only thing we can do here is to
192         check is that the compiler accepts the field constructs,
193         and that they seem to work, after a fashion, at
194         run time...
195                                                                 */
196
197    s3.threebit = 7;
198    s3.twobit = s3.threebit;
199    s3.threebit = s3.twobit;
200
201    if(s3.threebit != 3){
202      if(s3.threebit == -1){
203        if(pd0->flgm != 0) printf("Sign extension in fields\n");
204      }
205      else{
206            #ifdef NO_BITFIELDS
207                 if(pd0->flgd != 0) printf("NO_BITFIELDS\n");
208            #else
209                 if(pd0->flgd != 0) printf(s85er,2);
210                 rc = rc+2;
211            #endif
212      }
213    }
214
215    s3.onebit = 1;
216    if(s3.onebit != 1){
217      if(pd0->flgm != 0)
218       printf("Be especially careful with 1-bit fields!\n");
219    }
220
221         /* A union may be thought of as a structure all of whose
222         members begin at offset 0 and whose size is sufficient
223         to contain any of its members.
224                                                                 */
225
226    if( (char *)u0.u1 - (char *)&u0 != 0
227      ||(char *)u0.u2 - (char *)&u0 != 0
228      ||(char *)u0.u3 - (char *)&u0 != 0
229      ||(char *)u0.u4 - (char *)&u0 != 0
230      ||(char *)u0.u5 - (char *)&u0 != 0
231      ||(char *)u0.u6 - (char *)&u0 != 0
232      ||(char *)u0.u7 - (char *)&u0 != 0){
233      if(pd0->flgd != 0) printf(s85er,4);
234      rc = rc+4;
235    }
236
237    if( sizeof u0 < sizeof u0.u1
238      ||sizeof u0 < sizeof u0.u2
239      ||sizeof u0 < sizeof u0.u3
240      ||sizeof u0 < sizeof u0.u4
241      ||sizeof u0 < sizeof u0.u5
242      ||sizeof u0 < sizeof u0.u6
243      ||sizeof u0 < sizeof u0.u7){
244      if(pd0->flgd != 0) printf(s85er,8);
245      rc = rc+8;
246    }
247
248         /* Finally, we check that the pointers work.            */
249
250    s1.right = &s2;
251    s2.tword[0] = 2;
252    s1.right->tword[0] += 1;
253    if(s2.tword[0] != 3){
254      if(pd0->flgd != 0) printf(s85er,16);
255      rc = rc+16;
256    }
257    return rc;
258 }
259
260 #ifdef NO_LOCAL_PROTOTYPES
261 int one();
262 #endif
263
264 /*********************************************************************************************
265  the main loop that launches the sections
266 *********************************************************************************************/
267
268 #ifndef NO_TYPELESS_STRUCT_PTR
269         int section(int j,struct* pd0){
270 #else
271         int section(int j,void* pd0){
272 #endif
273         switch(j){
274                 case 0: return s85(pd0);
275         }
276 }
277
278 #define cq_sections 1
279
280 /*
281         C REFERENCE MANUAL (main)
282 */
283
284 #ifndef NO_OLD_FUNC_DECL
285 main(n,args)
286 int n;
287 char **args;
288 {
289 #else
290 int main(int n,char **args) {
291 #endif
292
293 int j;
294 static struct defs d0, *pd0;
295         
296    d0.flgs = 1;          /* These flags dictate            */
297    d0.flgm = 1;          /*     the verbosity of           */
298    d0.flgd = 1;          /*         the program.           */
299    d0.flgl = 1;
300
301    pd0 = &d0;
302
303    for (j=0; j<cq_sections; j++) {
304      d0.rrc=section(j,pd0);
305      d0.crc=d0.crc+d0.rrc;
306      if(d0.flgs != 0) printf("Section %s returned %d.\n",d0.rfs,d0.rrc);
307    }
308
309    if(d0.crc == 0) printf("\nNo errors detected.\n");
310    else printf("\nFailed.\n");
311
312    return d0.crc;
313 }