// 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 is castable to IPos (exact match on value types) Negative " IPos is not castable to IPos IPos is not castable to IPos */ using System; public class C : IPos, INeg { 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(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(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(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(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 cl = new C(); C co = new C(); C ci = new C(); // Primitives C cui = new C(); C ciEnum = new C(); C cuiEnum = new C(); C cint = new C(); // ============================== // TESTS for IsInst // ============================== Console.WriteLine("\n================================"); Console.WriteLine(" Positive tests for IsInst:"); Console.WriteLine("================================\n"); // Exact match on value types IsInstShouldWork>(cint); IsInstShouldWork>(cl); IsInstShouldWork>(cui); IsInstShouldWork>(cint); IsInstShouldWork>(cl); IsInstShouldWork>(cui); // Runtime type tests IsInstShouldWork>(ci); IsInstShouldWork>(ci); Console.WriteLine("\n================================"); Console.WriteLine(" Negative tests for IsInst:"); Console.WriteLine("================================\n"); IsInstShouldFail>(cl); IsInstShouldFail>(ci); // IPos --> IPos IsInstShouldFail>(cui); // IPos --> IPos IsInstShouldFail>(ci); // IPos --> IPos IsInstShouldFail>(ciEnum); // IPos --> IPos IsInstShouldFail>(ci); // IPos --> IPos IsInstShouldFail>(cuiEnum); // IPos --> IPos IsInstShouldFail>(cui); // IPos --> IPos IsInstShouldFail>(ciEnum); // IPos --> IPos IsInstShouldFail>(cui); // IPos --> IPos IsInstShouldFail>(cint); // IPos --> IPos IsInstShouldFail>(cint); // same for INeg<> IsInstShouldFail>(cui); IsInstShouldFail>(ci); IsInstShouldFail>(ciEnum); IsInstShouldFail>(ci); IsInstShouldFail>(cuiEnum); IsInstShouldFail>(cui); IsInstShouldFail>(ciEnum); IsInstShouldFail>(cui); IsInstShouldFail>(cint); IsInstShouldFail>(cint); // ============================== // TESTS for CastClass // ============================== Console.WriteLine("\n================================"); Console.WriteLine(" Positive tests for CastClass:"); Console.WriteLine("================================\n"); CastClassShouldWork>(ci); CastClassShouldWork>(ci); CastClassShouldWork>(cint); CastClassShouldWork>(cl); CastClassShouldWork>(cui); Console.WriteLine("\n================================"); Console.WriteLine(" Negative tests for CastClass:"); Console.WriteLine("================================\n"); CastClassShouldFail>(cui); CastClassShouldFail>(ci); CastClassShouldFail>(ciEnum); CastClassShouldFail>(ci); CastClassShouldFail>(cuiEnum); CastClassShouldFail>(cui); CastClassShouldFail>(ciEnum); CastClassShouldFail>(cui); CastClassShouldFail>(cint); CastClassShouldFail>(cint); CastClassShouldFail>(cui); CastClassShouldFail>(ci); CastClassShouldFail>(ciEnum); CastClassShouldFail>(ci); CastClassShouldFail>(cuiEnum); CastClassShouldFail>(cui); CastClassShouldFail>(ciEnum); CastClassShouldFail>(cui); CastClassShouldFail>(cint); CastClassShouldFail>(cint); if (pass) { Console.WriteLine("PASS"); return 100; } else { Console.WriteLine("FAIL"); return 101; } } }