2005-04-10 Zoltan Varga <vargaz@freemail.hu>
[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_bug_71454 () {
192                 int[,] a = new int[4,4];
193                 int[,] b = new int[4,4];
194                 for(int i = 0; i < 4; ++i) {
195                         b[0,0] = a[0,i % 4];
196                 }
197                 return 0;
198         }
199
200         public static int test_0_interface_array_cast () {
201                 try {
202                         object [] a = new ICloneable [2];
203                         ICloneable [] b = (ICloneable [])a;
204                 } catch {
205                         return 1;
206                 }
207                 return 0;
208         }
209
210         class Foo {
211                 public static Foo[][] foo;
212         }
213
214         public static int test_0_regress_74549 () {
215                 new Foo ();
216                 return 0;
217         }
218 }
219
220