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