This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / mini / basic-calls.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 void dummy () {
33         }
34
35         static int test_0_return () {
36                 dummy ();
37                 return 0;
38         }
39
40         static int dummy1 () {
41                 return 1;
42         }
43
44         static int test_2_int_return () {
45                 int r = dummy1 ();
46                 if (r == 1)
47                         return 2;
48                 return 0;
49         }
50
51         static int add1 (int val) {
52                 return val + 1;
53         }
54
55         static int test_1_int_pass () {
56                 int r = add1 (5);
57                 if (r == 6)
58                         return 1;
59                 return 0;
60         }
61
62         static int add_many (int val, short t, byte b, int da) {
63                 return val + t + b + da;
64         }
65
66         static int test_1_int_pass_many () {
67                 byte b = 6;
68                 int r = add_many (5, 2, b, 1);
69                 if (r == 14)
70                         return 1;
71                 return 0;
72         }
73
74         unsafe static float GetFloat (byte *ptr) {
75                 return *(float*)ptr;
76         }
77
78         unsafe public static float GetFloat(float value)
79                 {
80                         return GetFloat((byte *)&value);
81                 }
82
83         /* bug #42134 */
84         static int test_2_inline_saved_arg_type () {
85                 float f = 100.0f;
86                 return GetFloat (f) == f? 2: 1;
87         }
88
89         static int pass_many_types (int a, long b, int c, long d) {
90                 return a + (int)b + c + (int)d;
91         }
92
93         static int test_5_pass_longs () {
94                 return pass_many_types (1, 2, -5, 7);
95         }
96
97         static int overflow_registers (int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) {
98                 return a+b+c+d+e+f+g+h+i+j;
99         }
100
101         static int test_55_pass_even_more () {
102                 return overflow_registers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
103         }
104
105         static int pass_ints_longs (int a, long b, long c, long d, long e, int f, long g) {
106                 return (int)(a + b + c + d + e + f + g);
107         }
108
109         static int test_1_sparc_argument_passing () {
110                 // The 4. argument tests split reg/mem argument passing
111                 // The 5. argument tests mem argument passing
112                 // The 7. argument tests passing longs in misaligned memory
113                 // The MaxValues are needed so the MS word of the long is not 0
114                 return pass_ints_longs (1, 2, System.Int64.MaxValue, System.Int64.MinValue, System.Int64.MaxValue, 0, System.Int64.MinValue);
115         }
116
117         static int pass_bytes (byte a, byte b, byte c, byte d, byte e, byte f, byte g) {
118                 return (int)(a + b + c + d + e + f + g);
119         }
120
121         static int test_21_sparc_byte_argument_passing () {
122                 return pass_bytes (0, 1, 2, 3, 4, 5, 6);
123         }
124
125         static int pass_sbytes (sbyte a, sbyte b, sbyte c, sbyte d, sbyte e, sbyte f, sbyte g) {
126                 return (int)(a + b + c + d + e + f + g);
127         }
128
129         static int test_21_sparc_sbyte_argument_passing () {
130                 return pass_sbytes (0, 1, 2, 3, 4, 5, 6);
131         }
132
133         static int pass_shorts (short a, short b, short c, short d, short e, short f, short g) {
134                 return (int)(a + b + c + d + e + f + g);
135         }
136
137         static int test_21_sparc_short_argument_passing () {
138                 return pass_shorts (0, 1, 2, 3, 4, 5, 6);
139         }
140
141         static int pass_floats_doubles (float a, double b, double c, double d, double e, float f, double g) {
142                 return (int)(a + b + c + d + e + f + g);
143         }
144
145         static int test_721_sparc_float_argument_passing () {
146                 return pass_floats_doubles (100.0f, 101.0, 102.0, 103.0, 104.0, 105.0f, 106.0);
147         }
148
149         // The first argument must be passed on a dword aligned stack location
150         static int pass_byref_ints_longs (ref long a, ref int b, ref byte c, ref short d, ref long e, ref int f, ref long g) {
151                 return (int)(a + b + c + d + e + f + g);
152         }
153
154         static int pass_takeaddr_ints_longs (long a, int b, byte c, short d, long e, int f, long g) {
155                 return pass_byref_ints_longs (ref a, ref b, ref c, ref d, ref e, ref f, ref g);
156         }
157
158         // Test that arguments are moved to the stack from incoming registers
159         // when the argument must reside in the stack because its address is taken
160         static int test_2_sparc_takeaddr_argument_passing () {
161                 return pass_takeaddr_ints_longs (1, 2, 253, -253, System.Int64.MaxValue, 0, System.Int64.MinValue);
162         }
163
164         static void pass_byref_double (out double d) {
165                 d = 5.0;
166         }
167
168         // Test byref double argument passing
169         static int test_0_sparc_byref_double_argument_passing () {
170                 double d;
171                 pass_byref_double (out d);
172                 return (d == 5.0) ? 0 : 1;
173         }
174
175         static void shift_un_arg (ulong value) {
176                 do {
177                         value = value >> 4;
178                 } while (value != 0);
179         }
180
181         // Test that assignment to long arguments work
182         static int test_0_long_arg_assign ()
183         {
184                 ulong c = 0x800000ff00000000;
185                         
186                 shift_un_arg (c >> 4);
187
188                 return 0;
189         }
190
191         static unsafe void* ptr_return (void *ptr)
192         {
193                 return ptr;
194         }
195
196         static unsafe int test_0_ptr_return ()
197         {
198                 void *ptr = new IntPtr (55).ToPointer ();
199
200                 if (ptr_return (ptr) == ptr)
201                         return 0;
202                 else
203                         return 1;
204         }
205
206         static bool isnan (float f) {
207                 return (f != f);
208         }
209
210         static int test_0_isnan () {
211                 float f = 1.0f;
212                 return isnan (f) ? 1 : 0;
213         }
214
215         struct FooStruct {
216
217                 public FooStruct (long l) {
218                 }
219         }
220
221         static int test_0_calls_opcode_emulation () {
222                 // Test that emulated opcodes do not clobber arguments already in
223                 // out registers
224                 checked {
225                         long val = 10000;
226                         new FooStruct (val * 10000);
227                 }
228                 return 0;
229         }
230
231 }
232