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