Added regression tests for compound comparisons.
[fw/sdcc] / src / regression / compare7.c
1 #define __16F873
2 #include "p16f873.h"
3 // Signed comparisons of the form:  (variable<LIT)
4 //
5 // This regression test exercises all of the boundary
6 // conditions in literal less than comparisons. There
7 // are numerous opportunities to optimize these comparison
8 // and each one has an astonishing capability of failing
9 // a boundary condition.
10
11 unsigned char success = 0;
12 unsigned char failures = 0;
13 unsigned char dummy = 0;
14 unsigned char result = 0;
15
16 //bit bit0 = 0;
17 int int0 = 0;
18 int int1 = 0;
19 signed char char0 = 0;
20 signed char char1 = 0;
21 char long0 = 0;
22 char long1 = 0;
23
24
25 /* copied from 16f877.inc file supplied with gpasm */
26
27 #define _CP_ALL          0x0FCF
28 #define _CP_HALF         0x1FDF
29 #define _CP_UPPER_256    0x2FEF
30 #define _CP_OFF          0x3FFF
31 #define _DEBUG_ON        0x37FF
32 #define _DEBUG_OFF       0x3FFF
33 #define _WRT_ENABLE_ON   0x3FFF
34 #define _WRT_ENABLE_OFF  0x3DFF
35 #define _CPD_ON          0x3EFF
36 #define _CPD_OFF         0x3FFF
37 #define _LVP_ON          0x3FFF
38 #define _LVP_OFF         0x3F7F
39 #define _BODEN_ON        0x3FFF
40 #define _BODEN_OFF       0x3FBF
41 #define _PWRTE_OFF       0x3FFF
42 #define _PWRTE_ON        0x3FF7
43 #define _WDT_ON          0x3FFF
44 #define _WDT_OFF         0x3FFB
45 #define _LP_OSC          0x3FFC
46 #define _XT_OSC          0x3FFD
47 #define _HS_OSC          0x3FFE
48 #define _RC_OSC          0x3FFF
49
50 /* *** NOTE ***  This particular test takes quite a while to run 
51  * ~ 10,000,000 instruction cycles. (2.5 seconds on a 20Mhz PIC).
52  * The WDT will reset the CPU if it's enabled. So disable it...
53 */
54
55 typedef unsigned int word;
56
57 word at 0x2007  CONFIG = _WDT_OFF & _PWRTE_ON;
58
59 void
60 done ()
61 {
62   dummy++;
63 }
64
65
66 void c_char_lt_lit1(unsigned char expected_result)
67 {
68   result = 0;
69
70   if(char0 < -0x7f)
71     result |= 1;
72
73
74   if(char0 < -1)
75     result |= 2;
76
77   if(char0 < 0)
78     result |= 4;
79
80   if(char0 < 1)
81     result |= 8;
82
83   if(char0 < 0x7f)
84     result |= 0x10;
85   
86   if(result != expected_result)
87     failures++;
88 }
89
90
91 void char_compare(void)
92 {
93   char0 = 0x7f;
94   c_char_lt_lit1(0);
95
96   char0 = 0x7e;
97   c_char_lt_lit1(0x10);
98
99   char0 = 0x40;
100   c_char_lt_lit1(0x10);
101
102   char0 = 0x2;
103   c_char_lt_lit1(0x10);
104
105   char0 = 0x1;
106   c_char_lt_lit1(0x10);
107
108   char0 = 0;
109   c_char_lt_lit1(0x18);
110
111   char0 = -1;
112   c_char_lt_lit1(0x1c);
113
114   char0 = -2;
115   c_char_lt_lit1(0x1e);
116
117   char0 = -0x40;
118   c_char_lt_lit1(0x1e);
119
120   char0 = -0x7e;
121   c_char_lt_lit1(0x1e);
122
123   char0 = -0x7f;
124   c_char_lt_lit1(0x1e);
125
126   char0 = 0x80;
127   c_char_lt_lit1(0x1f);
128
129
130   /* Now test entire range */
131
132   for(char0=1; char0 != 0x7f; char0++)
133     c_char_lt_lit1(0x10);
134
135
136   for(char0=-0x7f; char0 != -1; char0++)
137     c_char_lt_lit1(0x1e);
138
139
140 }
141
142 void c_int_lt_lit1(unsigned char expected_result)
143 {
144   result = 0;
145
146   if(int0 < 0)
147     result |= 1;
148
149   if(int0 < 1)
150     result |= 2;
151
152
153   if(int0 < 0xff)
154     result |= 4;
155
156   if(int0 < 0x100)
157     result |= 8;
158
159   if(int0 < 0x0101)
160     result |= 0x10;
161   
162   if(int0 < 0x01ff)
163     result |= 0x20;
164   
165   if(int0 < 0x0200)
166     result |= 0x40;
167
168   if(int0 < 0x0201)
169     result |= 0x80;
170
171   if(result != expected_result)
172     failures=1;
173
174 }
175
176
177 void int_compare1(void)
178 {
179   int0 = -1;
180   c_int_lt_lit1(0xff);
181
182   int0 = 0;
183   c_int_lt_lit1(0xfe);
184
185   int0 = 1;
186   c_int_lt_lit1(0xfc);
187
188   int0 = 2;
189   c_int_lt_lit1(0xfc);
190
191   int0 = 0xfe;
192   c_int_lt_lit1(0xfc);
193
194   int0 = 0xff;
195   c_int_lt_lit1(0xf8);
196
197   int0 = 0x100;
198   c_int_lt_lit1(0xf0);
199
200   int0 = 0x101;
201   c_int_lt_lit1(0xe0);
202
203   int0 = 0x1fe;
204   c_int_lt_lit1(0xe0);
205
206   int0 = 0x1ff;
207   c_int_lt_lit1(0xc0);
208
209   int0 = 0x200;
210   c_int_lt_lit1(0x80);
211
212   int0 = 0x201;
213   c_int_lt_lit1(0x0);
214
215   int0 = 0x7f00;
216   c_int_lt_lit1(0x0);
217
218   /* now check contiguous ranges */
219
220   for(int0 = -0x7fff; int0 != -1; int0++)
221     c_int_lt_lit1(0xff);
222
223   for(int0 = 1; int0 != 0xff; int0++)
224     c_int_lt_lit1(0xfc);
225
226   for(int0 = 0x201; int0 != 0x7fff; int0++)
227     c_int_lt_lit1(0);
228
229 }
230
231
232
233 void c_int_lt_lit2(unsigned char expected_result)
234 {
235   result = 0;
236
237   if(int0 < -0x7fff)
238     result |= 1;
239
240   if(int0 < -0x7f00)
241     result |= 2;
242
243   if(int0 < -0x7eff)
244     result |= 4;
245
246   if(int0 < -0x7e00)
247     result |= 8;
248
249   if(int0 < -0x0101)
250     result |= 0x10;
251   
252   if(int0 < -0x0100)
253     result |= 0x20;
254   
255   if(int0 < -0xff)
256     result |= 0x40;
257
258   if(int0 < -1)
259     result |= 0x80;
260
261   if(result != expected_result)
262     failures=1;
263 }
264
265 void int_compare2(void)
266 {
267   int0 = -0x7fff;
268   c_int_lt_lit2(0xfe);
269
270   int0 = -0x7f00;
271   c_int_lt_lit2(0xfc);
272
273   int0 = -0x7eff;
274   c_int_lt_lit2(0xf8);
275
276   int0 = -0x7e00;
277   c_int_lt_lit2(0xf0);
278
279   int0 = -0x4567;
280   c_int_lt_lit2(0xf0);
281
282   int0 = -0x200;
283   c_int_lt_lit2(0xf0);
284
285   int0 = -0x102;
286   c_int_lt_lit2(0xf0);
287
288   int0 = -0x101;
289   c_int_lt_lit2(0xe0);
290
291   int0 = -0x100;
292   c_int_lt_lit2(0xc0);
293
294   int0 = -0xff;
295   c_int_lt_lit2(0x80);
296
297   int0 = -0x02;
298   c_int_lt_lit2(0x80);
299
300   int0 = -0x01;
301   c_int_lt_lit2(0x00);
302
303   int0 = 0;
304   c_int_lt_lit2(0x00);
305
306   int0 = 1;
307   c_int_lt_lit2(0x00);
308
309   int0 = 0x7fff;
310   c_int_lt_lit2(0x00);
311
312   /* now check contiguous ranges */
313   int0 = -0x7f01;
314   c_int_lt_lit2(0xfe);
315
316   for(int0 = -0x7ffe; int0 != -0x7f01; int0++)
317     c_int_lt_lit2(0xfe);
318
319   for(int0 = -0x7e00; int0 != -0x101; int0++)
320     c_int_lt_lit2(0xf0);
321
322   for(int0 = -1; int0 != 0x7fff; int0++)
323     c_int_lt_lit2(0);
324 }
325
326
327 void
328 main (void)
329 {
330   char_compare();
331   int_compare1();
332   int_compare2();
333
334   success = failures;
335   done ();
336 }