update rx (mobile builds).
[mono-project.git] / mono / mini / arrays.cs
blob67a35426e290547fa612bd3164cf56abda8dce7b
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 /* Null check */
438 object[,] a13 = null;
439 try {
440 a13 [0, 0] = new Object ();
441 return 13;
442 } catch (NullReferenceException) {
445 return 0;
448 public static int test_100_3_dimensional_arrays () {
449 int[,,] test = new int[10, 10, 10];
451 test [1, 1, 1] = 100;
452 return test [1, 1, 1];
455 public static int test_0_bug_71454 () {
456 int[,] a = new int[4,4];
457 int[,] b = new int[4,4];
458 for(int i = 0; i < 4; ++i) {
459 b[0,0] = a[0,i % 4];
461 return 0;
464 public static int test_0_interface_array_cast () {
465 try {
466 object [] a = new ICloneable [2];
467 ICloneable [] b = (ICloneable [])a;
468 } catch {
469 return 1;
471 return 0;
474 class Foo {
475 public static Foo[][] foo;
478 public static int test_0_regress_74549 () {
479 new Foo ();
480 return 0;
483 public static int test_0_regress_75832 () {
484 int[] table = new int[] { 0, 0 };
486 int x = 0;
488 int temp = -1 ^ x;
489 temp = 2 + temp;
490 int y = table[temp];
492 return y;
495 public static int test_0_stelem_ref_null_opt () {
496 object[] arr = new Tests [1];
498 arr [0] = new Tests ();
499 arr [0] = null;
501 return arr [0] == null ? 0 : 1;
504 public static int test_0_invalid_new_array_size () {
505 int size;
506 object res = null;
507 size = -1;
508 try {
509 res = new float [size];
510 } catch (OverflowException e) {
512 } catch (Exception) {
513 return 1;
515 if (res != null)
516 return 2;
518 size = -2147483648;
519 try {
520 res = new float [size];
521 } catch (OverflowException e) {
523 } catch (Exception) {
524 return 3;
527 if (res != null)
528 return 4;
530 return 0;
533 public static int test_0_multidym_array_with_negative_lower_bound () {
534 int[,] x = (int[,]) Array.CreateInstance(typeof (int), new int[] { 2, 2 }, new int[] { -2, -3 });
536 if(x.GetLowerBound (0) != -2)
537 return 1;
538 if (x.GetLowerBound (1) != -3)
539 return 2;
541 x.SetValue (10, new int [] { -2, -3 });
542 x.SetValue (20, new int [] { -2, -2 });
543 x.SetValue (30, new int [] { -1, -3 });
544 x.SetValue (40, new int [] { -1, -2 });
546 try {
547 x.SetValue (10, new int [] { -3, -3 });
548 return 3;
549 } catch (IndexOutOfRangeException) { }
551 try {
552 x.SetValue (10, new int [] { -2, -4 });
553 return 4;
554 } catch (IndexOutOfRangeException) { }
556 try {
557 x.SetValue (10, new int [] { 0, -3 });
558 return 5;
559 } catch (IndexOutOfRangeException) { }
561 try {
562 x.SetValue (10, new int [] { -1, -1 });
563 return 6;
564 } catch (IndexOutOfRangeException) { }
566 if ((int)x.GetValue (new int [] { -2, -3 }) != 10)
567 return 7;
568 if ((int)x.GetValue (new int [] { -2, -2 }) != 20)
569 return 8;
570 if ((int)x.GetValue (new int [] { -1, -3 }) != 30)
571 return 9;
572 if ((int)x.GetValue (new int [] { -1, -2 }) != 40)
573 return 10;
575 try {
576 x.GetValue (new int [] { -3, -3 });
577 return 11;
578 } catch (IndexOutOfRangeException) { }
580 try {
581 x.GetValue ( new int [] { -2, -4 });
582 return 12;
583 } catch (IndexOutOfRangeException) { }
585 try {
586 x.GetValue (new int [] { 0, -3 });
587 return 13;
588 } catch (IndexOutOfRangeException) { }
590 try {
591 x.GetValue (new int [] { -1, -1 });
592 return 14;
593 } catch (IndexOutOfRangeException) { }
594 return 0;
598 public static int test_0_invalid_new_multi_dym_array_size () {
599 int dym_size = 1;
600 int size;
601 object res = null;
602 size = -1;
603 try {
604 res = new float [dym_size, size];
605 } catch (OverflowException e) {
607 } catch (Exception) {
608 return 1;
610 if (res != null)
611 return 2;
613 size = -2147483648;
614 try {
615 res = new float [size, dym_size];
616 } catch (OverflowException e) {
618 } catch (Exception) {
619 return 3;
622 if (res != null)
623 return 4;
625 return 0;
628 public enum IntEnum {
629 A,B,C
632 public enum UintEnum : uint {
633 A,B,C
636 static bool TryCast<T> (object o) {
637 return o is T[];
640 public static int test_0_primitive_array_cast () {
641 object a = new int[1];
642 object b = new uint[1];
643 object c = new IntEnum[1];
644 object d = new UintEnum[1];
646 object[] arr = new object[] { a, b, c, d };
647 int err = 1;
649 foreach (var v in arr) {
650 if (!TryCast<int> (v))
651 return err;
652 if (!TryCast<uint> (v))
653 return err + 1;
654 if (!TryCast<IntEnum> (v))
655 return err + 2;
656 if (!TryCast<UintEnum> (v))
657 return err + 3;
658 err += 4;
661 foreach (var v in arr) {
662 if (!(v is int[]))
663 return err;
664 if (!(v is uint[]))
665 return err;
666 if (!(v is IntEnum[]))
667 return err;
668 if (!(v is UintEnum[]))
669 return err;
670 err += 4;
672 return 0;
675 public static int test_0_intptr_array_cast () {
676 object[] a = new object[] { new int[1], new uint[1] };
677 object[] b = new object[] { new long[1], new ulong[1] };
678 object[] c = new object[] { new IntPtr[1], new UIntPtr[1] };
680 int err = 1;
681 if (IntPtr.Size == 4) {
682 foreach (var v in a) {
683 if (!(v is IntPtr[]))
684 return err;
685 if (!(v is IntPtr[]))
686 return err;
687 err += 2;
689 foreach (var v in b) {
690 if (v is IntPtr[])
691 return err;
692 if (v is IntPtr[])
693 return err;
694 err += 2;
697 foreach (var v in c) {
698 if (!(v is int[]))
699 return err;
700 if (!(v is uint[]))
701 return err;
702 err += 2;
704 } else {
705 foreach (var v in a) {
706 if (v is IntPtr[])
707 return err;
708 if (v is IntPtr[])
709 return err;
710 err += 2;
712 foreach (var v in b) {
713 if (!(v is IntPtr[]))
714 return err;
715 if (!(v is IntPtr[]))
716 return err;
717 err += 2;
719 foreach (var v in c) {
720 if (!(v is long[]))
721 return err;
722 if (!(v is ulong[]))
723 return err;
724 err += 2;
727 return 0;
730 public static int test_0_long_indices () {
731 int[] arr = new int [10];
732 int[,] arr2 = new int [10, 10];
733 long index = 1;
734 arr [index] = 5;
735 if (arr [index] != 5)
736 return 1;
737 arr2 [index, index] = 5;
738 if (arr2 [index, index] != 5)
739 return 2;
740 return 0;
743 // #7438
744 public static int test_0_ldelema_2_64bit () {
745 bool[,] test = new bool[201,201];
746 int x,y;
747 for(x=-100;x<100;x++) for(y=-100;y<100;y++){
748 test[x+100,y+100] = true;
750 return 0;