2009-12-30 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / arrays.cs
blob0bd8bb300fa2bb58492559bc80f776a3ef87f7c5
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_0_bug_71454 () {
441 int[,] a = new int[4,4];
442 int[,] b = new int[4,4];
443 for(int i = 0; i < 4; ++i) {
444 b[0,0] = a[0,i % 4];
446 return 0;
449 public static int test_0_interface_array_cast () {
450 try {
451 object [] a = new ICloneable [2];
452 ICloneable [] b = (ICloneable [])a;
453 } catch {
454 return 1;
456 return 0;
459 class Foo {
460 public static Foo[][] foo;
463 public static int test_0_regress_74549 () {
464 new Foo ();
465 return 0;
468 public static int test_0_regress_75832 () {
469 int[] table = new int[] { 0, 0 };
471 int x = 0;
473 int temp = -1 ^ x;
474 temp = 2 + temp;
475 int y = table[temp];
477 return y;
480 public static int test_0_stelem_ref_null_opt () {
481 object[] arr = new Tests [1];
483 arr [0] = new Tests ();
484 arr [0] = null;
486 return arr [0] == null ? 0 : 1;
489 public static int test_0_invalid_new_array_size () {
490 int size;
491 object res = null;
492 size = -1;
493 try {
494 res = new float [size];
495 } catch (OverflowException e) {
497 } catch (Exception) {
498 return 1;
500 if (res != null)
501 return 2;
503 size = -2147483648;
504 try {
505 res = new float [size];
506 } catch (OverflowException e) {
508 } catch (Exception) {
509 return 3;
512 if (res != null)
513 return 4;
515 return 0;
518 public static int test_0_multidym_array_with_negative_lower_bound () {
519 int[,] x = (int[,]) Array.CreateInstance(typeof (int), new int[] { 2, 2 }, new int[] { -2, -3 });
521 if(x.GetLowerBound (0) != -2)
522 return 1;
523 if (x.GetLowerBound (1) != -3)
524 return 2;
526 x.SetValue (10, new int [] { -2, -3 });
527 x.SetValue (20, new int [] { -2, -2 });
528 x.SetValue (30, new int [] { -1, -3 });
529 x.SetValue (40, new int [] { -1, -2 });
531 try {
532 x.SetValue (10, new int [] { -3, -3 });
533 return 3;
534 } catch (IndexOutOfRangeException) { }
536 try {
537 x.SetValue (10, new int [] { -2, -4 });
538 return 4;
539 } catch (IndexOutOfRangeException) { }
541 try {
542 x.SetValue (10, new int [] { 0, -3 });
543 return 5;
544 } catch (IndexOutOfRangeException) { }
546 try {
547 x.SetValue (10, new int [] { -1, -1 });
548 return 6;
549 } catch (IndexOutOfRangeException) { }
551 if ((int)x.GetValue (new int [] { -2, -3 }) != 10)
552 return 7;
553 if ((int)x.GetValue (new int [] { -2, -2 }) != 20)
554 return 8;
555 if ((int)x.GetValue (new int [] { -1, -3 }) != 30)
556 return 9;
557 if ((int)x.GetValue (new int [] { -1, -2 }) != 40)
558 return 10;
560 try {
561 x.GetValue (new int [] { -3, -3 });
562 return 11;
563 } catch (IndexOutOfRangeException) { }
565 try {
566 x.GetValue ( new int [] { -2, -4 });
567 return 12;
568 } catch (IndexOutOfRangeException) { }
570 try {
571 x.GetValue (new int [] { 0, -3 });
572 return 13;
573 } catch (IndexOutOfRangeException) { }
575 try {
576 x.GetValue (new int [] { -1, -1 });
577 return 14;
578 } catch (IndexOutOfRangeException) { }
579 return 0;
583 public static int test_0_invalid_new_multi_dym_array_size () {
584 int dym_size = 1;
585 int size;
586 object res = null;
587 size = -1;
588 try {
589 res = new float [dym_size, size];
590 } catch (OverflowException e) {
592 } catch (Exception) {
593 return 1;
595 if (res != null)
596 return 2;
598 size = -2147483648;
599 try {
600 res = new float [size, dym_size];
601 } catch (OverflowException e) {
603 } catch (Exception) {
604 return 3;
607 if (res != null)
608 return 4;
610 return 0;
613 public static int long_indices () {
614 int[] arr = new int [10];
615 int[,] arr2 = new int [10, 10];
616 long index = 1;
617 arr [index] = 5;
618 if (arr [index] != 5)
619 return 1;
620 arr2 [index, index] = 5;
621 if (arr2 [index, index] != 5)
622 return 2;
623 return 0;