Yet again, signed comparisons to literals has been fixed.
[fw/sdcc] / src / regression / compare9.c
1 // Signed comparisons of the form:  (variable<=LIT)
2 //
3 // This regression test exercises all of the boundary
4 // conditions in literal less than or equal comparisons. There
5 // are numerous opportunities to optimize these comparison
6 // and each one has an astonishing capability of failing
7 // a boundary condition.
8
9 unsigned char success = 0;
10 unsigned char failures = 0;
11 unsigned char dummy = 0;
12 unsigned char result = 0;
13
14 int int0 = 0;
15 int int1 = 0;
16 signed char char0 = 0;
17 signed char char1 = 0;
18
19 void
20 done ()
21 {
22   dummy++;
23 }
24
25
26 void c_char_lte_lit1(unsigned char expected_result)
27 {
28   result = 0;
29
30   if(char0 <= -0x7f)
31     result |= 1;
32
33   if(char0 <= -1)
34     result |= 2;
35
36   if(char0 <= 0)
37     result |= 4;
38
39   if(char0 <= 1)
40     result |= 8;
41
42   if(char0 <= 0x7f)
43     result |= 0x10;
44   
45   if(result != expected_result)
46     failures++;
47 }
48
49
50 void char_compare(void)
51 {
52   char0 = 0x7f;
53   c_char_lte_lit1(0x10);
54
55   char0 = 0x7e;
56   c_char_lte_lit1(0x10);
57
58   char0 = 0x40;
59   c_char_lte_lit1(0x10);
60
61   char0 = 0x2;
62   c_char_lte_lit1(0x10);
63
64   char0 = 0x1;
65   c_char_lte_lit1(0x18);
66
67   char0 = 0;
68   c_char_lte_lit1(0x1c);
69
70   char0 = -1;
71   c_char_lte_lit1(0x1e);
72
73   char0 = -2;
74   c_char_lte_lit1(0x1e);
75
76   char0 = -0x40;
77   c_char_lte_lit1(0x1e);
78
79   char0 = -0x7e;
80   c_char_lte_lit1(0x1e);
81
82   char0 = -0x7f;
83   c_char_lte_lit1(0x1f);
84
85   char0 = 0x80;
86   //  c_char_lte_lit1(0x1f);
87
88
89   /* Now test entire range */
90
91   for(char0=2; char0 != 0x7f; char0++)
92     c_char_lte_lit1(0x10);
93
94
95   for(char0=-0x7e; char0 != 0; char0++)
96     c_char_lte_lit1(0x1e);
97
98
99 }
100
101 void c_int_lte_lit1(unsigned char expected_result)
102 {
103   result = 0;
104
105   if(int0 <= 0)
106     result |= 1;
107
108   if(int0 <= 1)
109     result |= 2;
110
111
112   if(int0 <= 0xff)
113     result |= 4;
114
115   if(int0 <= 0x100)
116     result |= 8;
117
118   if(int0 <= 0x0101)
119     result |= 0x10;
120   
121   if(int0 <= 0x01ff)
122     result |= 0x20;
123   
124   if(int0 <= 0x0200)
125     result |= 0x40;
126
127   if(int0 <= 0x0201)
128     result |= 0x80;
129
130   if(result != expected_result)
131     failures=1;
132
133 }
134
135
136 void int_compare1(void)
137 {
138   int0 = -1;
139   c_int_lte_lit1(0xff);
140
141   int0 = 0;
142   c_int_lte_lit1(0xff);
143
144   int0 = 1;
145   c_int_lte_lit1(0xfe);
146
147   int0 = 2;
148   c_int_lte_lit1(0xfc);
149
150   int0 = 0xfe;
151   c_int_lte_lit1(0xfc);
152
153   int0 = 0xff;
154   c_int_lte_lit1(0xfc);
155
156   int0 = 0x100;
157   c_int_lte_lit1(0xf8);
158
159   int0 = 0x101;
160   c_int_lte_lit1(0xf0);
161
162   int0 = 0x1fe;
163   c_int_lte_lit1(0xe0);
164
165   int0 = 0x1ff;
166   c_int_lte_lit1(0xe0);
167
168   int0 = 0x200;
169   c_int_lte_lit1(0xc0);
170
171   int0 = 0x201;
172   c_int_lte_lit1(0x80);
173
174   int0 = 0x7f00;
175   c_int_lte_lit1(0x0);
176
177   /* now check contiguous ranges */
178
179   for(int0 = -0x7fff; int0 != 1; int0++)
180     c_int_lte_lit1(0xff);
181
182   for(int0 = 2; int0 != 0xff; int0++)
183     c_int_lte_lit1(0xfc);
184
185   for(int0 = 0x202; int0 != 0x7fff; int0++)
186     c_int_lte_lit1(0);
187
188 }
189
190
191
192 void c_int_lte_lit2(unsigned char expected_result)
193 {
194   result = 0;
195
196   if(int0 <= -0x7fff)
197     result |= 1;
198
199   if(int0 <= -0x7f00)
200     result |= 2;
201
202   if(int0 <= -0x7eff)
203     result |= 4;
204
205   if(int0 <= -0x7e00)
206     result |= 8;
207
208   if(int0 <= -0x0101)
209     result |= 0x10;
210   
211   if(int0 <= -0x0100)
212     result |= 0x20;
213   
214   if(int0 <= -0xff)
215     result |= 0x40;
216
217   if(int0 <= -1)
218     result |= 0x80;
219
220   if(result != expected_result)
221     failures=1;
222 }
223
224 void int_compare2(void)
225 {
226   int0 = -0x7fff;
227   c_int_lte_lit2(0xff);
228
229   int0 = -0x7f00;
230   c_int_lte_lit2(0xfe);
231
232   int0 = -0x7eff;
233   c_int_lte_lit2(0xfc);
234
235   int0 = -0x7e00;
236   c_int_lte_lit2(0xf8);
237
238   int0 = -0x4567;
239   c_int_lte_lit2(0xf0);
240
241   int0 = -0x200;
242   c_int_lte_lit2(0xf0);
243
244   int0 = -0x102;
245   c_int_lte_lit2(0xf0);
246
247   int0 = -0x101;
248   c_int_lte_lit2(0xf0);
249
250   int0 = -0x100;
251   c_int_lte_lit2(0xe0);
252
253   int0 = -0xff;
254   c_int_lte_lit2(0xc0);
255
256   int0 = -0x02;
257   c_int_lte_lit2(0x80);
258
259   int0 = -0x01;
260   c_int_lte_lit2(0x80);
261
262   int0 = 0;
263   c_int_lte_lit2(0x00);
264
265   int0 = 1;
266   c_int_lte_lit2(0x00);
267
268   int0 = 0x7fff;
269   c_int_lte_lit2(0x00);
270
271   /* now check contiguous ranges */
272
273   for(int0 = -0x7ffe; int0 != -0x7f00; int0++)
274     c_int_lte_lit2(0xfe);
275
276   for(int0 = -0x7dff; int0 != -0x101; int0++)
277     c_int_lte_lit2(0xf0);
278
279   for(int0 = 0; int0 != 0x7fff; int0++)
280     c_int_lte_lit2(0);
281
282 }
283
284
285 void
286 main (void)
287 {
288   char_compare();
289   int_compare1();
290   int_compare2();
291
292   success = failures;
293   done ();
294 }