aboutsummaryrefslogtreecommitdiffstats
path: root/lcc/tst/switch.c
blob: dd62d79cb4bfcebe1cc5cea0852a200ea1866ce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
main()
{
	int i; char *s;

	for (s = "bfnrtvx"; *s; s++)
		printf("%c = 0x%x\n", *s, backslash(*s));
	f();
	g();
	h();
	for (i = 0x1000000; i&0x7000000; i += 0x1000000)
		big(i);
	limit();
	return 0;
}	

backslash(c)
{
	switch (c) {
	case 'b':
		return '\b';
	case 'f':
		return '\f';
	case 'n':
		return '\n';
	case 'r':
		return '\r';
	case 't':
		return '\t';
	case 'v':
		return '\v';
	}
	return c;
}

f() {
	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);
	}
}

g() {
	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;
	}
}

h()
{
	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);
}

big(x) unsigned x; {
	switch(x&0x6000000){
	case -1:
	case -2:
	case 0x0000000:
		printf("x = 0x%x\n", x); break;
	case 0x2000000:
		printf("x = 0x%x\n", x); break;
	case 0x4000000:
		printf("x = 0x%x\n", x); break;
	default:
		printf("x = 0x%x (default)\n", x); break;
	}
}

#include <limits.h>

limit() {
	int i;

	for (i = INT_MIN; i <= INT_MIN+5; 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;
		}
}