2007-06-28 Martin Baulig <martin@ximian.com>
[mono.git] / mono / mini / generics.2.cs
1 using System;
2
3 class Tests {
4
5         struct TestStruct {
6                 public int i;
7
8                 public TestStruct (int i) {
9                         this.i = i;
10                 }
11         }
12
13         static int Main ()
14         {
15                 return TestDriver.RunTests (typeof (Tests));
16         }
17
18         public static int test_1_nullable_unbox ()
19         {
20                 return Unbox<int?> (1).Value;
21         }
22
23         public static int test_1_nullable_unbox_null ()
24         {
25                 return Unbox<int?> (null).HasValue ? 0 : 1;
26         }
27
28         public static int test_1_nullable_box ()
29         {
30                 return (int) Box<int?> (1);
31         }
32
33         public static int test_1_nullable_box_null ()
34         {
35                 return Box<int?> (null) == null ? 1 : 0;
36         }
37
38         public static int test_1_isinst_nullable ()
39         {
40                 object o = 1;
41                 return (o is int?) ? 1 : 0;
42         }
43
44         public static int test_1_nullable_unbox_vtype ()
45         {
46                 return Unbox<TestStruct?> (new TestStruct (1)).Value.i;
47         }
48
49         public static int test_1_nullable_unbox_null_vtype ()
50         {
51                 return Unbox<TestStruct?> (null).HasValue ? 0 : 1;
52         }
53
54         public static int test_1_nullable_box_vtype ()
55         {
56                 return ((TestStruct)(Box<TestStruct?> (new TestStruct (1)))).i;
57         }
58
59         public static int test_1_nullable_box_null_vtype ()
60         {
61                 return Box<TestStruct?> (null) == null ? 1 : 0;
62         }
63
64         public static int test_1_isinst_nullable_vtype ()
65         {
66                 object o = new TestStruct (1);
67                 return (o is TestStruct?) ? 1 : 0;
68         }
69
70         public static void stelem_any<T> (T[] arr, T elem) {
71                 arr [0] = elem;
72         }
73
74         public static T ldelem_any<T> (T[] arr) {
75                 return arr [0];
76         }
77
78         public static int test_1_ldelem_stelem_any_int () {
79                 int[] arr = new int [3];
80                 stelem_any (arr, 1);
81
82                 return ldelem_any (arr);
83         }
84
85         interface ITest
86         {
87                 void Foo<T> ();
88         }
89
90         public static int test_0_iface_call_null_bug_77442 () {
91                 ITest test = null;
92
93                 try {
94                         test.Foo<int> ();
95                 }
96                 catch (NullReferenceException) {
97                         return 0;
98                 }
99                 
100                 return 1;
101         }
102
103         public struct GenericStruct<T> {
104                 public T t;
105
106                 public GenericStruct (T t) {
107                         this.t = t;
108                 }
109         }
110
111         public class GenericClass<T> {
112                 public T t;
113
114                 public GenericClass (T t) {
115                         this.t = t;
116                 }
117         }
118
119         public class MRO : MarshalByRefObject {
120                 public GenericStruct<int> struct_field;
121                 public GenericClass<int> class_field;
122         }
123
124         public static int test_0_ldfld_stfld_mro () {
125                 MRO m = new MRO ();
126                 GenericStruct<int> s = new GenericStruct<int> (5);
127                 // This generates stfld
128                 m.struct_field = s;
129
130                 // This generates ldflda
131                 if (m.struct_field.t != 5)
132                         return 1;
133
134                 // This generates ldfld
135                 GenericStruct<int> s2 = m.struct_field;
136                 if (s2.t != 5)
137                         return 2;
138
139                 if (m.struct_field.t != 5)
140                         return 3;
141
142                 m.class_field = new GenericClass<int> (5);
143                 if (m.class_field.t != 5)
144                         return 4;
145
146                 return 0;
147         }
148
149     public static int test_0_generic_virtual_call_on_vtype_unbox () {
150                 object o = new Object ();
151         IMyHandler h = new Handler(o);
152
153         if (h.Bar<object> () != o)
154                         return 1;
155                 else
156                         return 0;
157     }
158
159         public static int test_0_box_brtrue_opt () {
160                 Foo<int> f = new Foo<int> (5);
161
162                 f [123] = 5;
163
164                 return 0;
165         }
166
167         public static int test_0_box_brtrue_opt_regress_81102 () {
168                 if (new Foo<int>(5).ToString () == "null")
169                         return 0;
170                 else
171                         return 1;
172         }
173
174         public class Foo<T1>
175         {
176                 public Foo(T1 t1)
177                 {
178                         m_t1 = t1;
179                 }
180                 
181                 public override string ToString()
182                 {
183                         return Bar(m_t1 == null ? "null" : "null");
184                 }
185
186                 public String Bar (String s) {
187                         return s;
188                 }
189
190                 public int this [T1 key] {
191                         set {
192                                 if (key == null)
193                                         throw new ArgumentNullException ("key");
194                         }
195                 }
196                 
197                 readonly T1 m_t1;
198         }
199
200         public interface IMyHandler {
201                 object Bar<T>();
202         }
203
204         struct Handler : IMyHandler {
205                 object o;
206
207                 public Handler(object o) {
208                         this.o = o;
209                 }
210
211                 public object Bar<T>() {
212                         return o;
213                 }
214         }
215
216         static object Box<T> (T t)
217         {
218                 return t;
219         }
220         
221         static T Unbox <T> (object o) {
222                 return (T) o;
223         }
224 }