Reduce TLS accesses. (#11487)
[mono-project.git] / mono / tests / array-coop-2.cs
blob8e5442aa0b031eef4d410fa8a0ef1f29573e707e
1 static bool slow_or_reflection = true; // FIXME test both
2 //static bool slow_or_reflection = false; // FIXME test both
4 t [] array1 = new t [10]{
5 newt (1), newt (2), newt (3), newt (4), newt (5),
6 newt (6), newt (7), newt (8), newt (9), newt (10)
7 };
9 t [,] array2 = new t [10, 3] {
10 {newt (10), newt (20), newt (30)},
11 {newt (100), newt (200), newt (300)},
12 {newt (1000), newt (2000), newt (3000)},
13 {newt (10000), newt (20000), newt (30000)},
14 {newt (100000), newt (200000), newt (300000)},
15 {newt (11), newt (21), newt (31)},
16 {newt (101), newt (201), newt (301)},
17 {newt (1001), newt (2001), newt (3001)},
18 {newt (10001), newt (20001), newt (30001)},
19 {newt (100001), newt (200001), newt (300001)}
22 t [][] array3 = new t [10][] {
23 new t [1]{newt (2)}, new t [1]{newt (3)}, new t [1]{newt (4)}, new t [1]{newt (5)}, new t [1]{newt (6)},
24 new t [1]{newt (7)}, new t [1]{newt (8)}, new t [1]{newt (9)}, new t [1]{newt (10)}, new t [1]{newt (11)}
27 static void assert (bool expr)
29 if (expr)
30 return;
31 System.Console.WriteLine ("failure");
32 Environment.Exit (1);
35 void test_clear ()
37 int [] a = new int [100]; // This not System.Array.Clear.
39 t [] array1 = new t [10] {
40 newt (1), newt (2), newt (3), newt (4), newt (5),
41 newt (6), newt (7), newt (8), newt (9), newt (10)
43 var dt0 = newt (0);
45 assert (array1 [0] != dt0);
46 assert (array1 [1] != dt0);
47 assert (array1 [2] != dt0);
48 assert (array1 [3] != dt0);
49 System.Array.Clear (array1, 0, 2);
50 assert (array1 [0] == dt0);
51 assert (array1 [1] == dt0);
52 assert (array1 [2] != dt0);
53 assert (array1 [3] != dt0);
54 System.Array.Clear (array1, 3, 1);
55 assert (array1 [0] == dt0);
56 assert (array1 [1] == dt0);
57 assert (array1 [2] != dt0);
58 assert (array1 [3] == dt0);
60 t [][] array3 = new t [10][] {
61 new t [1]{newt (2)}, new t [1]{newt (3)}, new t [1]{newt (4)}, new t [1]{newt (5)}, new t [1]{newt (6)},
62 new t [1]{newt (7)}, new t [1]{newt (8)}, new t [1]{newt (9)}, new t [1]{newt (10)}, new t [1]{newt (11)}
65 assert (array3 [0] != null);
66 assert (array3 [1] != null);
67 assert (array3 [2] != null);
68 assert (array3 [3] != null);
69 System.Array.Clear (array3, 1, 2);
70 assert (array3 [0] != null);
71 assert (array3 [1] == null);
72 assert (array3 [2] == null);
73 assert (array3 [3] != null);
76 void test_get_value ()
78 // Fast tests, should not print.
79 assert (array1 [0] != array1 [1]);
80 assert (array1 [2] != array1 [3]);
82 if (!slow_or_reflection)
83 return;
85 // While these are not reflecton, this is still presumed
86 // ok to be slow.
87 assert ((t)array1.GetValue (0) == array1 [0]);
88 assert ((t)array1.GetValue (3) == array1 [3]);
90 Type type = typeof (System.Array);
91 MethodInfo mi = type.GetMethod ("GetValue", new Type [] { typeof(int) } );
92 assert (mi != null);
93 assert ((t)mi.Invoke (array1, new object [ ] { 0 }) == array1 [0]);
94 assert ((t)mi.Invoke (array1, new object [ ] { 3 }) == array1 [3]);
97 void test_get_rank ()
99 // Fast tests, should not print.
100 assert (array1.Rank != array2.Rank);
101 assert (array2.Rank != array3.Rank);
103 if (!slow_or_reflection)
104 return;
105 Type type = typeof (System.Array);
106 PropertyInfo pi = type.GetProperty ("Rank");
107 assert ((int)pi.GetValue (array1) == array1.Rank);
108 assert ((int)pi.GetValue (array2) == array2.Rank);
109 assert ((int)pi.GetValue (array3) == array3.Rank);
110 assert ((int)pi.GetValue (array3 [0]) == array3 [0].Rank);
113 void test_get_length ()
115 // Fast tests, should not print.
116 assert (array1.Length != array2.Length);
117 assert (array2.Length != array3.Length);
119 if (!slow_or_reflection)
120 return;
121 Type type = typeof (System.Array);
122 PropertyInfo pi = type.GetProperty ("Length");
123 assert ((int)pi.GetValue (array1) == array1.Length);
124 assert ((int)pi.GetValue (array2) == array2.Length);
125 assert ((int)pi.GetValue (array3) == array3.Length);
128 void test_get_longlength ()
130 // Fast tests, should not print.
131 assert (array1.LongLength != array2.LongLength);
132 assert (array2.LongLength != array3.LongLength);
134 if (!slow_or_reflection)
135 return;
136 Type type = typeof (System.Array);
137 PropertyInfo pi = type.GetProperty ("LongLength");
138 assert ((long)pi.GetValue (array1) == array1.LongLength);
139 assert ((long)pi.GetValue (array2) == array2.LongLength);
140 assert ((long)pi.GetValue (array3) == array3.LongLength);
143 void test_get_lower_bound_and_get_value_with_bounds ()
145 // This test also goes overboard in testing slow paths.
147 Console.WriteLine ("test_get_lower_bound_and_get_value_with_bounds1");
149 var lengths = new int [ ] {1, 2, 3, 4};
150 var lower_bounds = new int [ ] {3, 2, 1, 0};
151 var a = (t[,,,])System.Array.CreateInstance (typeof (t), lengths, lower_bounds);
153 Console.WriteLine ("test_get_lower_bound_and_get_value_with_bounds1.1");
154 a [3, 2, 1, 0] = newt (-1);
155 Console.WriteLine ("test_get_lower_bound_and_get_value_with_bounds1.2");
156 a [3, 3, 2, 1] = newt (-2);
157 Console.WriteLine ("test_get_lower_bound_and_get_value_with_bounds1.3");
159 MethodInfo mi = null;
160 if (slow_or_reflection) {
161 Type type = typeof (System.Array);
162 mi = type.GetMethod ("GetLowerBound", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
163 assert (mi != null);
166 Console.WriteLine ("test_get_lower_bound_and_get_value_with_bounds2");
168 for (int b = 0; b < 3; ++b)
170 assert (a.GetLowerBound (b) == lower_bounds [b]);
171 if (slow_or_reflection)
172 assert ((int)mi.Invoke (a, new object [ ] { b }) == lower_bounds [b]);
175 var a2 = new t [1];
176 assert (a2 .GetLowerBound (0) == 0);
177 assert (array1.GetLowerBound (0) == 0);
178 assert (array2.GetLowerBound (0) == 0);
179 assert (array3.GetLowerBound (0) == 0);
181 Console.WriteLine ("test_get_lower_bound_and_get_value_with_bounds3");
183 if (slow_or_reflection) {
184 assert ((int)mi.Invoke (a2, new object [ ] { 0 }) == 0);
185 assert ((int)mi.Invoke (array1, new object [ ] { 0 }) == 0);
186 assert ((int)mi.Invoke (array2, new object [ ] { 0 }) == 0);
187 assert ((int)mi.Invoke (array3, new object [ ] { 0 }) == 0);
189 assert ((t)a.GetValue (new int [ ] {3, 2, 1, 0}) == newt (-1));
190 assert ((t)a.GetValue (new int [ ] {3, 3, 2, 1}) == newt (-2));
193 void test_get_generic_value ()
195 return; // fails for FullAOT, or even with AOT runtime, but not with full JIT that CI does not run
197 if (!slow_or_reflection)
198 return;
200 Type type = typeof (System.Array);
201 MethodInfo mig = type.GetMethod ("GetGenericValueImpl", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
202 assert (mig != null);
203 MethodInfo mi = mig.MakeGenericMethod (typeof (t));
204 assert (mi != null);
206 var args = new object [2];
207 for (int i = 0; i < array1.Length; ++i) {
208 args [0] = i;
209 args [1] = null;
210 mi.Invoke (array1, args);
211 assert (array1 [i] == (t)args [1]);
215 void test_set_generic_value ()
217 if (!slow_or_reflection)
218 return;
220 t [] array2 = new t [10];
222 for (int i = 0; i < array1.Length; ++i)
223 assert (array1 [i] != array2 [i]);
225 Type type = typeof (System.Array);
226 MethodInfo mig = type.GetMethod ("SetGenericValueImpl", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
227 assert (mig != null);
228 MethodInfo mi = mig.MakeGenericMethod (typeof (t));
229 assert (mi != null);
231 var args = new object [2];
232 for (int i = 0; i < array1.Length; ++i) {
233 args [0] = i;
234 args [1] = newt (i + 1);
235 mi.Invoke (array2, args);
238 for (int i = 0; i < array1.Length; ++i)
239 assert (array1 [i] == array2 [i]);
242 void main ()
244 Console.WriteLine ("test_set_generic_value");
245 try {
246 test_set_generic_value ();
248 catch (System.Reflection.TargetInvocationException) // for FullAOT
250 Console.WriteLine ("test_set_generic_value raise exception");
252 Console.WriteLine ("test_get_generic_value");
253 test_get_generic_value ();
254 Console.WriteLine ("test_clear");
255 test_clear ();
256 Console.WriteLine ("test_get_value");
257 test_get_value ();
258 Console.WriteLine ("test_get_rank");
259 test_get_rank ();
260 Console.WriteLine ("test_get_length");
261 test_get_length ();
262 Console.WriteLine ("test_get_longlength");
263 test_get_longlength ();
264 Console.WriteLine ("test_get_lower_bound_and_get_value_with_bounds");
265 test_get_lower_bound_and_get_value_with_bounds ();
268 public static void Main (string[] args)
270 new test ().main ();