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