2005-07-21 Atsushi Enomoto <atsushi@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  * public 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         public static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         public static int test_10_simple_cast () {
33                 long a = 10;
34                 return (int)a;
35         }
36
37         public 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         public 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         public 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         public 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         public 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         public 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         public static int test_0_beq () {
96                 long a = 0xffffffffff;
97                 if (a != 0xffffffffff)
98                         return 1;
99                 return 0;
100         }
101
102         public static int test_0_bne_un () {
103                 long a = 0xffffffffff;
104                 if (a == 0xfffffffffe)
105                         return 1;
106                 return 0;
107         }
108
109         public static int test_0_ble () {
110                 long a = 0xffffffffff;
111                 if (a > 0xffffffffff)
112                         return 1;
113                 return 0;
114         }
115
116         public static int test_0_ble_un () {
117                 ulong a = 0xffffffffff;
118                 if (a > 0xffffffffff)
119                         return 1;
120                 return 0;
121         }
122
123         public static int test_0_bge () {
124                 long a = 0xffffffffff;
125                 if (a < 0xffffffffff)
126                         return 1;
127                 return 0;
128         }
129
130         public static int test_0_bge_un () {
131                 ulong a = 0xffffffffff;
132                 if (a < 0xffffffffff)
133                         return 1;
134                 return 0;
135         }
136
137         public static int test_0_blt () {
138                 long a = 0xfffffffffe;
139                 if (a >= 0xffffffffff)
140                         return 1;
141                 return 0;
142         }
143
144         public static int test_0_blt_un () {
145                 ulong a = 0xfffffffffe;
146                 if (a >= 0xffffffffff)
147                         return 1;
148                 return 0;
149         }
150
151         public static int test_0_bgt () {
152                 long a = 0xffffffffff;
153                 if (a <= 0xfffffffffe)
154                         return 1;
155                 return 0;
156         }
157
158         public static int test_0_conv_to_i4 () {
159                 long a = 0;
160
161                 return (int)a;
162         }
163
164         public static int test_32_conv_to_u4 () {
165                 long a = 32;
166
167                 return (int)(uint)a;
168         }
169
170         public static int test_15_conv_to_u4_2 () {
171                 long a = 0x10000000f;
172
173                 return (int)(uint)a;
174         }
175
176         public static int test_0_conv_from_i4 () {
177                 long a = 2;
178                 if (a != 2)
179                         return 1;
180
181                 int b = 2;
182
183                 if (a != b)
184                     return 2;
185                 return 0;
186         }
187
188         public static int test_0_conv_from_i4_negative () {
189                 long a = -2;
190                 if (a != -2)
191                         return 1;
192
193                 int b = -2;
194
195                 if (a != b)
196                     return 2;
197                 return 0;
198         }
199
200         /*
201         public static int test_0_conv_from_r8 () {
202                 double b = 2.0;
203                 long a = (long)b;
204
205                 if (a != 2)
206                         return 1;
207                 return 0;
208         }
209
210         public static int test_0_conv_from_r4 () {
211                 float b = 2.0F;
212                 long a = (long)b;
213
214                 if (a != 2)
215                         return 1;
216                 return 0;
217         }
218         */
219         
220         public static int test_8_and () {
221                 long a = 0xffffffffff;
222                 long b = 8;             
223                 return (int)(a & b);
224         }
225
226         public static int test_8_and_imm () {
227                 long a = 0xffffffffff;
228                 return (int)(a & 8);
229         }
230
231         public static int test_10_or () {
232                 long a = 8;
233                 long b = 2;             
234                 return (int)(a | b);
235         }
236
237         public static int test_10_or_imm () {
238                 long a = 8;
239                 return (int)(a | 2);
240         }
241
242         public static int test_5_xor () {
243                 long a = 7;
244                 long b = 2;             
245                 return (int)(a ^ b);
246         }
247
248         public static int test_5_xor_imm () {
249                 long a = 7;
250                 return (int)(a ^ 2);
251         }
252
253         public static int test_5_add () {
254                 long a = 2;
255                 long b = 3;             
256                 return (int)(a + b);
257         }
258
259         public static int test_5_add_imm () {
260                 long a = 2;
261                 return (int)(a + 3);
262         }
263
264         public static int test_0_add_imm_carry () {
265                 long a = -1;
266                 return (int)(a + 1);
267         }
268
269         public static int test_0_add_imm_no_inc () {
270                 // we can't blindly convert an add x, 1 to an inc x
271                 long a = 0x1ffffffff;
272                 long c;
273                 c = a + 2;
274                 if (c == ((a + 1) + 1))
275                         return 0;
276                 return 1;
277         }
278
279         public static int test_4_addcc_imm () {
280                 long a = 3;
281                 long b = 0;
282                 return (int)(a - b + 1);
283         }
284
285         public static int test_5_sub () {
286                 long a = 8;
287                 long b = 3;             
288                 return (int)(a - b);
289         }
290
291         public static int test_5_sub_imm () {
292                 long a = 8;
293                 return (int)(a - 3);
294         }
295
296         public static int test_0_sub_imm_carry () {
297                 long a = 0;
298                 return (int)((a - 1) + 1);
299         }
300
301         public static int test_0_add_ovf () {
302                 long i, j, k;
303
304                 checked {
305                         i = System.Int64.MinValue;
306                         j = 0;
307                         k = i + j;
308                 }
309
310                 if (k != System.Int64.MinValue)
311                         return 1;
312
313                 checked {
314                         i = System.Int64.MaxValue;
315                         j = 0;
316                         k = i + j;
317                 }
318
319                 if (k != System.Int64.MaxValue)
320                         return 2;
321
322                 checked {
323                         i = System.Int64.MinValue;
324                         j = System.Int64.MaxValue;
325                         k = i + j;
326                 }
327
328                 if (k != -1)
329                         return 3;
330
331                 checked {
332                         i = System.Int64.MaxValue;
333                         j = System.Int64.MinValue;
334                         k = i + j;
335                 }
336
337                 if (k != -1)
338                         return 4;
339
340                 checked {
341                         i = System.Int64.MinValue + 1234;
342                         j = -1234;
343                         k = i + j;
344                 }
345
346                 if (k != System.Int64.MinValue)
347                         return 5;
348
349                 checked {
350                         i = System.Int64.MaxValue - 1234;
351                         j = 1234;
352                         k = i + j;
353                 }
354
355                 if (k != System.Int64.MaxValue)
356                         return 6;
357
358                 return 0;
359         }
360
361         public static int test_0_add_un_ovf () {
362                 ulong n = (ulong)134217728 * 16;
363                 ulong number = checked (n + (uint)0);
364
365                 return number == n ? 0 : 1;
366         }
367
368         public static int test_0_sub_ovf () {
369                 long i, j, k;
370
371                 checked {
372                         i = System.Int64.MinValue;
373                         j = 0;
374                         k = i - j;
375                 }
376
377                 if (k != System.Int64.MinValue)
378                         return 1;
379
380                 checked {
381                         i = System.Int64.MaxValue;
382                         j = 0;
383                         k = i - j;
384                 }
385
386                 if (k != System.Int64.MaxValue)
387                         return 2;
388
389                 checked {
390                         i = System.Int64.MinValue;
391                         j = System.Int64.MinValue + 1234;
392                         k = i - j;
393                 }
394
395                 if (k != -1234)
396                         return 3;
397
398                 checked {
399                         i = System.Int64.MaxValue;
400                         j = 1234;
401                         k = i - j;
402                 }
403
404                 if (k != System.Int64.MaxValue - 1234)
405                         return 4;
406
407                 checked {
408                         i = System.Int64.MaxValue - 1234;
409                         j = -1234;
410                         k = i - j;
411                 }
412
413                 if (k != System.Int64.MaxValue)
414                         return 5;
415
416                 checked {
417                         i = System.Int64.MinValue + 1234;
418                         j = 1234;
419                         k = i - j;
420                 }
421
422                 if (k != System.Int64.MinValue)
423                         return 6;
424
425                 return 0;
426         }
427
428         public static int test_0_sub_ovf_un () {
429                 ulong i, j, k;
430
431                 checked {
432                         i = System.UInt64.MaxValue;
433                         j = 0;
434                         k = i - j;
435                 }
436
437                 if (k != System.UInt64.MaxValue)
438                         return 1;
439
440                 checked {
441                         i = System.UInt64.MaxValue;
442                         j = System.UInt64.MaxValue;
443                         k = i - j;
444                 }
445
446                 if (k != 0)
447                         return 2;
448
449                 return 0;
450         }
451
452         public static int test_2_neg () {
453                 long a = -2;            
454                 return (int)(-a);
455         }       
456
457         public static int test_0_neg_large () {
458                 long min = -9223372036854775808;
459                 unchecked {
460                         ulong ul = (ulong)min;
461                         return (min == -(long)ul) ? 0 : 1;
462                 }
463         }       
464
465         public static int test_5_shift ()
466         {
467                 long a = 9;
468                 int b = 1;
469                 int count = 0;
470                 
471                 if ((a >> b) != 4)
472                         return count;
473                 count++;
474
475                 if ((a >> 63) != 0)
476                         return count;
477                 count++;
478
479                 if ((a << 1) != 18)
480                         return count;
481                 count++;
482
483                 if ((a << b) != 18)
484                         return count;
485                 count++;
486
487                 a = -9;
488                 if ((a >> b) != -5)
489                         return count;
490                 count++;
491
492                 return count;
493         }
494
495         public static int test_1_shift_u ()
496         {
497                 ulong a;
498                 int count = 0;
499
500                 // The JIT optimizes this
501                 a = 8589934592UL;
502                 if ((a >> 32) != 2)
503                         return 0;
504                 count ++;
505
506                 return count;
507         }
508
509         public static int test_1_simple_neg () {
510                 long a = 9;
511                 
512                 if (-a != -9)
513                         return 0;
514                 return 1;
515         }
516
517         public static int test_2_compare () {
518                 long a = 1;
519                 long b = 1;
520                 
521                 if (a != b)
522                         return 0;
523                 return 2;
524         }
525
526         public static int test_9_alu ()
527         {
528                 long a = 9, b = 6;
529                 int count = 0;
530                 
531                 if ((a + b) != 15)
532                         return count;
533                 count++;
534                 
535                 if ((a - b) != 3)
536                         return count;
537                 count++;
538
539                 if ((a & 8) != 8)
540                         return count;
541                 count++;
542
543                 if ((a | 2) != 11)
544                         return count;
545                 count++;
546
547                 if ((a * b) != 54)
548                         return count;
549                 count++;
550                 
551                 if ((a / 4) != 2)
552                         return count;
553                 count++;
554                 
555                 if ((a % 4) != 1)
556                         return count;
557                 count++;
558
559                 if (-a != -9)
560                         return count;
561                 count++;
562
563                 b = -1;
564                 if (~b != 0)
565                         return count;
566                 count++;
567
568                 return count;
569         }
570         
571         public static int test_24_mul () {
572                 long a = 8;
573                 long b = 3;             
574                 return (int)(a * b);
575         }       
576         
577         public static int test_24_mul_ovf () {
578                 long a = 8;
579                 long b = 3;
580                 long res;
581                 
582                 checked {
583                         res = a * b;
584                 }
585                 return (int)res;
586         }       
587
588         public static int test_24_mul_un () {
589                 ulong a = 8;
590                 ulong b = 3;            
591                 return (int)(a * b);
592         }       
593         
594         public static int test_24_mul_ovf_un () {
595                 ulong a = 8;
596                 ulong b = 3;
597                 ulong res;
598                 
599                 checked {
600                         res = a * b;
601                 }
602                 return (int)res;
603         }       
604
605         public static int test_0_mul_imm () {
606             long i = 4;
607
608                 if ((i * 0) != 0)
609                         return 1;
610                 if ((i * 1) != 4)
611                         return 2;
612                 if ((i * 2) != 8)
613                         return 3;
614                 if ((i * 3) != 12)
615                         return 4;
616                 if ((i * 1234) != 4936)
617                         return 5;
618                 if ((i * -1) != -4)
619                         return 6;
620                 if ((i * -2) != -8)
621                         return 7;
622                 if ((i * -3) != -12)
623                         return 8;
624                 if ((i * -1234) != -4936)
625                         return 9;
626
627                 return 0;
628         }
629         
630         public static int test_4_divun () {
631                 uint b = 12;
632                 int a = 3;
633                 return (int)(b / a);
634         }
635
636         public static int test_1431655764_bigdivun_imm () {
637                 unchecked {
638                         uint b = (uint)-2;
639                         return (int)(b / 3);
640                 }
641         }
642
643         public static int test_1431655764_bigdivun () {
644                 unchecked {
645                         uint b = (uint)-2;
646                         int a = 3;
647                         return (int)(b / a);
648                 }
649         }
650
651         public static int test_1_remun () {
652                 uint b = 13;
653                 int a = 3;
654                 return (int)(b % a);
655         }
656
657         public static int test_2_bigremun () {
658                 unchecked {
659                         uint b = (uint)-2;
660                         int a = 3;
661                         return (int)(b % a);
662                 }
663         }
664
665         public static int test_0_ceq () {
666                 long a = 2;
667                 long b = 2;
668                 long c = 3;
669                 long d = 0xff00000002;
670                 
671                 bool val = (a == b); // this should produce a ceq
672                 if (!val)
673                         return 1;
674                 
675                 val = (a == c); // this should produce a ceq
676                 if (val)
677                         return 2;
678                 
679                 val = (a == d); // this should produce a ceq
680                 if (val)
681                         return 3;
682                 
683                 return 0;
684         }
685
686         public static int test_0_ceq_complex () {
687                 long l = 1, ll = 2;
688
689                 if (l < 0 != ll < 0)
690                         return 1;
691
692                 return 0;
693         }
694         
695         public static int test_0_clt () {
696                 long a = 2;
697                 long b = 2;
698                 long c = 3;
699                 long d = 0xff00000002L;
700                 long e = -1;
701                 
702                 bool val = (a < b); // this should produce a clt
703                 if (val)
704                         return 1;
705                 
706                 val = (a < c); // this should produce a clt
707                 if (!val)
708                         return 2;
709                 
710                 val = (c < a); // this should produce a clt
711                 if (val)
712                         return 3;
713                 
714                 val = (e < d); // this should produce a clt
715                 if (!val)
716                         return 4;
717                 
718                 val = (d < e); // this should produce a clt
719                 if (val)
720                         return 5;
721                 
722                 return 0;
723         }
724         
725         public static int test_0_clt_un () {
726                 ulong a = 2;
727                 ulong b = 2;
728                 ulong c = 3;
729                 ulong d = 0xff00000002;
730                 ulong e = 0xffffffffffffffff;
731                 
732                 bool val = (a < b); // this should produce a clt_un
733                 if (val)
734                         return 1;
735                 
736                 val = (a < c); // this should produce a clt_un
737                 if (!val)
738                         return 1;
739                 
740                 val = (d < e); // this should produce a clt_un
741                 if (!val)
742                         return 1;
743                 
744                 val = (e < d); // this should produce a clt_un
745                 if (val)
746                         return 1;
747                 
748                 return 0;
749         }
750
751         public static int test_0_cgt () {
752                 long a = 2;
753                 long b = 2;
754                 long c = 3;
755                 long d = 0xff00000002L;
756                 long e = -1;
757                 
758                 bool val = (a > b); // this should produce a cgt
759                 if (val)
760                         return 1;
761                 
762                 val = (a > c); // this should produce a cgt
763                 if (val)
764                         return 2;
765                 
766                 val = (c > a); // this should produce a cgt
767                 if (!val)
768                         return 3;
769                 
770                 val = (e > d); // this should produce a cgt
771                 if (val)
772                         return 4;
773                 
774                 val = (d > e); // this should produce a cgt
775                 if (!val)
776                         return 5;
777                 
778                 return 0;
779         }
780
781         public static int test_0_cgt_un () {
782                 ulong a = 2;
783                 ulong b = 2;
784                 ulong c = 3;
785                 ulong d = 0xff00000002;
786                 ulong e = 0xffffffffffffffff;
787                 
788                 bool val = (a > b); // this should produce a cgt_un
789                 if (val)
790                         return 1;
791                 
792                 val = (a > c); // this should produce a cgt_un
793                 if (val)
794                         return 1;
795                 
796                 val = (d > e); // this should produce a cgt_un
797                 if (val)
798                         return 1;
799                 
800                 val = (e > d); // this should produce a cgt_un
801                 if (!val)
802                         return 1;
803                 
804                 return 0;
805         }
806
807         static long return_5low () {
808                 return 5;
809         }
810         
811         static long return_5high () {
812                 return 0x500000000;
813         }
814
815         public static int test_3_long_ret () {
816                 long val = return_5low ();
817                 return (int) (val - 2);
818         }
819
820         public static int test_1_long_ret2 () {
821                 long val = return_5high ();
822                 if (val > 0xffffffff)
823                         return 1;
824                 return 0;
825         }
826
827         public static int test_3_byte_cast () {
828                 ulong val = 0xff00ff00f0f0f0f0;
829                 byte b;
830                 b = (byte) (val & 0xFF);
831                 if (b != 0xf0)
832                         return 1;
833
834                 return 3;
835         }
836
837         public static int test_4_ushort_cast () {
838                 ulong val = 0xff00ff00f0f0f0f0;
839                 ushort b;
840                 b = (ushort) (val & 0xFFFF);
841                 if (b != 0xf0f0)
842                         return 1;
843                 return 4;
844         }
845
846         public static int test_500_mul_div () {
847                 long val = 1000;
848                 long exp = 10;
849                 long maxexp = 20;
850                 long res = val * exp / maxexp;
851
852                 return (int)res;
853         }
854
855         static long position = 0;
856
857         public static int test_4_static_inc_long () {
858
859                 int count = 4;
860
861                 position = 0;
862
863                 position += count;
864
865                 return (int)position;
866         }
867         
868         static void doit (double value, out long m) {
869                 m = (long) value;
870         }
871         
872         public static int test_3_checked_cast_un () {
873                 ulong i = 2;
874                 long j;
875
876                 checked { j = (long)i; }
877
878                 if (j != 2)
879                         return 0;
880                 return 3;
881         }
882         
883         public static int test_4_checked_cast () {
884                 long i = 3;
885                 ulong j;
886
887                 checked { j = (ulong)i; }
888
889                 if (j != 3)
890                         return 0;
891                 return 4;
892         }
893
894         public static int test_12_checked_i1_cast () {
895                 long l = 12;
896
897                 checked {
898                         return (sbyte)l;
899                 }
900         }
901
902         public static int test_127_checked_i1_cast_un () {
903                 ulong l = 127;
904
905                 checked {
906                         return (sbyte)l;
907                 }
908         }
909
910         public static int test_1234_checked_i2_cast () {
911                 long l = 1234;
912
913                 checked {
914                         return (short)l;
915                 }
916         }
917
918         public static int test_32767_checked_i2_cast_un () {
919                 ulong l = 32767;
920
921                 checked {
922                         return (ushort)l;
923                 }
924         }
925
926         public static int test_1234_checked_i4_cast () {
927                 ulong ul = 1234;
928
929                 checked {
930                         return (int)ul;
931                 }
932         }
933
934         public static int test_10_int_uint_compare () {
935                 uint size = 10;
936                 int j = 0;
937                 for (int i = 0; i < size; ++i) {
938                         j++;
939                 }
940                 return j;
941         }
942
943         public static int test_0_ftol_clobber () {
944                 long m;
945                 doit (1.3, out m);
946                 if (m != 1)
947                         return 2;
948                 return 0;
949         }
950
951         public static int test_0_ulong_regress () {
952                 ulong u = 4257145737;
953                 u --;
954                 return (u == 4257145736) ? 0 : 1;
955         }
956         
957         public static int test_0_assemble_long ()
958         {
959                 uint a = 5;
960                 ulong x = 0x12345678;
961                 ulong y = 1;
962                 
963                 
964                 ulong z = ((x - y) << 32) | a;
965                 
966                 if (z != 0x1234567700000005)
967                         return 1;
968                 
969                 return 0;
970         }
971         
972         public static int test_0_hash ()
973         {
974                 ulong x = 0x1234567887654321;
975                 int h = (int)(x & 0xffffffff) ^ (int)(x >> 32);
976                 if (h != unchecked ((int)(0x87654321 ^ 0x12345678)))
977                         return h;
978                 return 0;
979                                 
980         }
981
982         public static int test_0_shift_regress () {
983                 long a = 0; 
984                 int b = 6; 
985                 UInt16 c = 3;
986
987                 return ((a >> (b - c)) == 0) ? 0 : 1;
988         }
989 }
990