Switch to compiler-tester
[mono.git] / mcs / class / corlib / Test / System.Diagnostics / StackTraceTest.cs
1 //
2 // MonoTests.System.Diagnostics.StackTraceTest.cs
3 //
4 // Authors:
5 //      Alexander Klyubin (klyubin@aqris.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2001
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Diagnostics;
33 using System.Reflection;
34 using System.Threading;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Diagnostics {
38
39         [TestFixture]
40         public class StackTraceTest {
41
42                 private StackTrace trace;
43                 private StackFrame frame;
44                 
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         frame = new StackFrame ("dir/someFile", 13, 45);
49                         trace = new StackTrace (frame);
50                 }
51
52                 [TearDown]
53                 public void TearDown ()
54                 {
55                         trace = null;
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
60                 public void StackTrace_Int_Negative ()
61                 {
62                         new StackTrace (-1);
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (ArgumentNullException))]
67                 public void StackTrace_Exception_Null ()
68                 {
69                         Exception e = null;
70                         new StackTrace (e);
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentNullException))]
75                 public void StackTrace_ExceptionBool_Null ()
76                 {
77                         Exception e = null;
78                         new StackTrace (e, true);
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (ArgumentNullException))]
83                 public void StackTrace_ExceptionInt_Null ()
84                 {
85                         Exception e = null;
86                         new StackTrace (e, 1);
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
91                 public void StackTrace_ExceptionInt_Negative ()
92                 {
93                         new StackTrace (new Exception (), -1);
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentNullException))]
98                 public void StackTrace_ExceptionIntBool_Null ()
99                 {
100                         Exception e = null;
101                         new StackTrace (e, 1, true);
102                 }
103
104                 [Test]
105                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
106                 public void StackTrace_ExceptionIntBool_Negative ()
107                 {
108                         new StackTrace (new Exception (), -1, true);
109                 }
110
111                 [Test]
112                 public void StackTrace_StackFrame_Null ()
113                 {
114                         StackFrame sf = null;
115                         StackTrace st = new StackTrace (sf);
116                         // no exception
117                         Assert.AreEqual (1, st.FrameCount, "FrameCount");
118                         Assert.IsNull (st.GetFrame (0), "Empty Frame");
119                 }
120
121                 [Test]
122                 [Ignore ("Not supported in Mono")]
123                 public void StackTrace_Thread_Null ()
124                 {
125                         Thread t = null;
126                         StackTrace st = new StackTrace (t, true);
127                         // no exception
128                 }
129
130                 static void EmptyThread ()
131                 {
132                         Thread.Sleep (1000);
133                 }
134
135                 [Test]
136 #if !NET_2_0
137                 // on MS .NET 1.x, ThreadState after Start() is Unstarted
138                 [Category ("NotDotNet")]
139 #endif
140                 [ExpectedException (typeof (ThreadStateException))]
141                 [Ignore ("Not supported in Mono")]
142                 public void StackTrace_Thread_NotSuspended ()
143                 {
144                         Thread t = new Thread (new ThreadStart (EmptyThread));
145                         t.Start ();
146                         new StackTrace (t, true);
147                 }
148
149                 [Test]
150                 [Ignore ("Not supported in Mono")]
151                 public void StackTrace_Thread_Suspended ()
152                 {
153                         Thread t = new Thread (new ThreadStart (EmptyThread));
154                         t.Start ();
155                         t.Suspend ();
156                         new StackTrace (t, true);
157                 }
158
159                 [Test]
160                 public void FrameCount ()
161                 {
162                         Assert.AreEqual (1, trace.FrameCount, "Frame count");
163                 }
164
165                 [Test]
166                 public void GetFrame_OutOfRange ()
167                 {
168                         Assert.IsNull (trace.GetFrame (-1), "-1");
169                         Assert.IsNull (trace.GetFrame (-129), "-129");
170                         Assert.IsNull (trace.GetFrame (1), "1");
171                         Assert.IsNull (trace.GetFrame (145), "145");
172
173                         Assert.IsNull (trace.GetFrame (Int32.MinValue), "MinValue");
174                         Assert.IsNull (trace.GetFrame (Int32.MaxValue), "MaxValue");
175                 }
176
177                 [Test]
178                 public void GetFrame ()
179                 {
180                         Assert.AreEqual (frame, trace.GetFrame (0), "0");
181                 }
182 #if NET_2_0
183                 [Test]
184                 public void GetFrames ()
185                 {
186                         StackTrace st = new StackTrace ();
187                         StackFrame[] sf = st.GetFrames ();
188                         Assert.AreEqual (st.FrameCount, sf.Length, "Count");
189                         for (int i=0; i < sf.Length; i++) {
190                                 Assert.AreEqual (sf [i], st.GetFrame (i), i.ToString ());
191                         }
192                 }
193 #endif
194                 [Test]
195                 public void UnthrownException ()
196                 {
197                         Assert.AreEqual (0, new StackTrace (new Exception ()).FrameCount, "Unthrown exception");
198                 }
199         }
200 }