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