2005-12-06 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / arrays.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         public static int test_10_create () {
33                 int[] a = new int [10];
34                 return a.Length;
35         }
36
37         public static int test_0_unset_value () {
38                 int[] a = new int [10];
39                 return a [5];
40         }
41
42         public static int test_3_set_value () {
43                 int[] a = new int [10];
44                 a [5] = 3;
45                 return a [5];
46         }
47
48         public static int test_0_char_array_1 () {
49                 int value = -30;
50                 char[] tmp = new char [20];
51                 char[] digitLowerTable = new char[16];
52                 tmp[0] = digitLowerTable[-(value % 10)];
53                 return 0;
54         }
55         
56         public static int test_0_char_array_2 () {
57                 int value = 5;
58                 char[] tmp = new char [20];
59                 char[] digitLowerTable = new char[16];
60                 tmp[0] = digitLowerTable[value % 10];
61                 return 0;
62         }
63
64         public static int test_0_char_array_3 () {
65                 int value = -1;
66                 char[] tmp = new char [20];
67                 char[] digitLowerTable = new char[16];
68                 tmp [0] = digitLowerTable[value & 15];          
69                 return 0;
70         }
71
72         public unsafe static int test_0_byte_array () {
73                 byte [] src = new byte [8];
74                 double ret;
75                 byte *dst = (byte *)&ret;
76                 int start = 0;
77
78                 dst[0] = src[4 + start];
79                 
80                 return 0;
81         }
82         
83         public static int test_0_set_after_shift () {
84                 int [] n = new int [1];
85                 int b = 16;
86                    
87                 n [0] = 100 + (1 << (16 - b));
88
89                 if (n [0] != 101)
90                         return 1;
91
92                 return 0;
93         }
94
95         /* Regression test for #30073 */
96         public static int test_0_newarr_emulation () {
97                 double d = 500;
98                 checked {
99                         double [] arr = new double [(int)d];
100                 }
101                 return 0;
102         }
103
104         private Int32[] m_array = new int [10];
105         
106         void setBit (int bitIndex, bool value) {
107                 int index = bitIndex/32;
108                 int shift = bitIndex%32;
109
110                 Int32 theBit = 1 << shift;
111                 if (value)
112                         m_array[index] |= theBit;
113                 else
114                         m_array[index] &= ~theBit;
115         }
116         
117         bool getBit (int bitIndex) {
118                 int index = bitIndex/32;
119                 int shift = bitIndex%32;
120
121                 Int32 theBit = m_array[index] & (1 << shift);
122                 return (theBit == 0) ? false : true;
123
124         }
125         
126         public static int test_1_bit_index () {
127                 Tests t = new Tests ();
128                 t.setBit (0, true);
129                 t.setBit (3, true);
130                 if (t.getBit (1))
131                         return 4;
132                 if (!t.getBit (0))
133                         return 5;
134                 if (!t.getBit (3))
135                         return 6;
136                 return 1;
137         }
138
139         class helper1 {
140
141                 int [] ma = new int [56];
142                 const int MBIG = int.MaxValue;
143
144                 public helper1 () {
145                         for (int k = 1; k < 5; k++) {
146                                 for (int i = 1; i < 56; i++) {
147                                         ma [i] -= ma [1 + (i + 30) % 55];
148                                         if (ma [i] < 0)
149                                                 ma [i] += MBIG;
150                                 }
151                         }
152                 }
153         }
154
155         public static int test_2_regalloc () {
156                 helper1 h = new helper1 ();
157                 return 2;
158         }
159         
160         public static int test_0_stelemref_1 () {
161                 object [] o = new object [1];
162                 o [0] = null;
163                 
164                 return 0;
165         }
166         
167         public static int test_0_stelemref_2 () {
168                 object [] o = new object [1];
169                 o [0] = 1;
170                 
171                 return 0;
172         }
173         
174         interface IFace {}
175         class Face : IFace {}
176         
177         public static int test_0_stelemref_3 () {
178                 object [] o = new IFace [1];
179                 o [0] = new Face ();
180                 
181                 return 0;
182         }
183         
184         public static int test_0_stelemref_4 () {
185                 object [][] o = new object [5] [];
186                 o [0] = new object [5];
187                 
188                 return 0;
189         }
190
191         public static int test_0_multi_dimension_arrays () {
192                 int sum;
193
194                 byte[,] a1 = new byte [10, 10];
195                 for (int i = 0; i < 10; ++i)
196                         a1 [i, i] = (byte)i;
197                 sum = 0;
198                 for (int i = 0; i < 10; ++i)
199                         sum += a1 [i, i];
200                 if (sum != 45)
201                         return 1;
202
203                 sbyte[,] a2 = new sbyte [10, 10];
204                 for (int i = 0; i < 10; ++i)
205                         a2 [i, i] = (sbyte)i;
206                 sum = 0;
207                 for (int i = 0; i < 10; ++i)
208                         sum += a2 [i, i];
209                 if (sum != 45)
210                         return 3;
211
212                 short[,] a3 = new short [10, 10];
213                 for (int i = 0; i < 10; ++i)
214                         a3 [i, i] = (short)i;
215                 sum = 0;
216                 for (int i = 0; i < 10; ++i)
217                         sum += a3 [i, i];
218                 if (sum != 45)
219                         return 4;
220
221                 ushort[,] a4 = new ushort [10, 10];
222                 for (int i = 0; i < 10; ++i)
223                         a4 [i, i] = (ushort)i;
224                 sum = 0;
225                 for (int i = 0; i < 10; ++i)
226                         sum += a4 [i, i];
227                 if (sum != 45)
228                         return 5;
229
230                 int[,] a5 = new int [10, 10];
231                 for (int i = 0; i < 10; ++i)
232                         a5 [i, i] = (int)i;
233                 sum = 0;
234                 for (int i = 0; i < 10; ++i)
235                         sum += a5 [i, i];
236                 if (sum != 45)
237                         return 6;
238
239                 uint[,] a6 = new uint [10, 10];
240                 for (int i = 0; i < 10; ++i)
241                         a6 [i, i] = (uint)i;
242                 sum = 0;
243                 for (int i = 0; i < 10; ++i)
244                         sum += (int)a6 [i, i];
245                 if (sum != 45)
246                         return 6;
247
248                 long[,] a7 = new long [10, 10];
249                 for (int i = 0; i < 10; ++i)
250                         a7 [i, i] = i;
251                 sum = 0;
252                 for (int i = 0; i < 10; ++i)
253                         sum += (int)a7 [i, i];
254                 if (sum != 45)
255                         return 7;
256
257                 ulong[,] a8 = new ulong [10, 10];
258                 for (int i = 0; i < 10; ++i)
259                         a8 [i, i] = (ulong)i;
260                 sum = 0;
261                 for (int i = 0; i < 10; ++i)
262                         sum += (int)a8 [i, i];
263                 if (sum != 45)
264                         return 8;
265
266                 float[,] a9 = new float [10, 10];
267                 for (int i = 0; i < 10; ++i)
268                         a9 [i, i] = (float)i;
269                 sum = 0;
270                 for (int i = 0; i < 10; ++i)
271                         sum += (int)a9 [i, i];
272                 if (sum != 45)
273                         return 9;
274
275                 double[,] a10 = new double [10, 10];
276                 for (int i = 0; i < 10; ++i)
277                         a10 [i, i] = i;
278                 sum = 0;
279                 for (int i = 0; i < 10; ++i)
280                         sum += (int)a10 [i, i];
281                 if (sum != 45)
282                         return 10;
283
284                 object[,] a11 = new object [10, 10];
285                 object o = new Object ();
286                 for (int i = 0; i < 10; ++i)
287                         a11 [i, i] = o;
288                 for (int i = 0; i < 10; ++i)
289                    if (a11 [i, i] != o)
290                                  return 11;
291
292                 return 0;
293         }
294
295         public static int test_0_bug_71454 () {
296                 int[,] a = new int[4,4];
297                 int[,] b = new int[4,4];
298                 for(int i = 0; i < 4; ++i) {
299                         b[0,0] = a[0,i % 4];
300                 }
301                 return 0;
302         }
303
304         public static int test_0_interface_array_cast () {
305                 try {
306                         object [] a = new ICloneable [2];
307                         ICloneable [] b = (ICloneable [])a;
308                 } catch {
309                         return 1;
310                 }
311                 return 0;
312         }
313
314         class Foo {
315                 public static Foo[][] foo;
316         }
317
318         public static int test_0_regress_74549 () {
319                 new Foo ();
320                 return 0;
321         }
322
323         public static int test_0_regress_75832 () {
324                 int[] table = new int[] { 0, 0 };
325                 
326                 int x = 0;
327                 
328                 int temp = -1 ^ x;
329                 temp = 2 + temp;
330                 int y = table[temp];
331
332                 return y;
333         }
334 }
335
336