2004-05-16 Patrik Torstensson <totte@hiddenpeaks.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                 unchecked {
440                         uint b = (uint)-2;
441                         return (int)(b / 3);
442                 }
443         }
444
445         static int test_1431655764_bigdivun () {
446                 unchecked {
447                         uint b = (uint)-2;
448                         int a = 3;
449                         return (int)(b / a);
450                 }
451         }
452
453         static int test_1_remun () {
454                 uint b = 13;
455                 int a = 3;
456                 return (int)(b % a);
457         }
458
459         static int test_2_bigremun () {
460                 unchecked {
461                         uint b = (uint)-2;
462                         int a = 3;
463                         return (int)(b % a);
464                 }
465         }
466
467         static int test_0_ceq () {
468                 long a = 2;
469                 long b = 2;
470                 long c = 3;
471                 long d = 0xff00000002;
472                 
473                 bool val = (a == b); // this should produce a ceq
474                 if (!val)
475                         return 1;
476                 
477                 val = (a == c); // this should produce a ceq
478                 if (val)
479                         return 2;
480                 
481                 val = (a == d); // this should produce a ceq
482                 if (val)
483                         return 3;
484                 
485                 return 0;
486         }
487         
488         static int test_0_clt () {
489                 long a = 2;
490                 long b = 2;
491                 long c = 3;
492                 long d = 0xff00000002L;
493                 long e = -1;
494                 
495                 bool val = (a < b); // this should produce a clt
496                 if (val)
497                         return 1;
498                 
499                 val = (a < c); // this should produce a clt
500                 if (!val)
501                         return 2;
502                 
503                 val = (c < a); // this should produce a clt
504                 if (val)
505                         return 3;
506                 
507                 val = (e < d); // this should produce a clt
508                 if (!val)
509                         return 4;
510                 
511                 val = (d < e); // this should produce a clt
512                 if (val)
513                         return 5;
514                 
515                 return 0;
516         }
517         
518         static int test_0_clt_un () {
519                 ulong a = 2;
520                 ulong b = 2;
521                 ulong c = 3;
522                 ulong d = 0xff00000002;
523                 ulong e = 0xffffffffffffffff;
524                 
525                 bool val = (a < b); // this should produce a clt_un
526                 if (val)
527                         return 1;
528                 
529                 val = (a < c); // this should produce a clt_un
530                 if (!val)
531                         return 1;
532                 
533                 val = (d < e); // this should produce a clt_un
534                 if (!val)
535                         return 1;
536                 
537                 val = (e < d); // this should produce a clt_un
538                 if (val)
539                         return 1;
540                 
541                 return 0;
542         }
543
544         static int test_0_cgt () {
545                 long a = 2;
546                 long b = 2;
547                 long c = 3;
548                 long d = 0xff00000002L;
549                 long e = -1;
550                 
551                 bool val = (a > b); // this should produce a cgt
552                 if (val)
553                         return 1;
554                 
555                 val = (a > c); // this should produce a cgt
556                 if (val)
557                         return 2;
558                 
559                 val = (c > a); // this should produce a cgt
560                 if (!val)
561                         return 3;
562                 
563                 val = (e > d); // this should produce a cgt
564                 if (val)
565                         return 4;
566                 
567                 val = (d > e); // this should produce a cgt
568                 if (!val)
569                         return 5;
570                 
571                 return 0;
572         }
573
574         static int test_0_cgt_un () {
575                 ulong a = 2;
576                 ulong b = 2;
577                 ulong c = 3;
578                 ulong d = 0xff00000002;
579                 ulong e = 0xffffffffffffffff;
580                 
581                 bool val = (a > b); // this should produce a cgt_un
582                 if (val)
583                         return 1;
584                 
585                 val = (a > c); // this should produce a cgt_un
586                 if (val)
587                         return 1;
588                 
589                 val = (d > e); // this should produce a cgt_un
590                 if (val)
591                         return 1;
592                 
593                 val = (e > d); // this should produce a cgt_un
594                 if (!val)
595                         return 1;
596                 
597                 return 0;
598         }
599
600         static long return_5low () {
601                 return 5;
602         }
603         
604         static long return_5high () {
605                 return 0x500000000;
606         }
607
608         static int test_3_long_ret () {
609                 long val = return_5low ();
610                 return (int) (val - 2);
611         }
612
613         static int test_1_long_ret2 () {
614                 long val = return_5high ();
615                 if (val > 0xffffffff)
616                         return 1;
617                 return 0;
618         }
619
620         static int test_3_byte_cast () {
621                 ulong val = 0xff00ff00f0f0f0f0;
622                 byte b;
623                 b = (byte) (val & 0xFF);
624                 if (b != 0xf0)
625                         return 1;
626                 return 3;
627         }
628         
629         static int test_4_ushort_cast () {
630                 ulong val = 0xff00ff00f0f0f0f0;
631                 ushort b;
632                 b = (ushort) (val & 0xFFFF);
633                 if (b != 0xf0f0)
634                         return 1;
635                 return 4;
636         }
637
638         static int test_500_mul_div () {
639                 long val = 1000;
640                 long exp = 10;
641                 long maxexp = 20;
642                 long res = val * exp / maxexp;
643
644                 return (int)res;
645         }
646
647         static long position = 0;
648
649         static int test_4_static_inc_long () {
650
651                 int count = 4;
652
653                 position = 0;
654
655                 position += count;
656
657                 return (int)position;
658         }
659         
660         static void doit (double value, out long m) {
661                 m = (long) value;
662         }
663         
664         static int test_3_checked_cast_un () {
665                 ulong i = 2;
666                 long j;
667
668                 checked { j = (long)i; }
669
670                 if (j != 2)
671                         return 0;
672                 return 3;
673         }
674         
675         static int test_4_checked_cast () {
676                 long i = 3;
677                 ulong j;
678
679                 checked { j = (ulong)i; }
680
681                 if (j != 3)
682                         return 0;
683                 return 4;
684         }
685
686         static int test_10_int_uint_compare () {
687                 uint size = 10;
688                 int j = 0;
689                 for (int i = 0; i < size; ++i) {
690                         j++;
691                 }
692                 return j;
693         }
694
695         static int test_0_ftol_clobber () {
696                 long m;
697                 doit (1.3, out m);
698                 if (m != 1)
699                         return 2;
700                 return 0;
701         }
702 }
703