struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / switch.c.in
blobd1260e5f8470d75bca754275ce7cd4f2bd5353f1
1 /* Test the types of switch statement.
3 type: char, int, long
4 sign: signed, unsigned
5 storage: static,
6 attr: volatile
7 */
8 #include <testfwk.h>
10 {sign} {type}
11 sparseSwitch({sign} {type} val)
13 {sign} {type} ret;
14 {storage} {attr} {sign} {type} local;
16 local = val;
18 switch (local) {
19 case 1:
20 ret = 7;
21 break;
22 case 4:
23 ret = 12;
24 break;
25 case 6:
26 ret = 13;
27 /* Fall through */
28 case 12:
29 ret = 14;
30 break;
31 case 13:
32 case 14:
33 case 15:
34 ret = 19;
35 break;
36 default:
37 ret = 30;
40 return ret;
43 void
44 testSparseSwitch(void)
46 ASSERT(sparseSwitch(1) == 7);
47 ASSERT(sparseSwitch(4) == 12);
48 ASSERT(sparseSwitch(6) == 14);
49 ASSERT(sparseSwitch(12) == 14);
50 ASSERT(sparseSwitch(13) == 19);
51 #ifndef __SDCC_pdk14 // Lack of memory
52 ASSERT(sparseSwitch(14) == 19);
53 ASSERT(sparseSwitch(15) == 19);
54 ASSERT(sparseSwitch(19) == 30);
55 ASSERT(sparseSwitch(0) == 30);
56 #endif
59 #ifndef __SDCC_pdk14 // Lack of memory
60 {sign} {type}
61 denseSwitch({sign} {type} val)
63 {sign} {type} ret;
64 {storage} {attr} {sign} {type} local;
66 local = val;
67 ret = 12;
69 switch (local) {
70 case 0:
71 ret = 1;
72 break;
73 case 1:
74 ret = 14;
75 break;
76 case 2:
77 ret = 15;
78 break;
79 case 3:
80 ret = 34;
81 break;
82 case 4:
83 ret = 17;
84 break;
85 /* No default */
88 return ret;
90 #endif
92 void
93 testDenseSwitch(void)
95 #ifndef __SDCC_pdk14 // Lack of memory
96 ASSERT(denseSwitch(0) == 1);
97 ASSERT(denseSwitch(1) == 14);
98 ASSERT(denseSwitch(2) == 15);
99 ASSERT(denseSwitch(3) == 34);
100 ASSERT(denseSwitch(4) == 17);
101 ASSERT(denseSwitch(5) == 12);
102 ASSERT(denseSwitch(100) == 12);
103 #endif
106 void
107 testDenseIntSwitch(void)
109 #ifndef __SDCC_pdk14 // Lack of memory
110 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
111 volatile int val = 1000;
112 volatile int ret = 0;
114 switch (val) {
115 case 999:
116 ret = 5;
117 break;
118 case 1000:
119 ret = 6;
120 break;
121 case 1001:
122 ret = 7;
123 break;
124 case 1002:
125 ret = 12;
126 break;
127 case 1003:
128 ret = 14;
129 break;
132 ASSERT(ret == 6);
134 val = 129;
136 switch (val) {
137 case 126:
138 ret = 5;
139 break;
140 case 127:
141 ret = 6;
142 break;
143 case 128:
144 ret = 7;
145 break;
146 case 129:
147 ret = 8;
148 break;
149 default:
150 ret = 9;
153 ASSERT(ret == 8);
154 #endif
155 #endif