]> git.sur5r.net Git - cc65/blob - test/val/compare10.c
Merge remote-tracking branch 'upstream/master' into pcenginetarget
[cc65] / test / val / compare10.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_gte_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   {
68 /*    LOG_ERROR(1); */
69     printf("c_char_gte_lit1: char %02x - result %02x expected: %02x\n",char0,result,expected_result);
70     failures++;
71   }
72
73 /*  printf("char %02x - %02x failures: %d\n",char0,expected_result,failures); */
74 }
75
76 void char_compare(void)
77 {
78   char0 = 0x7f;
79   c_char_gte_lit1(0x3f);
80
81   char0 = 0x7e;
82   c_char_gte_lit1(0x1f);
83
84   char0 = 0x40;
85   c_char_gte_lit1(0x0f);
86
87   char0 = 0x2;
88   c_char_gte_lit1(0x0f);
89
90   char0 = 0x1;
91   c_char_gte_lit1(0x0f);
92
93   char0 = 0;
94   c_char_gte_lit1(0x07);
95
96   char0 = -1;
97   c_char_gte_lit1(0x03);
98
99   char0 = -2;
100   c_char_gte_lit1(0x01);
101
102   char0 = -0x40;
103   c_char_gte_lit1(0x01);
104
105   char0 = -0x7e;
106   c_char_gte_lit1(0x01);
107
108   char0 = -0x7f;
109   c_char_gte_lit1(0x01);
110
111   char0 = 0x80;
112   c_char_gte_lit1(0x00);
113
114   /* Now test entire range */
115
116   for(char0=1; char0 != 0x7e; char0++)
117     c_char_gte_lit1(0x0f);
118
119   for(char0=-0x7f; char0 != -1; char0++)
120     c_char_gte_lit1(0x01);
121 }
122
123 void c_int_gte_lit1(unsigned char expected_result)
124 {
125   result = 0;
126
127   if(int0 >= 0)
128     result |= 1;
129
130   if(int0 >= 1)
131     result |= 2;
132
133   if(int0 >= 0xff)
134     result |= 4;
135
136   if(int0 >= 0x100)
137     result |= 8;
138
139   if(int0 >= 0x0101)
140     result |= 0x10;
141   
142   if(int0 >= 0x01ff)
143     result |= 0x20;
144   
145   if(int0 >= 0x0200)
146     result |= 0x40;
147
148   if(int0 >= 0x0201)
149     result |= 0x80;
150
151   if(result != expected_result)
152     failures=1;
153 }
154
155 void int_compare1(void)
156 {
157   int0 = -1;
158   c_int_gte_lit1(0x00);
159
160   int0 = 0;
161   c_int_gte_lit1(0x01);
162
163   int0 = 1;
164   c_int_gte_lit1(0x03);
165
166   int0 = 2;
167   c_int_gte_lit1(0x03);
168
169   int0 = 0xfe;
170   c_int_gte_lit1(0x03);
171
172   int0 = 0xff;
173   c_int_gte_lit1(0x07);
174
175   int0 = 0x100;
176   c_int_gte_lit1(0x0f);
177
178   int0 = 0x101;
179   c_int_gte_lit1(0x1f);
180
181   int0 = 0x102;
182   c_int_gte_lit1(0x1f);
183
184   int0 = 0x1fe;
185   c_int_gte_lit1(0x1f);
186
187   int0 = 0x1ff;
188   c_int_gte_lit1(0x3f);
189
190   int0 = 0x200;
191   c_int_gte_lit1(0x7f);
192
193   int0 = 0x201;
194   c_int_gte_lit1(0xff);
195
196   int0 = 0x7f00;
197   c_int_gte_lit1(0xff);
198
199   /* now check contiguous ranges */
200
201   for(int0 = -0x7fff; int0 != -1; int0++)
202     c_int_gte_lit1(0x00);
203
204   for(int0 = 1; int0 != 0xff; int0++)
205     c_int_gte_lit1(0x03);
206
207   for(int0 = 0x201; int0 != 0x7fff; int0++)
208     c_int_gte_lit1(0xff);
209 }
210
211 void c_int_gte_lit2(unsigned char expected_result)
212 {
213   result = 0;
214
215   if(int0 >= -0x7fff)
216     result |= 1;
217
218   if(int0 >= -0x7f00)
219     result |= 2;
220
221   if(int0 >= -0x7eff)
222     result |= 4;
223
224   if(int0 >= -0x7e00)
225     result |= 8;
226
227   if(int0 >= -0x0101)
228     result |= 0x10;
229   
230   if(int0 >= -0x0100)
231     result |= 0x20;
232   
233   if(int0 >= -0xff)
234     result |= 0x40;
235
236   if(int0 >= -1)
237     result |= 0x80;
238
239   if(result != expected_result)
240     failures=1;
241 }
242
243 void int_compare2(void)
244 {
245   int0 = -0x7fff;
246   c_int_gte_lit2(0x01);
247
248   int0 = -0x7f00;
249   c_int_gte_lit2(0x03);
250
251   int0 = -0x7eff;
252   c_int_gte_lit2(0x07);
253
254   int0 = -0x7e00;
255   c_int_gte_lit2(0x0f);
256
257   int0 = -0x7dff;
258   c_int_gte_lit2(0x0f);
259
260   int0 = -0x4567;
261   c_int_gte_lit2(0x0f);
262
263   int0 = -0x200;
264   c_int_gte_lit2(0x0f);
265
266   int0 = -0x102;
267   c_int_gte_lit2(0x0f);
268
269   int0 = -0x101;
270   c_int_gte_lit2(0x1f);
271
272   int0 = -0x100;
273   c_int_gte_lit2(0x3f);
274
275   int0 = -0xff;
276   c_int_gte_lit2(0x7f);
277
278   int0 = -0x02;
279   c_int_gte_lit2(0x7f);
280
281   int0 = -0x01;
282   c_int_gte_lit2(0xff);
283
284   int0 = 0;
285   c_int_gte_lit2(0xff);
286
287   int0 = 1;
288   c_int_gte_lit2(0xff);
289
290   int0 = 0x7fff;
291   c_int_gte_lit2(0xff);
292
293   /* now check contiguous ranges */
294
295   for(int0 = -0x7fff; int0 != -0x7f00; int0++)
296     c_int_gte_lit2(0x01);
297
298   for(int0 = -0x7e00; int0 != -0x101; int0++)
299     c_int_gte_lit2(0x0f);
300
301   for(int0 = -1; int0 != 0x7fff; int0++)
302     c_int_gte_lit2(0xff);
303 }
304
305 int
306 main (void)
307 {
308   char_compare();
309   printf("failures: %d\n",failures);
310   int_compare1();
311   printf("failures: %d\n",failures);
312   int_compare2();
313
314   success = failures;
315   done ();
316   printf("failures: %d\n",failures);
317
318   return failures;
319 }