Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / gtest-284.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4
5 public interface Y
6 { }
7
8 public class X : Y
9 {
10 }
11
12 public struct Foo : Y
13 { }
14
15 public static class CollectionTester
16 {
17         static int Test<T> (IList<T> list)
18         {
19                 if (list.Count != 1)
20                         return 1;
21
22                 ICollection<T> collection = list;
23                 if (collection.Count != 1)
24                         return 2;
25
26                 IEnumerable<T> enumerable = list;
27                 IEnumerator<T> enumerator = enumerable.GetEnumerator ();
28                 if (!enumerator.MoveNext ())
29                         return 3;
30                 if (enumerator.MoveNext ())
31                         return 4;
32
33                 return 0;
34         }
35
36         public static int Test ()
37         {
38 #region X
39                 X[] xarray = new X [] { new X () };
40
41                 int result;
42                 result = Test<X> (xarray);
43                 if (result != 0)
44                         return result;
45
46                 result = Test<object> (xarray);
47                 if (result != 0)
48                         return 10 + result;
49
50                 result = Test<Y> (xarray);
51                 if (result != 0)
52                         return 20 + result;
53 #endregion
54
55 #region int
56                 int[] iarray = new int [] { 5 };
57                 result = Test<int> (iarray);
58                 if (result != 0)
59                         return 30 + result;
60
61                 result = Test<uint> ((IList<uint>) (object) iarray);
62                 if (result != 0)
63                         return 40 + result;
64
65                 uint[] uiarray = new uint [] { 5 };
66                 result = Test<int> ((IList<int>) (object) uiarray);
67                 if (result != 0)
68                         return 50 + result;
69
70                 result = Test<uint> (uiarray);
71                 if (result != 0)
72                         return 60 + result;
73 #endregion
74
75 #region long
76                 long[] larray = new long [] { 5 };
77                 result = Test<long> (larray);
78                 if (result != 0)
79                         return 70 + result;
80
81                 result = Test<ulong> ((IList<ulong>) (object) larray);
82                 if (result != 0)
83                         return 80 + result;
84
85                 ulong[] ularray = new ulong [] { 5 };
86                 result = Test<long> ((IList<long>) (object) ularray);
87                 if (result != 0)
88                         return 90 + result;
89
90                 result = Test<ulong> (ularray);
91                 if (result != 0)
92                         return 100 + result;
93 #endregion
94
95 #region short
96                 short[] sarray = new short [] { 5 };
97                 result = Test<short> (sarray);
98                 if (result != 0)
99                         return 110 + result;
100
101                 result = Test<ushort> ((IList<ushort>) (object) sarray);
102                 if (result != 0)
103                         return 120 + result;
104
105                 ushort[] usarray = new ushort [] { 5 };
106                 result = Test<short> ((IList<short>) (object) usarray);
107                 if (result != 0)
108                         return 130 + result;
109
110                 result = Test<ushort> (usarray);
111                 if (result != 0)
112                         return 140 + result;
113 #endregion
114
115 #region byte
116                 byte[] barray = new byte [] { 5 };
117                 result = Test<byte> (barray);
118                 if (result != 0)
119                         return 150 + result;
120
121                 result = Test<sbyte> ((IList<sbyte>) (object) barray);
122                 if (result != 0)
123                         return 160 + result;
124
125                 sbyte[] sbarray = new sbyte [] { 5 };
126                 result = Test<byte> ((IList<byte>) (object) sbarray);
127                 if (result != 0)
128                         return 170 + result;
129
130                 result = Test<sbyte> (sbarray);
131                 if (result != 0)
132                         return 180 + result;
133 #endregion
134
135                 return 0;
136         }
137 }
138
139 public static class InterfaceTester
140 {
141         public const bool Debug = false;
142
143         static readonly Type ilist_type;
144         static readonly Type icollection_type;
145         static readonly Type ienumerable_type;
146         static readonly Type generic_ilist_type;
147         static readonly Type generic_icollection_type;
148         static readonly Type generic_ienumerable_type;
149         static readonly Type icloneable_type;
150
151         static InterfaceTester ()
152         {
153                 ilist_type = typeof (IList);
154                 icollection_type = typeof (ICollection);
155                 ienumerable_type = typeof (IEnumerable);
156                 generic_ilist_type = typeof (IList<>);
157                 generic_icollection_type = typeof (ICollection<>);
158                 generic_ienumerable_type = typeof (IEnumerable<>);
159                 icloneable_type = typeof (ICloneable);
160         }
161
162         enum State {
163                 Missing,
164                 Found,
165                 Extra
166         }
167
168         static int Test (Type t, params Type[] iface_types)
169         {
170                 Hashtable ifaces = new Hashtable ();
171                 ifaces.Add (ilist_type, State.Missing);
172                 ifaces.Add (icollection_type, State.Missing);
173                 ifaces.Add (ienumerable_type, State.Missing);
174                 ifaces.Add (icloneable_type, State.Missing);
175                 ifaces.Add (typeof (IStructuralEquatable), State.Missing);
176                 ifaces.Add (typeof (IStructuralComparable), State.Missing);
177                 Type array_type = t.MakeArrayType ();
178
179                 if (Debug) {
180                         Console.WriteLine ("Checking {0}", t);
181                         foreach (Type iface in t.GetInterfaces ())
182                                 Console.WriteLine ("  {0}", iface);
183                 }
184
185                 foreach (Type iface in iface_types) {
186                         Type[] gargs = new Type[] { iface };
187                         ifaces.Add (generic_ilist_type.MakeGenericType (gargs), State.Missing);
188                         ifaces.Add (generic_icollection_type.MakeGenericType (gargs), State.Missing);
189                         ifaces.Add (generic_ienumerable_type.MakeGenericType (gargs), State.Missing);
190
191 #if NET_4_5
192                         ifaces.Add (typeof (IReadOnlyCollection<>).MakeGenericType (gargs), State.Missing);
193                         ifaces.Add (typeof (IReadOnlyList<>).MakeGenericType (gargs), State.Missing);
194 #endif
195                 }
196
197                 foreach (Type iface in array_type.GetInterfaces ()) {
198                         if (ifaces.Contains (iface))
199                                 ifaces [iface] = State.Found;
200                         else
201                                 ifaces.Add (iface, State.Extra);
202                 }
203
204                 int errors = 0;
205
206                 foreach (Type iface in ifaces.Keys) {
207                         State state = (State) ifaces [iface];
208                         if (state == State.Found) {
209                                 if (Debug)
210                                         Console.WriteLine ("Found {0}", iface);
211                                 continue;
212                         } else {
213                                 if (Debug)
214                                         Console.WriteLine ("ERROR: {0} {1}", iface, state);
215                                 errors++;
216                         }
217                 }
218
219                 if (Debug)
220                         Console.WriteLine ();
221
222                 return errors;
223         }
224
225         public static int Test ()
226         {
227                 int result = Test (typeof (X), typeof (X));
228                 if (result != 0)
229                         return result;
230
231                 result = Test (typeof (Y), typeof (Y));
232                 if (result != 0)
233                         return 100 + result;
234
235                 result = Test (typeof (DateTime), typeof (DateTime));
236                 if (result != 0)
237                         return 200 + result;
238
239                 result = Test (typeof (float), typeof (float));
240                 if (result != 0)
241                         return 300 + result;
242
243                 result = Test (typeof (int), typeof (int));
244                 if (result != 0)
245                         return 400 + result;
246
247                 result = Test (typeof (uint), typeof (uint));
248                 if (result != 0)
249                         return 500 + result;
250
251                 result = Test (typeof (long), typeof (long));
252                 if (result != 0)
253                         return 600 + result;
254
255                 result = Test (typeof (ulong), typeof (ulong));
256                 if (result != 0)
257                         return 700 + result;
258
259                 result = Test (typeof (short), typeof (short));
260                 if (result != 0)
261                         return 800 + result;
262
263                 result = Test (typeof (ushort), typeof (ushort));
264                 if (result != 0)
265                         return 900 + result;
266
267                 result = Test (typeof (Foo), typeof (Foo));
268                 if (result != 0)
269                         return 1000 + result;
270
271                 return 0;
272         }
273 }
274
275 class Z
276 {
277         static int Test ()
278         {
279                 int result;
280                 result = CollectionTester.Test ();
281                 if (result != 0)
282                         return result;
283                 result = InterfaceTester.Test ();
284                 if (result != 0)
285                         return 10000 + result;
286                 return 0;
287         }
288
289         public static int Main ()
290         {
291                 int result = Test ();
292                 if (result == 0)
293                         Console.WriteLine ("OK");
294                 else
295                         Console.WriteLine ("ERROR: {0}", result);
296                 return result;
297         }
298 }