]> git.sur5r.net Git - cc65/blob - test/val/cq241.c
added tests as prepared by oliver
[cc65] / test / val / cq241.c
1 /*
2   !!DESCRIPTION!! C-Manual Chapter 2.41: integer constants, 2.42: explizit long constants
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 /*
45         2.4.1 Integer constants
46         2.4.2 Explicit long constants
47 */
48
49 /* Calculate 2**n by multiplying, not shifting  */
50 #ifndef NO_OLD_FUNC_DECL
51 long pow2(n)
52 long n;
53 {
54 #else
55 long pow2(long n) {
56 #endif
57    long s;
58    s = 1;
59    while(n--) s = s*2;
60    return s;
61 }
62
63    long d[39], o[39], x[39];
64
65 #ifndef NO_OLD_FUNC_DECL
66 s241(pd0)
67 struct defs *pd0;
68 {
69 #else
70 int s241(struct defs *pd0) {
71 #endif
72
73 /*   long pow2(); */
74    static char s241er[] = "s241,er%d\n";
75    static char qs241[8] = "s241   ";
76    char *ps, *pt;
77    int rc, j, lrc;
78    static long g[39] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
79                         0,6,0,8,0,12,0,16,0,18,0,20,0,24,
80                         0,28,0,30,0,32,0,36};
81 /*   long d[39], o[39], x[39]; */
82
83    rc = 0;
84    lrc = 0;
85    ps = qs241;
86    pt = pd0 -> rfs;
87    while (*pt++ = *ps++);
88
89      /* An integer constant consisting of a sequence of digits is
90         taken to be octal if it begins with 0 (digit zero), decimal
91         otherwise.                                            */
92
93    if (   8 !=  010
94      ||  16 !=  020
95      ||  24 !=  030
96      ||  32 !=  040
97      ||  40 !=  050
98      ||  48 !=  060
99      ||  56 !=  070
100      ||  64 != 0100
101      ||  72 != 0110
102      ||  80 != 0120
103      ||   9 != 0011
104      ||  17 != 0021
105      ||  25 != 0031
106      ||  33 != 0041
107      ||  41 != 0051
108      ||  49 != 0061
109      ||  57 != 0071
110      ||  65 != 0101
111      ||  73 != 0111
112      ||  81 != 0121 ){
113      rc = rc+1;
114      if( pd0->flgd != 0 ) printf(s241er,1);
115    }
116
117      /* A sequence of digits preceded by 0x or 0X (digit zero)
118         is taken to be a hexadecimal integer. The hexadecimal
119         digits include a or A through f or F with values 10
120         through 15.     */
121
122    if ( 0x00abcdef != 0xabcdef
123      || 0xabcdef != 0Xabcdef || 0Xabcdef != 0XAbcdef
124      || 0XAbcdef != 0XABcdef || 0XABcdef != 0XABCdef
125      || 0XABCdef != 0XABCDef || 0XABCDef != 0XABCDEf
126      || 0XABCDEf != 0XABCDEF || 0xABCDEF != 11259375 ){
127      rc = rc+2;
128      if( pd0->flgd != 0 ) printf(s241er,2);
129    }
130
131      /* A decimal constant whose value exceeds the largest signed
132         machine integer is taken to be long; an octal or hex con-
133         stant which exceeds the largest unsigned machine integer
134         is likewise taken to be long.     */
135 #if defined(REFERENCE) && defined(REFCC_SIZEOF_LONG_64BIT)
136 /*#warning "sizeof(long)!=4, skipping test"*/
137 #else
138    if ( sizeof 010000000000 != sizeof(long)      /* 2**30 */
139      || sizeof 1073741824   != sizeof(long)      /* ditto */
140      || sizeof 0x40000000   != sizeof(long) ){   /*   "   */
141
142      rc = rc+4;
143      if( pd0->flgd != 0 ) printf(s241er,4);
144    }
145 #endif
146      /* A decimal, octal, or hexadecimal constant immediately followed
147         by l (letter ell) or L is a long constant.    */
148
149    if ( sizeof   67l != sizeof(long)
150      || sizeof   67L != sizeof(long)
151      || sizeof  067l != sizeof(long)
152      || sizeof  067L != sizeof(long)
153      || sizeof 0X67l != sizeof(long)
154      || sizeof 0x67L != sizeof(long) ){
155      rc = rc+8;
156      if( pd0 -> flgd != 0 ) printf(s241er,8);
157    }
158
159      /* Finally, we test to see that decimal (d), octal (o),
160         and hexadecimal (x) constants representing the same values
161         agree among themselves, and with computed values, at spec-
162         ified points over an appropriate range. The points select-
163         ed here are those with the greatest potential for caus-
164         ing trouble, i.e., zero, 1-16, and values of 2**n and
165         2**n - 1 where n is some multiple of 4 or 6. Unfortunately,
166         just what happens when a value is too big to fit in a
167         long is undefined; however, it would be nice if what
168         happened were at least consistent...      */
169
170    for ( j=0; j<17; j++ ) g[j] = j;
171    for ( j=18; j<39; ) {
172      g[j] = pow2(g[j]);
173      g[j-1] = g[j] - 1;
174      j = j+2;
175    }
176
177    d[0] = 0;                o[0] = 00;               x[0] = 0x0;
178    d[1] = 1;                o[1] = 01;               x[1] = 0x1;
179    d[2] = 2;                o[2] = 02;               x[2] = 0x2;
180    d[3] = 3;                o[3] = 03;               x[3] = 0x3;
181    d[4] = 4;                o[4] = 04;               x[4] = 0x4;
182    d[5] = 5;                o[5] = 05;               x[5] = 0x5;
183    d[6] = 6;                o[6] = 06;               x[6] = 0x6;
184    d[7] = 7;                o[7] = 07;               x[7] = 0x7;
185    d[8] = 8;                o[8] = 010;              x[8] = 0x8;
186    d[9] = 9;                o[9] = 011;              x[9] = 0x9;
187    d[10] = 10;              o[10] = 012;             x[10] = 0xa;
188    d[11] = 11;              o[11] = 013;             x[11] = 0xb;
189    d[12] = 12;              o[12] = 014;             x[12] = 0xc;
190    d[13] = 13;              o[13] = 015;             x[13] = 0xd;
191    d[14] = 14;              o[14] = 016;             x[14] = 0xe;
192    d[15] = 15;              o[15] = 017;             x[15] = 0xf;
193    d[16] = 16;              o[16] = 020;             x[16] = 0x10;
194    d[17] = 63;              o[17] = 077;             x[17] = 0x3f;
195    d[18] = 64;              o[18] = 0100;            x[18] = 0x40;
196    d[19] = 255;             o[19] = 0377;            x[19] = 0xff;
197    d[20] = 256;             o[20] = 0400;            x[20] = 0x100;
198    d[21] = 4095;            o[21] = 07777;           x[21] = 0xfff;
199    d[22] = 4096;            o[22] = 010000;          x[22] = 0x1000;
200    d[23] = 65535;           o[23] = 0177777;         x[23] = 0xffff;
201    d[24] = 65536;           o[24] = 0200000;         x[24] = 0x10000;
202    d[25] = 262143;          o[25] = 0777777;         x[25] = 0x3ffff;
203    d[26] = 262144;          o[26] = 01000000;        x[26] = 0x40000;
204    d[27] = 1048575;         o[27] = 03777777;        x[27] = 0xfffff;
205    d[28] = 1048576;         o[28] = 04000000;        x[28] = 0x100000;
206    d[29] = 16777215;        o[29] = 077777777;       x[29] = 0xffffff;
207    d[30] = 16777216;        o[30] = 0100000000;      x[30] = 0x1000000;
208    d[31] = 268435455;       o[31] = 01777777777;     x[31] = 0xfffffff;
209    d[32] = 268435456;       o[32] = 02000000000;     x[32] = 0x10000000;
210    d[33] = 1073741823;      o[33] = 07777777777;     x[33] = 0x3fffffff;
211    d[34] = 1073741824;      o[34] = 010000000000;    x[34] = 0x40000000;
212    d[35] = 4294967295;      o[35] = 037777777777;    x[35] = 0xffffffff;
213    d[36] = 4294967296;      o[36] = 040000000000;    x[36] = 0x100000000;
214    d[37] = 68719476735;     o[37] = 0777777777777;   x[37] = 0xfffffffff;
215    d[38] = 68719476736;     o[38] = 01000000000000;  x[38] = 0x1000000000;
216
217    /* WHEW! */
218
219    for (j=0; j<39; j++){
220      if ( g[j] != d[j]
221        || d[j] != o[j]
222        || o[j] != x[j]) {
223        if( pd0 -> flgm != 0 ) {
224 /*       printf(s241er,16);          save in case opinions change...     */
225          printf("Decimal and octal/hex constants sometimes give\n");
226          printf("   different results when assigned to longs.\n");
227        }
228 /*     lrc = 1;   save...   */
229      }
230    }
231
232    if (lrc != 0) rc =16;
233
234    return rc;
235 }
236
237 /*********************************************************************************************
238  the main loop that launches the sections
239 *********************************************************************************************/
240
241 #define cq_sections 1
242
243 #ifndef NO_TYPELESS_STRUCT_PTR
244         int section(int j,struct* pd0){
245 #else
246         int section(int j,void* pd0){
247 #endif
248         switch(j){
249                 case 0: return s241(pd0);
250         }
251 }
252
253 /*
254         C REFERENCE MANUAL (main)
255 */
256
257 #ifndef NO_OLD_FUNC_DECL
258 main(n,args)
259 int n;
260 char **args;
261 {
262 #else
263 int main(int n,char **args) {
264 #endif
265
266 int j;
267 static struct defs d0, *pd0;
268         
269    d0.flgs = 1;          /* These flags dictate            */
270    d0.flgm = 1;          /*     the verbosity of           */
271    d0.flgd = 1;          /*         the program.           */
272    d0.flgl = 1;
273
274    pd0 = &d0;
275
276    for (j=0; j<cq_sections; j++) {
277      d0.rrc=section(j,pd0);
278      d0.crc=d0.crc+d0.rrc;
279      if(d0.flgs != 0) printf("Section %s returned %d.\n",d0.rfs,d0.rrc);
280    }
281
282    if(d0.crc == 0) printf("\nNo errors detected.\n");
283    else printf("\nFailed.\n");
284
285    return d0.crc;
286 }