Wed Jun 11 18:01:06 CEST 2003 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / basic-long.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         static int test_1_bigmul1 () {
33                 int a;
34                 int b;
35                 long c;
36                 a = 10;
37                 b = 10;
38                 c = (long)a * (long)b;
39                 if (c == 100)
40                         return 1;
41                 return 0;
42         }
43
44         static int test_1_bigmul2 () {
45                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
46                 long s = System.Int64.MinValue;
47                 long c;
48                 c = s + (long) a * (long) b;
49                 if (c == -4611686022722355199)
50                         return 1;
51                 return 0;
52         }
53         
54         static int test_1_bigmul3 () {
55                 int a = 10, b = 10;
56                 ulong c;
57                 c = (ulong) a * (ulong) b;
58                 if (c == 100)
59                         return 1;
60                 return 0;
61         }
62
63         static int test_1_bigmul4 () {
64                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
65                 ulong c;
66                 c = (ulong) a * (ulong) b;
67                 if (c == 4611686014132420609)
68                         return 1;
69                 return 0;
70         }
71         
72         static int test_0_beq () {
73                 long a = 0xffffffffff;
74                 if (a != 0xffffffffff)
75                         return 1;
76                 return 0;
77         }
78
79         static int test_0_bne_un () {
80                 long a = 0xffffffffff;
81                 if (a == 0xfffffffffe)
82                         return 1;
83                 return 0;
84         }
85
86         static int test_0_ble () {
87                 long a = 0xffffffffff;
88                 if (a > 0xffffffffff)
89                         return 1;
90                 return 0;
91         }
92
93         static int test_0_ble_un () {
94                 ulong a = 0xffffffffff;
95                 if (a > 0xffffffffff)
96                         return 1;
97                 return 0;
98         }
99
100         static int test_0_bge () {
101                 long a = 0xffffffffff;
102                 if (a < 0xffffffffff)
103                         return 1;
104                 return 0;
105         }
106
107         static int test_0_bge_un () {
108                 ulong a = 0xffffffffff;
109                 if (a < 0xffffffffff)
110                         return 1;
111                 return 0;
112         }
113
114         static int test_0_blt () {
115                 long a = 0xfffffffffe;
116                 if (a >= 0xffffffffff)
117                         return 1;
118                 return 0;
119         }
120
121         static int test_0_blt_un () {
122                 ulong a = 0xfffffffffe;
123                 if (a >= 0xffffffffff)
124                         return 1;
125                 return 0;
126         }
127
128         static int test_0_bgt () {
129                 long a = 0xffffffffff;
130                 if (a <= 0xfffffffffe)
131                         return 1;
132                 return 0;
133         }
134
135         static int test_0_conv_to_i4 () {
136                 long a = 0;
137
138                 return (int)a;
139         }
140         static int test_0_conv_from_i4 () {
141                 long a = 2;
142                 if (a != 2)
143                         return 1;
144
145                 int b = 2;
146
147                 if (a != b)
148                     return 2;
149                 return 0;
150         }
151
152         static int test_0_conv_from_i4_negative () {
153                 long a = -2;
154                 if (a != -2)
155                         return 1;
156
157                 int b = -2;
158
159                 if (a != b)
160                     return 2;
161                 return 0;
162         }
163
164         /*
165         static int test_0_conv_from_r8 () {
166                 double b = 2.0;
167                 long a = (long)b;
168
169                 if (a != 2)
170                         return 1;
171                 return 0;
172         }
173
174         static int test_0_conv_from_r4 () {
175                 float b = 2.0F;
176                 long a = (long)b;
177
178                 if (a != 2)
179                         return 1;
180                 return 0;
181         }
182         */
183         
184         static int test_8_and () {
185                 long a = 0xffffffffff;
186                 long b = 8;             
187                 return (int)(a & b);
188         }
189
190         static int test_8_and_imm () {
191                 long a = 0xffffffffff;
192                 return (int)(a & 8);
193         }
194
195         static int test_10_or () {
196                 long a = 8;
197                 long b = 2;             
198                 return (int)(a | b);
199         }
200
201         static int test_10_or_imm () {
202                 long a = 8;
203                 return (int)(a | 2);
204         }
205
206         static int test_5_xor () {
207                 long a = 7;
208                 long b = 2;             
209                 return (int)(a ^ b);
210         }
211
212         static int test_5_xor_imm () {
213                 long a = 7;
214                 return (int)(a ^ 2);
215         }
216
217         static int test_5_add () {
218                 long a = 2;
219                 long b = 3;             
220                 return (int)(a + b);
221         }
222
223         static int test_5_add_imm () {
224                 long a = 2;
225                 return (int)(a + 3);
226         }
227
228         static int test_0_add_imm_no_inc () {
229                 // we can't blindly convert an add x, 1 to an inc x
230                 long a = 0x1ffffffff;
231                 long c;
232                 c = a + 2;
233                 if (c == ((a + 1) + 1))
234                         return 0;
235                 return 1;
236         }
237
238         static int test_5_sub () {
239                 long a = 8;
240                 long b = 3;             
241                 return (int)(a - b);
242         }
243
244         static int test_5_sub_imm () {
245                 long a = 8;
246                 return (int)(a - 3);
247         }
248
249         static int test_2_neg () {
250                 long a = -2;            
251                 return (int)(-a);
252         }       
253
254         static int test_0_shl () {
255                 long a = 9;
256                 int b = 1;
257                 
258                 if ((a >> b) != 4)
259                         return 1;
260
261
262                 return 0;
263         }
264         
265         static int test_1_rshift ()
266         {
267                 long a = 9;
268                 int b = 1;
269                 a = -9;
270                 if ((a >> b) != -5)
271                         return 0;
272                 return 1;
273         }
274
275         static int test_5_shift ()
276         {
277                 long a = 9;
278                 int b = 1;
279                 int count = 0;
280                 
281                 if ((a >> b) != 4)
282                         return count;
283                 count++;
284
285                 if ((a >> 63) != 0)
286                         return count;
287                 count++;
288
289                 if ((a << 1) != 18)
290                         return count;
291                 count++;
292
293                 if ((a << b) != 18)
294                         return count;
295                 count++;
296
297                 a = -9;
298                 if ((a >> b) != -5)
299                         return count;
300                 count++;
301
302                 return count;
303         }
304
305         static int test_1_simple_neg () {
306                 long a = 9;
307                 
308                 if (-a != -9)
309                         return 0;
310                 return 1;
311         }
312
313         static int test_2_compare () {
314                 long a = 1;
315                 long b = 1;
316                 
317                 if (a != b)
318                         return 0;
319                 return 2;
320         }
321
322         static int test_9_alu ()
323         {
324                 long a = 9, b = 6;
325                 int count = 0;
326                 
327                 if ((a + b) != 15)
328                         return count;
329                 count++;
330                 
331                 if ((a - b) != 3)
332                         return count;
333                 count++;
334
335                 if ((a & 8) != 8)
336                         return count;
337                 count++;
338
339                 if ((a | 2) != 11)
340                         return count;
341                 count++;
342
343                 if ((a * b) != 54)
344                         return count;
345                 count++;
346                 
347                 if ((a / 4) != 2)
348                         return count;
349                 count++;
350                 
351                 if ((a % 4) != 1)
352                         return count;
353                 count++;
354
355                 if (-a != -9)
356                         return count;
357                 count++;
358
359                 b = -1;
360                 if (~b != 0)
361                         return count;
362                 count++;
363
364                 return count;
365         }
366         
367         static int test_24_mul () {
368                 long a = 8;
369                 long b = 3;             
370                 return (int)(a * b);
371         }       
372         
373         static int test_24_mul_ovf () {
374                 long a = 8;
375                 long b = 3;
376                 long res;
377                 
378                 checked {
379                         res = a * b;
380                 }
381                 return (int)res;
382         }       
383
384         static int test_24_mul_un () {
385                 ulong a = 8;
386                 ulong b = 3;            
387                 return (int)(a * b);
388         }       
389         
390         static int test_24_mul_ovf_un () {
391                 ulong a = 8;
392                 ulong b = 3;
393                 ulong res;
394                 
395                 checked {
396                         res = a * b;
397                 }
398                 return (int)res;
399         }       
400         
401         static int test_4_divun () {
402                 uint b = 12;
403                 int a = 3;
404                 return (int)(b / a);
405         }
406
407         static int test_1431655764_bigdivun_imm () {
408                 uint b = (uint)-2;
409                 return (int)(b / 3);
410         }
411
412         static int test_1431655764_bigdivun () {
413                 uint b = (uint)-2;
414                 int a = 3;
415                 return (int)(b / a);
416         }
417
418         static int test_1_remun () {
419                 uint b = 13;
420                 int a = 3;
421                 return (int)(b % a);
422         }
423
424         static int test_2_bigremun () {
425                 uint b = (uint)-2;
426                 int a = 3;
427                 return (int)(b % a);
428         }
429
430         static int test_0_ceq () {
431                 long a = 2;
432                 long b = 2;
433                 long c = 3;
434                 long d = 0xff00000002;
435                 
436                 bool val = (a == b); // this should produce a ceq
437                 if (!val)
438                         return 1;
439                 
440                 val = (a == c); // this should produce a ceq
441                 if (val)
442                         return 2;
443                 
444                 val = (a == d); // this should produce a ceq
445                 if (val)
446                         return 3;
447                 
448                 return 0;
449         }
450         
451         static int test_0_clt () {
452                 long a = 2;
453                 long b = 2;
454                 long c = 3;
455                 long d = 0xff00000002L;
456                 long e = -1;
457                 
458                 bool val = (a < b); // this should produce a clt
459                 if (val)
460                         return 1;
461                 
462                 val = (a < c); // this should produce a clt
463                 if (!val)
464                         return 2;
465                 
466                 val = (c < a); // this should produce a clt
467                 if (val)
468                         return 3;
469                 
470                 val = (e < d); // this should produce a clt
471                 if (!val)
472                         return 4;
473                 
474                 val = (d < e); // this should produce a clt
475                 if (val)
476                         return 5;
477                 
478                 return 0;
479         }
480         
481         static int test_0_clt_un () {
482                 ulong a = 2;
483                 ulong b = 2;
484                 ulong c = 3;
485                 ulong d = 0xff00000002;
486                 ulong e = 0xffffffffffffffff;
487                 
488                 bool val = (a < b); // this should produce a clt_un
489                 if (val)
490                         return 1;
491                 
492                 val = (a < c); // this should produce a clt_un
493                 if (!val)
494                         return 1;
495                 
496                 val = (d < e); // this should produce a clt_un
497                 if (!val)
498                         return 1;
499                 
500                 val = (e < d); // this should produce a clt_un
501                 if (val)
502                         return 1;
503                 
504                 return 0;
505         }
506
507         static int test_0_cgt () {
508                 long a = 2;
509                 long b = 2;
510                 long c = 3;
511                 long d = 0xff00000002L;
512                 long e = -1;
513                 
514                 bool val = (a > b); // this should produce a cgt
515                 if (val)
516                         return 1;
517                 
518                 val = (a > c); // this should produce a cgt
519                 if (val)
520                         return 2;
521                 
522                 val = (c > a); // this should produce a cgt
523                 if (!val)
524                         return 3;
525                 
526                 val = (e > d); // this should produce a cgt
527                 if (val)
528                         return 4;
529                 
530                 val = (d > e); // this should produce a cgt
531                 if (!val)
532                         return 5;
533                 
534                 return 0;
535         }
536
537         static int test_0_cgt_un () {
538                 ulong a = 2;
539                 ulong b = 2;
540                 ulong c = 3;
541                 ulong d = 0xff00000002;
542                 ulong e = 0xffffffffffffffff;
543                 
544                 bool val = (a > b); // this should produce a cgt_un
545                 if (val)
546                         return 1;
547                 
548                 val = (a > c); // this should produce a cgt_un
549                 if (val)
550                         return 1;
551                 
552                 val = (d > e); // this should produce a cgt_un
553                 if (val)
554                         return 1;
555                 
556                 val = (e > d); // this should produce a cgt_un
557                 if (!val)
558                         return 1;
559                 
560                 return 0;
561         }
562
563         static long return_5low () {
564                 return 5;
565         }
566         
567         static long return_5high () {
568                 return 0x500000000;
569         }
570
571         static int test_3_long_ret () {
572                 long val = return_5low ();
573                 return (int) (val - 2);
574         }
575
576         static int test_1_long_ret2 () {
577                 long val = return_5high ();
578                 if (val > 0xffffffff)
579                         return 1;
580                 return 0;
581         }
582
583         static int test_3_byte_cast () {
584                 ulong val = 0xff00ff00f0f0f0f0;
585                 byte b;
586                 b = (byte) (val & 0xFF);
587                 if (b != 0xf0)
588                         return 1;
589                 return 3;
590         }
591         
592         static int test_4_ushort_cast () {
593                 ulong val = 0xff00ff00f0f0f0f0;
594                 ushort b;
595                 b = (ushort) (val & 0xFFFF);
596                 if (b != 0xf0f0)
597                         return 1;
598                 return 4;
599         }
600
601         static int test_500_mul_div () {
602                 long val = 1000;
603                 long exp = 10;
604                 long maxexp = 20;
605                 long res = val * exp / maxexp;
606
607                 return (int)res;
608         }
609
610         static long position = 0;
611
612         static int test_4_static_inc_long () {
613
614                 int count = 4;
615
616                 position = 0;
617
618                 position += count;
619
620                 return (int)position;
621         }
622         
623         static void doit (double value, out long m) {
624                 m = (long) value;
625         }
626         
627         static int test_0_ftol_clobber () {
628                 long m;
629                 doit (1.3, out m);
630                 if (m != 1)
631                         return 2;
632                 return 0;
633         }
634
635         static int test_3_checked_cast_un () {
636                 ulong i = 2;
637                 long j;
638
639                 checked { j = (long)i; }
640
641                 if (j != 2)
642                         return 0;
643                 return 3;
644         }
645         
646         static int test_4_checked_cast () {
647                 long i = 3;
648                 ulong j;
649
650                 checked { j = (ulong)i; }
651
652                 if (j != 3)
653                         return 0;
654                 return 4;
655         }
656 }
657