1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-24 11:31:31 +00:00
cc65/test/ref/switch.c

218 lines
5.3 KiB
C
Raw Normal View History

2014-09-24 14:45:10 +00:00
/*
!!DESCRIPTION!! switch statement
!!ORIGIN!! LCC 4.1 Testsuite
!!LICENCE!! own, freely distributeable for non-profit. read CPYRIGHT.LCC
*/
#include "common.h"
2014-09-24 14:45:10 +00:00
#include <limits.h>
#ifdef NO_IMPLICIT_FUNC_PROTOTYPES
testbig();
testbackslash();
backslash(int c);
f();
g();
h();
limit();
big(
# ifdef ASSUME_32BIT_UNSIGNED
2019-02-12 21:50:49 +00:00
unsigned
2014-09-24 14:45:10 +00:00
# else
2019-02-12 21:50:49 +00:00
unsigned long
2014-09-24 14:45:10 +00:00
# endif
x);
#endif
main()
{
2019-02-12 21:50:49 +00:00
testbackslash();
f();
g();
h();
testbig(); /* ! broken long int compare (?) */
limit(); /* ! broken long int compare (?) */
return 0;
2014-09-24 14:45:10 +00:00
}
testbig()
{
2019-02-12 21:50:49 +00:00
#ifdef ASSUME_32BIT_INT
int i;
#else
signed long i;
#endif
/* 2341234 2341234 2341234 */
for (i = 0x1000000; i&0x7000000; i += 0x1000000) {
/* printf("i = 0x%lx\n", i); */
big(i);
}
2014-09-24 14:45:10 +00:00
}
#ifdef NO_LOCAL_STRING_INIT
/* static char _s[8]={"bfnrtvx"}; */
static char _s[8]="bfnrtvx";
#endif
testbackslash()
{
char *s;
#ifdef NO_STRINGS_IN_FOR
# ifndef NO_LOCAL_STRING_INIT
char _s[8]={"bfnrtvx"};
# endif
for (s=_s; *s; s++) {
#else
for (s = "bfnrtvx"; *s; s++) {
#endif
2019-02-12 21:50:49 +00:00
printf("%c = %c\n", *s, backslash(*s));
2014-09-24 14:45:10 +00:00
}
}
backslash(c)
{
2019-02-12 21:50:49 +00:00
switch (c)
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
case 'b':
return 'b';
case 'f':
return 'f';
case 'n':
return 'n';
case 'r':
return 'r';
case 't':
return 't';
case 'v':
2014-09-24 14:45:10 +00:00
return 'v';
2019-02-12 21:50:49 +00:00
}
2014-09-24 14:45:10 +00:00
2019-02-12 21:50:49 +00:00
return 'x';
2014-09-24 14:45:10 +00:00
}
f() {
2019-02-12 21:50:49 +00:00
int i, x = 0, y;
printf("f:\n");
for (i = 0; i <= 20; i++) {
y = i;
switch (i) {
case 1: x = i; break;
case 2: x = i; break;
case 7: x = i; break;
case 8: x = i; break;
case 9: x = i; break;
case 16: x = i; break;
case 17: x = i; break;
case 18: x = i; break;
case 19: x = i; break;
case 20: x = i; break;
}
printf("x = %d\n", x);
}
2014-09-24 14:45:10 +00:00
}
g() {
2019-02-12 21:50:49 +00:00
int i;
printf("g:\n");
for (i = 1; i <= 10; i++)
switch (i) {
case 1: case 2: printf("1 %d\n", i); break;
case 3: case 4: case 5: printf("2 %d\n", i); break;
case 6: case 7: case 8: printf("3 %d\n", i);
default:
printf("d %d\n", i); break;
case 1001: case 1002: case 1003: case 1004:
printf("5 %d\n", i); break;
case 3001: case 3002: case 3003: case 3004:
printf("6 %d\n", i); break;
}
2014-09-24 14:45:10 +00:00
}
h()
{
2019-02-12 21:50:49 +00:00
int i, n=0;
printf("h:\n");
for (i = 1; i <= 500; i++)
switch (i) {
default: n++; continue;
case 128: printf("i = %d\n", i); break;
case 16: printf("i = %d\n", i); break;
case 8: printf("i = %d\n", i); break;
case 120: printf("i = %d\n", i); break;
case 280: printf("i = %d\n", i); break;
case 264: printf("i = %d\n", i); break;
case 248: printf("i = %d\n", i); break;
case 272: printf("i = %d\n", i); break;
case 304: printf("i = %d\n", i); break;
case 296: printf("i = %d\n", i); break;
case 288: printf("i = %d\n", i); break;
case 312: printf("i = %d\n", i); break;
}
printf("%d defaults\n", n);
2014-09-24 14:45:10 +00:00
}
#ifdef NO_OLD_FUNC_DECL
big(
#else
big(x)
#endif
# ifdef ASSUME_32BIT_UNSIGNED
2019-02-12 21:50:49 +00:00
unsigned
2014-09-24 14:45:10 +00:00
# else
2019-02-12 21:50:49 +00:00
unsigned long
2014-09-24 14:45:10 +00:00
# endif
#ifdef NO_OLD_FUNC_DECL
x) {
#else
x; {
#endif
2019-02-12 21:50:49 +00:00
/* printf("x = 0x%x\n", x); */
switch(x&0x6000000){
case -1:
case -2:
case 0x0000000:
printf("x = 0x%lx\n", x); break;
case 0x2000000:
printf("x = 0x%lx\n", x); break;
case 0x4000000:
printf("x = 0x%lx\n", x); break;
default:
printf("x = 0x%lx (default)\n", x); break;
}
2014-09-24 14:45:10 +00:00
}
limit() {
2019-02-12 21:50:49 +00:00
int i;
for (i = INT_MIN; i <= INT_MIN+5; i++)
/* for (i = INT_MIN; i < INT_MIN+6; i++) */
switch (i) {
case INT_MIN: printf("0\n"); break;
case INT_MIN+1: printf("1\n"); break;
case INT_MIN+2: printf("2\n"); break;
case INT_MIN+3: printf("3\n"); break;
case INT_MIN+4: printf("4\n"); break;
default: printf("5\n"); break;
}
for (i = INT_MAX; i >= INT_MAX-5; i--)
switch (i) {
case INT_MAX: printf("0\n"); break;
case INT_MAX-1: printf("1\n"); break;
case INT_MAX-2: printf("2\n"); break;
case INT_MAX-3: printf("3\n"); break;
case INT_MAX-4: printf("4\n"); break;
default: printf("5\n"); break;
}
2014-09-24 14:45:10 +00:00
}