2004-06-05 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / mini / basic-long.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * 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_5_sub () {
267                 long a = 8;
268                 long b = 3;             
269                 return (int)(a - b);
270         }
271
272         static int test_5_sub_imm () {
273                 long a = 8;
274                 return (int)(a - 3);
275         }
276
277         static int test_0_sub_imm_carry () {
278                 long a = 0;
279                 return (int)((a - 1) + 1);
280         }
281
282         static int test_2_neg () {
283                 long a = -2;            
284                 return (int)(-a);
285         }       
286
287         static int test_0_neg_large () {
288                 long min = -9223372036854775808;
289                 unchecked {
290                         ulong ul = (ulong)min;
291                         return (min == -(long)ul) ? 0 : 1;
292                 }
293         }       
294
295         static int test_0_shl () {
296                 long a = 9;
297                 int b = 1;
298                 
299                 if ((a >> b) != 4)
300                         return 1;
301
302
303                 return 0;
304         }
305         
306         static int test_1_rshift ()
307         {
308                 long a = 9;
309                 int b = 1;
310                 a = -9;
311                 if ((a >> b) != -5)
312                         return 0;
313                 return 1;
314         }
315
316         static int test_5_shift ()
317         {
318                 long a = 9;
319                 int b = 1;
320                 int count = 0;
321                 
322                 if ((a >> b) != 4)
323                         return count;
324                 count++;
325
326                 if ((a >> 63) != 0)
327                         return count;
328                 count++;
329
330                 if ((a << 1) != 18)
331                         return count;
332                 count++;
333
334                 if ((a << b) != 18)
335                         return count;
336                 count++;
337
338                 a = -9;
339                 if ((a >> b) != -5)
340                         return count;
341                 count++;
342
343                 return count;
344         }
345
346         static int test_1_simple_neg () {
347                 long a = 9;
348                 
349                 if (-a != -9)
350                         return 0;
351                 return 1;
352         }
353
354         static int test_2_compare () {
355                 long a = 1;
356                 long b = 1;
357                 
358                 if (a != b)
359                         return 0;
360                 return 2;
361         }
362
363         static int test_9_alu ()
364         {
365                 long a = 9, b = 6;
366                 int count = 0;
367                 
368                 if ((a + b) != 15)
369                         return count;
370                 count++;
371                 
372                 if ((a - b) != 3)
373                         return count;
374                 count++;
375
376                 if ((a & 8) != 8)
377                         return count;
378                 count++;
379
380                 if ((a | 2) != 11)
381                         return count;
382                 count++;
383
384                 if ((a * b) != 54)
385                         return count;
386                 count++;
387                 
388                 if ((a / 4) != 2)
389                         return count;
390                 count++;
391                 
392                 if ((a % 4) != 1)
393                         return count;
394                 count++;
395
396                 if (-a != -9)
397                         return count;
398                 count++;
399
400                 b = -1;
401                 if (~b != 0)
402                         return count;
403                 count++;
404
405                 return count;
406         }
407         
408         static int test_24_mul () {
409                 long a = 8;
410                 long b = 3;             
411                 return (int)(a * b);
412         }       
413         
414         static int test_24_mul_ovf () {
415                 long a = 8;
416                 long b = 3;
417                 long res;
418                 
419                 checked {
420                         res = a * b;
421                 }
422                 return (int)res;
423         }       
424
425         static int test_24_mul_un () {
426                 ulong a = 8;
427                 ulong b = 3;            
428                 return (int)(a * b);
429         }       
430         
431         static int test_24_mul_ovf_un () {
432                 ulong a = 8;
433                 ulong b = 3;
434                 ulong res;
435                 
436                 checked {
437                         res = a * b;
438                 }
439                 return (int)res;
440         }       
441         
442         static int test_4_divun () {
443                 uint b = 12;
444                 int a = 3;
445                 return (int)(b / a);
446         }
447
448         static int test_1431655764_bigdivun_imm () {
449                 unchecked {
450                         uint b = (uint)-2;
451                         return (int)(b / 3);
452                 }
453         }
454
455         static int test_1431655764_bigdivun () {
456                 unchecked {
457                         uint b = (uint)-2;
458                         int a = 3;
459                         return (int)(b / a);
460                 }
461         }
462
463         static int test_1_remun () {
464                 uint b = 13;
465                 int a = 3;
466                 return (int)(b % a);
467         }
468
469         static int test_2_bigremun () {
470                 unchecked {
471                         uint b = (uint)-2;
472                         int a = 3;
473                         return (int)(b % a);
474                 }
475         }
476
477         static int test_0_ceq () {
478                 long a = 2;
479                 long b = 2;
480                 long c = 3;
481                 long d = 0xff00000002;
482                 
483                 bool val = (a == b); // this should produce a ceq
484                 if (!val)
485                         return 1;
486                 
487                 val = (a == c); // this should produce a ceq
488                 if (val)
489                         return 2;
490                 
491                 val = (a == d); // this should produce a ceq
492                 if (val)
493                         return 3;
494                 
495                 return 0;
496         }
497
498         static int test_0_ceq_complex () {
499                 long l = 1, ll = 2;
500
501                 if (l < 0 != ll < 0)
502                         return 1;
503
504                 return 0;
505         }
506         
507         static int test_0_clt () {
508                 long a = 2;
509                 long b = 2;
510                 long c = 3;
511                 long d = 0xff00000002L;
512                 long e = -1;
513                 
514                 bool val = (a < b); // this should produce a clt
515                 if (val)
516                         return 1;
517                 
518                 val = (a < c); // this should produce a clt
519                 if (!val)
520                         return 2;
521                 
522                 val = (c < a); // this should produce a clt
523                 if (val)
524                         return 3;
525                 
526                 val = (e < d); // this should produce a clt
527                 if (!val)
528                         return 4;
529                 
530                 val = (d < e); // this should produce a clt
531                 if (val)
532                         return 5;
533                 
534                 return 0;
535         }
536         
537         static int test_0_clt_un () {
538                 ulong a = 2;
539                 ulong b = 2;
540                 ulong c = 3;
541                 ulong d = 0xff00000002;
542                 ulong e = 0xffffffffffffffff;
543                 
544                 bool val = (a < b); // this should produce a clt_un
545                 if (val)
546                         return 1;
547                 
548                 val = (a < c); // this should produce a clt_un
549                 if (!val)
550                         return 1;
551                 
552                 val = (d < e); // this should produce a clt_un
553                 if (!val)
554                         return 1;
555                 
556                 val = (e < d); // this should produce a clt_un
557                 if (val)
558                         return 1;
559                 
560                 return 0;
561         }
562
563         static int test_0_cgt () {
564                 long a = 2;
565                 long b = 2;
566                 long c = 3;
567                 long d = 0xff00000002L;
568                 long e = -1;
569                 
570                 bool val = (a > b); // this should produce a cgt
571                 if (val)
572                         return 1;
573                 
574                 val = (a > c); // this should produce a cgt
575                 if (val)
576                         return 2;
577                 
578                 val = (c > a); // this should produce a cgt
579                 if (!val)
580                         return 3;
581                 
582                 val = (e > d); // this should produce a cgt
583                 if (val)
584                         return 4;
585                 
586                 val = (d > e); // this should produce a cgt
587                 if (!val)
588                         return 5;
589                 
590                 return 0;
591         }
592
593         static int test_0_cgt_un () {
594                 ulong a = 2;
595                 ulong b = 2;
596                 ulong c = 3;
597                 ulong d = 0xff00000002;
598                 ulong e = 0xffffffffffffffff;
599                 
600                 bool val = (a > b); // this should produce a cgt_un
601                 if (val)
602                         return 1;
603                 
604                 val = (a > c); // this should produce a cgt_un
605                 if (val)
606                         return 1;
607                 
608                 val = (d > e); // this should produce a cgt_un
609                 if (val)
610                         return 1;
611                 
612                 val = (e > d); // this should produce a cgt_un
613                 if (!val)
614                         return 1;
615                 
616                 return 0;
617         }
618
619         static long return_5low () {
620                 return 5;
621         }
622         
623         static long return_5high () {
624                 return 0x500000000;
625         }
626
627         static int test_3_long_ret () {
628                 long val = return_5low ();
629                 return (int) (val - 2);
630         }
631
632         static int test_1_long_ret2 () {
633                 long val = return_5high ();
634                 if (val > 0xffffffff)
635                         return 1;
636                 return 0;
637         }
638
639         static int test_3_byte_cast () {
640                 ulong val = 0xff00ff00f0f0f0f0;
641                 byte b;
642                 b = (byte) (val & 0xFF);
643                 if (b != 0xf0)
644                         return 1;
645                 return 3;
646         }
647         
648         static int test_4_ushort_cast () {
649                 ulong val = 0xff00ff00f0f0f0f0;
650                 ushort b;
651                 b = (ushort) (val & 0xFFFF);
652                 if (b != 0xf0f0)
653                         return 1;
654                 return 4;
655         }
656
657         static int test_500_mul_div () {
658                 long val = 1000;
659                 long exp = 10;
660                 long maxexp = 20;
661                 long res = val * exp / maxexp;
662
663                 return (int)res;
664         }
665
666         static long position = 0;
667
668         static int test_4_static_inc_long () {
669
670                 int count = 4;
671
672                 position = 0;
673
674                 position += count;
675
676                 return (int)position;
677         }
678         
679         static void doit (double value, out long m) {
680                 m = (long) value;
681         }
682         
683         static int test_3_checked_cast_un () {
684                 ulong i = 2;
685                 long j;
686
687                 checked { j = (long)i; }
688
689                 if (j != 2)
690                         return 0;
691                 return 3;
692         }
693         
694         static int test_4_checked_cast () {
695                 long i = 3;
696                 ulong j;
697
698                 checked { j = (ulong)i; }
699
700                 if (j != 3)
701                         return 0;
702                 return 4;
703         }
704
705         static int test_10_int_uint_compare () {
706                 uint size = 10;
707                 int j = 0;
708                 for (int i = 0; i < size; ++i) {
709                         j++;
710                 }
711                 return j;
712         }
713
714         static int test_0_ftol_clobber () {
715                 long m;
716                 doit (1.3, out m);
717                 if (m != 1)
718                         return 2;
719                 return 0;
720         }
721
722         static int test_71_long_shift_right () {
723                 ulong value = 38654838087;
724                 int x = 0;
725                 byte [] buffer = new byte [1];
726                 buffer [x] = ((byte)(value >> x));
727                 return buffer [x];
728         }
729
730         static int test_0_ulong_regress () {
731                 ulong u = 4257145737;
732                 u --;
733                 return (u == 4257145736) ? 0 : 1;
734         }
735 }
736