2010-06-21 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / generics.cs
bloba75e3ad59a8f5c5a21a2c5a0b29374f3787d9914
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
5 class Tests {
7 struct TestStruct {
8 public int i;
9 public int j;
11 public TestStruct (int i, int j) {
12 this.i = i;
13 this.j = j;
17 class Enumerator <T> : MyIEnumerator <T> {
18 T MyIEnumerator<T>.Current {
19 get {
20 return default(T);
24 bool MyIEnumerator<T>.MoveNext () {
25 return true;
29 class Comparer <T> : IComparer <T> {
30 bool IComparer<T>.Compare (T x, T y) {
31 return true;
35 static int Main (string[] args)
37 return TestDriver.RunTests (typeof (Tests), args);
40 public static int test_1_nullable_unbox ()
42 return Unbox<int?> (1).Value;
45 public static int test_1_nullable_unbox_null ()
47 return Unbox<int?> (null).HasValue ? 0 : 1;
50 public static int test_1_nullable_box ()
52 return (int) Box<int?> (1);
55 public static int test_1_nullable_box_null ()
57 return Box<int?> (null) == null ? 1 : 0;
60 public static int test_1_isinst_nullable ()
62 object o = 1;
63 return (o is int?) ? 1 : 0;
66 public static int test_1_nullable_unbox_vtype ()
68 return Unbox<TestStruct?> (new TestStruct (1, 2)).Value.i;
71 public static int test_1_nullable_unbox_null_vtype ()
73 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
76 public static int test_1_nullable_box_vtype ()
78 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1, 2)))).i;
81 public static int test_1_nullable_box_null_vtype ()
83 return Box<TestStruct?> (null) == null ? 1 : 0;
86 public static int test_1_isinst_nullable_vtype ()
88 object o = new TestStruct (1, 2);
89 return (o is TestStruct?) ? 1 : 0;
92 public static int test_0_nullable_normal_unbox ()
94 int? i = 5;
96 object o = i;
97 // This uses unbox instead of unbox_any
98 int? j = (int?)o;
100 if (j != 5)
101 return 1;
103 return 0;
106 public static void stelem_any<T> (T[] arr, T elem) {
107 arr [0] = elem;
110 public static T ldelem_any<T> (T[] arr) {
111 return arr [0];
114 public static int test_1_ldelem_stelem_any_int () {
115 int[] arr = new int [3];
116 stelem_any (arr, 1);
118 return ldelem_any (arr);
121 public static T return_ref<T> (ref T t) {
122 return t;
125 public static T ldelema_any<T> (T[] arr) {
126 return return_ref<T> (ref arr [0]);
129 public static int test_0_ldelema () {
130 string[] arr = new string [1];
132 arr [0] = "Hello";
134 if (ldelema_any <string> (arr) == "Hello")
135 return 0;
136 else
137 return 1;
140 public static T[,] newarr_multi<T> () {
141 return new T [1, 1];
144 public static int test_0_newarr_multi_dim () {
145 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
148 interface ITest
150 void Foo<T> ();
153 public static int test_0_iface_call_null_bug_77442 () {
154 ITest test = null;
156 try {
157 test.Foo<int> ();
159 catch (NullReferenceException) {
160 return 0;
163 return 1;
166 public static int test_18_ldobj_stobj_generics () {
167 GenericClass<int> t = new GenericClass <int> ();
168 int i = 5;
169 int j = 6;
170 return t.ldobj_stobj (ref i, ref j) + i + j;
173 public static int test_5_ldelem_stelem_generics () {
174 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
176 TestStruct s = new TestStruct (5, 5);
177 return t.ldelem_stelem (s).i;
180 public static int test_0_constrained_vtype_box () {
181 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
183 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
186 public static int test_0_constrained_vtype () {
187 GenericClass<int> t = new GenericClass<int> ();
189 return t.toString (1234) == "1234" ? 0 : 1;
192 public static int test_0_constrained_reftype () {
193 GenericClass<String> t = new GenericClass<String> ();
195 return t.toString ("1234") == "1234" ? 0 : 1;
198 public static int test_0_box_brtrue_optimizations () {
199 if (IsNull<int>(5))
200 return 1;
202 if (!IsNull<object>(null))
203 return 1;
205 return 0;
208 [Category ("!FULLAOT")]
209 public static int test_0_generic_get_value_optimization_int () {
210 int[] x = new int[] {100, 200};
212 if (GenericClass<int>.Z (x, 0) != 100)
213 return 2;
215 if (GenericClass<int>.Z (x, 1) != 200)
216 return 3;
218 return 0;
221 public static int test_0_generic_get_value_optimization_vtype () {
222 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
223 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
224 TestStruct s;
225 int sum = 0;
226 while (enumerator.MoveNext ()) {
227 s = enumerator.Current;
228 sum += s.i + s.j;
231 if (sum != 1000)
232 return 1;
234 s = GenericClass<TestStruct>.Z (arr, 0);
235 if (s.i != 100 || s.j != 200)
236 return 2;
238 s = GenericClass<TestStruct>.Z (arr, 1);
239 if (s.i != 300 || s.j != 400)
240 return 3;
242 return 0;
245 public static int test_0_nullable_ldflda () {
246 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
249 public struct GenericStruct<T> {
250 public T t;
252 public GenericStruct (T t) {
253 this.t = t;
257 public class GenericClass<T> {
258 public T t;
260 public GenericClass (T t) {
261 this.t = t;
264 public GenericClass () {
267 public T ldobj_stobj (ref T t1, ref T t2) {
268 t1 = t2;
269 T t = t1;
271 return t;
274 public T ldelem_stelem (T t) {
275 T[] arr = new T [10];
276 arr [0] = t;
278 return arr [0];
281 public String toString (T t) {
282 return t.ToString ();
285 public static IEnumerator<T> Y (IEnumerable <T> x)
287 return x.GetEnumerator ();
290 public static T Z (IList<T> x, int index)
292 return x [index];
295 protected static T NullB = default(T);
296 private static Nullable<bool> _BIsA = null;
297 public static bool BIsAClazz {
298 get {
299 _BIsA = false;
300 return _BIsA.Value;
305 public class MRO : MarshalByRefObject {
306 public GenericStruct<int> struct_field;
307 public GenericClass<int> class_field;
310 public static int test_0_ldfld_stfld_mro () {
311 MRO m = new MRO ();
312 GenericStruct<int> s = new GenericStruct<int> (5);
313 // This generates stfld
314 m.struct_field = s;
316 // This generates ldflda
317 if (m.struct_field.t != 5)
318 return 1;
320 // This generates ldfld
321 GenericStruct<int> s2 = m.struct_field;
322 if (s2.t != 5)
323 return 2;
325 if (m.struct_field.t != 5)
326 return 3;
328 m.class_field = new GenericClass<int> (5);
329 if (m.class_field.t != 5)
330 return 4;
332 return 0;
335 // FIXME:
336 [Category ("!FULLAOT")]
337 public static int test_0_generic_virtual_call_on_vtype_unbox () {
338 object o = new Object ();
339 IFoo h = new Handler(o);
341 if (h.Bar<object> () != o)
342 return 1;
343 else
344 return 0;
347 public static int test_0_box_brtrue_opt () {
348 Foo<int> f = new Foo<int> (5);
350 f [123] = 5;
352 return 0;
355 public static int test_0_box_brtrue_opt_regress_81102 () {
356 if (new Foo<int>(5).ToString () == "null")
357 return 0;
358 else
359 return 1;
362 struct S {
363 public int i;
366 public static int test_0_ldloca_initobj_opt () {
367 if (new Foo<S> (new S ()).get_default ().i != 0)
368 return 1;
369 if (new Foo<object> (null).get_default () != null)
370 return 2;
371 return 0;
374 public static int test_0_variance_reflection () {
375 // covariance on IEnumerator
376 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
377 return 1;
378 // covariance on IEnumerator and covariance on arrays
379 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
380 return 2;
381 // covariance and implemented interfaces
382 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
383 return 3;
385 // contravariance on IComparer
386 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
387 return 4;
388 // contravariance on IComparer, contravariance on arrays
389 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
390 return 5;
391 // contravariance and interface inheritance
392 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
393 return 6;
394 return 0;
397 public static int test_0_ldvirtftn_generic_method () {
398 new Tests ().ldvirtftn<string> ();
400 return the_type == typeof (string) ? 0 : 1;
403 public static int test_0_throw_dead_this () {
404 new Foo<string> ("").throw_dead_this ();
405 return 0;
408 struct S<T> {}
410 public static int test_0_inline_infinite_polymorphic_recursion () {
411 f<int>(0);
413 return 0;
416 private static void f<T>(int i) {
417 if(i==42) f<S<T>>(i);
420 // This cannot be made to work with full-aot, since there it is impossible to
421 // statically determine that Foo<string>.Bar <int> is needed, the code only
422 // references IFoo.Bar<int>
423 [Category ("!FULLAOT")]
424 public static int test_0_generic_virtual_on_interfaces () {
425 Foo<string>.count1 = 0;
426 Foo<string>.count2 = 0;
427 Foo<string>.count3 = 0;
429 IFoo f = new Foo<string> ("");
430 for (int i = 0; i < 1000; ++i) {
431 f.Bar <int> ();
432 f.Bar <string> ();
433 f.NonGeneric ();
436 if (Foo<string>.count1 != 1000)
437 return 1;
438 if (Foo<string>.count2 != 1000)
439 return 2;
440 if (Foo<string>.count3 != 1000)
441 return 3;
443 VirtualInterfaceCallFromGenericMethod<long> (f);
445 return 0;
448 //repro for #505375
449 [Category ("!FULLAOT")]
450 public static int test_2_cprop_bug () {
451 int idx = 0;
452 int a = 1;
453 var cmp = System.Collections.Generic.Comparer<int>.Default ;
454 if (cmp.Compare (a, 0) > 0)
455 a = 0;
456 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
457 return idx;
460 enum MyEnumUlong : ulong {
461 Value_2 = 2
464 public static int test_0_regress_550964_constrained_enum_long () {
465 MyEnumUlong a = MyEnumUlong.Value_2;
466 MyEnumUlong b = MyEnumUlong.Value_2;
468 return Pan (a, b) ? 0 : 1;
471 static bool Pan<T> (T a, T b)
473 return a.Equals (b);
476 public class XElement {
477 public string Value {
478 get; set;
482 public static int test_0_fullaot_linq () {
483 var allWords = new XElement [] { new XElement { Value = "one" } };
484 var filteredWords = allWords.Where(kw => kw.Value.StartsWith("T"));
485 return filteredWords.Count ();
488 public static int test_0_fullaot_comparer_t () {
489 var l = new SortedList <TimeSpan, int> ();
490 return l.Count;
493 static void enumerate<T> (IEnumerable<T> arr) {
494 foreach (var o in arr)
496 int c = ((ICollection<T>)arr).Count;
499 /* Test that treating arrays as generic collections works with full-aot */
500 public static int test_0_fullaot_array_wrappers () {
501 Tests[] arr = new Tests [10];
502 enumerate<Tests> (arr);
503 return 0;
506 static int cctor_count = 0;
508 public abstract class Beta<TChanged>
510 static Beta()
512 cctor_count ++;
516 public class Gamma<T> : Beta<T>
518 static Gamma()
523 // #519336
524 public static int test_2_generic_class_init_gshared_ctor () {
525 new Gamma<object>();
526 new Gamma<string>();
528 return cctor_count;
531 public static Type get_type<T> () {
532 return typeof (T);
535 public static int test_0_gshared_delegate_rgctx () {
536 Func<Type> t = new Func<Type> (get_type<string>);
538 if (t () == typeof (string))
539 return 0;
540 else
541 return 1;
544 // Creating a delegate from a generic method from gshared code
545 public static int test_0_gshared_delegate_from_gshared () {
546 if (gshared_delegate_from_gshared <object> () != 0)
547 return 1;
548 if (gshared_delegate_from_gshared <string> () != 0)
549 return 2;
550 return 0;
553 public static int gshared_delegate_from_gshared <T> () {
554 Func<Type> t = new Func<Type> (get_type<T>);
556 return t () == typeof (T) ? 0 : 1;
559 public static int test_0_marshalbyref_call_from_gshared_virt_elim () {
560 /* Calling a virtual method from gshared code which is changed to a nonvirt call */
561 Class1<object> o = new Class1<object> ();
562 o.Do (new Class2<object> ());
563 return 0;
566 public static int test_0_partial_sharing () {
567 if (PartialShared1 (new List<string> (), 1) != typeof (string))
568 return 1;
569 if (PartialShared1 (new List<Tests> (), 1) != typeof (Tests))
570 return 2;
571 if (PartialShared2 (new List<string> (), 1) != typeof (int))
572 return 3;
573 if (PartialShared2 (new List<Tests> (), 1) != typeof (int))
574 return 4;
575 return 0;
578 public static int test_6_partial_sharing_linq () {
579 var messages = new List<Message> ();
581 messages.Add (new Message () { MessageID = 5 });
582 messages.Add (new Message () { MessageID = 6 });
584 return messages.Max(i => i.MessageID);
587 public static int test_0_partial_shared_method_in_nonshared_class () {
588 var c = new Class1<double> ();
589 return (c.Foo<string> (5).GetType () == typeof (Class1<string>)) ? 0 : 1;
592 class Message {
593 public int MessageID {
594 get; set;
598 public static Type PartialShared1<T, K> (List<T> list, K k) {
599 return typeof (T);
602 public static Type PartialShared2<T, K> (List<T> list, K k) {
603 return typeof (K);
606 public class Class1<T> {
607 public virtual void Do (Class2<T> t) {
608 t.Foo ();
611 public virtual object Foo<U> (T t) {
612 return new Class1<U> ();
616 public interface IFace1<T> {
617 void Foo ();
620 public class Class2<T> : MarshalByRefObject, IFace1<T> {
621 public void Foo () {
627 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
628 f.Bar <T> ();
631 public static Type the_type;
633 public void ldvirtftn<T> () {
634 Foo <T> binding = new Foo <T> (default (T));
636 binding.GenericEvent += event_handler;
637 binding.fire ();
640 public virtual void event_handler<T> (Foo<T> sender) {
641 the_type = typeof (T);
644 public interface IFoo {
645 void NonGeneric ();
646 object Bar<T>();
649 public class Foo<T1> : IFoo
651 public Foo(T1 t1)
653 m_t1 = t1;
656 public override string ToString()
658 return Bar(m_t1 == null ? "null" : "null");
661 public String Bar (String s) {
662 return s;
665 public int this [T1 key] {
666 set {
667 if (key == null)
668 throw new ArgumentNullException ("key");
672 public void throw_dead_this () {
673 try {
674 new SomeClass().ThrowAnException();
676 catch {
680 public T1 get_default () {
681 return default (T1);
684 readonly T1 m_t1;
686 public delegate void GenericEventHandler (Foo<T1> sender);
688 public event GenericEventHandler GenericEvent;
690 public void fire () {
691 GenericEvent (this);
694 public static int count1, count2, count3;
696 public void NonGeneric () {
697 count3 ++;
700 public object Bar <T> () {
701 if (typeof (T) == typeof (int))
702 count1 ++;
703 else if (typeof (T) == typeof (string))
704 count2 ++;
705 return null;
709 public class SomeClass {
710 public void ThrowAnException() {
711 throw new Exception ("Something went wrong");
715 struct Handler : IFoo {
716 object o;
718 public Handler(object o) {
719 this.o = o;
722 public void NonGeneric () {
725 public object Bar<T>() {
726 return o;
730 static bool IsNull<T> (T t)
732 if (t == null)
733 return true;
734 else
735 return false;
738 static object Box<T> (T t)
740 return t;
743 static T Unbox <T> (object o) {
744 return (T) o;