2009-01-12 Geoff Norton <gnorton@novell.com>
[mono-project.git] / mono / mini / generics.cs
blob13e35108af95ce9f68040dbfb8c7e84b00e91b34
1 using System;
2 using System.Collections.Generic;
4 class Tests {
6 struct TestStruct {
7 public int i;
8 public int j;
10 public TestStruct (int i, int j) {
11 this.i = i;
12 this.j = j;
16 class Enumerator <T> : MyIEnumerator <T> {
17 T MyIEnumerator<T>.Current {
18 get {
19 return default(T);
23 bool MyIEnumerator<T>.MoveNext () {
24 return true;
28 class Comparer <T> : IComparer <T> {
29 bool IComparer<T>.Compare (T x, T y) {
30 return true;
34 static int Main ()
36 return TestDriver.RunTests (typeof (Tests));
39 public static int test_1_nullable_unbox ()
41 return Unbox<int?> (1).Value;
44 public static int test_1_nullable_unbox_null ()
46 return Unbox<int?> (null).HasValue ? 0 : 1;
49 public static int test_1_nullable_box ()
51 return (int) Box<int?> (1);
54 public static int test_1_nullable_box_null ()
56 return Box<int?> (null) == null ? 1 : 0;
59 public static int test_1_isinst_nullable ()
61 object o = 1;
62 return (o is int?) ? 1 : 0;
65 public static int test_1_nullable_unbox_vtype ()
67 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
70 public static int test_1_nullable_unbox_null_vtype ()
72 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
75 public static int test_1_nullable_box_vtype ()
77 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
80 public static int test_1_nullable_box_null_vtype ()
82 return Box<TestStruct?> (null) == null ? 1 : 0;
85 public static int test_1_isinst_nullable_vtype ()
87 object o = new TestStruct (1, 2);
88 return (o is TestStruct?) ? 1 : 0;
91 public static int test_0_nullable_normal_unbox ()
93 int? i = 5;
95 object o = i;
96 // This uses unbox instead of unbox_any
97 int? j = (int?)o;
99 if (j != 5)
100 return 1;
102 return 0;
105 public static void stelem_any<T> (T[] arr, T elem) {
106 arr [0] = elem;
109 public static T ldelem_any<T> (T[] arr) {
110 return arr [0];
113 public static int test_1_ldelem_stelem_any_int () {
114 int[] arr = new int [3];
115 stelem_any (arr, 1);
117 return ldelem_any (arr);
120 public static T return_ref<T> (ref T t) {
121 return t;
124 public static T ldelema_any<T> (T[] arr) {
125 return return_ref<T> (ref arr [0]);
128 public static int test_0_ldelema () {
129 string[] arr = new string [1];
131 arr [0] = "Hello";
133 if (ldelema_any <string> (arr) == "Hello")
134 return 0;
135 else
136 return 1;
139 public static T[,] newarr_multi<T> () {
140 return new T [1, 1];
143 public static int test_0_newarr_multi_dim () {
144 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
147 interface ITest
149 void Foo<T> ();
152 public static int test_0_iface_call_null_bug_77442 () {
153 ITest test = null;
155 try {
156 test.Foo<int> ();
158 catch (NullReferenceException) {
159 return 0;
162 return 1;
165 public static int test_18_ldobj_stobj_generics () {
166 GenericClass<int> t = new GenericClass <int> ();
167 int i = 5;
168 int j = 6;
169 return t.ldobj_stobj (ref i, ref j) + i + j;
172 public static int test_5_ldelem_stelem_generics () {
173 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
175 TestStruct s = new TestStruct (5, 5);
176 return t.ldelem_stelem (s).i;
179 public static int test_0_constrained_vtype_box () {
180 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
182 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
185 public static int test_0_constrained_vtype () {
186 GenericClass<int> t = new GenericClass<int> ();
188 return t.toString (1234) == "1234" ? 0 : 1;
191 public static int test_0_constrained_reftype () {
192 GenericClass<String> t = new GenericClass<String> ();
194 return t.toString ("1234") == "1234" ? 0 : 1;
197 public static int test_0_box_brtrue_optimizations () {
198 if (IsNull<int>(5))
199 return 1;
201 if (!IsNull<object>(null))
202 return 1;
204 return 0;
207 public static int test_0_generic_get_value_optimization_int () {
208 int[] x = new int[] {100, 200};
210 if (GenericClass<int>.Z (x, 0) != 100)
211 return 2;
213 if (GenericClass<int>.Z (x, 1) != 200)
214 return 3;
216 return 0;
219 public static int test_0_generic_get_value_optimization_vtype () {
220 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
221 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
222 TestStruct s;
223 int sum = 0;
224 while (enumerator.MoveNext ()) {
225 s = enumerator.Current;
226 sum += s.i + s.j;
229 if (sum != 1000)
230 return 1;
232 s = GenericClass<TestStruct>.Z (arr, 0);
233 if (s.i != 100 || s.j != 200)
234 return 2;
236 s = GenericClass<TestStruct>.Z (arr, 1);
237 if (s.i != 300 || s.j != 400)
238 return 3;
240 return 0;
243 public static int test_0_nullable_ldflda () {
244 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
247 public struct GenericStruct<T> {
248 public T t;
250 public GenericStruct (T t) {
251 this.t = t;
255 public class GenericClass<T> {
256 public T t;
258 public GenericClass (T t) {
259 this.t = t;
262 public GenericClass () {
265 public T ldobj_stobj (ref T t1, ref T t2) {
266 t1 = t2;
267 T t = t1;
269 return t;
272 public T ldelem_stelem (T t) {
273 T[] arr = new T [10];
274 arr [0] = t;
276 return arr [0];
279 public String toString (T t) {
280 return t.ToString ();
283 public static IEnumerator<T> Y (IEnumerable <T> x)
285 return x.GetEnumerator ();
288 public static T Z (IList<T> x, int index)
290 return x [index];
293 protected static T NullB = default(T);
294 private static Nullable<bool> _BIsA = null;
295 public static bool BIsAClazz {
296 get {
297 _BIsA = false;
298 return _BIsA.Value;
303 public class MRO : MarshalByRefObject {
304 public GenericStruct<int> struct_field;
305 public GenericClass<int> class_field;
308 public static int test_0_ldfld_stfld_mro () {
309 MRO m = new MRO ();
310 GenericStruct<int> s = new GenericStruct<int> (5);
311 // This generates stfld
312 m.struct_field = s;
314 // This generates ldflda
315 if (m.struct_field.t != 5)
316 return 1;
318 // This generates ldfld
319 GenericStruct<int> s2 = m.struct_field;
320 if (s2.t != 5)
321 return 2;
323 if (m.struct_field.t != 5)
324 return 3;
326 m.class_field = new GenericClass<int> (5);
327 if (m.class_field.t != 5)
328 return 4;
330 return 0;
333 public static int test_0_generic_virtual_call_on_vtype_unbox () {
334 object o = new Object ();
335 IMyHandler h = new Handler(o);
337 if (h.Bar<object> () != o)
338 return 1;
339 else
340 return 0;
343 public static int test_0_box_brtrue_opt () {
344 Foo<int> f = new Foo<int> (5);
346 f [123] = 5;
348 return 0;
351 public static int test_0_box_brtrue_opt_regress_81102 () {
352 if (new Foo<int>(5).ToString () == "null")
353 return 0;
354 else
355 return 1;
358 struct S {
359 public int i;
362 public static int test_0_ldloca_initobj_opt () {
363 if (new Foo<S> (new S ()).get_default ().i != 0)
364 return 1;
365 if (new Foo<object> (null).get_default () != null)
366 return 2;
367 return 0;
370 public static int test_0_variance_reflection () {
371 // covariance on IEnumerator
372 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
373 return 1;
374 // covariance on IEnumerator and covariance on arrays
375 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
376 return 2;
377 // covariance and implemented interfaces
378 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
379 return 3;
381 // contravariance on IComparer
382 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
383 return 4;
384 // contravariance on IComparer, contravariance on arrays
385 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
386 return 5;
387 // contravariance and interface inheritance
388 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
389 return 6;
390 return 0;
393 public static int test_0_ldvirtftn_generic_method () {
394 new Tests ().ldvirtftn<string> ();
396 return the_type == typeof (string) ? 0 : 1;
399 public static int test_0_throw_dead_this () {
400 new Foo<string> ("").throw_dead_this ();
401 return 0;
404 public static Type the_type;
406 public void ldvirtftn<T> () {
407 Foo <T> binding = new Foo <T> (default (T));
409 binding.GenericEvent += event_handler;
410 binding.fire ();
413 public virtual void event_handler<T> (Foo<T> sender) {
414 the_type = typeof (T);
417 public class Foo<T1>
419 public Foo(T1 t1)
421 m_t1 = t1;
424 public override string ToString()
426 return Bar(m_t1 == null ? "null" : "null");
429 public String Bar (String s) {
430 return s;
433 public int this [T1 key] {
434 set {
435 if (key == null)
436 throw new ArgumentNullException ("key");
440 public void throw_dead_this () {
441 try {
442 new SomeClass().ThrowAnException();
444 catch {
448 public T1 get_default () {
449 return default (T1);
452 readonly T1 m_t1;
454 public delegate void GenericEventHandler (Foo<T1> sender);
456 public event GenericEventHandler GenericEvent;
458 public void fire () {
459 GenericEvent (this);
464 public class SomeClass {
465 public void ThrowAnException() {
466 throw new Exception ("Something went wrong");
470 public interface IMyHandler {
471 object Bar<T>();
474 struct Handler : IMyHandler {
475 object o;
477 public Handler(object o) {
478 this.o = o;
481 public object Bar<T>() {
482 return o;
486 static bool IsNull<T> (T t)
488 if (t == null)
489 return true;
490 else
491 return false;
494 static object Box<T> (T t)
496 return t;
499 static T Unbox <T> (object o) {
500 return (T) o;