Add this for backwards compatibility
[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_0_mul_imm_opt ()
631         {
632                 long i;
633
634                 i = 1;
635                 if ((i * 2) != 2)
636                         return 1;
637                 i = -1;
638                 if ((i * 2) != -2)
639                         return 2;
640                 i = 1;
641                 if ((i * 3) != 3)
642                         return 3;
643                 i = -1;
644                 if ((i * 3) != -3)
645                         return 4;
646                 i = 1;
647                 if ((i * 5) != 5)
648                         return 5;
649                 i = -1;
650                 if ((i * 5) != -5)
651                         return 6;
652                 i = 1;
653                 if ((i * 6) != 6)
654                         return 7;
655                 i = -1;
656                 if ((i * 6) != -6)
657                         return 8;
658                 i = 1;
659                 if ((i * 9) != 9)
660                         return 9;
661                 i = -1;
662                 if ((i * 9) != -9)
663                         return 10;
664                 i = 1;
665                 if ((i * 10) != 10)
666                         return 11;
667                 i = -1;
668                 if ((i * 10) != -10)
669                         return 12;
670                 i = 1;
671                 if ((i * 12) != 12)
672                         return 13;
673                 i = -1;
674                 if ((i * 12) != -12)
675                         return 14;
676                 i = 1;
677                 if ((i * 25) != 25)
678                         return 15;
679                 i = -1;
680                 if ((i * 25) != -25)
681                         return 16;
682                 i = 1;
683                 if ((i * 100) != 100)
684                         return 17;
685                 i = -1;
686                 if ((i * 100) != -100)
687                         return 18;
688                 
689                 return 0;
690         }
691         
692         public static int test_4_divun () {
693                 uint b = 12;
694                 int a = 3;
695                 return (int)(b / a);
696         }
697
698         public static int test_1431655764_bigdivun_imm () {
699                 unchecked {
700                         uint b = (uint)-2;
701                         return (int)(b / 3);
702                 }
703         }
704
705         public static int test_1431655764_bigdivun () {
706                 unchecked {
707                         uint b = (uint)-2;
708                         int a = 3;
709                         return (int)(b / a);
710                 }
711         }
712
713         public static int test_1_remun () {
714                 uint b = 13;
715                 int a = 3;
716                 return (int)(b % a);
717         }
718
719         public static int test_2_bigremun () {
720                 unchecked {
721                         uint b = (uint)-2;
722                         int a = 3;
723                         return (int)(b % a);
724                 }
725         }
726
727         public static int test_0_ceq () {
728                 long a = 2;
729                 long b = 2;
730                 long c = 3;
731                 long d = 0xff00000002;
732                 
733                 bool val = (a == b); // this should produce a ceq
734                 if (!val)
735                         return 1;
736                 
737                 val = (a == c); // this should produce a ceq
738                 if (val)
739                         return 2;
740                 
741                 val = (a == d); // this should produce a ceq
742                 if (val)
743                         return 3;
744                 
745                 return 0;
746         }
747
748         public static int test_0_ceq_complex () {
749                 long l = 1, ll = 2;
750
751                 if (l < 0 != ll < 0)
752                         return 1;
753
754                 return 0;
755         }
756         
757         public static int test_0_clt () {
758                 long a = 2;
759                 long b = 2;
760                 long c = 3;
761                 long d = 0xff00000002L;
762                 long e = -1;
763                 
764                 bool val = (a < b); // this should produce a clt
765                 if (val)
766                         return 1;
767                 
768                 val = (a < c); // this should produce a clt
769                 if (!val)
770                         return 2;
771                 
772                 val = (c < a); // this should produce a clt
773                 if (val)
774                         return 3;
775                 
776                 val = (e < d); // this should produce a clt
777                 if (!val)
778                         return 4;
779                 
780                 val = (d < e); // this should produce a clt
781                 if (val)
782                         return 5;
783                 
784                 return 0;
785         }
786         
787         public static int test_0_clt_un () {
788                 ulong a = 2;
789                 ulong b = 2;
790                 ulong c = 3;
791                 ulong d = 0xff00000002;
792                 ulong e = 0xffffffffffffffff;
793                 
794                 bool val = (a < b); // this should produce a clt_un
795                 if (val)
796                         return 1;
797                 
798                 val = (a < c); // this should produce a clt_un
799                 if (!val)
800                         return 1;
801                 
802                 val = (d < e); // this should produce a clt_un
803                 if (!val)
804                         return 1;
805                 
806                 val = (e < d); // this should produce a clt_un
807                 if (val)
808                         return 1;
809                 
810                 return 0;
811         }
812
813         public static int test_0_cgt () {
814                 long a = 2;
815                 long b = 2;
816                 long c = 3;
817                 long d = 0xff00000002L;
818                 long e = -1;
819                 
820                 bool val = (a > b); // this should produce a cgt
821                 if (val)
822                         return 1;
823                 
824                 val = (a > c); // this should produce a cgt
825                 if (val)
826                         return 2;
827                 
828                 val = (c > a); // this should produce a cgt
829                 if (!val)
830                         return 3;
831                 
832                 val = (e > d); // this should produce a cgt
833                 if (val)
834                         return 4;
835                 
836                 val = (d > e); // this should produce a cgt
837                 if (!val)
838                         return 5;
839                 
840                 return 0;
841         }
842
843         public static int test_0_cgt_un () {
844                 ulong a = 2;
845                 ulong b = 2;
846                 ulong c = 3;
847                 ulong d = 0xff00000002;
848                 ulong e = 0xffffffffffffffff;
849                 
850                 bool val = (a > b); // this should produce a cgt_un
851                 if (val)
852                         return 1;
853                 
854                 val = (a > c); // this should produce a cgt_un
855                 if (val)
856                         return 1;
857                 
858                 val = (d > e); // this should produce a cgt_un
859                 if (val)
860                         return 1;
861                 
862                 val = (e > d); // this should produce a cgt_un
863                 if (!val)
864                         return 1;
865                 
866                 return 0;
867         }
868
869         static long return_5low () {
870                 return 5;
871         }
872         
873         static long return_5high () {
874                 return 0x500000000;
875         }
876
877         public static int test_3_long_ret () {
878                 long val = return_5low ();
879                 return (int) (val - 2);
880         }
881
882         public static int test_1_long_ret2 () {
883                 long val = return_5high ();
884                 if (val > 0xffffffff)
885                         return 1;
886                 return 0;
887         }
888
889         public static int test_3_byte_cast () {
890                 ulong val = 0xff00ff00f0f0f0f0;
891                 byte b;
892                 b = (byte) (val & 0xFF);
893                 if (b != 0xf0)
894                         return 1;
895
896                 return 3;
897         }
898
899         public static int test_4_ushort_cast () {
900                 ulong val = 0xff00ff00f0f0f0f0;
901                 ushort b;
902                 b = (ushort) (val & 0xFFFF);
903                 if (b != 0xf0f0)
904                         return 1;
905                 return 4;
906         }
907
908         public static int test_500_mul_div () {
909                 long val = 1000;
910                 long exp = 10;
911                 long maxexp = 20;
912                 long res = val * exp / maxexp;
913
914                 return (int)res;
915         }
916         
917         static void doit (double value, out long m) {
918                 m = (long) value;
919         }
920         
921         public static int test_3_checked_cast_un () {
922                 ulong i = 2;
923                 long j;
924
925                 checked { j = (long)i; }
926
927                 if (j != 2)
928                         return 0;
929                 return 3;
930         }
931         
932         public static int test_4_checked_cast () {
933                 long i = 3;
934                 ulong j;
935
936                 checked { j = (ulong)i; }
937
938                 if (j != 3)
939                         return 0;
940                 return 4;
941         }
942
943         public static int test_12_checked_i1_cast () {
944                 long l = 12;
945
946                 checked {
947                         return (sbyte)l;
948                 }
949         }
950
951         public static int test_127_checked_i1_cast_un () {
952                 ulong l = 127;
953
954                 checked {
955                         return (sbyte)l;
956                 }
957         }
958
959         public static int test_1234_checked_i2_cast () {
960                 long l = 1234;
961
962                 checked {
963                         return (short)l;
964                 }
965         }
966
967         public static int test_32767_checked_i2_cast_un () {
968                 ulong l = 32767;
969
970                 checked {
971                         return (ushort)l;
972                 }
973         }
974
975         public static int test_1234_checked_i4_cast () {
976                 ulong ul = 1234;
977
978                 checked {
979                         return (int)ul;
980                 }
981         }
982
983         public static int test_10_int_uint_compare () {
984                 uint size = 10;
985                 int j = 0;
986                 for (int i = 0; i < size; ++i) {
987                         j++;
988                 }
989                 return j;
990         }
991
992         public static int test_0_ftol_clobber () {
993                 long m;
994                 doit (1.3, out m);
995                 if (m != 1)
996                         return 2;
997                 return 0;
998         }
999
1000         public static int test_0_ulong_regress () {
1001                 ulong u = 4257145737;
1002                 u --;
1003                 return (u == 4257145736) ? 0 : 1;
1004         }
1005         
1006         public static int test_0_assemble_long ()
1007         {
1008                 uint a = 5;
1009                 ulong x = 0x12345678;
1010                 ulong y = 1;
1011                 
1012                 
1013                 ulong z = ((x - y) << 32) | a;
1014                 
1015                 if (z != 0x1234567700000005)
1016                         return 1;
1017                 
1018                 return 0;
1019         }
1020         
1021         public static int test_0_hash ()
1022         {
1023                 ulong x = 0x1234567887654321;
1024                 int h = (int)(x & 0xffffffff) ^ (int)(x >> 32);
1025                 if (h != unchecked ((int)(0x87654321 ^ 0x12345678)))
1026                         return h;
1027                 return 0;
1028                                 
1029         }
1030
1031         public static int test_0_shift_regress () {
1032                 long a = 0; 
1033                 int b = 6; 
1034                 UInt16 c = 3;
1035
1036                 return ((a >> (b - c)) == 0) ? 0 : 1;
1037         }
1038
1039         public static int test_1234_conv_ovf_u8 () {
1040                 int i = 1234;
1041
1042                 checked {
1043                         ulong l = (ulong)i;
1044                         return (int)l;
1045                 }
1046         }
1047 }
1048