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