2003-08-26 Martin Baulig <martin@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_1_bigmul1 () {
33                 int a;
34                 int b;
35                 long c;
36                 a = 10;
37                 b = 10;
38                 c = (long)a * (long)b;
39                 if (c == 100)
40                         return 1;
41                 return 0;
42         }
43
44         static int test_1_bigmul2 () {
45                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
46                 long s = System.Int64.MinValue;
47                 long c;
48                 c = s + (long) a * (long) b;
49                 if (c == -4611686022722355199)
50                         return 1;
51                 return 0;
52         }
53         
54         static int test_1_bigmul3 () {
55                 int a = 10, b = 10;
56                 ulong c;
57                 c = (ulong) a * (ulong) b;
58                 if (c == 100)
59                         return 1;
60                 return 0;
61         }
62
63         static int test_1_bigmul4 () {
64                 int a = System.Int32.MaxValue, b = System.Int32.MaxValue;
65                 ulong c;
66                 c = (ulong) a * (ulong) b;
67                 if (c == 4611686014132420609)
68                         return 1;
69                 return 0;
70         }
71         
72         static int test_1_bigmul5 () {
73                 int a = System.Int32.MaxValue, b = System.Int32.MinValue;
74                 long c;
75                 c = (long) a * (long) b;
76                 if (c == -4611686016279904256)
77                         return 1;
78                 return 0;
79         }
80         
81         static int test_1_bigmul6 () {
82                 uint a = System.UInt32.MaxValue, b = System.UInt32.MaxValue/(uint)2;
83                 ulong c;
84                 c = (ulong) a * (ulong) b;
85                 if (c == 9223372030412324865)
86                         return 1;
87                 return 0;
88         }
89         
90         static int test_0_beq () {
91                 long a = 0xffffffffff;
92                 if (a != 0xffffffffff)
93                         return 1;
94                 return 0;
95         }
96
97         static int test_0_bne_un () {
98                 long a = 0xffffffffff;
99                 if (a == 0xfffffffffe)
100                         return 1;
101                 return 0;
102         }
103
104         static int test_0_ble () {
105                 long a = 0xffffffffff;
106                 if (a > 0xffffffffff)
107                         return 1;
108                 return 0;
109         }
110
111         static int test_0_ble_un () {
112                 ulong a = 0xffffffffff;
113                 if (a > 0xffffffffff)
114                         return 1;
115                 return 0;
116         }
117
118         static int test_0_bge () {
119                 long a = 0xffffffffff;
120                 if (a < 0xffffffffff)
121                         return 1;
122                 return 0;
123         }
124
125         static int test_0_bge_un () {
126                 ulong a = 0xffffffffff;
127                 if (a < 0xffffffffff)
128                         return 1;
129                 return 0;
130         }
131
132         static int test_0_blt () {
133                 long a = 0xfffffffffe;
134                 if (a >= 0xffffffffff)
135                         return 1;
136                 return 0;
137         }
138
139         static int test_0_blt_un () {
140                 ulong a = 0xfffffffffe;
141                 if (a >= 0xffffffffff)
142                         return 1;
143                 return 0;
144         }
145
146         static int test_0_bgt () {
147                 long a = 0xffffffffff;
148                 if (a <= 0xfffffffffe)
149                         return 1;
150                 return 0;
151         }
152
153         static int test_0_conv_to_i4 () {
154                 long a = 0;
155
156                 return (int)a;
157         }
158         static int test_0_conv_from_i4 () {
159                 long a = 2;
160                 if (a != 2)
161                         return 1;
162
163                 int b = 2;
164
165                 if (a != b)
166                     return 2;
167                 return 0;
168         }
169
170         static int test_0_conv_from_i4_negative () {
171                 long a = -2;
172                 if (a != -2)
173                         return 1;
174
175                 int b = -2;
176
177                 if (a != b)
178                     return 2;
179                 return 0;
180         }
181
182         /*
183         static int test_0_conv_from_r8 () {
184                 double b = 2.0;
185                 long a = (long)b;
186
187                 if (a != 2)
188                         return 1;
189                 return 0;
190         }
191
192         static int test_0_conv_from_r4 () {
193                 float b = 2.0F;
194                 long a = (long)b;
195
196                 if (a != 2)
197                         return 1;
198                 return 0;
199         }
200         */
201         
202         static int test_8_and () {
203                 long a = 0xffffffffff;
204                 long b = 8;             
205                 return (int)(a & b);
206         }
207
208         static int test_8_and_imm () {
209                 long a = 0xffffffffff;
210                 return (int)(a & 8);
211         }
212
213         static int test_10_or () {
214                 long a = 8;
215                 long b = 2;             
216                 return (int)(a | b);
217         }
218
219         static int test_10_or_imm () {
220                 long a = 8;
221                 return (int)(a | 2);
222         }
223
224         static int test_5_xor () {
225                 long a = 7;
226                 long b = 2;             
227                 return (int)(a ^ b);
228         }
229
230         static int test_5_xor_imm () {
231                 long a = 7;
232                 return (int)(a ^ 2);
233         }
234
235         static int test_5_add () {
236                 long a = 2;
237                 long b = 3;             
238                 return (int)(a + b);
239         }
240
241         static int test_5_add_imm () {
242                 long a = 2;
243                 return (int)(a + 3);
244         }
245
246         static int test_0_add_imm_no_inc () {
247                 // we can't blindly convert an add x, 1 to an inc x
248                 long a = 0x1ffffffff;
249                 long c;
250                 c = a + 2;
251                 if (c == ((a + 1) + 1))
252                         return 0;
253                 return 1;
254         }
255
256         static int test_5_sub () {
257                 long a = 8;
258                 long b = 3;             
259                 return (int)(a - b);
260         }
261
262         static int test_5_sub_imm () {
263                 long a = 8;
264                 return (int)(a - 3);
265         }
266
267         static int test_2_neg () {
268                 long a = -2;            
269                 return (int)(-a);
270         }       
271
272         static int test_0_shl () {
273                 long a = 9;
274                 int b = 1;
275                 
276                 if ((a >> b) != 4)
277                         return 1;
278
279
280                 return 0;
281         }
282         
283         static int test_1_rshift ()
284         {
285                 long a = 9;
286                 int b = 1;
287                 a = -9;
288                 if ((a >> b) != -5)
289                         return 0;
290                 return 1;
291         }
292
293         static int test_5_shift ()
294         {
295                 long a = 9;
296                 int b = 1;
297                 int count = 0;
298                 
299                 if ((a >> b) != 4)
300                         return count;
301                 count++;
302
303                 if ((a >> 63) != 0)
304                         return count;
305                 count++;
306
307                 if ((a << 1) != 18)
308                         return count;
309                 count++;
310
311                 if ((a << b) != 18)
312                         return count;
313                 count++;
314
315                 a = -9;
316                 if ((a >> b) != -5)
317                         return count;
318                 count++;
319
320                 return count;
321         }
322
323         static int test_1_simple_neg () {
324                 long a = 9;
325                 
326                 if (-a != -9)
327                         return 0;
328                 return 1;
329         }
330
331         static int test_2_compare () {
332                 long a = 1;
333                 long b = 1;
334                 
335                 if (a != b)
336                         return 0;
337                 return 2;
338         }
339
340         static int test_9_alu ()
341         {
342                 long a = 9, b = 6;
343                 int count = 0;
344                 
345                 if ((a + b) != 15)
346                         return count;
347                 count++;
348                 
349                 if ((a - b) != 3)
350                         return count;
351                 count++;
352
353                 if ((a & 8) != 8)
354                         return count;
355                 count++;
356
357                 if ((a | 2) != 11)
358                         return count;
359                 count++;
360
361                 if ((a * b) != 54)
362                         return count;
363                 count++;
364                 
365                 if ((a / 4) != 2)
366                         return count;
367                 count++;
368                 
369                 if ((a % 4) != 1)
370                         return count;
371                 count++;
372
373                 if (-a != -9)
374                         return count;
375                 count++;
376
377                 b = -1;
378                 if (~b != 0)
379                         return count;
380                 count++;
381
382                 return count;
383         }
384         
385         static int test_24_mul () {
386                 long a = 8;
387                 long b = 3;             
388                 return (int)(a * b);
389         }       
390         
391         static int test_24_mul_ovf () {
392                 long a = 8;
393                 long b = 3;
394                 long res;
395                 
396                 checked {
397                         res = a * b;
398                 }
399                 return (int)res;
400         }       
401
402         static int test_24_mul_un () {
403                 ulong a = 8;
404                 ulong b = 3;            
405                 return (int)(a * b);
406         }       
407         
408         static int test_24_mul_ovf_un () {
409                 ulong a = 8;
410                 ulong b = 3;
411                 ulong res;
412                 
413                 checked {
414                         res = a * b;
415                 }
416                 return (int)res;
417         }       
418         
419         static int test_4_divun () {
420                 uint b = 12;
421                 int a = 3;
422                 return (int)(b / a);
423         }
424
425         static int test_1431655764_bigdivun_imm () {
426                 uint b = (uint)-2;
427                 return (int)(b / 3);
428         }
429
430         static int test_1431655764_bigdivun () {
431                 uint b = (uint)-2;
432                 int a = 3;
433                 return (int)(b / a);
434         }
435
436         static int test_1_remun () {
437                 uint b = 13;
438                 int a = 3;
439                 return (int)(b % a);
440         }
441
442         static int test_2_bigremun () {
443                 uint b = (uint)-2;
444                 int a = 3;
445                 return (int)(b % a);
446         }
447
448         static int test_0_ceq () {
449                 long a = 2;
450                 long b = 2;
451                 long c = 3;
452                 long d = 0xff00000002;
453                 
454                 bool val = (a == b); // this should produce a ceq
455                 if (!val)
456                         return 1;
457                 
458                 val = (a == c); // this should produce a ceq
459                 if (val)
460                         return 2;
461                 
462                 val = (a == d); // this should produce a ceq
463                 if (val)
464                         return 3;
465                 
466                 return 0;
467         }
468         
469         static int test_0_clt () {
470                 long a = 2;
471                 long b = 2;
472                 long c = 3;
473                 long d = 0xff00000002L;
474                 long e = -1;
475                 
476                 bool val = (a < b); // this should produce a clt
477                 if (val)
478                         return 1;
479                 
480                 val = (a < c); // this should produce a clt
481                 if (!val)
482                         return 2;
483                 
484                 val = (c < a); // this should produce a clt
485                 if (val)
486                         return 3;
487                 
488                 val = (e < d); // this should produce a clt
489                 if (!val)
490                         return 4;
491                 
492                 val = (d < e); // this should produce a clt
493                 if (val)
494                         return 5;
495                 
496                 return 0;
497         }
498         
499         static int test_0_clt_un () {
500                 ulong a = 2;
501                 ulong b = 2;
502                 ulong c = 3;
503                 ulong d = 0xff00000002;
504                 ulong e = 0xffffffffffffffff;
505                 
506                 bool val = (a < b); // this should produce a clt_un
507                 if (val)
508                         return 1;
509                 
510                 val = (a < c); // this should produce a clt_un
511                 if (!val)
512                         return 1;
513                 
514                 val = (d < e); // this should produce a clt_un
515                 if (!val)
516                         return 1;
517                 
518                 val = (e < d); // this should produce a clt_un
519                 if (val)
520                         return 1;
521                 
522                 return 0;
523         }
524
525         static int test_0_cgt () {
526                 long a = 2;
527                 long b = 2;
528                 long c = 3;
529                 long d = 0xff00000002L;
530                 long e = -1;
531                 
532                 bool val = (a > b); // this should produce a cgt
533                 if (val)
534                         return 1;
535                 
536                 val = (a > c); // this should produce a cgt
537                 if (val)
538                         return 2;
539                 
540                 val = (c > a); // this should produce a cgt
541                 if (!val)
542                         return 3;
543                 
544                 val = (e > d); // this should produce a cgt
545                 if (val)
546                         return 4;
547                 
548                 val = (d > e); // this should produce a cgt
549                 if (!val)
550                         return 5;
551                 
552                 return 0;
553         }
554
555         static int test_0_cgt_un () {
556                 ulong a = 2;
557                 ulong b = 2;
558                 ulong c = 3;
559                 ulong d = 0xff00000002;
560                 ulong e = 0xffffffffffffffff;
561                 
562                 bool val = (a > b); // this should produce a cgt_un
563                 if (val)
564                         return 1;
565                 
566                 val = (a > c); // this should produce a cgt_un
567                 if (val)
568                         return 1;
569                 
570                 val = (d > e); // this should produce a cgt_un
571                 if (val)
572                         return 1;
573                 
574                 val = (e > d); // this should produce a cgt_un
575                 if (!val)
576                         return 1;
577                 
578                 return 0;
579         }
580
581         static long return_5low () {
582                 return 5;
583         }
584         
585         static long return_5high () {
586                 return 0x500000000;
587         }
588
589         static int test_3_long_ret () {
590                 long val = return_5low ();
591                 return (int) (val - 2);
592         }
593
594         static int test_1_long_ret2 () {
595                 long val = return_5high ();
596                 if (val > 0xffffffff)
597                         return 1;
598                 return 0;
599         }
600
601         static int test_3_byte_cast () {
602                 ulong val = 0xff00ff00f0f0f0f0;
603                 byte b;
604                 b = (byte) (val & 0xFF);
605                 if (b != 0xf0)
606                         return 1;
607                 return 3;
608         }
609         
610         static int test_4_ushort_cast () {
611                 ulong val = 0xff00ff00f0f0f0f0;
612                 ushort b;
613                 b = (ushort) (val & 0xFFFF);
614                 if (b != 0xf0f0)
615                         return 1;
616                 return 4;
617         }
618
619         static int test_500_mul_div () {
620                 long val = 1000;
621                 long exp = 10;
622                 long maxexp = 20;
623                 long res = val * exp / maxexp;
624
625                 return (int)res;
626         }
627
628         static long position = 0;
629
630         static int test_4_static_inc_long () {
631
632                 int count = 4;
633
634                 position = 0;
635
636                 position += count;
637
638                 return (int)position;
639         }
640         
641         static void doit (double value, out long m) {
642                 m = (long) value;
643         }
644         
645         static int test_0_ftol_clobber () {
646                 long m;
647                 doit (1.3, out m);
648                 if (m != 1)
649                         return 2;
650                 return 0;
651         }
652
653         static int test_3_checked_cast_un () {
654                 ulong i = 2;
655                 long j;
656
657                 checked { j = (long)i; }
658
659                 if (j != 2)
660                         return 0;
661                 return 3;
662         }
663         
664         static int test_4_checked_cast () {
665                 long i = 3;
666                 ulong j;
667
668                 checked { j = (ulong)i; }
669
670                 if (j != 3)
671                         return 0;
672                 return 4;
673         }
674 }
675