* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System / Test / System.Collections.Generic / StackTest.cs
1 //
2 // StackTest.cs
3 //
4 // Author:
5 //  Ben Maurer (bmaurer@ximian.com)
6 //
7
8 #if NET_2_0
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Collections.Generic {
16         [TestFixture]
17         public class StackTest: Assertion
18         {
19                 [Test]
20                 public void TestCtor ()
21                 {
22                         Stack <int> a = new Stack <int> ();
23                         Stack <int> b = new Stack <int> (1);
24                         Stack <object> c = new Stack <object> ();
25                         Stack <object> d = new Stack <object> (1);
26                         Stack <object> e = new Stack <object> (0);
27                 }
28                 
29                 [Test]
30                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
31                 public void TestCtorEx ()
32                 {
33                         Stack <int> a = new Stack <int> (-1);
34                 }
35                 
36                 [Test]
37                 public void TestCtorEnum ()
38                 {
39                         List <int> l = new List <int> ();
40                         l.Add (1);
41                         l.Add (2);
42                         l.Add (3);
43                         
44                         Stack <int> s = new Stack <int> (l);
45                         
46                         // Things get pop'd in reverse
47                         AssertPop (s, 3);
48                         AssertPop (s, 2);
49                         AssertPop (s, 1);
50                 }
51                 
52                 [Test]
53                 [ExpectedException (typeof (ArgumentNullException))]
54                 public void TestCtorEnumNull ()
55                 {
56                         Stack <int> s = new Stack <int> (null);
57                 }
58                 
59                 [Test]
60                 public void TestClear()
61                 {
62                         Stack <int> s = new Stack <int> ();
63                         s.Clear ();
64                         
65                         AssertEquals (s.Count, 0);
66                         
67                         s.Push (1);
68                         s.Push (2);
69                         
70                         AssertEquals (s.Count, 2);
71                         
72                         s.Clear ();
73                         
74                         AssertEquals (s.Count, 0);
75                 }
76                 
77                 [Test]
78                 public void TestContains ()
79                 {
80                         Stack <int> s = new Stack <int> ();
81                         
82                         AssertEquals (s.Contains (1), false);
83                         
84                         s.Push (1);
85                         
86                         AssertEquals (s.Contains (1), true);
87                         AssertEquals (s.Contains (0), false);
88                 }
89                 
90                 [Test]
91                 public void TestCopyTo ()
92                 {
93                         int [] x = new int [3];
94                         Stack <int> z = new Stack <int> ();
95                         z.Push (1);
96                         z.Push (2);
97                         x [0] = 10;
98                         z.CopyTo (x, 1);
99                         
100                         AssertEquals (x [0], 10);
101                         AssertEquals (x [1], 2);
102                         AssertEquals (x [2], 1);
103                 }
104                 
105                 [Test]
106                 public void TestPeek ()
107                 {
108                         Stack <int> s = new Stack <int> ();
109                         s.Push (1);
110                         
111                         AssertEquals (s.Peek (), 1);
112                         AssertEquals (s.Count, 1);
113                 }
114                 
115                 [Test]
116                 [ExpectedException (typeof (InvalidOperationException))]
117                 public void TestPeekEx ()
118                 {
119                         Stack <int> s = new Stack <int> ();
120                         s.Peek ();
121                 }
122                 
123                 [Test]
124                 [ExpectedException (typeof (InvalidOperationException))]
125                 public void TestPeekEx2 ()
126                 {
127                         Stack <int> s = new Stack <int> ();
128                         s.Push (1);
129                         s.Pop ();                       
130                         s.Peek ();
131                 }
132                 
133                 [Test]
134                 public void TestPop ()
135                 {
136                         Stack <int> s = new Stack <int> ();
137                         s.Push (1);
138                         
139                         AssertEquals (s.Pop (), 1);
140                         AssertEquals (s.Count, 0);
141                 }
142                 
143                 [Test]
144                 [ExpectedException (typeof (InvalidOperationException))]
145                 public void TestPopEx ()
146                 {
147                         Stack <int> s = new Stack <int> ();
148                         s.Pop ();
149                 }
150                 
151                 [Test]
152                 [ExpectedException (typeof (InvalidOperationException))]
153                 public void TestPopEx2 ()
154                 {
155                         Stack <int> s = new Stack <int> ();
156                         s.Push (1);
157                         s.Pop ();                       
158                         s.Pop ();
159                 }
160                 
161                 [Test]
162                 public void TestPush ()
163                 {
164                         Stack <int> s = new Stack <int> ();
165                         s.Push (1);
166                         AssertEquals (s.Count, 1);
167                         s.Push (2);
168                         AssertEquals (s.Count, 2);
169                         
170                         for (int i = 0; i < 100; i ++)
171                                 s.Push (i);
172                         
173                         AssertEquals (s.Count, 102);
174                 }
175                 
176                 [Test]
177                 public void TestToArray ()
178                 {
179                         Stack <int> s = new Stack <int> ();
180                         
181                         int [] x = s.ToArray ();
182                         
183                         AssertEquals (x.Length, 0);
184                         
185                         s.Push (1);
186                         x = s.ToArray ();
187                         AssertEquals (x.Length, 1);
188                         AssertEquals (x [0], 1);
189                 }
190                 
191                 [Test]
192                 public void TestEnumerator ()
193                 {
194                         Stack <int> s = new Stack <int> ();
195                         
196                         foreach (int x in s)
197                                 Fail ();
198                         
199                         s.Push (1);
200                         
201                         int i = 0;
202                         
203                         foreach (int x in s) {
204                                 AssertEquals (i, 0);
205                                 AssertEquals (x, 1);
206                                 i ++;
207                         }
208                         
209                         i = 0;
210                         
211                         s.Push (2);
212                         s.Push (3);
213                         
214                         foreach (int x in s) {
215                                 AssertEquals (x, 3 - i);
216                                 Assert (i < 3);
217                                 i ++;
218                         }
219                 }
220                 
221                 void AssertPop <T> (Stack <T> s, T t)
222                 {
223                         AssertEquals (s.Pop (), t);
224                 }
225         }
226 }
227 #endif