2010-04-06 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / arrays.cs
blob464dfa565cc2ee07d930a67d3f5d3693798231fb
1 using System;
2 using System.Reflection;
4 /*
5 * Regression tests for the mono JIT.
7 * Each test needs to be of the form:
9 * static int test_<result>_<name> ();
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.
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)
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.
26 class Tests {
28 static int Main () {
29 return TestDriver.RunTests (typeof (Tests));
32 public static int test_10_create () {
33 int[] a = new int [10];
34 return a.Length;
37 public static int test_0_unset_value () {
38 int[] a = new int [10];
39 return a [5];
42 public static int test_3_set_value () {
43 int[] a = new int [10];
44 a [5] = 3;
45 return a [5];
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;
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;
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;
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;
78 dst[0] = src[4 + start];
80 return 0;
83 public static int test_0_set_after_shift () {
84 int [] n = new int [1];
85 int b = 16;
87 n [0] = 100 + (1 << (16 - b));
89 if (n [0] != 101)
90 return 1;
92 return 0;
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];
101 return 0;
104 private Int32[] m_array = new int [10];
106 void setBit (int bitIndex, bool value) {
107 int index = bitIndex/32;
108 int shift = bitIndex%32;
110 Int32 theBit = 1 << shift;
111 if (value)
112 m_array[index] |= theBit;
113 else
114 m_array[index] &= ~theBit;
117 bool getBit (int bitIndex) {
118 int index = bitIndex/32;
119 int shift = bitIndex%32;
121 Int32 theBit = m_array[index] & (1 << shift);
122 return (theBit == 0) ? false : true;
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;
139 class helper1 {
141 int [] ma = new int [56];
142 const int MBIG = int.MaxValue;
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;
155 public static int test_2_regalloc () {
156 helper1 h = new helper1 ();
157 return 2;
160 public static int test_0_stelemref_1 () {
161 object [] o = new object [1];
162 o [0] = null;
164 return 0;
167 public static int test_0_stelemref_2 () {
168 object [] o = new object [1];
169 o [0] = 1;
171 return 0;
174 interface IFace {}
175 class Face : IFace {}
177 public static int test_0_stelemref_3 () {
178 object [] o = new IFace [1];
179 o [0] = new Face ();
181 return 0;
184 public static int test_0_stelemref_4 () {
185 object [][] o = new object [5] [];
186 o [0] = new object [5];
188 return 0;
191 struct FooStruct {
192 public int i;
194 public FooStruct (int i) {
195 this.i = i;
199 public static int test_0_arrays () {
201 int sum;
203 byte[] a1 = new byte [10];
204 for (int i = 0; i < 10; ++i)
205 a1 [i] = (byte)i;
206 sum = 0;
207 for (int i = 0; i < 10; ++i)
208 sum += a1 [i];
209 if (sum != 45)
210 return 1;
212 sbyte[] a2 = new sbyte [10];
213 for (int i = 0; i < 10; ++i)
214 a2 [i] = (sbyte)i;
215 sum = 0;
216 for (int i = 0; i < 10; ++i)
217 sum += a2 [i];
218 if (sum != 45)
219 return 2;
221 short[] a3 = new short [10];
222 for (int i = 0; i < 10; ++i)
223 a3 [i] = (short)i;
224 sum = 0;
225 for (int i = 0; i < 10; ++i)
226 sum += a3 [i];
227 if (sum != 45)
228 return 3;
230 ushort[] a4 = new ushort [10];
231 for (int i = 0; i < 10; ++i)
232 a4 [i] = (ushort)i;
233 sum = 0;
234 for (int i = 0; i < 10; ++i)
235 sum += a4 [i];
236 if (sum != 45)
237 return 4;
239 int[] a5 = new int [10];
240 for (int i = 0; i < 10; ++i)
241 a5 [i] = (int)i;
242 sum = 0;
243 for (int i = 0; i < 10; ++i)
244 sum += a5 [i];
245 if (sum != 45)
246 return 5;
248 uint[] a6 = new uint [10];
249 for (int i = 0; i < 10; ++i)
250 a6 [i] = (uint)i;
251 sum = 0;
252 for (int i = 0; i < 10; ++i)
253 sum += (int)a6 [i];
254 if (sum != 45)
255 return 6;
257 long[] a7 = new long [10];
258 for (int i = 0; i < 10; ++i)
259 a7 [i] = i;
260 sum = 0;
261 for (int i = 0; i < 10; ++i)
262 sum += (int)a7 [i];
263 if (sum != 45)
264 return 7;
266 ulong[] a8 = new ulong [10];
267 for (int i = 0; i < 10; ++i)
268 a8 [i] = (ulong)i;
269 sum = 0;
270 for (int i = 0; i < 10; ++i)
271 sum += (int)a8 [i];
272 if (sum != 45)
273 return 8;
275 float[] a9 = new float [10];
276 for (int i = 0; i < 10; ++i)
277 a9 [i] = (float)i;
278 sum = 0;
279 for (int i = 0; i < 10; ++i)
280 sum += (int)a9 [i];
281 if (sum != 45)
282 return 9;
284 double[] a10 = new double [10];
285 for (int i = 0; i < 10; ++i)
286 a10 [i] = i;
287 sum = 0;
288 for (int i = 0; i < 10; ++i)
289 sum += (int)a10 [i];
290 if (sum != 45)
291 return 10;
293 object[] a11 = new object [10];
294 object o = new Object ();
295 for (int i = 0; i < 10; ++i)
296 a11 [i] = o;
297 for (int i = 0; i < 10; ++i)
298 if (a11 [i] != o)
299 return 11;
301 FooStruct[] a12 = new FooStruct [10];
302 for (int i = 0; i < 10; ++i)
303 a12 [i] = new FooStruct (i);
304 sum = 0;
305 for (int i = 0; i < 10; ++i)
306 sum += a12 [i].i;
307 if (sum != 45)
308 return 12;
310 return 0;
313 public static int test_0_multi_dimension_arrays () {
314 int sum;
316 byte[,] a1 = new byte [10, 10];
317 for (int i = 0; i < 10; ++i)
318 a1 [i, i] = (byte)i;
319 sum = 0;
320 for (int i = 0; i < 10; ++i)
321 sum += a1 [i, i];
322 if (sum != 45)
323 return 1;
325 sbyte[,] a2 = new sbyte [10, 10];
326 for (int i = 0; i < 10; ++i)
327 a2 [i, i] = (sbyte)i;
328 sum = 0;
329 for (int i = 0; i < 10; ++i)
330 sum += a2 [i, i];
331 if (sum != 45)
332 return 2;
334 short[,] a3 = new short [10, 10];
335 for (int i = 0; i < 10; ++i)
336 a3 [i, i] = (short)i;
337 sum = 0;
338 for (int i = 0; i < 10; ++i)
339 sum += a3 [i, i];
340 if (sum != 45)
341 return 3;
343 ushort[,] a4 = new ushort [10, 10];
344 for (int i = 0; i < 10; ++i)
345 a4 [i, i] = (ushort)i;
346 sum = 0;
347 for (int i = 0; i < 10; ++i)
348 sum += a4 [i, i];
349 if (sum != 45)
350 return 4;
352 int[,] a5 = new int [10, 10];
353 for (int i = 0; i < 10; ++i)
354 a5 [i, i] = (int)i;
355 sum = 0;
356 for (int i = 0; i < 10; ++i)
357 sum += a5 [i, i];
358 if (sum != 45)
359 return 5;
361 uint[,] a6 = new uint [10, 10];
362 for (int i = 0; i < 10; ++i)
363 a6 [i, i] = (uint)i;
364 sum = 0;
365 for (int i = 0; i < 10; ++i)
366 sum += (int)a6 [i, i];
367 if (sum != 45)
368 return 6;
370 long[,] a7 = new long [10, 10];
371 for (int i = 0; i < 10; ++i)
372 a7 [i, i] = i;
373 sum = 0;
374 for (int i = 0; i < 10; ++i)
375 sum += (int)a7 [i, i];
376 if (sum != 45)
377 return 7;
379 ulong[,] a8 = new ulong [10, 10];
380 for (int i = 0; i < 10; ++i)
381 a8 [i, i] = (ulong)i;
382 sum = 0;
383 for (int i = 0; i < 10; ++i)
384 sum += (int)a8 [i, i];
385 if (sum != 45)
386 return 8;
388 float[,] a9 = new float [10, 10];
389 for (int i = 0; i < 10; ++i)
390 a9 [i, i] = (float)i;
391 sum = 0;
392 for (int i = 0; i < 10; ++i)
393 sum += (int)a9 [i, i];
394 if (sum != 45)
395 return 9;
397 double[,] a10 = new double [10, 10];
398 for (int i = 0; i < 10; ++i)
399 a10 [i, i] = i;
400 sum = 0;
401 for (int i = 0; i < 10; ++i)
402 sum += (int)a10 [i, i];
403 if (sum != 45)
404 return 10;
406 object[,] a11 = new object [10, 10];
407 object o = new Object ();
408 for (int i = 0; i < 10; ++i)
409 a11 [i, i] = o;
410 for (int i = 0; i < 10; ++i)
411 if (a11 [i, i] != o)
412 return 11;
414 FooStruct[,] a12 = new FooStruct [10, 10];
415 for (int i = 0; i < 10; ++i)
416 for (int j = 0; j < 10; ++j) {
417 /* This one calls Address */
418 a12 [i, j] = new FooStruct (i + j);
420 /* Test Set as well */
421 FooStruct s = new FooStruct (i + j);
422 a12 [i, j] = s;
424 sum = 0;
425 for (int i = 0; i < 10; ++i)
426 for (int j = 0; j < 10; ++j) {
427 /* This one calls Address */
428 sum += a12 [i, j].i;
430 /* Test Get as well */
431 FooStruct s = a12 [i, j];
432 sum += s.i;
434 if (sum != 1800)
435 return 12;
437 return 0;
440 public static int test_100_3_dimensional_arrays () {
441 int[,,] test = new int[10, 10, 10];
443 test [1, 1, 1] = 100;
444 return test [1, 1, 1];
447 public static int test_0_bug_71454 () {
448 int[,] a = new int[4,4];
449 int[,] b = new int[4,4];
450 for(int i = 0; i < 4; ++i) {
451 b[0,0] = a[0,i % 4];
453 return 0;
456 public static int test_0_interface_array_cast () {
457 try {
458 object [] a = new ICloneable [2];
459 ICloneable [] b = (ICloneable [])a;
460 } catch {
461 return 1;
463 return 0;
466 class Foo {
467 public static Foo[][] foo;
470 public static int test_0_regress_74549 () {
471 new Foo ();
472 return 0;
475 public static int test_0_regress_75832 () {
476 int[] table = new int[] { 0, 0 };
478 int x = 0;
480 int temp = -1 ^ x;
481 temp = 2 + temp;
482 int y = table[temp];
484 return y;
487 public static int test_0_stelem_ref_null_opt () {
488 object[] arr = new Tests [1];
490 arr [0] = new Tests ();
491 arr [0] = null;
493 return arr [0] == null ? 0 : 1;
496 public static int test_0_invalid_new_array_size () {
497 int size;
498 object res = null;
499 size = -1;
500 try {
501 res = new float [size];
502 } catch (OverflowException e) {
504 } catch (Exception) {
505 return 1;
507 if (res != null)
508 return 2;
510 size = -2147483648;
511 try {
512 res = new float [size];
513 } catch (OverflowException e) {
515 } catch (Exception) {
516 return 3;
519 if (res != null)
520 return 4;
522 return 0;
525 public static int test_0_multidym_array_with_negative_lower_bound () {
526 int[,] x = (int[,]) Array.CreateInstance(typeof (int), new int[] { 2, 2 }, new int[] { -2, -3 });
528 if(x.GetLowerBound (0) != -2)
529 return 1;
530 if (x.GetLowerBound (1) != -3)
531 return 2;
533 x.SetValue (10, new int [] { -2, -3 });
534 x.SetValue (20, new int [] { -2, -2 });
535 x.SetValue (30, new int [] { -1, -3 });
536 x.SetValue (40, new int [] { -1, -2 });
538 try {
539 x.SetValue (10, new int [] { -3, -3 });
540 return 3;
541 } catch (IndexOutOfRangeException) { }
543 try {
544 x.SetValue (10, new int [] { -2, -4 });
545 return 4;
546 } catch (IndexOutOfRangeException) { }
548 try {
549 x.SetValue (10, new int [] { 0, -3 });
550 return 5;
551 } catch (IndexOutOfRangeException) { }
553 try {
554 x.SetValue (10, new int [] { -1, -1 });
555 return 6;
556 } catch (IndexOutOfRangeException) { }
558 if ((int)x.GetValue (new int [] { -2, -3 }) != 10)
559 return 7;
560 if ((int)x.GetValue (new int [] { -2, -2 }) != 20)
561 return 8;
562 if ((int)x.GetValue (new int [] { -1, -3 }) != 30)
563 return 9;
564 if ((int)x.GetValue (new int [] { -1, -2 }) != 40)
565 return 10;
567 try {
568 x.GetValue (new int [] { -3, -3 });
569 return 11;
570 } catch (IndexOutOfRangeException) { }
572 try {
573 x.GetValue ( new int [] { -2, -4 });
574 return 12;
575 } catch (IndexOutOfRangeException) { }
577 try {
578 x.GetValue (new int [] { 0, -3 });
579 return 13;
580 } catch (IndexOutOfRangeException) { }
582 try {
583 x.GetValue (new int [] { -1, -1 });
584 return 14;
585 } catch (IndexOutOfRangeException) { }
586 return 0;
590 public static int test_0_invalid_new_multi_dym_array_size () {
591 int dym_size = 1;
592 int size;
593 object res = null;
594 size = -1;
595 try {
596 res = new float [dym_size, size];
597 } catch (OverflowException e) {
599 } catch (Exception) {
600 return 1;
602 if (res != null)
603 return 2;
605 size = -2147483648;
606 try {
607 res = new float [size, dym_size];
608 } catch (OverflowException e) {
610 } catch (Exception) {
611 return 3;
614 if (res != null)
615 return 4;
617 return 0;
620 public static int long_indices () {
621 int[] arr = new int [10];
622 int[,] arr2 = new int [10, 10];
623 long index = 1;
624 arr [index] = 5;
625 if (arr [index] != 5)
626 return 1;
627 arr2 [index, index] = 5;
628 if (arr2 [index, index] != 5)
629 return 2;
630 return 0;