]> git.sur5r.net Git - cc65/blob - test/val/compare8.c
goto.c warning fix for implicit truncation
[cc65] / test / val / compare8.c
1 /*
2   !!DESCRIPTION!! Signed comparisons of the form:  (variable>LIT)
3   !!ORIGIN!!      SDCC regression tests
4   !!LICENCE!!     GPL, read COPYING.GPL
5 */
6
7 #include <stdio.h>
8 #include <limits.h>
9
10 /* This regression test exercises all of the boundary
11  conditions in literal less than comparisons. There
12  are numerous opportunities to optimize these comparison
13  and each one has an astonishing capability of failing
14  a boundary condition.
15 */
16 unsigned char success = 0;
17 unsigned char failures = 0;
18 unsigned char dummy = 0;
19 unsigned char result = 0;
20
21 #ifdef SUPPORT_BIT_TYPES
22 bit bit0 = 0;
23 #endif
24 int int0 = 0;
25 int int1 = 0;
26 unsigned char uchar0 = 0;
27 unsigned char uchar1 = 0;
28 signed char char0 = 0;
29 signed char char1 = 0;
30 char long0 = 0;
31 char long1 = 0;
32
33 /* *** NOTE ***  This particular test takes quite a while to run
34  * ~ 10,000,000 instruction cycles. (2.5 seconds on a 20Mhz PIC).
35  * The WDT will reset the CPU if it's enabled. So disable it...
36 */
37
38 void
39 done ()
40 {
41   dummy++;
42 }
43
44 void c_char_gt_lit1(unsigned char expected_result)
45 {
46   result = 0;
47
48   if(char0 > -0x7f)
49     result |= 1;
50
51   if(char0 > -1)
52     result |= 2;
53
54   if(char0 > 0)
55     result |= 4;
56
57   if(char0 > 1)
58     result |= 8;
59
60   if(char0 > 0x7e)
61     result |= 0x10;
62   
63   if(char0 > 0x7f)
64     result |= 0x20;
65   
66   if(result != expected_result)
67     failures++;
68 }
69
70 void char_compare(void)
71 {
72   char0 = 0x7f;
73   c_char_gt_lit1(0x1f);
74
75   char0 = 0x7e;
76   c_char_gt_lit1(0x0f);
77
78   char0 = 0x40;
79   c_char_gt_lit1(0x0f);
80
81   char0 = 0x2;
82   c_char_gt_lit1(0x0f);
83
84   char0 = 0x1;
85   c_char_gt_lit1(0x07);
86
87   char0 = 0;
88   c_char_gt_lit1(0x03);
89
90   char0 = -1;
91   c_char_gt_lit1(0x01);
92
93   char0 = -2;
94   c_char_gt_lit1(0x01);
95
96   char0 = -0x40;
97   c_char_gt_lit1(0x01);
98
99   char0 = -0x7e;
100   c_char_gt_lit1(0x01);
101
102   char0 = -0x7f;
103   c_char_gt_lit1(0x00);
104
105   char0 = 0x80;
106   c_char_gt_lit1(0x00);
107
108   /* Now test entire range */
109
110   for(char0=2; char0 != 0x7f; char0++)
111     c_char_gt_lit1(0x0f);
112
113   for(char0=-0x7e; char0 != -1; char0++)
114     c_char_gt_lit1(0x01);
115 }
116
117 void c_int_gt_lit1(unsigned char expected_result)
118 {
119   result = 0;
120
121   if(int0 > 0)
122     result |= 1;
123
124   if(int0 > 1)
125     result |= 2;
126
127   if(int0 > 0xff)
128     result |= 4;
129
130   if(int0 > 0x100)
131     result |= 8;
132
133   if(int0 > 0x0101)
134     result |= 0x10;
135   
136   if(int0 > 0x01ff)
137     result |= 0x20;
138   
139   if(int0 > 0x0200)
140     result |= 0x40;
141
142   if(int0 > 0x0201)
143     result |= 0x80;
144
145   if(result != expected_result)
146     failures=1;
147 }
148
149 void int_compare1(void)
150 {
151   int0 = -1;
152   c_int_gt_lit1(0x00);
153
154   int0 = 0;
155   c_int_gt_lit1(0x00);
156
157   int0 = 1;
158   c_int_gt_lit1(0x01);
159
160   int0 = 2;
161   c_int_gt_lit1(0x03);
162
163   int0 = 0xfe;
164   c_int_gt_lit1(0x03);
165
166   int0 = 0xff;
167   c_int_gt_lit1(0x03);
168
169   int0 = 0x100;
170   c_int_gt_lit1(0x07);
171
172   int0 = 0x101;
173   c_int_gt_lit1(0x0f);
174
175   int0 = 0x102;
176   c_int_gt_lit1(0x1f);
177
178   int0 = 0x1fe;
179   c_int_gt_lit1(0x1f);
180
181   int0 = 0x1ff;
182   c_int_gt_lit1(0x1f);
183
184   int0 = 0x200;
185   c_int_gt_lit1(0x3f);
186
187   int0 = 0x201;
188   c_int_gt_lit1(0x7f);
189
190   int0 = 0x7f00;
191   c_int_gt_lit1(0xff);
192
193   /* now check contiguous ranges */
194
195   for(int0 = -0x7fff; int0 != -1; int0++)
196     c_int_gt_lit1(0x00);
197
198   for(int0 = 2; int0 != 0xff; int0++)
199     c_int_gt_lit1(0x03);
200
201   for(int0 = 0x202; int0 != 0x7fff; int0++)
202     c_int_gt_lit1(0xff);
203 }
204
205 void c_int_gt_lit2(unsigned char expected_result)
206 {
207   result = 0;
208
209   if(int0 > -0x7fff)
210     result |= 1;
211
212   if(int0 > -0x7f00)
213     result |= 2;
214
215   if(int0 > -0x7eff)
216     result |= 4;
217
218   if(int0 > -0x7e00)
219     result |= 8;
220
221   if(int0 > -0x0101)
222     result |= 0x10;
223   
224   if(int0 > -0x0100)
225     result |= 0x20;
226   
227   if(int0 > -0xff)
228     result |= 0x40;
229
230   if(int0 > -1)
231     result |= 0x80;
232
233   if(result != expected_result)
234     failures=1;
235 }
236
237 void int_compare2(void)
238 {
239   int0 = -0x7fff;
240   c_int_gt_lit2(0x00);
241
242   int0 = -0x7f00;
243   c_int_gt_lit2(0x01);
244
245   int0 = -0x7eff;
246   c_int_gt_lit2(0x03);
247
248   int0 = -0x7e00;
249   c_int_gt_lit2(0x07);
250
251   int0 = -0x7dff;
252   c_int_gt_lit2(0x0f);
253
254   int0 = -0x4567;
255   c_int_gt_lit2(0x0f);
256
257   int0 = -0x200;
258   c_int_gt_lit2(0x0f);
259
260   int0 = -0x102;
261   c_int_gt_lit2(0x0f);
262
263   int0 = -0x101;
264   c_int_gt_lit2(0x0f);
265
266   int0 = -0x100;
267   c_int_gt_lit2(0x1f);
268
269   int0 = -0xff;
270   c_int_gt_lit2(0x3f);
271
272   int0 = -0x02;
273   c_int_gt_lit2(0x7f);
274
275   int0 = -0x01;
276   c_int_gt_lit2(0x7f);
277
278   int0 = 0;
279   c_int_gt_lit2(0xff);
280
281   int0 = 1;
282   c_int_gt_lit2(0xff);
283
284   int0 = 0x7fff;
285   c_int_gt_lit2(0xff);
286
287   /* now check contiguous ranges */
288
289   for(int0 = -0x7ffe; int0 != -0x7f01; int0++)
290     c_int_gt_lit2(0x01);
291
292   for(int0 = -0x7dff; int0 != -0x101; int0++)
293     c_int_gt_lit2(0x0f);
294
295   for(int0 = 0; int0 != 0x7fff; int0++)
296     c_int_gt_lit2(0xff);
297 }
298
299 int
300 main (void)
301 {
302   char_compare();
303   int_compare1();
304   int_compare2();
305
306   success = failures;
307   done ();
308   printf("failures: %d\n",failures);
309
310   return failures;
311 }