2010-04-15 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Diagnostics / StackTraceTest.cs
blob14af812b27783f2c0b7486d48aab0fbb125054d5
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)
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.
31 using System;
32 using System.Diagnostics;
33 using System.Reflection;
34 using System.Threading;
35 using NUnit.Framework;
37 namespace MonoTests.System.Diagnostics {
39 [TestFixture]
40 public class StackTraceTest {
42 private StackTrace trace;
43 private StackFrame frame;
45 [SetUp]
46 public void SetUp ()
48 frame = new StackFrame ("dir/someFile", 13, 45);
49 trace = new StackTrace (frame);
52 [TearDown]
53 public void TearDown ()
55 trace = null;
58 [Test]
59 [ExpectedException (typeof (ArgumentOutOfRangeException))]
60 public void StackTrace_Int_Negative ()
62 new StackTrace (-1);
65 [Test]
66 [ExpectedException (typeof (ArgumentNullException))]
67 public void StackTrace_Exception_Null ()
69 Exception e = null;
70 new StackTrace (e);
73 [Test]
74 [ExpectedException (typeof (ArgumentNullException))]
75 public void StackTrace_ExceptionBool_Null ()
77 Exception e = null;
78 new StackTrace (e, true);
81 [Test]
82 [ExpectedException (typeof (ArgumentNullException))]
83 public void StackTrace_ExceptionInt_Null ()
85 Exception e = null;
86 new StackTrace (e, 1);
89 [Test]
90 [ExpectedException (typeof (ArgumentOutOfRangeException))]
91 public void StackTrace_ExceptionInt_Negative ()
93 new StackTrace (new Exception (), -1);
96 [Test]
97 [ExpectedException (typeof (ArgumentNullException))]
98 public void StackTrace_ExceptionIntBool_Null ()
100 Exception e = null;
101 new StackTrace (e, 1, true);
104 [Test]
105 [ExpectedException (typeof (ArgumentOutOfRangeException))]
106 public void StackTrace_ExceptionIntBool_Negative ()
108 new StackTrace (new Exception (), -1, true);
111 [Test]
112 public void StackTrace_StackFrame_Null ()
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");
121 [Test]
122 [Ignore ("Not supported in Mono")]
123 public void StackTrace_Thread_Null ()
125 Thread t = null;
126 StackTrace st = new StackTrace (t, true);
127 // no exception
130 static void EmptyThread ()
132 Thread.Sleep (1000);
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 ()
144 Thread t = new Thread (new ThreadStart (EmptyThread));
145 t.Start ();
146 new StackTrace (t, true);
149 [Test]
150 [Ignore ("Not supported in Mono")]
151 public void StackTrace_Thread_Suspended ()
153 Thread t = new Thread (new ThreadStart (EmptyThread));
154 t.Start ();
155 t.Suspend ();
156 new StackTrace (t, true);
159 [Test]
160 public void FrameCount ()
162 Assert.AreEqual (1, trace.FrameCount, "Frame count");
165 [Test]
166 public void GetFrame_OutOfRange ()
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");
173 Assert.IsNull (trace.GetFrame (Int32.MinValue), "MinValue");
174 Assert.IsNull (trace.GetFrame (Int32.MaxValue), "MaxValue");
177 [Test]
178 public void GetFrame ()
180 Assert.AreEqual (frame, trace.GetFrame (0), "0");
182 #if NET_2_0
183 [Test]
184 public void GetFrames ()
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 ());
193 #endif
194 [Test]
195 public void UnthrownException ()
197 Assert.AreEqual (0, new StackTrace (new Exception ()).FrameCount, "Unthrown exception");