2 using System
.Collections
.Generic
;
11 public TestStruct (int i
, int j
) {
17 class Enumerator
<T
> : MyIEnumerator
<T
> {
18 T MyIEnumerator
<T
>.Current
{
24 bool MyIEnumerator
<T
>.MoveNext () {
29 class Comparer
<T
> : IComparer
<T
> {
30 bool IComparer
<T
>.Compare (T x
, T y
) {
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 ()
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 ()
97 // This uses unbox instead of unbox_any
106 public static void stelem_any
<T
> (T
[] arr
, T elem
) {
110 public static T ldelem_any
<T
> (T
[] arr
) {
114 public static int test_1_ldelem_stelem_any_int () {
115 int[] arr
= new int [3];
118 return ldelem_any (arr
);
121 public static T return_ref
<T
> (ref T 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];
134 if (ldelema_any
<string> (arr
) == "Hello")
140 public static T
[,] newarr_multi
<T
> () {
144 public static int test_0_newarr_multi_dim () {
145 return newarr_multi
<string> ().GetType () == typeof (string[,]) ? 0 : 1;
153 public static int test_0_iface_call_null_bug_77442 () {
159 catch (NullReferenceException
) {
166 public static int test_18_ldobj_stobj_generics () {
167 GenericClass
<int> t
= new GenericClass
<int> ();
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 () {
202 if (!IsNull
<object>(null))
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)
215 if (GenericClass
<int>.Z (x
, 1) != 200)
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
);
226 while (enumerator
.MoveNext ()) {
227 s
= enumerator
.Current
;
234 s
= GenericClass
<TestStruct
>.Z (arr
, 0);
235 if (s
.i
!= 100 || s
.j
!= 200)
238 s
= GenericClass
<TestStruct
>.Z (arr
, 1);
239 if (s
.i
!= 300 || s
.j
!= 400)
245 public static int test_0_nullable_ldflda () {
246 return GenericClass
<string>.BIsAClazz
== false ? 0 : 1;
249 public struct GenericStruct
<T
> {
252 public GenericStruct (T t
) {
257 public class GenericClass
<T
> {
260 public GenericClass (T t
) {
264 public GenericClass () {
267 public T
ldobj_stobj (ref T t1
, ref T t2
) {
274 public T
ldelem_stelem (T t
) {
275 T
[] arr
= new T
[10];
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
)
295 protected static T NullB
= default(T
);
296 private static Nullable
<bool> _BIsA
= null;
297 public static bool BIsAClazz
{
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 () {
312 GenericStruct
<int> s
= new GenericStruct
<int> (5);
313 // This generates stfld
316 // This generates ldflda
317 if (m
.struct_field
.t
!= 5)
320 // This generates ldfld
321 GenericStruct
<int> s2
= m
.struct_field
;
325 if (m
.struct_field
.t
!= 5)
328 m
.class_field
= new GenericClass
<int> (5);
329 if (m
.class_field
.t
!= 5)
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
)
347 public static int test_0_box_brtrue_opt () {
348 Foo
<int> f
= new Foo
<int> (5);
355 public static int test_0_box_brtrue_opt_regress_81102 () {
356 if (new Foo
<int>(5).ToString () == "null")
366 public static int test_0_ldloca_initobj_opt () {
367 if (new Foo
<S
> (new S ()).get_default ().i
!= 0)
369 if (new Foo
<object> (null).get_default () != null)
374 public static int test_0_variance_reflection () {
375 // covariance on IEnumerator
376 if (!typeof (MyIEnumerator
<object>).IsAssignableFrom (typeof (MyIEnumerator
<string>)))
378 // covariance on IEnumerator and covariance on arrays
379 if (!typeof (MyIEnumerator
<object>[]).IsAssignableFrom (typeof (MyIEnumerator
<string>[])))
381 // covariance and implemented interfaces
382 if (!typeof (MyIEnumerator
<object>).IsAssignableFrom (typeof (Enumerator
<string>)))
385 // contravariance on IComparer
386 if (!typeof (IComparer
<string>).IsAssignableFrom (typeof (IComparer
<object>)))
388 // contravariance on IComparer, contravariance on arrays
389 if (!typeof (IComparer
<string>[]).IsAssignableFrom (typeof (IComparer
<object>[])))
391 // contravariance and interface inheritance
392 if (!typeof (IComparer
<string>[]).IsAssignableFrom (typeof (IKeyComparer
<object>[])))
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 ();
408 // This cannot be made to work with full-aot, since there it is impossible to
409 // statically determine that Foo<string>.Bar <int> is needed, the code only
410 // references IFoo.Bar<int>
411 [Category ("!FULLAOT")]
412 public static int test_0_generic_virtual_on_interfaces () {
413 Foo
<string>.count1
= 0;
414 Foo
<string>.count2
= 0;
415 Foo
<string>.count3
= 0;
417 IFoo f
= new Foo
<string> ("");
418 for (int i
= 0; i
< 1000; ++i
) {
424 if (Foo
<string>.count1
!= 1000)
426 if (Foo
<string>.count2
!= 1000)
428 if (Foo
<string>.count3
!= 1000)
431 VirtualInterfaceCallFromGenericMethod
<long> (f
);
437 [Category ("!FULLAOT")]
438 public static int test_2_cprop_bug () {
441 var cmp
= System
.Collections
.Generic
.Comparer
<int>.Default
;
442 if (cmp
.Compare (a
, 0) > 0)
444 do { idx++; }
while (cmp
.Compare (idx
- 1, a
) == 0);
448 enum MyEnumUlong
: ulong {
452 public static int test_0_regress_550964_constrained_enum_long () {
453 MyEnumUlong a
= MyEnumUlong
.Value_2
;
454 MyEnumUlong b
= MyEnumUlong
.Value_2
;
456 return Pan (a
, b
) ? 0 : 1;
459 static bool Pan
<T
> (T a
, T b
)
464 public class XElement
{
465 public string Value
{
470 public static int test_0_fullaot_linq () {
471 var allWords
= new XElement
[] { new XElement { Value = "one" }
};
472 var filteredWords
= allWords
.Where(kw
=> kw
.Value
.StartsWith("T"));
473 return filteredWords
.Count ();
476 public static int test_0_fullaot_comparer_t () {
477 var l
= new SortedList
<TimeSpan
, int> ();
481 static void enumerate
<T
> (IEnumerable
<T
> arr
) {
482 foreach (var o
in arr
)
484 int c
= ((ICollection
<T
>)arr
).Count
;
487 /* Test that treating arrays as generic collections works with full-aot */
488 public static int test_0_fullaot_array_wrappers () {
489 Tests
[] arr
= new Tests
[10];
490 enumerate
<Tests
> (arr
);
494 static int cctor_count
= 0;
496 public abstract class Beta
<TChanged
>
504 public class Gamma
<T
> : Beta
<T
>
512 public static int test_2_generic_class_init_gshared_ctor () {
519 public static void VirtualInterfaceCallFromGenericMethod
<T
> (IFoo f
) {
523 public static Type the_type
;
525 public void ldvirtftn
<T
> () {
526 Foo
<T
> binding
= new Foo
<T
> (default (T
));
528 binding
.GenericEvent
+= event_handler
;
532 public virtual void event_handler
<T
> (Foo
<T
> sender
) {
533 the_type
= typeof (T
);
536 public interface IFoo
{
541 public class Foo
<T1
> : IFoo
548 public override string ToString()
550 return Bar(m_t1
== null ? "null" : "null");
553 public String
Bar (String s
) {
557 public int this [T1 key
] {
560 throw new ArgumentNullException ("key");
564 public void throw_dead_this () {
566 new SomeClass().ThrowAnException();
572 public T1
get_default () {
578 public delegate void GenericEventHandler (Foo
<T1
> sender
);
580 public event GenericEventHandler GenericEvent
;
582 public void fire () {
586 public static int count1
, count2
, count3
;
588 public void NonGeneric () {
592 public object Bar
<T
> () {
593 if (typeof (T
) == typeof (int))
595 else if (typeof (T
) == typeof (string))
601 public class SomeClass
{
602 public void ThrowAnException() {
603 throw new Exception ("Something went wrong");
607 struct Handler
: IFoo
{
610 public Handler(object o
) {
614 public void NonGeneric () {
617 public object Bar
<T
>() {
622 static bool IsNull
<T
> (T t
)
630 static object Box
<T
> (T t
)
635 static T Unbox
<T
> (object o
) {