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