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: Assertion
23 private Stack stackInt;
25 public void TestConstructor()
27 AssertEquals(false, stack1 == null);
30 public void TestICollectionConstructor1()
32 Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
34 for (int i = 4; i >= 0; i--)
35 AssertEquals(i, stackTest.Pop());
37 AssertEquals(0, stackTest.Count);
40 public void TestICollectionConstructor2()
42 bool exceptionThrown = false;
44 Stack stackTest = new Stack(null);
45 } catch (ArgumentNullException e) {
46 exceptionThrown = true;
47 AssertEquals("ParamName must be \"col\"","col",e.ParamName);
49 Assert("null argument must throw ArgumentNullException", exceptionThrown);
53 public void TestIntConstructor1()
55 Stack stackTest = new Stack(50);
57 Assert(stackTest != null);
60 public void TestIntConstructor2()
62 bool exceptionThrown = false;
64 Stack stackTest = new Stack(-1);
66 catch (ArgumentOutOfRangeException e)
68 exceptionThrown = true;
69 AssertEquals("ParamName must be \"initialCapacity\"","initialCapacity",e.ParamName);
71 Assert("negative argument must throw ArgumentOutOfRangeException", exceptionThrown);
74 public void TestCount()
76 Stack stackTest = new Stack();
83 AssertEquals(4, stackTest.Count);
86 public void TestIsSyncronized()
88 AssertEquals(false, stack1.IsSynchronized);
89 AssertEquals(true, Stack.Synchronized(stack1).IsSynchronized);
92 public void TestSyncRoot()
94 AssertEquals(false, stack1.SyncRoot == null);
97 public void TestGetEnumerator1()
103 foreach (int i in stackInt)
105 AssertEquals(j--, i);
110 IEnumerator e = stackInt.GetEnumerator();
112 AssertEquals(false, e.MoveNext());
115 public void TestGetEnumerator2()
118 IEnumerator e = stackInt.GetEnumerator();
121 // Tests InvalidOperationException if enumerator is uninitialized
122 Object o = e.Current;
123 Fail("InvalidOperationException should be thrown");
124 } catch (InvalidOperationException) {}
127 public void TestGetEnumerator3()
130 IEnumerator e = stack1.GetEnumerator();
134 // Tests InvalidOperationException if enumeration has ended
135 Object o = e.Current;
136 Fail("InvalidOperationException should be thrown");
137 } catch (InvalidOperationException) {}
140 public void TestEnumeratorReset1()
142 IEnumerator e = stackInt.GetEnumerator();
145 AssertEquals("current value", 4, e.Current);
151 AssertEquals("current value after reset", 4, e.Current);
154 public void TestEnumeratorReset2()
156 IEnumerator e = stackInt.GetEnumerator();
159 AssertEquals("current value", 4, e.Current);
161 // modifies underlying the stack. Reset must throw InvalidOperationException
167 Fail("InvalidOperationException should be thrown");
169 catch (InvalidOperationException) {}
172 public void TestEnumeratorMoveNextException()
174 IEnumerator e = stackInt.GetEnumerator();
176 // modifies underlying the stack. MoveNext must throw InvalidOperationException
182 Fail("InvalidOperationException should be thrown");
184 catch (InvalidOperationException) {}
188 public void TestClear()
192 AssertEquals(0, stackInt.Count);
195 public void TestClone()
197 Stack clone = (Stack)stackInt.Clone();
199 while (stackInt.Count > 0)
201 AssertEquals(stackInt.Pop(), clone.Pop());
205 public void TestContains()
207 string toLocate = "test";
210 stackInt.Push(toLocate);
212 stackInt.Push("chaff");
216 Assert(stackInt.Contains(toLocate));
218 Assert("must contain null", stackInt.Contains(null));
224 Assert(stackInt.Contains(toLocate));
228 Assert(!stackInt.Contains(toLocate));
231 Assert(stackInt.Contains(null));
233 Assert(!stackInt.Contains(null));
238 public void TestCopyTo()
240 int[] arr = new int[stackInt.Count - 1];
245 stackInt.CopyTo(null, 0);
246 Fail("Should throw an ArgumentNullException");
247 } catch (ArgumentNullException e) {
248 AssertEquals("ParamName must be \"array\"","array",e.ParamName);
253 stackInt.CopyTo(arr, -1);
254 Fail("Should throw an ArgumentOutOfRangeException");
256 catch (ArgumentOutOfRangeException e)
258 AssertEquals("ParamName must be \"index\"","index",e.ParamName);
263 stackInt.CopyTo(arrMulti = new int[1, 1], 1);
264 Fail("Should throw an ArgumentException");
266 catch (ArgumentException) {}
270 stackInt.CopyTo(arr = new int[2], 3);
271 Fail("Should throw an ArgumentException");
273 catch (ArgumentException) {}
277 stackInt.CopyTo(arr = new int[3], 2);
278 Fail("Should throw an ArgumentException");
280 catch (ArgumentException) {}
284 stackInt.CopyTo(arr = new int[2], 3);
285 Fail("Should throw an ArgumentException");
287 catch (ArgumentException) {}
289 arr = new int[stackInt.Count];
291 stackInt.CopyTo(arr, 0);
295 for (int i = 0; i < 4; i++)
297 AssertEquals(j--, arr[i]);
301 public void TestSyncronized()
303 Stack syncStack = Stack.Synchronized(stackInt);
307 for (int i = 5; i >= 0; i--)
308 AssertEquals(i, syncStack.Pop());
311 public void TestPushPeekPop()
315 int topVal = (int)stackInt.Peek();
317 AssertEquals(3, topVal);
319 AssertEquals(4, stackInt.Count);
321 AssertEquals(topVal, stackInt.Pop());
323 AssertEquals(2, stackInt.Pop());
325 Stack test = new Stack();
328 AssertEquals(null, test.Pop());
332 public void TestPop()
334 for (int i = 4; i >= 0; i--)
336 AssertEquals(i, stackInt.Pop());
340 Fail("must throw InvalidOperationException");
341 } catch (InvalidOperationException){
345 public void TestToArray()
347 object[] arr = stackInt.ToArray();
349 AssertEquals(stackInt.Count, arr.Length);
351 for (int i = 0; i < 5; i++)
352 AssertEquals(arr[i], stackInt.Pop());
355 public void TestResize()
357 Stack myStack = new Stack(20);
359 for (int i = 0; i < 500; i++)
362 AssertEquals("push count test",i+1, myStack.Count);
365 for (int i = 499; i >= 0; i--)
367 AssertEquals(i, myStack.Pop());
368 AssertEquals("pop count test",i, myStack.Count);
372 public void TestEmptyCopyTo ()
374 Stack stack = new Stack ();
375 string [] arr = new string [0];
376 stack.CopyTo (arr, 0);
380 public void TestICollectionCtorUsesEnum ()
382 BitArray x = new BitArray (10, true);
383 Stack s = new Stack (x);
387 protected void SetUp()
389 stack1 = new Stack();
390 stack2 = new Stack();
392 stackInt = new Stack();
394 for (int i = 0; i < 5; i++)