2004-09-11 Ben Maurer <bmaurer@users.sourceforge.net>
[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_10_simple_cast () {
33                 long a = 10;
34                 return (int)a;
35         }
36
37         static int test_1_bigmul1 () {
38                 int a;
39                 int b;
40                 long c;
41                 a = 10;
42                 b = 10;
43                 c = (long)a * (long)b;
44                 if (c == 100)
45                         return 1;
46                 return 0;
47         }
48
49         static int test_1_bigmul2 () {
50                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
51                 long s = System.Int64.MinValue;
52                 long c;
53                 c = s + (long) a * (long) b;
54                 if (c == -4611686022722355199)
55                         return 1;
56                 return 0;
57         }
58         
59         static int test_1_bigmul3 () {
60                 int a = 10, b = 10;
61                 ulong c;
62                 c = (ulong) a * (ulong) b;
63                 if (c == 100)
64                         return 1;
65                 return 0;
66         }
67
68         static int test_1_bigmul4 () {
69                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
70                 ulong c;
71                 c = (ulong) a * (ulong) b;
72                 if (c == 4611686014132420609)
73                         return 1;
74                 return 0;
75         }
76         
77         static int test_1_bigmul5 () {
78                 int a = System.Int32.MaxValue, b = System.Int32.MinValue;
79                 long c;
80                 c = (long) a * (long) b;
81                 if (c == -4611686016279904256)
82                         return 1;
83                 return 0;
84         }
85         
86         static int test_1_bigmul6 () {
87                 uint a = System.UInt32.MaxValue, b = System.UInt32.MaxValue/(uint)2;
88                 ulong c;
89                 c = (ulong) a * (ulong) b;
90                 if (c == 9223372030412324865)
91                         return 1;
92                 return 0;
93         }
94         
95         static int test_0_beq () {
96                 long a = 0xffffffffff;
97                 if (a != 0xffffffffff)
98                         return 1;
99                 return 0;
100         }
101
102         static int test_0_bne_un () {
103                 long a = 0xffffffffff;
104                 if (a == 0xfffffffffe)
105                         return 1;
106                 return 0;
107         }
108
109         static int test_0_ble () {
110                 long a = 0xffffffffff;
111                 if (a > 0xffffffffff)
112                         return 1;
113                 return 0;
114         }
115
116         static int test_0_ble_un () {
117                 ulong a = 0xffffffffff;
118                 if (a > 0xffffffffff)
119                         return 1;
120                 return 0;
121         }
122
123         static int test_0_bge () {
124                 long a = 0xffffffffff;
125                 if (a < 0xffffffffff)
126                         return 1;
127                 return 0;
128         }
129
130         static int test_0_bge_un () {
131                 ulong a = 0xffffffffff;
132                 if (a < 0xffffffffff)
133                         return 1;
134                 return 0;
135         }
136
137         static int test_0_blt () {
138                 long a = 0xfffffffffe;
139                 if (a >= 0xffffffffff)
140                         return 1;
141                 return 0;
142         }
143
144         static int test_0_blt_un () {
145                 ulong a = 0xfffffffffe;
146                 if (a >= 0xffffffffff)
147                         return 1;
148                 return 0;
149         }
150
151         static int test_0_bgt () {
152                 long a = 0xffffffffff;
153                 if (a <= 0xfffffffffe)
154                         return 1;
155                 return 0;
156         }
157
158         static int test_0_conv_to_i4 () {
159                 long a = 0;
160
161                 return (int)a;
162         }
163         static int test_0_conv_from_i4 () {
164                 long a = 2;
165                 if (a != 2)
166                         return 1;
167
168                 int b = 2;
169
170                 if (a != b)
171                     return 2;
172                 return 0;
173         }
174
175         static int test_0_conv_from_i4_negative () {
176                 long a = -2;
177                 if (a != -2)
178                         return 1;
179
180                 int b = -2;
181
182                 if (a != b)
183                     return 2;
184                 return 0;
185         }
186
187         /*
188         static int test_0_conv_from_r8 () {
189                 double b = 2.0;
190                 long a = (long)b;
191
192                 if (a != 2)
193                         return 1;
194                 return 0;
195         }
196
197         static int test_0_conv_from_r4 () {
198                 float b = 2.0F;
199                 long a = (long)b;
200
201                 if (a != 2)
202                         return 1;
203                 return 0;
204         }
205         */
206         
207         static int test_8_and () {
208                 long a = 0xffffffffff;
209                 long b = 8;             
210                 return (int)(a & b);
211         }
212
213         static int test_8_and_imm () {
214                 long a = 0xffffffffff;
215                 return (int)(a & 8);
216         }
217
218         static int test_10_or () {
219                 long a = 8;
220                 long b = 2;             
221                 return (int)(a | b);
222         }
223
224         static int test_10_or_imm () {
225                 long a = 8;
226                 return (int)(a | 2);
227         }
228
229         static int test_5_xor () {
230                 long a = 7;
231                 long b = 2;             
232                 return (int)(a ^ b);
233         }
234
235         static int test_5_xor_imm () {
236                 long a = 7;
237                 return (int)(a ^ 2);
238         }
239
240         static int test_5_add () {
241                 long a = 2;
242                 long b = 3;             
243                 return (int)(a + b);
244         }
245
246         static int test_5_add_imm () {
247                 long a = 2;
248                 return (int)(a + 3);
249         }
250
251         static int test_0_add_imm_carry () {
252                 long a = -1;
253                 return (int)(a + 1);
254         }
255
256         static int test_0_add_imm_no_inc () {
257                 // we can't blindly convert an add x, 1 to an inc x
258                 long a = 0x1ffffffff;
259                 long c;
260                 c = a + 2;
261                 if (c == ((a + 1) + 1))
262                         return 0;
263                 return 1;
264         }
265
266         static int test_4_addcc_imm () {
267                 long a = 3;
268                 long b = 0;
269                 return (int)(a - b + 1);
270         }
271
272         static int test_5_sub () {
273                 long a = 8;
274                 long b = 3;             
275                 return (int)(a - b);
276         }
277
278         static int test_5_sub_imm () {
279                 long a = 8;
280                 return (int)(a - 3);
281         }
282
283         static int test_0_sub_imm_carry () {
284                 long a = 0;
285                 return (int)((a - 1) + 1);
286         }
287
288         static int test_2_neg () {
289                 long a = -2;            
290                 return (int)(-a);
291         }       
292
293         static int test_0_neg_large () {
294                 long min = -9223372036854775808;
295                 unchecked {
296                         ulong ul = (ulong)min;
297                         return (min == -(long)ul) ? 0 : 1;
298                 }
299         }       
300
301         static int test_0_shl () {
302                 long a = 9;
303                 int b = 1;
304                 
305                 if ((a >> b) != 4)
306                         return 1;
307
308
309                 return 0;
310         }
311         
312         static int test_1_rshift ()
313         {
314                 long a = 9;
315                 int b = 1;
316                 a = -9;
317                 if ((a >> b) != -5)
318                         return 0;
319                 return 1;
320         }
321
322         static int test_5_shift ()
323         {
324                 long a = 9;
325                 int b = 1;
326                 int count = 0;
327                 
328                 if ((a >> b) != 4)
329                         return count;
330                 count++;
331
332                 if ((a >> 63) != 0)
333                         return count;
334                 count++;
335
336                 if ((a << 1) != 18)
337                         return count;
338                 count++;
339
340                 if ((a << b) != 18)
341                         return count;
342                 count++;
343
344                 a = -9;
345                 if ((a >> b) != -5)
346                         return count;
347                 count++;
348
349                 return count;
350         }
351
352         static int test_1_simple_neg () {
353                 long a = 9;
354                 
355                 if (-a != -9)
356                         return 0;
357                 return 1;
358         }
359
360         static int test_2_compare () {
361                 long a = 1;
362                 long b = 1;
363                 
364                 if (a != b)
365                         return 0;
366                 return 2;
367         }
368
369         static int test_9_alu ()
370         {
371                 long a = 9, b = 6;
372                 int count = 0;
373                 
374                 if ((a + b) != 15)
375                         return count;
376                 count++;
377                 
378                 if ((a - b) != 3)
379                         return count;
380                 count++;
381
382                 if ((a & 8) != 8)
383                         return count;
384                 count++;
385
386                 if ((a | 2) != 11)
387                         return count;
388                 count++;
389
390                 if ((a * b) != 54)
391                         return count;
392                 count++;
393                 
394                 if ((a / 4) != 2)
395                         return count;
396                 count++;
397                 
398                 if ((a % 4) != 1)
399                         return count;
400                 count++;
401
402                 if (-a != -9)
403                         return count;
404                 count++;
405
406                 b = -1;
407                 if (~b != 0)
408                         return count;
409                 count++;
410
411                 return count;
412         }
413         
414         static int test_24_mul () {
415                 long a = 8;
416                 long b = 3;             
417                 return (int)(a * b);
418         }       
419         
420         static int test_24_mul_ovf () {
421                 long a = 8;
422                 long b = 3;
423                 long res;
424                 
425                 checked {
426                         res = a * b;
427                 }
428                 return (int)res;
429         }       
430
431         static int test_24_mul_un () {
432                 ulong a = 8;
433                 ulong b = 3;            
434                 return (int)(a * b);
435         }       
436         
437         static int test_24_mul_ovf_un () {
438                 ulong a = 8;
439                 ulong b = 3;
440                 ulong res;
441                 
442                 checked {
443                         res = a * b;
444                 }
445                 return (int)res;
446         }       
447         
448         static int test_4_divun () {
449                 uint b = 12;
450                 int a = 3;
451                 return (int)(b / a);
452         }
453
454         static int test_1431655764_bigdivun_imm () {
455                 unchecked {
456                         uint b = (uint)-2;
457                         return (int)(b / 3);
458                 }
459         }
460
461         static int test_1431655764_bigdivun () {
462                 unchecked {
463                         uint b = (uint)-2;
464                         int a = 3;
465                         return (int)(b / a);
466                 }
467         }
468
469         static int test_1_remun () {
470                 uint b = 13;
471                 int a = 3;
472                 return (int)(b % a);
473         }
474
475         static int test_2_bigremun () {
476                 unchecked {
477                         uint b = (uint)-2;
478                         int a = 3;
479                         return (int)(b % a);
480                 }
481         }
482
483         static int test_0_ceq () {
484                 long a = 2;
485                 long b = 2;
486                 long c = 3;
487                 long d = 0xff00000002;
488                 
489                 bool val = (a == b); // this should produce a ceq
490                 if (!val)
491                         return 1;
492                 
493                 val = (a == c); // this should produce a ceq
494                 if (val)
495                         return 2;
496                 
497                 val = (a == d); // this should produce a ceq
498                 if (val)
499                         return 3;
500                 
501                 return 0;
502         }
503
504         static int test_0_ceq_complex () {
505                 long l = 1, ll = 2;
506
507                 if (l < 0 != ll < 0)
508                         return 1;
509
510                 return 0;
511         }
512         
513         static int test_0_clt () {
514                 long a = 2;
515                 long b = 2;
516                 long c = 3;
517                 long d = 0xff00000002L;
518                 long e = -1;
519                 
520                 bool val = (a < b); // this should produce a clt
521                 if (val)
522                         return 1;
523                 
524                 val = (a < c); // this should produce a clt
525                 if (!val)
526                         return 2;
527                 
528                 val = (c < a); // this should produce a clt
529                 if (val)
530                         return 3;
531                 
532                 val = (e < d); // this should produce a clt
533                 if (!val)
534                         return 4;
535                 
536                 val = (d < e); // this should produce a clt
537                 if (val)
538                         return 5;
539                 
540                 return 0;
541         }
542         
543         static int test_0_clt_un () {
544                 ulong a = 2;
545                 ulong b = 2;
546                 ulong c = 3;
547                 ulong d = 0xff00000002;
548                 ulong e = 0xffffffffffffffff;
549                 
550                 bool val = (a < b); // this should produce a clt_un
551                 if (val)
552                         return 1;
553                 
554                 val = (a < c); // this should produce a clt_un
555                 if (!val)
556                         return 1;
557                 
558                 val = (d < e); // this should produce a clt_un
559                 if (!val)
560                         return 1;
561                 
562                 val = (e < d); // this should produce a clt_un
563                 if (val)
564                         return 1;
565                 
566                 return 0;
567         }
568
569         static int test_0_cgt () {
570                 long a = 2;
571                 long b = 2;
572                 long c = 3;
573                 long d = 0xff00000002L;
574                 long e = -1;
575                 
576                 bool val = (a > b); // this should produce a cgt
577                 if (val)
578                         return 1;
579                 
580                 val = (a > c); // this should produce a cgt
581                 if (val)
582                         return 2;
583                 
584                 val = (c > a); // this should produce a cgt
585                 if (!val)
586                         return 3;
587                 
588                 val = (e > d); // this should produce a cgt
589                 if (val)
590                         return 4;
591                 
592                 val = (d > e); // this should produce a cgt
593                 if (!val)
594                         return 5;
595                 
596                 return 0;
597         }
598
599         static int test_0_cgt_un () {
600                 ulong a = 2;
601                 ulong b = 2;
602                 ulong c = 3;
603                 ulong d = 0xff00000002;
604                 ulong e = 0xffffffffffffffff;
605                 
606                 bool val = (a > b); // this should produce a cgt_un
607                 if (val)
608                         return 1;
609                 
610                 val = (a > c); // this should produce a cgt_un
611                 if (val)
612                         return 1;
613                 
614                 val = (d > e); // this should produce a cgt_un
615                 if (val)
616                         return 1;
617                 
618                 val = (e > d); // this should produce a cgt_un
619                 if (!val)
620                         return 1;
621                 
622                 return 0;
623         }
624
625         static long return_5low () {
626                 return 5;
627         }
628         
629         static long return_5high () {
630                 return 0x500000000;
631         }
632
633         static int test_3_long_ret () {
634                 long val = return_5low ();
635                 return (int) (val - 2);
636         }
637
638         static int test_1_long_ret2 () {
639                 long val = return_5high ();
640                 if (val > 0xffffffff)
641                         return 1;
642                 return 0;
643         }
644
645         static int test_3_byte_cast () {
646                 ulong val = 0xff00ff00f0f0f0f0;
647                 byte b;
648                 b = (byte) (val & 0xFF);
649                 if (b != 0xf0)
650                         return 1;
651                 return 3;
652         }
653         
654         static int test_4_ushort_cast () {
655                 ulong val = 0xff00ff00f0f0f0f0;
656                 ushort b;
657                 b = (ushort) (val & 0xFFFF);
658                 if (b != 0xf0f0)
659                         return 1;
660                 return 4;
661         }
662
663         static int test_500_mul_div () {
664                 long val = 1000;
665                 long exp = 10;
666                 long maxexp = 20;
667                 long res = val * exp / maxexp;
668
669                 return (int)res;
670         }
671
672         static long position = 0;
673
674         static int test_4_static_inc_long () {
675
676                 int count = 4;
677
678                 position = 0;
679
680                 position += count;
681
682                 return (int)position;
683         }
684         
685         static void doit (double value, out long m) {
686                 m = (long) value;
687         }
688         
689         static int test_3_checked_cast_un () {
690                 ulong i = 2;
691                 long j;
692
693                 checked { j = (long)i; }
694
695                 if (j != 2)
696                         return 0;
697                 return 3;
698         }
699         
700         static int test_4_checked_cast () {
701                 long i = 3;
702                 ulong j;
703
704                 checked { j = (ulong)i; }
705
706                 if (j != 3)
707                         return 0;
708                 return 4;
709         }
710
711         static int test_10_int_uint_compare () {
712                 uint size = 10;
713                 int j = 0;
714                 for (int i = 0; i < size; ++i) {
715                         j++;
716                 }
717                 return j;
718         }
719
720         static int test_0_ftol_clobber () {
721                 long m;
722                 doit (1.3, out m);
723                 if (m != 1)
724                         return 2;
725                 return 0;
726         }
727
728         static int test_71_long_shift_right () {
729                 ulong value = 38654838087;
730                 int x = 0;
731                 byte [] buffer = new byte [1];
732                 buffer [x] = ((byte)(value >> x));
733                 return buffer [x];
734         }
735
736         static int test_0_ulong_regress () {
737                 ulong u = 4257145737;
738                 u --;
739                 return (u == 4257145736) ? 0 : 1;
740         }
741 }
742