// // StackTest.cs // // Author: // Ben Maurer (bmaurer@ximian.com) // #if NET_2_0 using System; using System.Collections; using System.Collections.Generic; using NUnit.Framework; namespace MonoTests.System.Collections.Generic { [TestFixture] public class StackTest: Assertion { [Test] public void TestCtor () { Stack a = new Stack (); Stack b = new Stack (1); Stack c = new Stack (); Stack d = new Stack (1); Stack e = new Stack (0); } [Test] [ExpectedException (typeof (ArgumentOutOfRangeException))] public void TestCtorEx () { Stack a = new Stack (-1); } [Test] public void TestCtorEnum () { List l = new List (); l.Add (1); l.Add (2); l.Add (3); Stack s = new Stack (l); // Things get pop'd in reverse AssertPop (s, 3); AssertPop (s, 2); AssertPop (s, 1); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void TestCtorEnumNull () { Stack s = new Stack (null); } [Test] public void TestClear() { Stack s = new Stack (); s.Clear (); AssertEquals (s.Count, 0); s.Push (1); s.Push (2); AssertEquals (s.Count, 2); s.Clear (); AssertEquals (s.Count, 0); } [Test] public void TestContains () { Stack s = new Stack (); AssertEquals (s.Contains (1), false); s.Push (1); AssertEquals (s.Contains (1), true); AssertEquals (s.Contains (0), false); } [Test] public void TestCopyTo () { int [] x = new int [3]; Stack z = new Stack (); z.Push (1); z.Push (2); x [0] = 10; z.CopyTo (x, 1); AssertEquals (x [0], 10); AssertEquals (x [1], 2); AssertEquals (x [2], 1); } [Test] public void TestPeek () { Stack s = new Stack (); s.Push (1); AssertEquals (s.Peek (), 1); AssertEquals (s.Count, 1); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void TestPeekEx () { Stack s = new Stack (); s.Peek (); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void TestPeekEx2 () { Stack s = new Stack (); s.Push (1); s.Pop (); s.Peek (); } [Test] public void TestPop () { Stack s = new Stack (); s.Push (1); AssertEquals (s.Pop (), 1); AssertEquals (s.Count, 0); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void TestPopEx () { Stack s = new Stack (); s.Pop (); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void TestPopEx2 () { Stack s = new Stack (); s.Push (1); s.Pop (); s.Pop (); } [Test] public void TestPush () { Stack s = new Stack (); s.Push (1); AssertEquals (s.Count, 1); s.Push (2); AssertEquals (s.Count, 2); for (int i = 0; i < 100; i ++) s.Push (i); AssertEquals (s.Count, 102); } [Test] public void TestToArray () { Stack s = new Stack (); int [] x = s.ToArray (); AssertEquals (x.Length, 0); s.Push (1); x = s.ToArray (); AssertEquals (x.Length, 1); AssertEquals (x [0], 1); } [Test] public void TestEnumerator () { Stack s = new Stack (); foreach (int x in s) Fail (); s.Push (1); int i = 0; foreach (int x in s) { AssertEquals (i, 0); AssertEquals (x, 1); i ++; } i = 0; s.Push (2); s.Push (3); foreach (int x in s) { AssertEquals (x, 3 - i); Assert (i < 3); i ++; } } void AssertPop (Stack s, T t) { AssertEquals (s.Pop (), t); } } } #endif