[runtime] Remove all NACL support. It was unmaintained for a long time. (#4955)
[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                 if (m == null)
42                         continue;
43             Type t = m.DeclaringType;
44             if (m.IsGenericMethod) {
45                 Type[] margs = m.GetGenericArguments ();
46                 //Console.WriteLine (m.Name);
47                 if (margs.Length != 1)
48                     return false;
49                 if (margs [0] != type)
50                     return false;
51             }
52             if (t.IsGenericType) {
53                 Type[] targs = t.GetGenericArguments ();
54                 //Console.WriteLine (t.FullName);
55                 if (targs.Length != 1)
56                     return false;
57                 if (targs [0] != type)
58                     return false;
59             }
60         }
61         return true;
62     }
63
64     public static int Main () {
65         try {
66             callCallStaticCrash <int> ();
67         } catch (Exception exc) {
68             if (!test (exc, typeof (int)))
69                 return 1;
70         }
71         try {
72             callCallStaticCrash <object> ();
73         } catch (Exception exc) {
74             if (!test (exc, typeof (object)))
75                 return 1;
76         }
77         try {
78             callCallStaticCrash <string> ();
79         } catch (Exception exc) {
80             if (!test (exc, typeof (string)))
81                 return 1;
82         }
83         try {
84             callCallStaticCrash <Gen<string>> ();
85         } catch (Exception exc) {
86             if (!test (exc, typeof (Gen<string>)))
87                 return 1;
88         }
89
90         // Exception thrown in inherited method with different generic context
91         // (#509406)
92         try {
93                 new Bar <string> ().Throw ();
94         } catch (Exception ex) {
95                 Console.WriteLine (new StackTrace (ex));
96         }
97
98         try {
99                 new Bar <string> ().Throw<Bar<string>> ();
100         } catch (Exception ex) {
101                 Console.WriteLine (new StackTrace (ex));
102         }
103
104         return 0;
105     }
106 }