f2665988d522a91068e6ee20178ed89ea405fc15
[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                 [ExpectedException (typeof (ThreadStateException))]
137                 [Ignore ("Not supported in Mono")]
138                 public void StackTrace_Thread_NotSuspended ()
139                 {
140                         Thread t = new Thread (new ThreadStart (EmptyThread));
141                         t.Start ();
142                         new StackTrace (t, true);
143                 }
144
145                 [Test]
146                 [Ignore ("Not supported in Mono")]
147                 public void StackTrace_Thread_Suspended ()
148                 {
149                         Thread t = new Thread (new ThreadStart (EmptyThread));
150                         t.Start ();
151                         t.Suspend ();
152                         new StackTrace (t, true);
153                 }
154
155                 [Test]
156                 public void FrameCount ()
157                 {
158                         Assert.AreEqual (1, trace.FrameCount, "Frame count");
159                 }
160
161                 [Test]
162                 public void GetFrame_OutOfRange ()
163                 {
164                         Assert.IsNull (trace.GetFrame (-1), "-1");
165                         Assert.IsNull (trace.GetFrame (-129), "-129");
166                         Assert.IsNull (trace.GetFrame (1), "1");
167                         Assert.IsNull (trace.GetFrame (145), "145");
168
169                         Assert.IsNull (trace.GetFrame (Int32.MinValue), "MinValue");
170                         Assert.IsNull (trace.GetFrame (Int32.MaxValue), "MaxValue");
171                 }
172
173                 [Test]
174                 public void GetFrame ()
175                 {
176                         Assert.AreEqual (frame, trace.GetFrame (0), "0");
177                 }
178                 [Test]
179                 public void GetFrames ()
180                 {
181                         StackTrace st = new StackTrace ();
182                         StackFrame[] sf = st.GetFrames ();
183                         Assert.AreEqual (st.FrameCount, sf.Length, "Count");
184                         for (int i=0; i < sf.Length; i++) {
185                                 Assert.AreEqual (sf [i], st.GetFrame (i), i.ToString ());
186                         }
187                 }
188                 [Test]
189                 public void UnthrownException ()
190                 {
191                         Assert.AreEqual (0, new StackTrace (new Exception ()).FrameCount, "Unthrown exception");
192                 }
193         }
194 }