NaCl runtime fixes
[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         public static int Main (string[] args) {
29                 return TestDriver.RunTests (typeof (Tests), args);
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         struct FooStruct {
192                 public int i;
193
194                 public FooStruct (int i) {
195                         this.i = i;
196                 }
197         }
198
199         public static int test_0_arrays () {
200
201                 int sum;
202
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;
211
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;
220
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;
229
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;
238
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;
247
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;
256
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;
265
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;
274
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;
283
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;
292
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;
300
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;
309
310                 return 0;
311         }
312
313         public static int test_0_multi_dimension_arrays () {
314                 int sum;
315
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;
324
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;
333
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;
342
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;
351
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;
360
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;
369
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;
378
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;
387
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;
396
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;
405
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;
413
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);
419
420                                 /* Test Set as well */
421                                 FooStruct s = new FooStruct (i + j);
422                                 a12 [i, j] = s;
423                         }
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;
429
430                                 /* Test Get as well */
431                                 FooStruct s = a12 [i, j];
432                                 sum += s.i;
433                         }
434                 if (sum != 1800)
435                         return 12;
436
437                 /* Null check */
438                 object[,] a13 = null;
439                 try {
440                         a13 [0, 0] = new Object ();
441                         return 13;
442                 } catch (NullReferenceException) {
443                 }
444
445                 return 0;
446         }
447
448         public static int test_100_3_dimensional_arrays () {
449         int[,,] test = new int[10, 10, 10];
450
451                 test [1, 1, 1] = 100;
452                 return test [1, 1, 1];
453         }
454
455         public static int test_100_4_dimensional_arrays () {
456         int[,,,] test = new int[10, 10, 10, 10];
457
458                 test [1, 1, 1, 1] = 100;
459                 return test [1, 1, 1, 1];
460         }
461
462         public static int test_0_bug_71454 () {
463                 int[,] a = new int[4,4];
464                 int[,] b = new int[4,4];
465                 for(int i = 0; i < 4; ++i) {
466                         b[0,0] = a[0,i % 4];
467                 }
468                 return 0;
469         }
470
471         public static int test_0_interface_array_cast () {
472                 try {
473                         object [] a = new ICloneable [2];
474                         ICloneable [] b = (ICloneable [])a;
475                 } catch {
476                         return 1;
477                 }
478                 return 0;
479         }
480
481         class Foo {
482                 public static Foo[][] foo;
483         }
484
485         public static int test_0_regress_74549 () {
486                 new Foo ();
487                 return 0;
488         }
489
490         public static int test_0_regress_75832 () {
491                 int[] table = new int[] { 0, 0 };
492                 
493                 int x = 0;
494                 
495                 int temp = -1 ^ x;
496                 temp = 2 + temp;
497                 int y = table[temp];
498
499                 return y;
500         }
501
502         public static int test_0_stelem_ref_null_opt () {
503                 object[] arr = new Tests [1];
504
505                 arr [0] = new Tests ();
506                 arr [0] = null;
507
508                 return arr [0] == null ? 0 : 1;
509         }
510
511         public static int test_0_invalid_new_array_size () {
512                 int size;
513                 object res = null;
514                 size = -1;
515                 try {
516                         res = new float [size];
517                 } catch (OverflowException e) {
518
519                 } catch (Exception) {
520                         return 1;
521                 }
522                 if (res != null)
523                         return 2;
524
525                 size = -2147483648;
526                 try {
527                         res = new float [size];
528                 } catch (OverflowException e) {
529
530                 } catch (Exception) {
531                         return 3;
532                 }
533
534                 if (res != null)
535                         return 4;
536
537                 return 0;
538         }
539
540         public static int test_0_multidym_array_with_negative_lower_bound () {
541                 int[,] x = (int[,]) Array.CreateInstance(typeof (int), new int[] { 2, 2 }, new int[] { -2, -3 });
542
543                 if(x.GetLowerBound (0) != -2)
544                         return 1;
545                 if (x.GetLowerBound (1) != -3)
546                         return 2;
547
548                 x.SetValue (10, new int [] { -2, -3 });
549                 x.SetValue (20, new int [] { -2, -2 });
550                 x.SetValue (30, new int [] { -1, -3 });
551                 x.SetValue (40, new int [] { -1, -2 });
552
553                 try {
554                         x.SetValue (10, new int [] { -3, -3 });
555                         return 3;
556                 } catch (IndexOutOfRangeException) { }
557
558                 try {
559                         x.SetValue (10, new int [] { -2, -4 });
560                         return 4;
561                 } catch (IndexOutOfRangeException) { }
562
563                 try {
564                         x.SetValue (10, new int [] { 0, -3 });
565                         return 5;
566                 } catch (IndexOutOfRangeException) { }
567
568                 try {
569                         x.SetValue (10, new int [] { -1, -1 });
570                         return 6;
571                 } catch (IndexOutOfRangeException) { }
572
573                 if ((int)x.GetValue (new int [] { -2, -3 }) != 10)
574                         return 7;
575                 if ((int)x.GetValue (new int [] { -2, -2 }) != 20)
576                         return 8;
577                 if ((int)x.GetValue (new int [] { -1, -3 }) != 30)
578                         return 9;
579                 if ((int)x.GetValue (new int [] { -1, -2 }) != 40)
580                         return 10;
581
582                 try {
583                         x.GetValue (new int [] { -3, -3 });
584                         return 11;
585                 } catch (IndexOutOfRangeException) { }
586
587                 try {
588                         x.GetValue ( new int [] { -2, -4 });
589                         return 12;
590                 } catch (IndexOutOfRangeException) { }
591
592                 try {
593                         x.GetValue (new int [] { 0, -3 });
594                         return 13;
595                 } catch (IndexOutOfRangeException) { }
596
597                 try {
598                         x.GetValue (new int [] { -1, -1 });
599                         return 14;
600                 } catch (IndexOutOfRangeException) { }
601                 return 0;
602         }
603
604
605         public static int test_0_invalid_new_multi_dym_array_size () {
606                 int dym_size = 1;
607                 int size;
608                 object res = null;
609                 size = -1;
610                 try {
611                         res = new float [dym_size, size];
612                 } catch (OverflowException e) {
613
614                 } catch (Exception) {
615                         return 1;
616                 }
617                 if (res != null)
618                         return 2;
619
620                 size = -2147483648;
621                 try {
622                         res = new float [size, dym_size];
623                 } catch (OverflowException e) {
624
625                 } catch (Exception) {
626                         return 3;
627                 }
628
629                 if (res != null)
630                         return 4;
631
632                 return 0;
633         }
634
635         public enum IntEnum {
636                 A,B,C
637         }
638
639         public enum UintEnum : uint {
640                 A,B,C
641         }
642
643         static bool TryCast<T> (object o) {
644                 return o is T[];
645         }
646
647         public static int test_0_primitive_array_cast () {
648                 object a = new int[1];
649                 object b = new uint[1];
650                 object c = new IntEnum[1];
651                 object d = new UintEnum[1];
652
653                 object[] arr = new object[] { a, b, c, d };
654                 int err = 1;
655
656                 foreach (var v in arr) {
657                         if (!TryCast<int> (v))
658                                 return err;
659                         if (!TryCast<uint> (v))
660                                 return err + 1;
661                         if (!TryCast<IntEnum> (v))
662                                 return err + 2;
663                         if (!TryCast<UintEnum> (v))
664                                 return err + 3;
665                         err += 4;
666                 }
667
668                 foreach (var v in arr) {
669                         if (!(v is int[]))
670                                 return err;
671                         if (!(v is uint[]))
672                                 return err;
673                         if (!(v is IntEnum[]))
674                                 return err;
675                         if (!(v is UintEnum[]))
676                                 return err;
677                         err += 4;
678                 }
679                 return 0;
680         }
681
682         public static int test_0_intptr_array_cast () {
683                 object[] a = new object[] { new int[1], new uint[1] };
684                 object[] b = new object[] { new long[1], new ulong[1] };
685                 object[] c = new object[] { new IntPtr[1], new UIntPtr[1] };
686
687                 int err = 1;
688                 if (IntPtr.Size == 4) {
689                         foreach (var v in a) {
690                                 if (!(v is IntPtr[]))
691                                         return err;
692                                 if (!(v is IntPtr[]))
693                                         return err;
694                                 err += 2;
695                         }
696                         foreach (var v in b) {
697                                 if (v is IntPtr[])
698                                         return err;
699                                 if (v is IntPtr[])
700                                         return err;
701                                 err += 2;
702                         }
703
704                         foreach (var v in c) {
705                                 if (!(v is int[]))
706                                         return err;
707                                 if (!(v is uint[]))
708                                         return err;
709                                 err += 2;
710                         }
711                 } else {
712                         foreach (var v in a) {
713                                 if (v is IntPtr[])
714                                         return err;
715                                 if (v is IntPtr[])
716                                         return err;
717                                 err += 2;
718                         }
719                         foreach (var v in b) {
720                                 if (!(v is IntPtr[]))
721                                         return err;
722                                 if (!(v is IntPtr[]))
723                                         return err;
724                                 err += 2;
725                         }
726                         foreach (var v in c) {
727                                 if (!(v is long[]))
728                                         return err;
729                                 if (!(v is ulong[]))
730                                         return err;
731                                 err += 2;
732                         }
733                 }
734                 return 0;
735         }
736
737         public static int test_0_long_indices () {
738                 int[] arr = new int [10];
739                 int[,] arr2 = new int [10, 10];
740                 long index = 1;
741                 arr [index] = 5;
742                 if (arr [index] != 5)
743                         return 1;
744                 arr2 [index, index] = 5;
745                 if (arr2 [index, index] != 5)
746                         return 2;
747                 return 0;
748         }
749
750         // #7438
751         public static int test_0_ldelema_2_64bit () {
752         bool[,] test = new bool[201,201];
753         int x,y;
754         for(x=-100;x<100;x++) for(y=-100;y<100;y++){
755             test[x+100,y+100] = true;
756         }
757                 return 0;
758         }               
759 }
760
761