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