2009-03-20 Zoltan Varga <vargaz@gmail.com>
[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 public class main {
17     public static void callCallStaticCrash<T> () {
18         Gen<T> gt = new Gen<T> ();
19         gt.callStaticCrash ();
20     }
21
22     public static bool test (Exception exc, Type type) {
23         StackTrace st = new StackTrace (exc);
24         for (int i = 0; i < st.FrameCount; ++i) {
25             StackFrame sf = st.GetFrame (i);
26             MethodBase m = sf.GetMethod ();
27             Type t = m.DeclaringType;
28             if (m.IsGenericMethod) {
29                 Type[] margs = m.GetGenericArguments ();
30                 //Console.WriteLine (m.Name);
31                 if (margs.Length != 1)
32                     return false;
33                 if (margs [0] != type)
34                     return false;
35             }
36             if (t.IsGenericType) {
37                 Type[] targs = t.GetGenericArguments ();
38                 //Console.WriteLine (t.FullName);
39                 if (targs.Length != 1)
40                     return false;
41                 if (targs [0] != type)
42                     return false;
43             }
44         }
45         return true;
46     }
47
48     public static int Main () {
49         try {
50             callCallStaticCrash <int> ();
51         } catch (Exception exc) {
52             if (!test (exc, typeof (int)))
53                 return 1;
54         }
55         try {
56             callCallStaticCrash <object> ();
57         } catch (Exception exc) {
58             if (!test (exc, typeof (object)))
59                 return 1;
60         }
61         try {
62             callCallStaticCrash <string> ();
63         } catch (Exception exc) {
64             if (!test (exc, typeof (string)))
65                 return 1;
66         }
67         try {
68             callCallStaticCrash <Gen<string>> ();
69         } catch (Exception exc) {
70             if (!test (exc, typeof (Gen<string>)))
71                 return 1;
72         }
73         return 0;
74     }
75 }