X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Ftests%2Fgenerics-sharing.2.cs;h=6110cf2f8fad627ea0ccfb11c59fb75767efc348;hb=45d6da52ce69cbd24f5930e1cad88d425e706186;hp=19faeee1b2327a86c9711b8f99af80b2244882fa;hpb=89866fadb0772bc092d7c77a188053eae5b4be4a;p=mono.git diff --git a/mono/tests/generics-sharing.2.cs b/mono/tests/generics-sharing.2.cs index 19faeee1b23..6110cf2f8fa 100644 --- a/mono/tests/generics-sharing.2.cs +++ b/mono/tests/generics-sharing.2.cs @@ -1,18 +1,104 @@ using System; using System.Collections.Generic; +using System.Reflection; + +namespace GenericSharingTest { + +public delegate int IntVoidDelegate (); public class ClassA {} public class ClassB {} public class ClassC {} +public class GenExc : Exception {} + public class NonGen { public static int field = 123; + + public static void doThrow () { + throw new GenExc (); + } +} + +public class GenBi { + public static int field = 123; + public static float floatField = 1.0f; + + public static int staticMethod (int x) { + return x + field; + } + + public static void staticVoidMethod (int x) { + field = x; + } + + public static float staticFloatMethod () { + return floatField; + } + + public static long staticLongMethod (long x) { + return x + field; + } + + public static GenStruct staticValueMethod (int x) { + return new GenStruct (x); + } +} + +public struct GenStruct { + public static int staticField; + + public int field; + public int dummy1; + public int dummy2; + public int dummy3; + + public GenStruct (int f) { + field = f; + dummy1 = dummy2 = dummy3 = 0; + } + + public int method (int x) { + return x + field; + } +} + +public interface IGen { + T[] iMethod (); + void voidIMethod (int x); + long longIMethod (long x); + float floatIMethod (); + GenStruct valueIMethod (int x); +} + +public class IGenImpl : IGen { + public int field; + + public T[] iMethod () { + return new T[3]; + } + + public void voidIMethod (int x) { + field = x; + } + + public long longIMethod (long x) { + return x + 1; + } + + public float floatIMethod () { + return 1.0f; + } + + public GenStruct valueIMethod (int x) { + return new GenStruct (x); + } } public class GenA { public static T[] arr; - public static GenA () { + static GenA () { arr = new T [3]; } @@ -34,6 +120,10 @@ public class GenA { return NonGen.field; } + public int getGenStructStaticField () { + return GenStruct.staticField; + } + public T[] getArr () { return arr; } @@ -66,6 +156,130 @@ public class GenA { public T cast (Object obj) { return (T)obj; } + + public GenStruct structCast (Object obj) { + return (GenStruct)obj; + } + + public Type ldtokenT () { + return typeof (T); + } + + public Type ldtokenIGenT () { + return typeof (IGen); + } + + public Type ldtokenGenAIGenT () { + return typeof (GenA>); + } + + public Type ldtokenGenB () { + return typeof (GenB<>); + } + + public GenStruct? makeNullable (Object obj) { + return (GenStruct?)obj; + } + + public object unmakeNullable (GenStruct? obj) { + return (object)obj; + } + + public void except () { + try { + NonGen.doThrow (); + } + catch (GenExc) { + //Console.WriteLine("exception thrown"); + } + } + + public static void staticExcept () { + try { + NonGen.doThrow (); + } + catch (GenExc) { + Console.WriteLine("exception thrown and caught"); + } + } + + public static int staticField = 54321; + + public static int staticMethod () { + return staticField; + } + + public static int staticMethodCaller () { + return staticMethod (); + } + + public static float staticFloatField = 1.0f; + + public static float staticFloatMethod () { + return staticFloatField; + } + + public static int staticBiCaller (int x) { + return GenBi.staticMethod (x); + } + + public static void staticBiVoidCaller (int x) { + GenBi.staticVoidMethod (x); + } + + public static float staticBiFloatCaller () { + return GenBi.staticFloatMethod (); + } + + public static GenStruct staticBiValueCaller (int x) { + return GenBi.staticValueMethod (x); + } + + public static int staticSharedBiCaller (int x) { + return GenBi.staticMethod (x); + } + + public static void staticSharedBiVoidCaller (int x) { + GenBi.staticVoidMethod (x); + } + + public static float staticSharedBiFloatCaller () { + return GenBi.staticFloatMethod (); + } + + public static GenStruct staticSharedBiValueCaller (int x) { + return GenBi.staticValueMethod (x); + } + + public static long staticBiLongCaller (long x) { + return GenBi.staticLongMethod (x); + } + + public int structCaller (int x) { + GenStruct> gs = new GenStruct> (123); + + return gs.method (x); + } + + public T[] callInterface (IGen ig) { + return ig.iMethod (); + } + + public void callVoidInterface (IGen ig, int x) { + ig.voidIMethod (x); + } + + public long callLongInterface (IGen ig, long x) { + return ig.longIMethod (x); + } + + public float callFloatInterface (IGen ig) { + return ig.floatIMethod (); + } + + public GenStruct callValueInterface (IGen ig, int x) { + return ig.valueIMethod (x); + } } public class GenB { @@ -75,7 +289,7 @@ public class GenB { public class GenC { public static int field ; - public static GenC () { + static GenC () { field = 1234; } } @@ -112,7 +326,7 @@ public class GenABDeriv : GenA> { public class NonGenUser where T : NonGen { public int getNonGenField () { - return T.field; + return NonGen.field; } } @@ -201,19 +415,37 @@ public class RGCTXTestSubASubSub : RGCTXTestSubASub { } public class main { + delegate void ActionDelegate (); + static bool haveError = false; - public static void error (string message) { + static void error (string message) { haveError = true; Console.WriteLine (message); } - public static void typeCheck (String method, Object obj, Type t) { + static void typeCheck (String method, Object obj, Type t) { if (obj.GetType () != t) error ("object from " + method + " should have type " + t.ToString () + " but has type " + obj.GetType ().ToString ()); } - public static void work (T obj) { + static int callStaticMethod () { + return GenA.staticMethod (); + } + + static void checkException (String method, ActionDelegate action) where T : Exception { + try { + try { + action (); + } catch (T) { + return; + } + } catch (Exception exc) { + error ("method " + method + " should have thrown " + typeof (T).ToString () + " but did throw " + exc); + } + } + + public static void work (T obj, bool mustCatch) { EqualityComparer comp = EqualityComparer.Default; GenA ga = new GenA (); @@ -232,6 +464,11 @@ public class main { if (ga.getNonGenField () != 123) error ("getNonGenField"); + GenStruct.staticField = 321; + if (ga.getGenStructStaticField () != 321) + error ("getGenStructStaticField"); + GenStruct.staticField = -1; + ga.hash (obj); if (!comp.Equals (ga.ident (obj), obj)) @@ -239,9 +476,103 @@ public class main { if (!comp.Equals (ga.cast (obj), obj)) error ("cast"); + if (typeof (T).IsValueType) { + checkException ("cast null value", delegate { ga.cast (null); }); + } else { + if (ga.cast (null) != null) + error ("cast null"); + } + + GenStruct genstructt = new GenStruct (453); + if (ga.structCast ((object)genstructt).field != 453) + error ("structCast"); + checkException ("structCast null", delegate { ga.structCast (null); }); + + if (ga.makeNullable ((object)genstructt).Value.field != 453) + error ("makeNullable"); + if (ga.makeNullable (null) != null) + error ("makeNullable null"); + + if (ga.ldtokenT () != typeof (T)) + error ("ldtokenT"); + if (ga.ldtokenIGenT () != typeof (IGen)) + error ("ldtokenIGenT"); + if (ga.ldtokenGenAIGenT () != typeof (GenA>)) + error ("ldtokenGenAIGenT"); + if (ga.ldtokenGenB () != typeof (GenB<>)) + error ("ldtokenGenB"); + + if (callStaticMethod () != 54321) + error ("staticMethod"); + + GenBi.field = 123; + if (GenA.staticBiCaller (123) != 246) + error ("staticBiCaller"); + GenA.staticBiVoidCaller (1234); + if (GenBi.field != 1234) + error ("staticBiVoidCaller"); + if (GenA.staticBiFloatCaller () != 1.0f) + error ("staticBiFloatCaller"); + if (GenA.staticBiLongCaller (123) != 123 + 1234) + error ("staticBiLongCaller"); + GenStruct gs = GenA.staticBiValueCaller (987); + if (gs.field != 987) + error ("staticBiValueCaller"); + + GenBi.field = 123; + if (GenA.staticSharedBiCaller (123) != 246) + error ("staticSharedBiCaller"); + GenA.staticSharedBiVoidCaller (1234); + if (GenBi.field != 1234) + error ("staticSharedBiVoidCaller"); + if (GenA.staticSharedBiFloatCaller () != 1.0f) + error ("staticSharedBiFloatCaller"); + GenStruct gss = GenA.staticSharedBiValueCaller (987); + if (gss.field != 987) + error ("staticSharedBiValueCaller"); + + IntVoidDelegate ivdel = new IntVoidDelegate (GenA.staticMethod); + if (ivdel () != 54321) + error ("staticMethod delegate"); + + Type gatype = typeof (GenA); + MethodInfo staticMethodInfo = gatype.GetMethod ("staticMethod"); + if ((Convert.ToInt32 (staticMethodInfo.Invoke (null, null))) != 54321) + error ("staticMethod reflection"); + + if (GenA.staticMethodCaller () != 54321) + error ("staticMethodCaller"); + + if (GenA.staticFloatMethod () != 1.0) + error ("staticFloatMethod"); + + if (ga.structCaller (234) != 357) + error ("structCaller"); + + IGenImpl igi = new IGenImpl (); + + typeCheck ("callInterface", ga.callInterface (igi), typeof (T[])); + if (ga.callLongInterface (igi, 345) != 346) + error ("callLongInterface"); + GenStruct gst = ga.callValueInterface (igi, 543); + if (gst.field != 543) + error ("callValueInterface"); + ga.callVoidInterface (igi, 654); + if (igi.field != 654) + error ("callVoidInterface"); + if (ga.callFloatInterface (igi) != 1.0f) + error ("callFloatInterface"); new GenADeriv (); + if (mustCatch) { + checkException> ("except", delegate { ga.except (); }); + checkException> ("staticExcept", delegate { GenA.staticExcept (); }); + } else { + ga.except (); + GenA.staticExcept (); + } + MyDict dtb = new MyDict (obj, new ClassB ()); typeCheck ("MyPair", dtb.p, typeof (MyPair)); @@ -299,12 +630,16 @@ public class main { public static int Main () { - work (new ClassA ()); - work (new ClassB ()); - work (new ClassC ()); - work> (new GenA ()); - work (new int[3]); - work (123); + work (new ClassA (), false); + work (new ClassB (), true); + work (new ClassB (), true); + work (new ClassC (), true); + work> (new GenA (), true); + work (new int[3], true); + work (123, true); + work (123, true); + work?> (new GenStruct (123), true); + work?> (null, true); StaticTest sa = new StaticTest (1234); StaticTest sb = new StaticTest (2345); @@ -343,3 +678,5 @@ public class main { return 0; } } + +}