2 using System.Collections.Generic;
10 public TestStruct (int i, int j) {
16 class Enumerator <T> : MyIEnumerator <T> {
17 T MyIEnumerator<T>.Current {
23 bool MyIEnumerator<T>.MoveNext () {
28 class Comparer <T> : IComparer <T> {
29 bool IComparer<T>.Compare (T x, T y) {
34 static int Main (string[] args)
36 return TestDriver.RunTests (typeof (Tests), args);
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 ()
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 ()
96 // This uses unbox instead of unbox_any
105 public static void stelem_any<T> (T[] arr, T elem) {
109 public static T ldelem_any<T> (T[] arr) {
113 public static int test_1_ldelem_stelem_any_int () {
114 int[] arr = new int [3];
117 return ldelem_any (arr);
120 public static T return_ref<T> (ref T 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];
133 if (ldelema_any <string> (arr) == "Hello")
139 public static T[,] newarr_multi<T> () {
143 public static int test_0_newarr_multi_dim () {
144 return newarr_multi<string> ().GetType () == typeof (string[,]) ? 0 : 1;
152 public static int test_0_iface_call_null_bug_77442 () {
158 catch (NullReferenceException) {
165 public static int test_18_ldobj_stobj_generics () {
166 GenericClass<int> t = new GenericClass <int> ();
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 () {
201 if (!IsNull<object>(null))
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)
213 if (GenericClass<int>.Z (x, 1) != 200)
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);
224 while (enumerator.MoveNext ()) {
225 s = enumerator.Current;
232 s = GenericClass<TestStruct>.Z (arr, 0);
233 if (s.i != 100 || s.j != 200)
236 s = GenericClass<TestStruct>.Z (arr, 1);
237 if (s.i != 300 || s.j != 400)
243 public static int test_0_nullable_ldflda () {
244 return GenericClass<string>.BIsAClazz == false ? 0 : 1;
247 public struct GenericStruct<T> {
250 public GenericStruct (T t) {
255 public class GenericClass<T> {
258 public GenericClass (T t) {
262 public GenericClass () {
265 public T ldobj_stobj (ref T t1, ref T t2) {
272 public T ldelem_stelem (T t) {
273 T[] arr = new T [10];
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)
293 protected static T NullB = default(T);
294 private static Nullable<bool> _BIsA = null;
295 public static bool BIsAClazz {
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 () {
310 GenericStruct<int> s = new GenericStruct<int> (5);
311 // This generates stfld
314 // This generates ldflda
315 if (m.struct_field.t != 5)
318 // This generates ldfld
319 GenericStruct<int> s2 = m.struct_field;
323 if (m.struct_field.t != 5)
326 m.class_field = new GenericClass<int> (5);
327 if (m.class_field.t != 5)
334 [Category ("!FULLAOT")]
335 public static int test_0_generic_virtual_call_on_vtype_unbox () {
336 object o = new Object ();
337 IFoo h = new Handler(o);
339 if (h.Bar<object> () != o)
345 public static int test_0_box_brtrue_opt () {
346 Foo<int> f = new Foo<int> (5);
353 public static int test_0_box_brtrue_opt_regress_81102 () {
354 if (new Foo<int>(5).ToString () == "null")
364 public static int test_0_ldloca_initobj_opt () {
365 if (new Foo<S> (new S ()).get_default ().i != 0)
367 if (new Foo<object> (null).get_default () != null)
372 public static int test_0_variance_reflection () {
373 // covariance on IEnumerator
374 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
376 // covariance on IEnumerator and covariance on arrays
377 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
379 // covariance and implemented interfaces
380 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
383 // contravariance on IComparer
384 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
386 // contravariance on IComparer, contravariance on arrays
387 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
389 // contravariance and interface inheritance
390 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
395 public static int test_0_ldvirtftn_generic_method () {
396 new Tests ().ldvirtftn<string> ();
398 return the_type == typeof (string) ? 0 : 1;
401 public static int test_0_throw_dead_this () {
402 new Foo<string> ("").throw_dead_this ();
406 // This cannot be made to work with full-aot, since there it is impossible to
407 // statically determine that Foo<string>.Bar <int> is needed, the code only
408 // references IFoo.Bar<int>
409 [Category ("!FULLAOT")]
410 public static int test_0_generic_virtual_on_interfaces () {
411 Foo<string>.count1 = 0;
412 Foo<string>.count2 = 0;
413 Foo<string>.count3 = 0;
415 IFoo f = new Foo<string> ("");
416 for (int i = 0; i < 1000; ++i) {
422 if (Foo<string>.count1 != 1000)
424 if (Foo<string>.count2 != 1000)
426 if (Foo<string>.count3 != 1000)
429 VirtualInterfaceCallFromGenericMethod<long> (f);
435 [Category ("!FULLAOT")]
436 public static int test_2_cprop_bug () {
439 var cmp = System.Collections.Generic.Comparer<int>.Default ;
440 if (cmp.Compare (a, 0) > 0)
442 do { idx++; } while (cmp.Compare (idx - 1, a) == 0);
446 public static void VirtualInterfaceCallFromGenericMethod <T> (IFoo f) {
450 public static Type the_type;
452 public void ldvirtftn<T> () {
453 Foo <T> binding = new Foo <T> (default (T));
455 binding.GenericEvent += event_handler;
459 public virtual void event_handler<T> (Foo<T> sender) {
460 the_type = typeof (T);
463 public interface IFoo {
468 public class Foo<T1> : IFoo
475 public override string ToString()
477 return Bar(m_t1 == null ? "null" : "null");
480 public String Bar (String s) {
484 public int this [T1 key] {
487 throw new ArgumentNullException ("key");
491 public void throw_dead_this () {
493 new SomeClass().ThrowAnException();
499 public T1 get_default () {
505 public delegate void GenericEventHandler (Foo<T1> sender);
507 public event GenericEventHandler GenericEvent;
509 public void fire () {
513 public static int count1, count2, count3;
515 public void NonGeneric () {
519 public object Bar <T> () {
520 if (typeof (T) == typeof (int))
522 else if (typeof (T) == typeof (string))
528 public class SomeClass {
529 public void ThrowAnException() {
530 throw new Exception ("Something went wrong");
534 struct Handler : IFoo {
537 public Handler(object o) {
541 public void NonGeneric () {
544 public object Bar<T>() {
549 static bool IsNull<T> (T t)
557 static object Box<T> (T t)
562 static T Unbox <T> (object o) {