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) {
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 ()
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);
125 public static int test_0_iface_call_null_bug_77442 () {
131 catch (NullReferenceException) {
138 public static int test_18_ldobj_stobj_generics () {
139 GenericClass<int> t = new GenericClass <int> ();
142 return t.ldobj_stobj (ref i, ref j) + i + j;
145 public static int test_5_ldelem_stelem_generics () {
146 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
148 TestStruct s = new TestStruct (5, 5);
149 return t.ldelem_stelem (s).i;
152 public static int test_0_constrained_vtype_box () {
153 GenericClass<TestStruct> t = new GenericClass<TestStruct> ();
155 return t.toString (new TestStruct ()) == "Tests+TestStruct" ? 0 : 1;
158 public static int test_0_constrained_vtype () {
159 GenericClass<int> t = new GenericClass<int> ();
161 return t.toString (1234) == "1234" ? 0 : 1;
164 public static int test_0_constrained_reftype () {
165 GenericClass<String> t = new GenericClass<String> ();
167 return t.toString ("1234") == "1234" ? 0 : 1;
170 public static int test_0_box_brtrue_optimizations () {
174 if (!IsNull<object>(null))
180 public static int test_0_generic_get_value_optimization_int () {
181 int[] x = new int[] {100, 200};
183 if (GenericClass<int>.Z (x, 0) != 100)
186 if (GenericClass<int>.Z (x, 1) != 200)
192 public static int test_0_generic_get_value_optimization_vtype () {
193 TestStruct[] arr = new TestStruct[] { new TestStruct (100, 200), new TestStruct (300, 400) };
194 IEnumerator<TestStruct> enumerator = GenericClass<TestStruct>.Y (arr);
197 while (enumerator.MoveNext ()) {
198 s = enumerator.Current;
205 s = GenericClass<TestStruct>.Z (arr, 0);
206 if (s.i != 100 || s.j != 200)
209 s = GenericClass<TestStruct>.Z (arr, 1);
210 if (s.i != 300 || s.j != 400)
216 public struct GenericStruct<T> {
219 public GenericStruct (T t) {
224 public class GenericClass<T> {
227 public GenericClass (T t) {
231 public GenericClass () {
234 public T ldobj_stobj (ref T t1, ref T t2) {
241 public T ldelem_stelem (T t) {
242 T[] arr = new T [10];
248 public String toString (T t) {
249 return t.ToString ();
252 public static IEnumerator<T> Y (IEnumerable <T> x)
254 return x.GetEnumerator ();
257 public static T Z (IList<T> x, int index)
263 public class MRO : MarshalByRefObject {
264 public GenericStruct<int> struct_field;
265 public GenericClass<int> class_field;
268 public static int test_0_ldfld_stfld_mro () {
270 GenericStruct<int> s = new GenericStruct<int> (5);
271 // This generates stfld
274 // This generates ldflda
275 if (m.struct_field.t != 5)
278 // This generates ldfld
279 GenericStruct<int> s2 = m.struct_field;
283 if (m.struct_field.t != 5)
286 m.class_field = new GenericClass<int> (5);
287 if (m.class_field.t != 5)
293 public static int test_0_generic_virtual_call_on_vtype_unbox () {
294 object o = new Object ();
295 IMyHandler h = new Handler(o);
297 if (h.Bar<object> () != o)
303 public static int test_0_box_brtrue_opt () {
304 Foo<int> f = new Foo<int> (5);
311 public static int test_0_box_brtrue_opt_regress_81102 () {
312 if (new Foo<int>(5).ToString () == "null")
322 public static int test_0_ldloca_initobj_opt () {
323 if (new Foo<S> (new S ()).get_default ().i != 0)
325 if (new Foo<object> (null).get_default () != null)
330 public static int test_0_variance_reflection () {
331 // covariance on IEnumerator
332 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (MyIEnumerator<string>)))
334 // covariance on IEnumerator and covariance on arrays
335 if (!typeof (MyIEnumerator<object>[]).IsAssignableFrom (typeof (MyIEnumerator<string>[])))
337 // covariance and implemented interfaces
338 if (!typeof (MyIEnumerator<object>).IsAssignableFrom (typeof (Enumerator<string>)))
341 // contravariance on IComparer
342 if (!typeof (IComparer<string>).IsAssignableFrom (typeof (IComparer<object>)))
344 // contravariance on IComparer, contravariance on arrays
345 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IComparer<object>[])))
347 // contravariance and interface inheritance
348 if (!typeof (IComparer<string>[]).IsAssignableFrom (typeof (IKeyComparer<object>[])))
353 public static int test_0_ldvirtftn_generic_method () {
354 new Tests ().ldvirtftn<string> ();
356 return the_type == typeof (string) ? 0 : 1;
359 public static Type the_type;
361 public void ldvirtftn<T> () {
362 Foo <T> binding = new Foo <T> (default (T));
364 binding.GenericEvent += event_handler;
368 public virtual void event_handler<T> (Foo<T> sender) {
369 the_type = typeof (T);
379 public override string ToString()
381 return Bar(m_t1 == null ? "null" : "null");
384 public String Bar (String s) {
388 public int this [T1 key] {
391 throw new ArgumentNullException ("key");
395 public T1 get_default () {
401 public delegate void GenericEventHandler (Foo<T1> sender);
403 public event GenericEventHandler GenericEvent;
405 public void fire () {
411 public interface IMyHandler {
415 struct Handler : IMyHandler {
418 public Handler(object o) {
422 public object Bar<T>() {
427 static bool IsNull<T> (T t)
435 static object Box<T> (T t)
440 static T Unbox <T> (object o) {