]> git.sur5r.net Git - cc65/blob - test/val/cq4.c
Merge pull request #135 from ikorb/patch1
[cc65] / test / val / cq4.c
1 /*
2   !!DESCRIPTION!! C-Manual Chapter 4: what's in a name?
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 /*#include "cq26.c"*/ /* hardware check */
45
46 int extvar;
47
48 #ifdef NO_IMPLICIT_FUNC_PROTOTYPES
49 int s4(struct defs *pd0);
50 int svtest(int n);
51 zero();
52 testev();
53 setev();
54 #endif
55
56 #ifndef NO_OLD_FUNC_DECL
57 s4(pd0)                    /* 4. What's in a name?             */
58 struct defs *pd0;
59 {
60 #else
61 int s4(struct defs *pd0) {
62 #endif
63    static char s4er[] = "s4,er%d\n";
64    static char qs4[8] = "s4     ";
65    char *ps, *pt;
66    int j, rc;
67
68    short sint;             /* short integer, for size test      */
69    int pint;               /* plain                             */
70    long lint;              /* long                              */
71    unsigned target;
72    unsigned int mask;
73
74    rc = 0;
75    ps = qs4;
76    pt = pd0->rfs;
77
78    while(*pt++ = *ps++);
79
80 /*   There are four declarable storage classes: automatic,
81 static, external, and register. Automatic variables have
82 been dealt with extensively thus far, and will not be specif-
83 ically treated in this section. Register variables are treated
84 in section s81.
85
86      Static variables are local to a block, but retain their
87 values upon reentry to a block, even after control has left
88 the block.                                                     */
89
90    for (j=0; j<3; j++)
91      if(svtest(j) != zero()){
92        rc = 1;
93        if(pd0->flgd != 0) printf(s4er,1);
94      }
95    ;
96
97 /*   External variables exist and retain their values throughout
98 the execution of the entire program, and may be used for comm-
99 unication between functions, even separately compiled functions.
100                                                                 */
101
102    setev();
103    if(testev() != 0){
104      rc=rc+2;
105      if(pd0->flgd != 0) printf(s4er,2);
106    }
107 /*
108      Characters have been tested elsewhere (in s243).
109
110      Up to three sizes of integer, declared short int, int, and
111 long int, are available. Longer integers provide no less storage
112 than shorter ones, but implementation may make either short
113 integers, or long integers, or both, equivalent to plain
114 integers.
115                                                                 */
116
117    if(sizeof lint < sizeof pint || sizeof pint < sizeof sint){
118      rc = rc+4;
119      if(pd0->flgd != 0) printf(s4er,4);
120    }
121
122 /*   Unsigned integers, declared unsigned, obey the laws of
123 arithmetic modulo 2**n, where n is the number of bits in the
124 implementation                                                  */
125
126    target = ~0U;
127    mask = 1;
128
129    for(j=0; j<(sizeof target)*pd0->cbits; j++){
130      mask = mask&target;
131      target = target>>1;
132    }
133
134    if(mask != 1 || target != 0){
135      rc = rc+8;
136      if(pd0->flgd != 0) printf(s4er,8);
137    }
138
139    return rc;
140 }
141
142 #ifndef NO_OLD_FUNC_DECL
143 svtest(n)
144 int n;
145 {
146 #else
147 int svtest(int n) {
148 #endif
149
150    static k;
151    int rc;
152    switch (n) {
153      case 0: k = 1978;
154              rc = 0;
155              break;
156
157      case 1: if(k != 1978) rc = 1;
158              else{
159               k = 1929;
160               rc = 0;
161              }
162              break;
163
164      case 2: if(k != 1929) rc = 1;
165              else rc = 0;
166              break;
167    }
168    return rc;
169 }
170 zero(){                 /* Returns a value of zero, possibly */
171    static k;            /* with side effects, as it's called */
172    int rc;              /* alternately with svtest, above,   */
173    k = 2;               /* and has the same internal storage */
174    rc = 0;              /* requirements.                     */
175    return rc;
176 }
177 testev(){
178    if(extvar != 1066) return 1;
179    else return 0;
180 }
181
182 /* Sets an external variable. Used  */
183 /* by s4, and should be compiled    */
184 /* separately from s4.              */
185
186 setev(){
187 #ifndef NO_SLOPPY_EXTERN
188    extern int extvar;
189 #endif
190    extvar = 1066;
191 }
192
193 /*********************************************************************************************
194  the main loop that launches the sections
195 *********************************************************************************************/
196
197 #ifndef NO_TYPELESS_STRUCT_PTR
198         int section(int j,struct* pd0){
199 #else
200         int section(int j,void* pd0){
201 #endif
202         switch(j){
203                 case 0: return s4(pd0);
204         }
205 }
206
207 #define cq_sections 1
208
209 /*
210         C REFERENCE MANUAL (main)
211 */
212
213 #ifndef NO_OLD_FUNC_DECL
214 main(n,args)
215 int n;
216 char **args;
217 {
218 #else
219 int main(int n,char **args) {
220 #endif
221
222 int j;
223 static struct defs d0, *pd0;
224         
225    d0.flgs = 1;          /* These flags dictate            */
226    d0.flgm = 1;          /*     the verbosity of           */
227    d0.flgd = 1;          /*         the program.           */
228    d0.flgl = 1;
229
230    pd0 = &d0;
231
232    for (j=0; j<cq_sections; j++) {
233      d0.rrc=section(j,pd0);
234      d0.crc=d0.crc+d0.rrc;
235      if(d0.flgs != 0) printf("Section %s returned %d.\n",d0.rfs,d0.rrc);
236    }
237
238    if(d0.crc == 0) printf("\nNo errors detected.\n");
239    else printf("\nFailed.\n");
240
241    return d0.crc;
242 }