]> git.sur5r.net Git - cc65/blob - test/ref/switch.c
0821d29ee67d3a5f683d617e2d9563607c62927e
[cc65] / test / ref / switch.c
1 /*
2   !!DESCRIPTION!! switch statement
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 #include "common.h"
8 #include <limits.h>
9
10 #ifdef NO_IMPLICIT_FUNC_PROTOTYPES
11 testbig();
12 testbackslash();
13 backslash(int c);
14 f();
15 g();
16 h();
17 limit();
18
19 big(
20 # ifdef ASSUME_32BIT_UNSIGNED
21         unsigned
22 # else
23         unsigned long
24 # endif
25 x);
26
27 #endif
28
29 main()
30 {
31         testbackslash();
32         f();
33         g();
34         h();
35         testbig(); /* ! broken long int compare (?) */
36         limit();   /* ! broken long int compare (?) */
37
38         return 0;
39 }
40
41 testbig()
42 {
43         #ifdef ASSUME_32BIT_INT
44         int i;
45         #else
46                 signed long i;
47         #endif
48                /*  2341234      2341234         2341234 */
49         for (i = 0x1000000; i&0x7000000; i += 0x1000000) {
50 /*              printf("i = 0x%lx\n", i); */
51                 big(i);
52         }
53 }
54
55 #ifdef NO_LOCAL_STRING_INIT
56 /*        static char _s[8]={"bfnrtvx"}; */
57         static char _s[8]="bfnrtvx";
58 #endif
59
60 testbackslash()
61 {
62         char *s;
63
64 #ifdef NO_STRINGS_IN_FOR
65 # ifndef NO_LOCAL_STRING_INIT
66         char _s[8]={"bfnrtvx"};
67 # endif
68         for (s=_s; *s; s++) {
69 #else
70         for (s = "bfnrtvx"; *s; s++) {
71 #endif
72                         printf("%c = %c\n", *s, backslash(*s));
73         }
74 }
75
76 backslash(c)
77 {
78         switch (c)
79     {
80         case 'b':
81                 return 'b';
82         case 'f':
83                 return 'f';
84         case 'n':
85                 return 'n';
86         case 'r':
87                 return 'r';
88         case 't':
89                 return 't';
90         case 'v':
91         return 'v';
92         }
93
94         return 'x';
95 }
96
97 f() {
98         int i, x = 0, y;
99
100         printf("f:\n");
101         for (i = 0; i <= 20; i++) {
102                 y = i;
103                 switch (i) {
104                 case 1: x = i; break;
105                 case 2: x = i; break;
106                 case 7: x = i; break;
107                 case 8: x = i; break;
108                 case 9: x = i; break;
109                 case 16: x = i; break;
110                 case 17: x = i; break;
111                 case 18: x = i; break;
112                 case 19: x = i; break;
113                 case 20: x = i; break;
114                 }
115                 printf("x = %d\n", x);
116         }
117 }
118
119 g() {
120         int i;
121
122         printf("g:\n");
123         for (i = 1; i <= 10; i++)
124                 switch (i) {
125                 case 1: case 2: printf("1 %d\n", i); break;
126                 case 3: case 4: case 5: printf("2 %d\n", i); break;
127                 case 6: case 7: case 8: printf("3 %d\n", i);
128                 default:
129                         printf("d %d\n", i); break;
130                 case 1001: case 1002: case 1003: case 1004:
131                         printf("5 %d\n", i); break;
132                 case 3001: case 3002: case 3003: case 3004:
133                         printf("6 %d\n", i); break;
134         }
135 }
136
137 h()
138 {
139         int i, n=0;
140
141         printf("h:\n");
142         for (i = 1; i <= 500; i++)
143                 switch (i) {
144                 default: n++; continue;
145                 case 128: printf("i = %d\n", i); break;
146                 case 16: printf("i = %d\n", i); break;
147                 case 8: printf("i = %d\n", i); break;
148                 case 120: printf("i = %d\n", i); break;
149                 case 280: printf("i = %d\n", i); break;
150                 case 264: printf("i = %d\n", i); break;
151                 case 248: printf("i = %d\n", i); break;
152                 case 272: printf("i = %d\n", i); break;
153                 case 304: printf("i = %d\n", i); break;
154                 case 296: printf("i = %d\n", i); break;
155                 case 288: printf("i = %d\n", i); break;
156                 case 312: printf("i = %d\n", i); break;
157                 }
158         printf("%d defaults\n", n);
159 }
160
161 #ifdef NO_OLD_FUNC_DECL
162         big(
163 #else
164         big(x)
165 #endif
166
167 # ifdef ASSUME_32BIT_UNSIGNED
168         unsigned
169 # else
170         unsigned long
171 # endif
172
173 #ifdef NO_OLD_FUNC_DECL
174         x) {
175 #else
176         x; {
177 #endif
178
179 /*      printf("x = 0x%x\n", x); */
180
181         switch(x&0x6000000){
182         case -1:
183         case -2:
184         case 0x0000000:
185                 printf("x = 0x%lx\n", x); break;
186         case 0x2000000:
187                 printf("x = 0x%lx\n", x); break;
188         case 0x4000000:
189                 printf("x = 0x%lx\n", x); break;
190         default:
191                 printf("x = 0x%lx (default)\n", x); break;
192         }
193 }
194
195 limit() {
196         int i;
197
198         for (i = INT_MIN; i <= INT_MIN+5; i++)
199 /*      for (i = INT_MIN; i <  INT_MIN+6; i++) */
200                 switch (i) {
201                         case INT_MIN:   printf("0\n"); break;
202                         case INT_MIN+1: printf("1\n"); break;
203                         case INT_MIN+2: printf("2\n"); break;
204                         case INT_MIN+3: printf("3\n"); break;
205                         case INT_MIN+4: printf("4\n"); break;
206                         default:        printf("5\n"); break;
207                 }
208         for (i = INT_MAX; i >= INT_MAX-5; i--)
209                 switch (i) {
210                         case INT_MAX:   printf("0\n"); break;
211                         case INT_MAX-1: printf("1\n"); break;
212                         case INT_MAX-2: printf("2\n"); break;
213                         case INT_MAX-3: printf("3\n"); break;
214                         case INT_MAX-4: printf("4\n"); break;
215                         default:            printf("5\n"); break;
216                 }
217 }