2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / basic-long.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         static int test_10_simple_cast () {
33                 long a = 10;
34                 return (int)a;
35         }
36
37         static int test_1_bigmul1 () {
38                 int a;
39                 int b;
40                 long c;
41                 a = 10;
42                 b = 10;
43                 c = (long)a * (long)b;
44                 if (c == 100)
45                         return 1;
46                 return 0;
47         }
48
49         static int test_1_bigmul2 () {
50                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
51                 long s = System.Int64.MinValue;
52                 long c;
53                 c = s + (long) a * (long) b;
54                 if (c == -4611686022722355199)
55                         return 1;
56                 return 0;
57         }
58         
59         static int test_1_bigmul3 () {
60                 int a = 10, b = 10;
61                 ulong c;
62                 c = (ulong) a * (ulong) b;
63                 if (c == 100)
64                         return 1;
65                 return 0;
66         }
67
68         static int test_1_bigmul4 () {
69                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
70                 ulong c;
71                 c = (ulong) a * (ulong) b;
72                 if (c == 4611686014132420609)
73                         return 1;
74                 return 0;
75         }
76         
77         static int test_1_bigmul5 () {
78                 int a = System.Int32.MaxValue, b = System.Int32.MinValue;
79                 long c;
80                 c = (long) a * (long) b;
81                 if (c == -4611686016279904256)
82                         return 1;
83                 return 0;
84         }
85         
86         static int test_1_bigmul6 () {
87                 uint a = System.UInt32.MaxValue, b = System.UInt32.MaxValue/(uint)2;
88                 ulong c;
89                 c = (ulong) a * (ulong) b;
90                 if (c == 9223372030412324865)
91                         return 1;
92                 return 0;
93         }
94         
95         static int test_0_beq () {
96                 long a = 0xffffffffff;
97                 if (a != 0xffffffffff)
98                         return 1;
99                 return 0;
100         }
101
102         static int test_0_bne_un () {
103                 long a = 0xffffffffff;
104                 if (a == 0xfffffffffe)
105                         return 1;
106                 return 0;
107         }
108
109         static int test_0_ble () {
110                 long a = 0xffffffffff;
111                 if (a > 0xffffffffff)
112                         return 1;
113                 return 0;
114         }
115
116         static int test_0_ble_un () {
117                 ulong a = 0xffffffffff;
118                 if (a > 0xffffffffff)
119                         return 1;
120                 return 0;
121         }
122
123         static int test_0_bge () {
124                 long a = 0xffffffffff;
125                 if (a < 0xffffffffff)
126                         return 1;
127                 return 0;
128         }
129
130         static int test_0_bge_un () {
131                 ulong a = 0xffffffffff;
132                 if (a < 0xffffffffff)
133                         return 1;
134                 return 0;
135         }
136
137         static int test_0_blt () {
138                 long a = 0xfffffffffe;
139                 if (a >= 0xffffffffff)
140                         return 1;
141                 return 0;
142         }
143
144         static int test_0_blt_un () {
145                 ulong a = 0xfffffffffe;
146                 if (a >= 0xffffffffff)
147                         return 1;
148                 return 0;
149         }
150
151         static int test_0_bgt () {
152                 long a = 0xffffffffff;
153                 if (a <= 0xfffffffffe)
154                         return 1;
155                 return 0;
156         }
157
158         static int test_0_conv_to_i4 () {
159                 long a = 0;
160
161                 return (int)a;
162         }
163         static int test_0_conv_from_i4 () {
164                 long a = 2;
165                 if (a != 2)
166                         return 1;
167
168                 int b = 2;
169
170                 if (a != b)
171                     return 2;
172                 return 0;
173         }
174
175         static int test_0_conv_from_i4_negative () {
176                 long a = -2;
177                 if (a != -2)
178                         return 1;
179
180                 int b = -2;
181
182                 if (a != b)
183                     return 2;
184                 return 0;
185         }
186
187         /*
188         static int test_0_conv_from_r8 () {
189                 double b = 2.0;
190                 long a = (long)b;
191
192                 if (a != 2)
193                         return 1;
194                 return 0;
195         }
196
197         static int test_0_conv_from_r4 () {
198                 float b = 2.0F;
199                 long a = (long)b;
200
201                 if (a != 2)
202                         return 1;
203                 return 0;
204         }
205         */
206         
207         static int test_8_and () {
208                 long a = 0xffffffffff;
209                 long b = 8;             
210                 return (int)(a & b);
211         }
212
213         static int test_8_and_imm () {
214                 long a = 0xffffffffff;
215                 return (int)(a & 8);
216         }
217
218         static int test_10_or () {
219                 long a = 8;
220                 long b = 2;             
221                 return (int)(a | b);
222         }
223
224         static int test_10_or_imm () {
225                 long a = 8;
226                 return (int)(a | 2);
227         }
228
229         static int test_5_xor () {
230                 long a = 7;
231                 long b = 2;             
232                 return (int)(a ^ b);
233         }
234
235         static int test_5_xor_imm () {
236                 long a = 7;
237                 return (int)(a ^ 2);
238         }
239
240         static int test_5_add () {
241                 long a = 2;
242                 long b = 3;             
243                 return (int)(a + b);
244         }
245
246         static int test_5_add_imm () {
247                 long a = 2;
248                 return (int)(a + 3);
249         }
250
251         static int test_0_add_imm_carry () {
252                 long a = -1;
253                 return (int)(a + 1);
254         }
255
256         static int test_0_add_imm_no_inc () {
257                 // we can't blindly convert an add x, 1 to an inc x
258                 long a = 0x1ffffffff;
259                 long c;
260                 c = a + 2;
261                 if (c == ((a + 1) + 1))
262                         return 0;
263                 return 1;
264         }
265
266         static int test_4_addcc_imm () {
267                 long a = 3;
268                 long b = 0;
269                 return (int)(a - b + 1);
270         }
271
272         static int test_5_sub () {
273                 long a = 8;
274                 long b = 3;             
275                 return (int)(a - b);
276         }
277
278         static int test_5_sub_imm () {
279                 long a = 8;
280                 return (int)(a - 3);
281         }
282
283         static int test_0_sub_imm_carry () {
284                 long a = 0;
285                 return (int)((a - 1) + 1);
286         }
287
288         static int test_2_neg () {
289                 long a = -2;            
290                 return (int)(-a);
291         }       
292
293         static int test_0_neg_large () {
294                 long min = -9223372036854775808;
295                 unchecked {
296                         ulong ul = (ulong)min;
297                         return (min == -(long)ul) ? 0 : 1;
298                 }
299         }       
300
301         static int test_0_shl () {
302                 long a = 9;
303                 int b = 1;
304                 
305                 if ((a >> b) != 4)
306                         return 1;
307
308
309                 return 0;
310         }
311         
312         static int test_1_rshift ()
313         {
314                 long a = 9;
315                 int b = 1;
316                 a = -9;
317                 if ((a >> b) != -5)
318                         return 0;
319                 return 1;
320         }
321
322         static int test_5_shift ()
323         {
324                 long a = 9;
325                 int b = 1;
326                 int count = 0;
327                 
328                 if ((a >> b) != 4)
329                         return count;
330                 count++;
331
332                 if ((a >> 63) != 0)
333                         return count;
334                 count++;
335
336                 if ((a << 1) != 18)
337                         return count;
338                 count++;
339
340                 if ((a << b) != 18)
341                         return count;
342                 count++;
343
344                 a = -9;
345                 if ((a >> b) != -5)
346                         return count;
347                 count++;
348
349                 return count;
350         }
351
352         static int test_1_shift_u ()
353         {
354                 ulong a;
355                 int b;
356                 int count = 0;
357
358                 // The JIT optimizes this
359                 a = 8589934592UL;
360                 if ((a >> 32) != 2)
361                         return 0;
362                 count ++;
363
364                 return count;
365         }
366
367         static int test_1_simple_neg () {
368                 long a = 9;
369                 
370                 if (-a != -9)
371                         return 0;
372                 return 1;
373         }
374
375         static int test_2_compare () {
376                 long a = 1;
377                 long b = 1;
378                 
379                 if (a != b)
380                         return 0;
381                 return 2;
382         }
383
384         static int test_9_alu ()
385         {
386                 long a = 9, b = 6;
387                 int count = 0;
388                 
389                 if ((a + b) != 15)
390                         return count;
391                 count++;
392                 
393                 if ((a - b) != 3)
394                         return count;
395                 count++;
396
397                 if ((a & 8) != 8)
398                         return count;
399                 count++;
400
401                 if ((a | 2) != 11)
402                         return count;
403                 count++;
404
405                 if ((a * b) != 54)
406                         return count;
407                 count++;
408                 
409                 if ((a / 4) != 2)
410                         return count;
411                 count++;
412                 
413                 if ((a % 4) != 1)
414                         return count;
415                 count++;
416
417                 if (-a != -9)
418                         return count;
419                 count++;
420
421                 b = -1;
422                 if (~b != 0)
423                         return count;
424                 count++;
425
426                 return count;
427         }
428         
429         static int test_24_mul () {
430                 long a = 8;
431                 long b = 3;             
432                 return (int)(a * b);
433         }       
434         
435         static int test_24_mul_ovf () {
436                 long a = 8;
437                 long b = 3;
438                 long res;
439                 
440                 checked {
441                         res = a * b;
442                 }
443                 return (int)res;
444         }       
445
446         static int test_24_mul_un () {
447                 ulong a = 8;
448                 ulong b = 3;            
449                 return (int)(a * b);
450         }       
451         
452         static int test_24_mul_ovf_un () {
453                 ulong a = 8;
454                 ulong b = 3;
455                 ulong res;
456                 
457                 checked {
458                         res = a * b;
459                 }
460                 return (int)res;
461         }       
462         
463         static int test_4_divun () {
464                 uint b = 12;
465                 int a = 3;
466                 return (int)(b / a);
467         }
468
469         static int test_1431655764_bigdivun_imm () {
470                 unchecked {
471                         uint b = (uint)-2;
472                         return (int)(b / 3);
473                 }
474         }
475
476         static int test_1431655764_bigdivun () {
477                 unchecked {
478                         uint b = (uint)-2;
479                         int a = 3;
480                         return (int)(b / a);
481                 }
482         }
483
484         static int test_1_remun () {
485                 uint b = 13;
486                 int a = 3;
487                 return (int)(b % a);
488         }
489
490         static int test_2_bigremun () {
491                 unchecked {
492                         uint b = (uint)-2;
493                         int a = 3;
494                         return (int)(b % a);
495                 }
496         }
497
498         static int test_0_ceq () {
499                 long a = 2;
500                 long b = 2;
501                 long c = 3;
502                 long d = 0xff00000002;
503                 
504                 bool val = (a == b); // this should produce a ceq
505                 if (!val)
506                         return 1;
507                 
508                 val = (a == c); // this should produce a ceq
509                 if (val)
510                         return 2;
511                 
512                 val = (a == d); // this should produce a ceq
513                 if (val)
514                         return 3;
515                 
516                 return 0;
517         }
518
519         static int test_0_ceq_complex () {
520                 long l = 1, ll = 2;
521
522                 if (l < 0 != ll < 0)
523                         return 1;
524
525                 return 0;
526         }
527         
528         static int test_0_clt () {
529                 long a = 2;
530                 long b = 2;
531                 long c = 3;
532                 long d = 0xff00000002L;
533                 long e = -1;
534                 
535                 bool val = (a < b); // this should produce a clt
536                 if (val)
537                         return 1;
538                 
539                 val = (a < c); // this should produce a clt
540                 if (!val)
541                         return 2;
542                 
543                 val = (c < a); // this should produce a clt
544                 if (val)
545                         return 3;
546                 
547                 val = (e < d); // this should produce a clt
548                 if (!val)
549                         return 4;
550                 
551                 val = (d < e); // this should produce a clt
552                 if (val)
553                         return 5;
554                 
555                 return 0;
556         }
557         
558         static int test_0_clt_un () {
559                 ulong a = 2;
560                 ulong b = 2;
561                 ulong c = 3;
562                 ulong d = 0xff00000002;
563                 ulong e = 0xffffffffffffffff;
564                 
565                 bool val = (a < b); // this should produce a clt_un
566                 if (val)
567                         return 1;
568                 
569                 val = (a < c); // this should produce a clt_un
570                 if (!val)
571                         return 1;
572                 
573                 val = (d < e); // this should produce a clt_un
574                 if (!val)
575                         return 1;
576                 
577                 val = (e < d); // this should produce a clt_un
578                 if (val)
579                         return 1;
580                 
581                 return 0;
582         }
583
584         static int test_0_cgt () {
585                 long a = 2;
586                 long b = 2;
587                 long c = 3;
588                 long d = 0xff00000002L;
589                 long e = -1;
590                 
591                 bool val = (a > b); // this should produce a cgt
592                 if (val)
593                         return 1;
594                 
595                 val = (a > c); // this should produce a cgt
596                 if (val)
597                         return 2;
598                 
599                 val = (c > a); // this should produce a cgt
600                 if (!val)
601                         return 3;
602                 
603                 val = (e > d); // this should produce a cgt
604                 if (val)
605                         return 4;
606                 
607                 val = (d > e); // this should produce a cgt
608                 if (!val)
609                         return 5;
610                 
611                 return 0;
612         }
613
614         static int test_0_cgt_un () {
615                 ulong a = 2;
616                 ulong b = 2;
617                 ulong c = 3;
618                 ulong d = 0xff00000002;
619                 ulong e = 0xffffffffffffffff;
620                 
621                 bool val = (a > b); // this should produce a cgt_un
622                 if (val)
623                         return 1;
624                 
625                 val = (a > c); // this should produce a cgt_un
626                 if (val)
627                         return 1;
628                 
629                 val = (d > e); // this should produce a cgt_un
630                 if (val)
631                         return 1;
632                 
633                 val = (e > d); // this should produce a cgt_un
634                 if (!val)
635                         return 1;
636                 
637                 return 0;
638         }
639
640         static long return_5low () {
641                 return 5;
642         }
643         
644         static long return_5high () {
645                 return 0x500000000;
646         }
647
648         static int test_3_long_ret () {
649                 long val = return_5low ();
650                 return (int) (val - 2);
651         }
652
653         static int test_1_long_ret2 () {
654                 long val = return_5high ();
655                 if (val > 0xffffffff)
656                         return 1;
657                 return 0;
658         }
659
660         static int test_3_byte_cast () {
661                 ulong val = 0xff00ff00f0f0f0f0;
662                 byte b;
663                 b = (byte) (val & 0xFF);
664                 if (b != 0xf0)
665                         return 1;
666
667                 return 3;
668         }
669
670         static int test_4_ushort_cast () {
671                 ulong val = 0xff00ff00f0f0f0f0;
672                 ushort b;
673                 b = (ushort) (val & 0xFFFF);
674                 if (b != 0xf0f0)
675                         return 1;
676                 return 4;
677         }
678
679         static int test_500_mul_div () {
680                 long val = 1000;
681                 long exp = 10;
682                 long maxexp = 20;
683                 long res = val * exp / maxexp;
684
685                 return (int)res;
686         }
687
688         static long position = 0;
689
690         static int test_4_static_inc_long () {
691
692                 int count = 4;
693
694                 position = 0;
695
696                 position += count;
697
698                 return (int)position;
699         }
700         
701         static void doit (double value, out long m) {
702                 m = (long) value;
703         }
704         
705         static int test_3_checked_cast_un () {
706                 ulong i = 2;
707                 long j;
708
709                 checked { j = (long)i; }
710
711                 if (j != 2)
712                         return 0;
713                 return 3;
714         }
715         
716         static int test_4_checked_cast () {
717                 long i = 3;
718                 ulong j;
719
720                 checked { j = (ulong)i; }
721
722                 if (j != 3)
723                         return 0;
724                 return 4;
725         }
726
727         static int test_1234_checked_i2_cast () {
728                 long l = 1234;
729
730                 checked {
731                         return (short)l;
732                 }
733         }
734
735         static int test_1234_checked_i4_cast () {
736                 ulong ul = 1234;
737
738                 checked {
739                         return (int)ul;
740                 }
741         }
742
743         static int test_10_int_uint_compare () {
744                 uint size = 10;
745                 int j = 0;
746                 for (int i = 0; i < size; ++i) {
747                         j++;
748                 }
749                 return j;
750         }
751
752         static int test_0_ftol_clobber () {
753                 long m;
754                 doit (1.3, out m);
755                 if (m != 1)
756                         return 2;
757                 return 0;
758         }
759
760         static int test_71_long_shift_right () {
761                 ulong value = 38654838087;
762                 int x = 0;
763                 byte [] buffer = new byte [1];
764                 buffer [x] = ((byte)(value >> x));
765                 return buffer [x];
766         }
767
768         static int test_0_ulong_regress () {
769                 ulong u = 4257145737;
770                 u --;
771                 return (u == 4257145736) ? 0 : 1;
772         }
773         
774         static long x;
775         static int test_0_addsub_mem ()
776         {
777                 x = 0;
778                 x += 5;
779                 
780                 if (x != 5)
781                         return 1;
782                 
783                 x -= 10;
784                 
785                 if (x != -5)
786                         return 2;
787                 
788                 return 0;
789         }
790         
791         static ulong y;
792         static int test_0_sh32_mem ()
793         {
794                 y = 0x0102130405060708;
795                 y >>= 32;
796                 
797                 if (y != 0x01021304)
798                         return 1;
799                 
800                 y = 0x0102130405060708;
801                 y <<= 32;
802                 
803                 if (y != 0x0506070800000000)
804                         return 2;
805                 
806                 x = 0x0102130405060708;
807                 x <<= 32;
808                 
809                 if (x != 0x0506070800000000)
810                         return 2;
811                 
812                 return 0;
813         }
814         
815         static int test_0_assemble_long ()
816         {
817                 uint a = 5;
818                 ulong x = 0x12345678;
819                 ulong y = 1;
820                 
821                 
822                 ulong z = ((x - y) << 32) | a;
823                 
824                 if (z != 0x1234567700000005)
825                         return 1;
826                 
827                 return 0;
828         }
829         
830         static int test_0_hash ()
831         {
832                 ulong x = 0x1234567887654321;
833                 int h = (int)(x & 0xffffffff) ^ (int)(x >> 32);
834                 if (h != unchecked ((int)(0x87654321 ^ 0x12345678)))
835                         return h;
836                 return 0;
837                                 
838         }
839
840         static int test_0_shift_regress () {
841                 long a = 0; 
842                 int b = 6; 
843                 UInt16 c = 3;
844
845                 return ((a >> (b - c)) == 0) ? 0 : 1;
846         }
847 }
848