Merge pull request #1390 from woodsb02/FreeBSDMacNetworkInterfaces
[mono.git] / mcs / class / corlib / Test / System.Collections / StackTest.cs
1 //
2 // StackTest.cs
3 //
4 // Author:
5 //  Chris Hynes <chrish@assistedsolutions.com>
6 //
7 // (C) 2001 Chris Hynes
8 //
9
10 using System;
11
12 using System.Collections;
13
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Collections
17 {
18         [TestFixture]
19         public class StackTest
20         {
21                 private Stack stack1;
22                 private Stack stackInt;
23
24                 [Test]
25                 public void TestConstructor()
26                 {
27                         Assert.AreEqual (stack1 == null, false);
28                 }
29                 
30                 [Test]
31                 public void TestICollectionConstructor1()
32                 {
33                         Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
34
35                         for (int i = 4; i >= 0; i--)
36                                 Assert.AreEqual (stackTest.Pop(), i);
37
38                         Assert.AreEqual (stackTest.Count, 0);
39                 }
40
41                 [Test]
42                 public void TestICollectionConstructor2()
43                 {
44                         bool exceptionThrown = false;
45                         try {
46                                 Stack stackTest = new Stack(null);
47                         } catch (ArgumentNullException e) {
48                                 exceptionThrown = true;
49                                 Assert.AreEqual ("col", e.ParamName, "ParamName must be \"col\"");
50                         }
51                         Assert.IsTrue (exceptionThrown, "null argument must throw ArgumentNullException");
52                                         
53                 }
54
55                 [Test]
56                 public void TestIntConstructor1()
57                 {
58                         Stack stackTest = new Stack(50);
59
60                         Assert.IsTrue (stackTest != null);
61                 }
62
63                 [Test]
64                 public void TestIntConstructor2()
65                 {
66                         bool exceptionThrown = false;
67                         try {
68                                 Stack stackTest = new Stack(-1);
69                         } 
70                         catch (ArgumentOutOfRangeException e) 
71                         {
72                                 exceptionThrown = true;
73                                 Assert.AreEqual ("initialCapacity", e.ParamName, "ParamName must be \"initialCapacity\"");
74                         }
75                         Assert.IsTrue (exceptionThrown, "negative argument must throw ArgumentOutOfRangeException");
76                 }
77
78                 [Test]
79                 public void TestCount()
80                 {
81                         Stack stackTest = new Stack();
82
83                         stackTest.Push(50);
84                         stackTest.Push(5);
85                         stackTest.Push(0);
86                         stackTest.Push(50);
87
88                         Assert.AreEqual (stackTest.Count, 4);
89                 }
90
91                 [Test]
92                 public void TestIsSyncronized()
93                 {
94                         Assert.AreEqual (stack1.IsSynchronized, false);
95                         Assert.AreEqual (Stack.Synchronized(stack1).IsSynchronized, true);
96                 }
97
98                 [Test]
99                 public void TestSyncRoot()
100                 {
101                         Assert.AreEqual (stack1.SyncRoot == null, false);
102                 }
103
104                 [Test]
105                 public void TestGetEnumerator1()
106                 {
107                         stackInt.Pop();
108
109                         int j = 3;
110
111                         foreach (int i in stackInt)
112                         {
113                                 Assert.AreEqual (i, j--);
114                         }
115
116                         stackInt.Clear();
117
118                         IEnumerator e = stackInt.GetEnumerator();
119
120                         Assert.AreEqual (e.MoveNext(), false);
121                 }
122
123                 [Test]
124                 public void TestGetEnumerator2()
125                 {
126                         
127                         IEnumerator e = stackInt.GetEnumerator();
128                         try 
129                         {
130                                 // Tests InvalidOperationException if enumerator is uninitialized
131                                 Object o = e.Current;
132                                 Assert.Fail ("InvalidOperationException should be thrown");
133                         } catch (InvalidOperationException) {}
134                 }
135
136                 [Test]
137                 public void TestGetEnumerator3()
138                 {
139                         
140                         IEnumerator e = stack1.GetEnumerator();
141                         e.MoveNext();
142                         try 
143                         {
144                                 // Tests InvalidOperationException if enumeration has ended
145                                 Object o = e.Current;
146                                 Assert.Fail ("InvalidOperationException should be thrown");
147                         } catch (InvalidOperationException) {}
148                 }
149         
150                 [Test]
151                 public void TestEnumeratorReset1() 
152                 {
153                         IEnumerator e = stackInt.GetEnumerator();
154
155                         e.MoveNext();
156                         Assert.AreEqual (4, e.Current, "current value");
157                         e.MoveNext();
158
159                         e.Reset();
160
161                         e.MoveNext();
162                         Assert.AreEqual (4, e.Current, "current value after reset");
163                 }
164
165                 [Test]
166                 public void TestEnumeratorReset2() 
167                 {
168                         IEnumerator e = stackInt.GetEnumerator();
169
170                         e.MoveNext();
171                         Assert.AreEqual (4, e.Current, "current value");
172
173                         // modifies underlying the stack. Reset must throw InvalidOperationException
174                         stackInt.Push(5);
175                         
176                         try 
177                         {
178                                 e.Reset();
179                                 Assert.Fail ("InvalidOperationException should be thrown");
180                         } 
181                         catch (InvalidOperationException) {}
182                 }
183
184                 [Test]
185                 public void TestEnumeratorMoveNextException() 
186                 {
187                         IEnumerator e = stackInt.GetEnumerator();
188
189                         // modifies underlying the stack. MoveNext must throw InvalidOperationException
190                         stackInt.Push(5);
191                         
192                         try 
193                         {
194                                 e.MoveNext();
195                                 Assert.Fail ("InvalidOperationException should be thrown");
196                         } 
197                         catch (InvalidOperationException) {}
198                 }
199
200
201                 [Test]
202                 public void TestClear()
203                 {
204                         stackInt.Clear();
205
206                         Assert.AreEqual (stackInt.Count, 0);
207                 }
208
209                 [Test]
210                 public void TestClone()
211                 {
212                         Stack clone = (Stack)stackInt.Clone();
213
214                         while (stackInt.Count > 0)
215                         {
216                                 Assert.AreEqual (clone.Pop(), stackInt.Pop());
217                         }
218                 }
219
220                 [Test]
221                 public void TestContains()
222                 {
223                         string toLocate = "test";
224
225
226                         stackInt.Push(toLocate);
227
228                         stackInt.Push("chaff");
229
230                         stackInt.Push(null);
231
232                         Assert.IsTrue (stackInt.Contains(toLocate));
233
234                         Assert.IsTrue (stackInt.Contains(null), "must contain null");
235
236                         stackInt.Pop();
237
238                         stackInt.Pop();
239
240                         Assert.IsTrue (stackInt.Contains(toLocate));
241
242                         stackInt.Pop();
243
244                         Assert.IsTrue (!stackInt.Contains(toLocate));
245                         
246                         stackInt.Push(null);
247                         Assert.IsTrue (stackInt.Contains(null));
248                         stackInt.Pop();
249                         Assert.IsTrue (!stackInt.Contains(null));
250                         
251                         
252                 }
253
254                 [Test]
255                 public void TestCopyTo()
256                 {
257                         int[] arr = new int[stackInt.Count - 1];
258                         int[,] arrMulti;
259
260                         try 
261                         {
262                                 stackInt.CopyTo(null, 0);
263                                 Assert.Fail ("Should throw an ArgumentNullException");
264                         } catch (ArgumentNullException e) {
265                                 Assert.AreEqual ("array", e.ParamName, "ParamName must be \"array\"");
266                         }
267
268                         try
269                         {
270                                 stackInt.CopyTo(arr, -1);
271                                 Assert.Fail ("Should throw an ArgumentOutOfRangeException");
272                         } 
273                         catch (ArgumentOutOfRangeException e) 
274                         {
275                                 Assert.AreEqual ("index", e.ParamName, "ParamName must be \"index\"");
276                         }
277
278                         try
279                         {
280                                 stackInt.CopyTo(arrMulti = new int[1, 1], 1);
281                                 Assert.Fail ("Should throw an ArgumentException");
282                         } 
283                         catch (ArgumentException) {}
284
285                         try
286                         {
287                                 stackInt.CopyTo(arr = new int[2], 3);
288                                 Assert.Fail ("Should throw an ArgumentException");
289                         } 
290                         catch (ArgumentException) {}
291
292                         try
293                         {
294                                 stackInt.CopyTo(arr = new int[3], 2);
295                                 Assert.Fail ("Should throw an ArgumentException");
296                         } 
297                         catch (ArgumentException) {}
298
299                         try
300                         {
301                                 stackInt.CopyTo(arr = new int[2], 3);
302                                 Assert.Fail ("Should throw an ArgumentException");
303                         } 
304                         catch (ArgumentException) {}
305
306                         arr = new int[stackInt.Count];
307
308                         stackInt.CopyTo(arr, 0);
309
310                         int j = 4;
311
312                         for (int i = 0; i < 4; i++)
313                         {
314                                Assert.AreEqual (arr[i], j--);
315                         }
316                 }
317
318                 [Test]
319                 public void TestSyncronized()
320                 {
321                         Stack syncStack = Stack.Synchronized(stackInt);
322
323                         syncStack.Push(5);
324
325                         for (int i = 5; i >= 0; i--)
326                                 Assert.AreEqual (syncStack.Pop(), i);
327                 }
328
329                 [Test]
330                 public void TestPushPeekPop()
331                 {
332                         stackInt.Pop();
333
334                         int topVal = (int)stackInt.Peek();
335
336                         Assert.AreEqual (topVal, 3);
337
338                         Assert.AreEqual (stackInt.Count, 4);
339
340                         Assert.AreEqual (stackInt.Pop(), topVal);
341
342                         Assert.AreEqual (stackInt.Pop(), 2);
343
344                         Stack test = new Stack();
345                         test.Push(null);
346
347                         Assert.AreEqual (test.Pop(), null);
348
349                 }
350                 
351                 [Test]
352                 public void TestPop()
353                 {
354                         for (int i = 4; i >= 0; i--) 
355                         {
356                                 Assert.AreEqual (stackInt.Pop(), i);
357                         }
358                         try {
359                                 stackInt.Pop();
360                                 Assert.Fail ("must throw InvalidOperationException");
361                         } catch (InvalidOperationException){
362                         }
363                 }
364
365                 [Test]
366                 public void TestToArray()
367                 {
368                         object[] arr = stackInt.ToArray();
369
370                         Assert.AreEqual (arr.Length, stackInt.Count);                       
371
372                         for (int i = 0; i < 5; i++)
373                                 Assert.AreEqual (stackInt.Pop(), arr[i]);
374                 }
375                 
376                 [Test]
377                 public void TestResize()
378                 {
379                         Stack myStack = new Stack(20);
380
381                         for (int i = 0; i < 500; i++) 
382                         {
383                                 myStack.Push(i);
384                                 Assert.AreEqual (i+1, myStack.Count, "push count test");
385                         }
386                         
387                         for (int i = 499; i >= 0; i--) 
388                         {
389                                 Assert.AreEqual (myStack.Pop(), i);
390                                 Assert.AreEqual (i, myStack.Count, "pop count test");
391                         }
392                 }
393
394                 [Test]
395                 public void TestEmptyCopyTo ()
396                 {
397                         Stack stack = new Stack ();
398                         string [] arr = new string [0];
399                         stack.CopyTo (arr, 0);
400                 }
401
402                 [Test]  
403                 public void TestICollectionCtorUsesEnum ()
404                 {
405                         BitArray x = new BitArray (10, true);
406                         Stack s = new Stack (x);
407                 }
408
409                 [SetUp]
410                 protected  void SetUp()
411                 {
412                         stack1 = new Stack();
413
414                         stackInt = new Stack();
415     
416                         for (int i = 0; i < 5; i++)
417                                 stackInt.Push(i);
418                 }
419         }
420 }