Standardized Mainsoft ConstraintCollection tests.
[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_2_neg () {
289                 long a = -2;            
290                 return (int)(-a);
291         }       
292
293         public 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         public 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         public 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         public 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         public static int test_1_shift_u ()
353         {
354                 ulong a;
355                 int count = 0;
356
357                 // The JIT optimizes this
358                 a = 8589934592UL;
359                 if ((a >> 32) != 2)
360                         return 0;
361                 count ++;
362
363                 return count;
364         }
365
366         public static int test_1_simple_neg () {
367                 long a = 9;
368                 
369                 if (-a != -9)
370                         return 0;
371                 return 1;
372         }
373
374         public static int test_2_compare () {
375                 long a = 1;
376                 long b = 1;
377                 
378                 if (a != b)
379                         return 0;
380                 return 2;
381         }
382
383         public static int test_9_alu ()
384         {
385                 long a = 9, b = 6;
386                 int count = 0;
387                 
388                 if ((a + b) != 15)
389                         return count;
390                 count++;
391                 
392                 if ((a - b) != 3)
393                         return count;
394                 count++;
395
396                 if ((a & 8) != 8)
397                         return count;
398                 count++;
399
400                 if ((a | 2) != 11)
401                         return count;
402                 count++;
403
404                 if ((a * b) != 54)
405                         return count;
406                 count++;
407                 
408                 if ((a / 4) != 2)
409                         return count;
410                 count++;
411                 
412                 if ((a % 4) != 1)
413                         return count;
414                 count++;
415
416                 if (-a != -9)
417                         return count;
418                 count++;
419
420                 b = -1;
421                 if (~b != 0)
422                         return count;
423                 count++;
424
425                 return count;
426         }
427         
428         public static int test_24_mul () {
429                 long a = 8;
430                 long b = 3;             
431                 return (int)(a * b);
432         }       
433         
434         public static int test_24_mul_ovf () {
435                 long a = 8;
436                 long b = 3;
437                 long res;
438                 
439                 checked {
440                         res = a * b;
441                 }
442                 return (int)res;
443         }       
444
445         public static int test_24_mul_un () {
446                 ulong a = 8;
447                 ulong b = 3;            
448                 return (int)(a * b);
449         }       
450         
451         public static int test_24_mul_ovf_un () {
452                 ulong a = 8;
453                 ulong b = 3;
454                 ulong res;
455                 
456                 checked {
457                         res = a * b;
458                 }
459                 return (int)res;
460         }       
461         
462         public static int test_4_divun () {
463                 uint b = 12;
464                 int a = 3;
465                 return (int)(b / a);
466         }
467
468         public static int test_1431655764_bigdivun_imm () {
469                 unchecked {
470                         uint b = (uint)-2;
471                         return (int)(b / 3);
472                 }
473         }
474
475         public static int test_1431655764_bigdivun () {
476                 unchecked {
477                         uint b = (uint)-2;
478                         int a = 3;
479                         return (int)(b / a);
480                 }
481         }
482
483         public static int test_1_remun () {
484                 uint b = 13;
485                 int a = 3;
486                 return (int)(b % a);
487         }
488
489         public static int test_2_bigremun () {
490                 unchecked {
491                         uint b = (uint)-2;
492                         int a = 3;
493                         return (int)(b % a);
494                 }
495         }
496
497         public static int test_0_ceq () {
498                 long a = 2;
499                 long b = 2;
500                 long c = 3;
501                 long d = 0xff00000002;
502                 
503                 bool val = (a == b); // this should produce a ceq
504                 if (!val)
505                         return 1;
506                 
507                 val = (a == c); // this should produce a ceq
508                 if (val)
509                         return 2;
510                 
511                 val = (a == d); // this should produce a ceq
512                 if (val)
513                         return 3;
514                 
515                 return 0;
516         }
517
518         public static int test_0_ceq_complex () {
519                 long l = 1, ll = 2;
520
521                 if (l < 0 != ll < 0)
522                         return 1;
523
524                 return 0;
525         }
526         
527         public static int test_0_clt () {
528                 long a = 2;
529                 long b = 2;
530                 long c = 3;
531                 long d = 0xff00000002L;
532                 long e = -1;
533                 
534                 bool val = (a < b); // this should produce a clt
535                 if (val)
536                         return 1;
537                 
538                 val = (a < c); // this should produce a clt
539                 if (!val)
540                         return 2;
541                 
542                 val = (c < a); // this should produce a clt
543                 if (val)
544                         return 3;
545                 
546                 val = (e < d); // this should produce a clt
547                 if (!val)
548                         return 4;
549                 
550                 val = (d < e); // this should produce a clt
551                 if (val)
552                         return 5;
553                 
554                 return 0;
555         }
556         
557         public static int test_0_clt_un () {
558                 ulong a = 2;
559                 ulong b = 2;
560                 ulong c = 3;
561                 ulong d = 0xff00000002;
562                 ulong e = 0xffffffffffffffff;
563                 
564                 bool val = (a < b); // this should produce a clt_un
565                 if (val)
566                         return 1;
567                 
568                 val = (a < c); // this should produce a clt_un
569                 if (!val)
570                         return 1;
571                 
572                 val = (d < e); // this should produce a clt_un
573                 if (!val)
574                         return 1;
575                 
576                 val = (e < d); // this should produce a clt_un
577                 if (val)
578                         return 1;
579                 
580                 return 0;
581         }
582
583         public static int test_0_cgt () {
584                 long a = 2;
585                 long b = 2;
586                 long c = 3;
587                 long d = 0xff00000002L;
588                 long e = -1;
589                 
590                 bool val = (a > b); // this should produce a cgt
591                 if (val)
592                         return 1;
593                 
594                 val = (a > c); // this should produce a cgt
595                 if (val)
596                         return 2;
597                 
598                 val = (c > a); // this should produce a cgt
599                 if (!val)
600                         return 3;
601                 
602                 val = (e > d); // this should produce a cgt
603                 if (val)
604                         return 4;
605                 
606                 val = (d > e); // this should produce a cgt
607                 if (!val)
608                         return 5;
609                 
610                 return 0;
611         }
612
613         public static int test_0_cgt_un () {
614                 ulong a = 2;
615                 ulong b = 2;
616                 ulong c = 3;
617                 ulong d = 0xff00000002;
618                 ulong e = 0xffffffffffffffff;
619                 
620                 bool val = (a > b); // this should produce a cgt_un
621                 if (val)
622                         return 1;
623                 
624                 val = (a > c); // this should produce a cgt_un
625                 if (val)
626                         return 1;
627                 
628                 val = (d > e); // this should produce a cgt_un
629                 if (val)
630                         return 1;
631                 
632                 val = (e > d); // this should produce a cgt_un
633                 if (!val)
634                         return 1;
635                 
636                 return 0;
637         }
638
639         static long return_5low () {
640                 return 5;
641         }
642         
643         static long return_5high () {
644                 return 0x500000000;
645         }
646
647         public static int test_3_long_ret () {
648                 long val = return_5low ();
649                 return (int) (val - 2);
650         }
651
652         public static int test_1_long_ret2 () {
653                 long val = return_5high ();
654                 if (val > 0xffffffff)
655                         return 1;
656                 return 0;
657         }
658
659         public static int test_3_byte_cast () {
660                 ulong val = 0xff00ff00f0f0f0f0;
661                 byte b;
662                 b = (byte) (val & 0xFF);
663                 if (b != 0xf0)
664                         return 1;
665
666                 return 3;
667         }
668
669         public static int test_4_ushort_cast () {
670                 ulong val = 0xff00ff00f0f0f0f0;
671                 ushort b;
672                 b = (ushort) (val & 0xFFFF);
673                 if (b != 0xf0f0)
674                         return 1;
675                 return 4;
676         }
677
678         public static int test_500_mul_div () {
679                 long val = 1000;
680                 long exp = 10;
681                 long maxexp = 20;
682                 long res = val * exp / maxexp;
683
684                 return (int)res;
685         }
686
687         static long position = 0;
688
689         public static int test_4_static_inc_long () {
690
691                 int count = 4;
692
693                 position = 0;
694
695                 position += count;
696
697                 return (int)position;
698         }
699         
700         static void doit (double value, out long m) {
701                 m = (long) value;
702         }
703         
704         public static int test_3_checked_cast_un () {
705                 ulong i = 2;
706                 long j;
707
708                 checked { j = (long)i; }
709
710                 if (j != 2)
711                         return 0;
712                 return 3;
713         }
714         
715         public static int test_4_checked_cast () {
716                 long i = 3;
717                 ulong j;
718
719                 checked { j = (ulong)i; }
720
721                 if (j != 3)
722                         return 0;
723                 return 4;
724         }
725
726         public static int test_12_checked_i1_cast () {
727                 long l = 12;
728
729                 checked {
730                         return (sbyte)l;
731                 }
732         }
733
734         public static int test_127_checked_i1_cast_un () {
735                 ulong l = 127;
736
737                 checked {
738                         return (sbyte)l;
739                 }
740         }
741
742         public static int test_1234_checked_i2_cast () {
743                 long l = 1234;
744
745                 checked {
746                         return (short)l;
747                 }
748         }
749
750         public static int test_32767_checked_i2_cast_un () {
751                 ulong l = 32767;
752
753                 checked {
754                         return (ushort)l;
755                 }
756         }
757
758         public static int test_1234_checked_i4_cast () {
759                 ulong ul = 1234;
760
761                 checked {
762                         return (int)ul;
763                 }
764         }
765
766         public static int test_10_int_uint_compare () {
767                 uint size = 10;
768                 int j = 0;
769                 for (int i = 0; i < size; ++i) {
770                         j++;
771                 }
772                 return j;
773         }
774
775         public static int test_0_ftol_clobber () {
776                 long m;
777                 doit (1.3, out m);
778                 if (m != 1)
779                         return 2;
780                 return 0;
781         }
782
783         public static int test_0_ulong_regress () {
784                 ulong u = 4257145737;
785                 u --;
786                 return (u == 4257145736) ? 0 : 1;
787         }
788         
789         public static int test_0_assemble_long ()
790         {
791                 uint a = 5;
792                 ulong x = 0x12345678;
793                 ulong y = 1;
794                 
795                 
796                 ulong z = ((x - y) << 32) | a;
797                 
798                 if (z != 0x1234567700000005)
799                         return 1;
800                 
801                 return 0;
802         }
803         
804         public static int test_0_hash ()
805         {
806                 ulong x = 0x1234567887654321;
807                 int h = (int)(x & 0xffffffff) ^ (int)(x >> 32);
808                 if (h != unchecked ((int)(0x87654321 ^ 0x12345678)))
809                         return h;
810                 return 0;
811                                 
812         }
813
814         public static int test_0_shift_regress () {
815                 long a = 0; 
816                 int b = 6; 
817                 UInt16 c = 3;
818
819                 return ((a >> (b - c)) == 0) ? 0 : 1;
820         }
821 }
822