summaryrefslogtreecommitdiff
path: root/tests/src/Loader/classloader/generics/Variance/IL/vsw543506.cs
blob: a7971d4ed88cebaff5a446984ed4bc897d00e526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Regression test for VSW 543506
/*

Testing that generic argument types for co/contravariant generic types are boxed
(VSW 543506)
Test: under Loader\ClassLoader\Generics\Variance\IL
Positive and negative tests on casting valuetype instantiations such as
Positive:
"	IPos<int> is castable to IPos<int>   (exact match on value types)

Negative
"	IPos<int> is not castable to IPos<unsigned int>
IPos<int> is not castable to IPos<MyEnum>


*/
using System;

public class C<T> : IPos<T>, INeg<T>
{
    public T produce()
    {
        return default(T);
    }

    public void consume(T t)
    {
    }
}

enum intEnum : int {}
enum uintEnum : uint {}

class Test
{

	public static bool pass;
	public static void IsInstShouldFail<T>(object o)
	{
		Console.WriteLine("cast from " + o.GetType() + " to  " + typeof(T));

    		if (o is T) 
		{
			Console.WriteLine("isinst on object of type " + o.GetType() + " to " + typeof(T) + " succeeded. Expected failure");
			pass = false;
    		}
  	}

  	public static void IsInstShouldWork<T>(object o) 
	{
		Console.WriteLine("cast from " + o.GetType() + " to  " + typeof(T));

		if (!(o is T)) 
		{
			Console.WriteLine("isinst on object of type " + o.GetType() + " to " + typeof(T) + " failed. Expected success");
			pass = false;
		}
  	}

  	public static void CastClassShouldFail<T>(object o)
  	{
		Console.WriteLine("cast from " + o.GetType() + " to  " + typeof(T));

     		try
     		{
	
	     		T obj = (T)o;
	      		Console.WriteLine("cast on object of type " + o.GetType() + " to " + typeof(T) + " succeeded. Expected failure");
			pass = false;
     		}
     		catch (InvalidCastException)
     		{
     			// expecting to get an InvalidCastException
     		}
		catch (Exception e)
     		{
     			Console.WriteLine("Caught unexpected exception: " + e);
     			pass = false;
     		}
  	}


  	public static void CastClassShouldWork<T>(object o)
  	{
		Console.WriteLine("cast from " + o.GetType() + " to  " + typeof(T));

     		try
     		{
	     		T obj = (T)o;
     		}
     		catch (Exception e)
     		{
	 		Console.WriteLine("cast on object of type " + o.GetType() + " to " + typeof(T) + " failed. Expected success");
			Console.WriteLine(e);
			pass = false;
     		}
  	}

   	

  	public static int Main() 
	{
		pass = true;
		
    		C<long> cl = new C<long>();
		C<object> co = new C<object>();
	  	C<IComparable> ci = new C<IComparable>();
   		

	    	// Primitives
	    	C<uint> cui = new C<uint>();
	    	C<intEnum> ciEnum = new C<intEnum>();
	    	C<uintEnum> cuiEnum = new C<uintEnum>();
	    	C<int> cint = new C<int>();
 

		// ==============================
		// TESTS for IsInst
		// ==============================
		Console.WriteLine("\n================================");
		Console.WriteLine("   Positive tests for IsInst:");
		Console.WriteLine("================================\n");

		
		// Exact match on value types

	    	IsInstShouldWork<IPos<int>>(cint);
	    	IsInstShouldWork<IPos<long>>(cl);
	    	IsInstShouldWork<IPos<uint>>(cui);


	    	IsInstShouldWork<INeg<int>>(cint);
	    	IsInstShouldWork<INeg<long>>(cl);
	    	IsInstShouldWork<INeg<uint>>(cui);

		// Runtime type tests
		IsInstShouldWork<IPos<object>>(ci);
	    	IsInstShouldWork<INeg<string>>(ci);
		
		Console.WriteLine("\n================================");
		Console.WriteLine("   Negative tests for IsInst:");
		Console.WriteLine("================================\n");

	    
	    	IsInstShouldFail<IPos<object>>(cl);
	    	IsInstShouldFail<INeg<long>>(ci);	    





		// IPos<unit> --> IPos<int>
	    	IsInstShouldFail<IPos<int>>(cui);

		// IPos<IComparable> --> IPos<uint>
	    	IsInstShouldFail<IPos<uint>>(ci);

		// IPos<intEnum> --> IPos<int>
	    	IsInstShouldFail<IPos<int>>(ciEnum);

		// IPos<IComparable> --> IPos<intEnum>
	    	IsInstShouldFail<IPos<intEnum>>(ci);

		// IPos<uintEnum> --> IPos<uint>
	    	IsInstShouldFail<IPos<uint>>(cuiEnum);
		
		// IPos<uint> --> IPos<uintEnum>
	    	IsInstShouldFail<IPos<uintEnum>>(cui);

		// IPos<intEnum> --> IPos<uint>
	    	IsInstShouldFail<IPos<uint>>(ciEnum);

		// IPos<uint> --> IPos<intEnum>
	    	IsInstShouldFail<IPos<intEnum>>(cui);

		// IPos<int> --> IPos<intEnum>
		IsInstShouldFail<IPos<intEnum>>(cint);

		// IPos<int> --> IPos<uint>
	    	IsInstShouldFail<IPos<uint>>(cint);


		// same for INeg<>
	    	IsInstShouldFail<IPos<int>>(cui);
	    	IsInstShouldFail<IPos<uint>>(ci);
	    	IsInstShouldFail<IPos<int>>(ciEnum);
	    	IsInstShouldFail<IPos<intEnum>>(ci);
	    	IsInstShouldFail<IPos<uint>>(cuiEnum);
	    	IsInstShouldFail<IPos<uintEnum>>(cui);
	    	IsInstShouldFail<IPos<uint>>(ciEnum);
	    	IsInstShouldFail<IPos<intEnum>>(cui);
		IsInstShouldFail<IPos<intEnum>>(cint);
	    	IsInstShouldFail<IPos<uint>>(cint);


		// ==============================
		// TESTS for CastClass
		// ==============================

		Console.WriteLine("\n================================");
		Console.WriteLine("   Positive tests for CastClass:");
		Console.WriteLine("================================\n");

		CastClassShouldWork<IPos<object>>(ci);
	    	CastClassShouldWork<INeg<string>>(ci);


	    	CastClassShouldWork<IPos<int>>(cint);
	    	CastClassShouldWork<IPos<long>>(cl);
	    	CastClassShouldWork<IPos<uint>>(cui);


		Console.WriteLine("\n================================");
		Console.WriteLine("   Negative tests for CastClass:");
		Console.WriteLine("================================\n");

	    	CastClassShouldFail<IPos<int>>(cui);
	    	CastClassShouldFail<IPos<uint>>(ci);
	    	CastClassShouldFail<IPos<int>>(ciEnum);
	    	CastClassShouldFail<IPos<intEnum>>(ci);
	    	CastClassShouldFail<IPos<uint>>(cuiEnum);
	    	CastClassShouldFail<IPos<uintEnum>>(cui);
	    	CastClassShouldFail<IPos<uint>>(ciEnum);
	    	CastClassShouldFail<IPos<intEnum>>(cui);
	    	CastClassShouldFail<IPos<intEnum>>(cint);
	    	CastClassShouldFail<IPos<uint>>(cint);
			
	    	CastClassShouldFail<INeg<int>>(cui);
	    	CastClassShouldFail<INeg<uint>>(ci);
	    	CastClassShouldFail<INeg<int>>(ciEnum);
	    	CastClassShouldFail<INeg<intEnum>>(ci);
	   	CastClassShouldFail<INeg<uint>>(cuiEnum);
	    	CastClassShouldFail<INeg<uintEnum>>(cui);
	   	CastClassShouldFail<INeg<uint>>(ciEnum);
	   	CastClassShouldFail<INeg<intEnum>>(cui);

	    	CastClassShouldFail<INeg<intEnum>>(cint);
	    	CastClassShouldFail<INeg<uint>>(cint);
		

	    	

	    	

		if (pass)
		{
			Console.WriteLine("PASS");
			return 100;
		}
		else
		{
			Console.WriteLine("FAIL");
			return 101;
		}
  	}
}