5 // Chris Hynes <chrish@assistedsolutions.com>
7 // (C) 2001 Chris Hynes
12 using System.Collections;
14 using NUnit.Framework;
16 namespace MonoTests.System.Collections
19 public class StackTest
22 private Stack stackInt;
25 public void TestConstructor()
27 Assert.AreEqual (stack1 == null, false);
31 public void TestICollectionConstructor1()
33 Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
35 for (int i = 4; i >= 0; i--)
36 Assert.AreEqual (stackTest.Pop(), i);
38 Assert.AreEqual (stackTest.Count, 0);
42 public void TestICollectionConstructor2()
44 bool exceptionThrown = false;
46 Stack stackTest = new Stack(null);
47 } catch (ArgumentNullException e) {
48 exceptionThrown = true;
49 Assert.AreEqual ("col", e.ParamName, "ParamName must be \"col\"");
51 Assert.IsTrue (exceptionThrown, "null argument must throw ArgumentNullException");
56 public void TestIntConstructor1()
58 Stack stackTest = new Stack(50);
60 Assert.IsTrue (stackTest != null);
64 public void TestIntConstructor2()
66 bool exceptionThrown = false;
68 Stack stackTest = new Stack(-1);
70 catch (ArgumentOutOfRangeException e)
72 exceptionThrown = true;
73 Assert.AreEqual ("initialCapacity", e.ParamName, "ParamName must be \"initialCapacity\"");
75 Assert.IsTrue (exceptionThrown, "negative argument must throw ArgumentOutOfRangeException");
79 public void TestCount()
81 Stack stackTest = new Stack();
88 Assert.AreEqual (stackTest.Count, 4);
92 public void TestIsSyncronized()
94 Assert.AreEqual (stack1.IsSynchronized, false);
95 Assert.AreEqual (Stack.Synchronized(stack1).IsSynchronized, true);
99 public void TestSyncRoot()
101 Assert.AreEqual (stack1.SyncRoot == null, false);
105 public void TestGetEnumerator1()
111 foreach (int i in stackInt)
113 Assert.AreEqual (i, j--);
118 IEnumerator e = stackInt.GetEnumerator();
120 Assert.AreEqual (e.MoveNext(), false);
124 public void TestGetEnumerator2()
127 IEnumerator e = stackInt.GetEnumerator();
130 // Tests InvalidOperationException if enumerator is uninitialized
131 Object o = e.Current;
132 Assert.Fail ("InvalidOperationException should be thrown");
133 } catch (InvalidOperationException) {}
137 public void TestGetEnumerator3()
140 IEnumerator e = stack1.GetEnumerator();
144 // Tests InvalidOperationException if enumeration has ended
145 Object o = e.Current;
146 Assert.Fail ("InvalidOperationException should be thrown");
147 } catch (InvalidOperationException) {}
151 public void TestEnumeratorReset1()
153 IEnumerator e = stackInt.GetEnumerator();
156 Assert.AreEqual (4, e.Current, "current value");
162 Assert.AreEqual (4, e.Current, "current value after reset");
166 public void TestEnumeratorReset2()
168 IEnumerator e = stackInt.GetEnumerator();
171 Assert.AreEqual (4, e.Current, "current value");
173 // modifies underlying the stack. Reset must throw InvalidOperationException
179 Assert.Fail ("InvalidOperationException should be thrown");
181 catch (InvalidOperationException) {}
185 public void TestEnumeratorMoveNextException()
187 IEnumerator e = stackInt.GetEnumerator();
189 // modifies underlying the stack. MoveNext must throw InvalidOperationException
195 Assert.Fail ("InvalidOperationException should be thrown");
197 catch (InvalidOperationException) {}
202 public void TestClear()
206 Assert.AreEqual (stackInt.Count, 0);
210 public void TestClone()
212 Stack clone = (Stack)stackInt.Clone();
214 while (stackInt.Count > 0)
216 Assert.AreEqual (clone.Pop(), stackInt.Pop());
221 public void TestContains()
223 string toLocate = "test";
226 stackInt.Push(toLocate);
228 stackInt.Push("chaff");
232 Assert.IsTrue (stackInt.Contains(toLocate));
234 Assert.IsTrue (stackInt.Contains(null), "must contain null");
240 Assert.IsTrue (stackInt.Contains(toLocate));
244 Assert.IsTrue (!stackInt.Contains(toLocate));
247 Assert.IsTrue (stackInt.Contains(null));
249 Assert.IsTrue (!stackInt.Contains(null));
255 public void TestCopyTo()
257 int[] arr = new int[stackInt.Count - 1];
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\"");
270 stackInt.CopyTo(arr, -1);
271 Assert.Fail ("Should throw an ArgumentOutOfRangeException");
273 catch (ArgumentOutOfRangeException e)
275 Assert.AreEqual ("index", e.ParamName, "ParamName must be \"index\"");
280 stackInt.CopyTo(arrMulti = new int[1, 1], 1);
281 Assert.Fail ("Should throw an ArgumentException");
283 catch (ArgumentException) {}
287 stackInt.CopyTo(arr = new int[2], 3);
288 Assert.Fail ("Should throw an ArgumentException");
290 catch (ArgumentException) {}
294 stackInt.CopyTo(arr = new int[3], 2);
295 Assert.Fail ("Should throw an ArgumentException");
297 catch (ArgumentException) {}
301 stackInt.CopyTo(arr = new int[2], 3);
302 Assert.Fail ("Should throw an ArgumentException");
304 catch (ArgumentException) {}
306 arr = new int[stackInt.Count];
308 stackInt.CopyTo(arr, 0);
312 for (int i = 0; i < 4; i++)
314 Assert.AreEqual (arr[i], j--);
319 public void TestSyncronized()
321 Stack syncStack = Stack.Synchronized(stackInt);
325 for (int i = 5; i >= 0; i--)
326 Assert.AreEqual (syncStack.Pop(), i);
330 public void TestPushPeekPop()
334 int topVal = (int)stackInt.Peek();
336 Assert.AreEqual (topVal, 3);
338 Assert.AreEqual (stackInt.Count, 4);
340 Assert.AreEqual (stackInt.Pop(), topVal);
342 Assert.AreEqual (stackInt.Pop(), 2);
344 Stack test = new Stack();
347 Assert.AreEqual (test.Pop(), null);
352 public void TestPop()
354 for (int i = 4; i >= 0; i--)
356 Assert.AreEqual (stackInt.Pop(), i);
360 Assert.Fail ("must throw InvalidOperationException");
361 } catch (InvalidOperationException){
366 public void TestToArray()
368 object[] arr = stackInt.ToArray();
370 Assert.AreEqual (arr.Length, stackInt.Count);
372 for (int i = 0; i < 5; i++)
373 Assert.AreEqual (stackInt.Pop(), arr[i]);
377 public void TestResize()
379 Stack myStack = new Stack(20);
381 for (int i = 0; i < 500; i++)
384 Assert.AreEqual (i+1, myStack.Count, "push count test");
387 for (int i = 499; i >= 0; i--)
389 Assert.AreEqual (myStack.Pop(), i);
390 Assert.AreEqual (i, myStack.Count, "pop count test");
395 public void TestEmptyCopyTo ()
397 Stack stack = new Stack ();
398 string [] arr = new string [0];
399 stack.CopyTo (arr, 0);
403 public void TestICollectionCtorUsesEnum ()
405 BitArray x = new BitArray (10, true);
406 Stack s = new Stack (x);
410 protected void SetUp()
412 stack1 = new Stack();
414 stackInt = new Stack();
416 for (int i = 0; i < 5; i++)