5 // Chris Hynes <chrish@assistedsolutions.com>
7 // (C) 2001 Chris Hynes
12 using System.Collections;
14 using NUnit.Framework;
16 namespace MonoTests.System.Collections
18 public class StackTest: TestCase
22 private Stack stackInt;
24 public void TestConstructor()
26 AssertEquals(false, stack1 == null);
29 public void TestICollectionConstructor()
31 Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
33 for (int i = 4; i >= 0; i--)
34 AssertEquals(i, stackTest.Pop());
36 AssertEquals(0, stackTest.Count);
39 public void TestIntConstructor()
41 Stack stackTest = new Stack(50);
43 AssertEquals(false, stackTest == null);
46 public void TestCount()
48 Stack stackTest = new Stack();
55 AssertEquals(4, stackTest.Count);
58 public void TestIsSyncronized()
60 AssertEquals(false, stack1.IsSynchronized);
61 AssertEquals(true, Stack.Synchronized(stack1).IsSynchronized);
64 public void TestSyncRoot()
66 AssertEquals(false, stack1.SyncRoot == null);
69 public void TestGetEnumerator()
75 foreach (int i in stackInt)
82 IEnumerator e = stackInt.GetEnumerator();
84 AssertEquals(false, e.MoveNext());
87 public void TestClear()
91 AssertEquals(0, stackInt.Count);
94 public void TestClone()
96 Stack clone = (Stack)stackInt.Clone();
98 while (stackInt.Count > 0)
100 AssertEquals(stackInt.Pop(), clone.Pop());
104 public void TestContains()
106 string toLocate = "test";
108 stackInt.Push(toLocate);
110 stackInt.Push("chaff");
112 Assert(stackInt.Contains(toLocate));
116 Assert(stackInt.Contains(toLocate));
120 Assert(!stackInt.Contains(toLocate));
123 public void TestCopyTo()
125 int[] arr = new int[stackInt.Count - 1];
130 stackInt.CopyTo(null, 0);
131 Fail("Should throw an ArgumentNullException");
133 catch (ArgumentNullException) {}
137 stackInt.CopyTo(arr, -1);
138 Fail("Should throw an ArgumentOutOfRangeException");
140 catch (ArgumentOutOfRangeException) {}
144 stackInt.CopyTo(arrMulti = new int[1, 1], 1);
145 Fail("Should throw an ArgumentException");
147 catch (ArgumentException) {}
151 stackInt.CopyTo(arr = new int[2], 3);
152 Fail("Should throw an ArgumentException");
154 catch (ArgumentException) {}
158 stackInt.CopyTo(arr = new int[3], 2);
159 Fail("Should throw an ArgumentException");
161 catch (ArgumentException) {}
165 stackInt.CopyTo(arr = new int[2], 3);
166 Fail("Should throw an ArgumentException");
168 catch (ArgumentException) {}
170 arr = new int[stackInt.Count];
172 stackInt.CopyTo(arr, 0);
176 for (int i = 0; i < 4; i++)
178 AssertEquals(j--, arr[i]);
182 public void TestSyncronized()
184 Stack syncStack = Stack.Synchronized(stackInt);
188 for (int i = 5; i >= 0; i--)
189 AssertEquals(i, syncStack.Pop());
192 public void TestPushPeekPop()
196 int topVal = (int)stackInt.Peek();
198 AssertEquals(3, topVal);
200 AssertEquals(4, stackInt.Count);
202 AssertEquals(topVal, stackInt.Pop());
204 AssertEquals(2, stackInt.Pop());
206 Stack test = new Stack();
209 AssertEquals(null, test.Pop());
212 public void TestToArray()
214 object[] arr = stackInt.ToArray();
216 AssertEquals(stackInt.Count, arr.Length);
218 for (int i = 0; i < 5; i++)
219 AssertEquals(arr[i], stackInt.Pop());
222 static void Main(string[] args)
225 // TODO: Add code to start application here
229 protected override void SetUp()
231 stack1 = new Stack();
232 stack2 = new Stack();
234 stackInt = new Stack();
236 for (int i = 0; i < 5; i++)
240 public static ITest Suite
244 TestSuite stackSuite = new TestSuite();
246 stackSuite.AddTest(new StackTest("TestConstructor"));
247 stackSuite.AddTest(new StackTest("TestICollectionConstructor"));
248 stackSuite.AddTest(new StackTest("TestIntConstructor"));
250 stackSuite.AddTest(new StackTest("TestCount"));
251 stackSuite.AddTest(new StackTest("TestIsSyncronized"));
252 stackSuite.AddTest(new StackTest("TestSyncRoot"));
254 stackSuite.AddTest(new StackTest("TestGetEnumerator"));
255 stackSuite.AddTest(new StackTest("TestClear"));
256 stackSuite.AddTest(new StackTest("TestClone"));
257 stackSuite.AddTest(new StackTest("TestContains"));
258 stackSuite.AddTest(new StackTest("TestPushPeekPop"));
259 stackSuite.AddTest(new StackTest("TestCopyTo"));
260 stackSuite.AddTest(new StackTest("TestSyncronized"));
261 stackSuite.AddTest(new StackTest("TestToArray"));
267 public StackTest(string name): base(name) {}