2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mono / tests / generic-stack-traces.2.cs
1 using System;
2 using System.Diagnostics;
3 using System.Reflection;
4
5 public class Gen<T> {
6     public static void staticCrash () {
7         object o = null;
8         o.GetType ();
9     }
10
11     public void callStaticCrash () {
12         staticCrash ();
13     }
14 }
15
16 class Foo<T1, T2> {
17
18         public void Throw () {
19                 throw new Exception ();
20         }
21
22         public void Throw<T3> () {
23                 throw new Exception ();
24         }
25 }
26
27 class Bar<T> : Foo<object, T> {
28 }
29
30 public class main {
31     public static void callCallStaticCrash<T> () {
32         Gen<T> gt = new Gen<T> ();
33         gt.callStaticCrash ();
34     }
35
36     public static bool test (Exception exc, Type type) {
37         StackTrace st = new StackTrace (exc);
38         for (int i = 0; i < st.FrameCount; ++i) {
39             StackFrame sf = st.GetFrame (i);
40             MethodBase m = sf.GetMethod ();
41             Type t = m.DeclaringType;
42             if (m.IsGenericMethod) {
43                 Type[] margs = m.GetGenericArguments ();
44                 //Console.WriteLine (m.Name);
45                 if (margs.Length != 1)
46                     return false;
47                 if (margs [0] != type)
48                     return false;
49             }
50             if (t.IsGenericType) {
51                 Type[] targs = t.GetGenericArguments ();
52                 //Console.WriteLine (t.FullName);
53                 if (targs.Length != 1)
54                     return false;
55                 if (targs [0] != type)
56                     return false;
57             }
58         }
59         return true;
60     }
61
62     public static int Main () {
63         try {
64             callCallStaticCrash <int> ();
65         } catch (Exception exc) {
66             if (!test (exc, typeof (int)))
67                 return 1;
68         }
69         try {
70             callCallStaticCrash <object> ();
71         } catch (Exception exc) {
72             if (!test (exc, typeof (object)))
73                 return 1;
74         }
75         try {
76             callCallStaticCrash <string> ();
77         } catch (Exception exc) {
78             if (!test (exc, typeof (string)))
79                 return 1;
80         }
81         try {
82             callCallStaticCrash <Gen<string>> ();
83         } catch (Exception exc) {
84             if (!test (exc, typeof (Gen<string>)))
85                 return 1;
86         }
87
88         // Exception thrown in inherited method with different generic context
89         // (#509406)
90         try {
91                 new Bar <string> ().Throw ();
92         } catch (Exception ex) {
93                 Console.WriteLine (new StackTrace (ex));
94         }
95
96         try {
97                 new Bar <string> ().Throw<Bar<string>> ();
98         } catch (Exception ex) {
99                 Console.WriteLine (new StackTrace (ex));
100         }
101
102         return 0;
103     }
104 }