| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- //
- // StackTest.cs
- //
- // Author:
- // Chris Hynes <[email protected]>
- //
- // (C) 2001 Chris Hynes
- //
- using System;
- using System.Collections;
- using NUnit.Framework;
- namespace MonoTests.System.Collections
- {
- public class StackTest: TestCase
- {
- private Stack stack1;
- private Stack stack2;
- private Stack stackInt;
- public void TestConstructor()
- {
- AssertEquals(false, stack1 == null);
- }
-
- public void TestICollectionConstructor()
- {
- Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
- for (int i = 4; i >= 0; i--)
- AssertEquals(i, stackTest.Pop());
- AssertEquals(0, stackTest.Count);
- }
- public void TestIntConstructor()
- {
- Stack stackTest = new Stack(50);
- AssertEquals(false, stackTest == null);
- }
- public void TestCount()
- {
- Stack stackTest = new Stack();
- stackTest.Push(50);
- stackTest.Push(5);
- stackTest.Push(0);
- stackTest.Push(50);
- AssertEquals(4, stackTest.Count);
- }
- public void TestIsSyncronized()
- {
- AssertEquals(false, stack1.IsSynchronized);
- AssertEquals(true, Stack.Synchronized(stack1).IsSynchronized);
- }
- public void TestSyncRoot()
- {
- AssertEquals(false, stack1.SyncRoot == null);
- }
- public void TestGetEnumerator()
- {
- stackInt.Pop();
- int j = 3;
- foreach (int i in stackInt)
- {
- AssertEquals(j--, i);
- }
- stackInt.Clear();
- IEnumerator e = stackInt.GetEnumerator();
- AssertEquals(false, e.MoveNext());
- }
- public void TestClear()
- {
- stackInt.Clear();
- AssertEquals(0, stackInt.Count);
- }
- public void TestClone()
- {
- Stack clone = (Stack)stackInt.Clone();
- while (stackInt.Count > 0)
- {
- AssertEquals(stackInt.Pop(), clone.Pop());
- }
- }
- public void TestContains()
- {
- string toLocate = "test";
- stackInt.Push(toLocate);
- stackInt.Push("chaff");
- Assert(stackInt.Contains(toLocate));
- stackInt.Pop();
- Assert(stackInt.Contains(toLocate));
- stackInt.Pop();
- Assert(!stackInt.Contains(toLocate));
- }
- public void TestCopyTo()
- {
- int[] arr = new int[stackInt.Count - 1];
- int[,] arrMulti;
- try
- {
- stackInt.CopyTo(null, 0);
- Fail("Should throw an ArgumentNullException");
- }
- catch (ArgumentNullException) {}
- try
- {
- stackInt.CopyTo(arr, -1);
- Fail("Should throw an ArgumentOutOfRangeException");
- }
- catch (ArgumentOutOfRangeException) {}
- try
- {
- stackInt.CopyTo(arrMulti = new int[1, 1], 1);
- Fail("Should throw an ArgumentException");
- }
- catch (ArgumentException) {}
- try
- {
- stackInt.CopyTo(arr = new int[2], 3);
- Fail("Should throw an ArgumentException");
- }
- catch (ArgumentException) {}
- try
- {
- stackInt.CopyTo(arr = new int[3], 2);
- Fail("Should throw an ArgumentException");
- }
- catch (ArgumentException) {}
- try
- {
- stackInt.CopyTo(arr = new int[2], 3);
- Fail("Should throw an ArgumentException");
- }
- catch (ArgumentException) {}
- arr = new int[stackInt.Count];
- stackInt.CopyTo(arr, 0);
- int j = 4;
- for (int i = 0; i < 4; i++)
- {
- AssertEquals(j--, arr[i]);
- }
- }
- public void TestSyncronized()
- {
- Stack syncStack = Stack.Synchronized(stackInt);
- syncStack.Push(5);
- for (int i = 5; i >= 0; i--)
- AssertEquals(i, syncStack.Pop());
- }
- public void TestPushPeekPop()
- {
- stackInt.Pop();
- int topVal = (int)stackInt.Peek();
- AssertEquals(3, topVal);
- AssertEquals(4, stackInt.Count);
- AssertEquals(topVal, stackInt.Pop());
- AssertEquals(2, stackInt.Pop());
- Stack test = new Stack();
- test.Push(null);
- AssertEquals(null, test.Pop());
- }
- public void TestToArray()
- {
- object[] arr = stackInt.ToArray();
- AssertEquals(stackInt.Count, arr.Length);
- for (int i = 0; i < 5; i++)
- AssertEquals(arr[i], stackInt.Pop());
- }
- protected override void SetUp()
- {
- stack1 = new Stack();
- stack2 = new Stack();
- stackInt = new Stack();
-
- for (int i = 0; i < 5; i++)
- stackInt.Push(i);
- }
- }
- }
|