2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / corlib / Test / System.Collections / StackTest.cs
1 //
2 // StackTest.cs
3 //
4 // Author:
5 //  Chris Hynes <chrish@assistedsolutions.com>
6 //
7 // (C) 2001 Chris Hynes
8 //
9
10 using System;
11
12 using System.Collections;
13
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Collections
17 {
18         [TestFixture]
19         public class StackTest: Assertion
20         {
21                 private Stack stack1;
22                 private Stack stack2;
23                 private Stack stackInt;
24
25                 public void TestConstructor()
26                 {
27                         AssertEquals(false, stack1 == null);
28                 }
29                 
30                 public void TestICollectionConstructor1()
31                 {
32                         Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
33
34                         for (int i = 4; i >= 0; i--)
35                                 AssertEquals(i, stackTest.Pop());
36
37                         AssertEquals(0, stackTest.Count);
38                 }
39
40                 public void TestICollectionConstructor2()
41                 {
42                         bool exceptionThrown = false;
43                         try {
44                                 Stack stackTest = new Stack(null);
45                         } catch (ArgumentNullException e) {
46                                 exceptionThrown = true;
47                                 AssertEquals("ParamName must be \"col\"","col",e.ParamName);
48                         }
49                         Assert("null argument must throw ArgumentNullException", exceptionThrown);
50                                         
51                 }
52
53                 public void TestIntConstructor1()
54                 {
55                         Stack stackTest = new Stack(50);
56
57                         Assert(stackTest != null);
58                 }
59
60                 public void TestIntConstructor2()
61                 {
62                         bool exceptionThrown = false;
63                         try {
64                                 Stack stackTest = new Stack(-1);
65                         } 
66                         catch (ArgumentOutOfRangeException e) 
67                         {
68                                 exceptionThrown = true;
69                                 AssertEquals("ParamName must be \"initialCapacity\"","initialCapacity",e.ParamName);
70                         }
71                         Assert("negative argument must throw ArgumentOutOfRangeException", exceptionThrown);
72                 }
73
74                 public void TestCount()
75                 {
76                         Stack stackTest = new Stack();
77
78                         stackTest.Push(50);
79                         stackTest.Push(5);
80                         stackTest.Push(0);
81                         stackTest.Push(50);
82
83                         AssertEquals(4, stackTest.Count);
84                 }
85
86                 public void TestIsSyncronized()
87                 {
88                         AssertEquals(false, stack1.IsSynchronized);
89                         AssertEquals(true, Stack.Synchronized(stack1).IsSynchronized);
90                 }
91
92                 public void TestSyncRoot()
93                 {
94                         AssertEquals(false, stack1.SyncRoot == null);
95                 }
96
97                 public void TestGetEnumerator1()
98                 {
99                         stackInt.Pop();
100
101                         int j = 3;
102
103                         foreach (int i in stackInt)
104                         {
105                                 AssertEquals(j--, i);
106                         }
107
108                         stackInt.Clear();
109
110                         IEnumerator e = stackInt.GetEnumerator();
111
112                         AssertEquals(false, e.MoveNext());
113                 }
114
115                 public void TestGetEnumerator2()
116                 {
117                         
118                         IEnumerator e = stackInt.GetEnumerator();
119                         try 
120                         {
121                                 // Tests InvalidOperationException if enumerator is uninitialized
122                                 Object o = e.Current;
123                                 Fail("InvalidOperationException should be thrown");
124                         } catch (InvalidOperationException) {}
125                 }
126
127                 public void TestGetEnumerator3()
128                 {
129                         
130                         IEnumerator e = stack1.GetEnumerator();
131                         e.MoveNext();
132                         try 
133                         {
134                                 // Tests InvalidOperationException if enumeration has ended
135                                 Object o = e.Current;
136                                 Fail("InvalidOperationException should be thrown");
137                         } catch (InvalidOperationException) {}
138                 }
139         
140                 public void TestEnumeratorReset1() 
141                 {
142                         IEnumerator e = stackInt.GetEnumerator();
143
144                         e.MoveNext();
145                         AssertEquals("current value", 4, e.Current);
146                         e.MoveNext();
147
148                         e.Reset();
149
150                         e.MoveNext();
151                         AssertEquals("current value after reset", 4, e.Current);
152                 }
153
154                 public void TestEnumeratorReset2() 
155                 {
156                         IEnumerator e = stackInt.GetEnumerator();
157
158                         e.MoveNext();
159                         AssertEquals("current value", 4, e.Current);
160
161                         // modifies underlying the stack. Reset must throw InvalidOperationException
162                         stackInt.Push(5);
163                         
164                         try 
165                         {
166                                 e.Reset();
167                                 Fail("InvalidOperationException should be thrown");
168                         } 
169                         catch (InvalidOperationException) {}
170                 }
171
172                 public void TestEnumeratorMoveNextException() 
173                 {
174                         IEnumerator e = stackInt.GetEnumerator();
175
176                         // modifies underlying the stack. MoveNext must throw InvalidOperationException
177                         stackInt.Push(5);
178                         
179                         try 
180                         {
181                                 e.MoveNext();
182                                 Fail("InvalidOperationException should be thrown");
183                         } 
184                         catch (InvalidOperationException) {}
185                 }
186
187
188                 public void TestClear()
189                 {
190                         stackInt.Clear();
191
192                         AssertEquals(0, stackInt.Count);
193                 }
194
195                 public void TestClone()
196                 {
197                         Stack clone = (Stack)stackInt.Clone();
198
199                         while (stackInt.Count > 0)
200                         {
201                                 AssertEquals(stackInt.Pop(), clone.Pop());
202                         }
203                 }
204
205                 public void TestContains()
206                 {
207                         string toLocate = "test";
208
209
210                         stackInt.Push(toLocate);
211
212                         stackInt.Push("chaff");
213
214                         stackInt.Push(null);
215
216                         Assert(stackInt.Contains(toLocate));
217
218                         Assert("must contain null", stackInt.Contains(null));
219
220                         stackInt.Pop();
221
222                         stackInt.Pop();
223
224                         Assert(stackInt.Contains(toLocate));
225
226                         stackInt.Pop();
227
228                         Assert(!stackInt.Contains(toLocate));
229                         
230                         stackInt.Push(null);
231                         Assert(stackInt.Contains(null));
232                         stackInt.Pop();
233                         Assert(!stackInt.Contains(null));
234                         
235                         
236                 }
237
238                 public void TestCopyTo()
239                 {
240                         int[] arr = new int[stackInt.Count - 1];
241                         int[,] arrMulti;
242
243                         try 
244                         {
245                                 stackInt.CopyTo(null, 0);
246                                 Fail("Should throw an ArgumentNullException");
247                         } catch (ArgumentNullException e) {
248                                 AssertEquals("ParamName must be \"array\"","array",e.ParamName);
249                         }
250
251                         try
252                         {
253                                 stackInt.CopyTo(arr, -1);
254                                 Fail("Should throw an ArgumentOutOfRangeException");
255                         } 
256                         catch (ArgumentOutOfRangeException e) 
257                         {
258                                 AssertEquals("ParamName must be \"index\"","index",e.ParamName);
259                         }
260
261                         try
262                         {
263                                 stackInt.CopyTo(arrMulti = new int[1, 1], 1);
264                                 Fail("Should throw an ArgumentException");
265                         } 
266                         catch (ArgumentException) {}
267
268                         try
269                         {
270                                 stackInt.CopyTo(arr = new int[2], 3);
271                                 Fail("Should throw an ArgumentException");
272                         } 
273                         catch (ArgumentException) {}
274
275                         try
276                         {
277                                 stackInt.CopyTo(arr = new int[3], 2);
278                                 Fail("Should throw an ArgumentException");
279                         } 
280                         catch (ArgumentException) {}
281
282                         try
283                         {
284                                 stackInt.CopyTo(arr = new int[2], 3);
285                                 Fail("Should throw an ArgumentException");
286                         } 
287                         catch (ArgumentException) {}
288
289                         arr = new int[stackInt.Count];
290
291                         stackInt.CopyTo(arr, 0);
292
293                         int j = 4;
294
295                         for (int i = 0; i < 4; i++)
296                         {
297                                AssertEquals(j--, arr[i]);
298                         }
299                 }
300
301                 public void TestSyncronized()
302                 {
303                         Stack syncStack = Stack.Synchronized(stackInt);
304
305                         syncStack.Push(5);
306
307                         for (int i = 5; i >= 0; i--)
308                                 AssertEquals(i, syncStack.Pop());
309                 }
310
311                 public void TestPushPeekPop()
312                 {
313                         stackInt.Pop();
314
315                         int topVal = (int)stackInt.Peek();
316
317                         AssertEquals(3, topVal);
318
319                         AssertEquals(4, stackInt.Count);
320
321                         AssertEquals(topVal, stackInt.Pop());
322
323                         AssertEquals(2, stackInt.Pop());
324
325                         Stack test = new Stack();
326                         test.Push(null);
327
328                         AssertEquals(null, test.Pop());
329
330                 }
331                 
332                 public void TestPop()
333                 {
334                         for (int i = 4; i >= 0; i--) 
335                         {
336                                 AssertEquals(i, stackInt.Pop());
337                         }
338                         try {
339                                 stackInt.Pop();
340                                 Fail("must throw InvalidOperationException");
341                         } catch (InvalidOperationException){
342                         }
343                 }
344
345                 public void TestToArray()
346                 {
347                         object[] arr = stackInt.ToArray();
348
349                         AssertEquals(stackInt.Count, arr.Length);                       
350
351                         for (int i = 0; i < 5; i++)
352                                 AssertEquals(arr[i], stackInt.Pop());
353                 }
354                 
355                 public void TestResize()
356                 {
357                         Stack myStack = new Stack(20);
358
359                         for (int i = 0; i < 500; i++) 
360                         {
361                                 myStack.Push(i);
362                                 AssertEquals("push count test",i+1, myStack.Count);
363                         }
364                         
365                         for (int i = 499; i >= 0; i--) 
366                         {
367                                 AssertEquals(i, myStack.Pop());
368                                 AssertEquals("pop count test",i, myStack.Count);
369                         }
370                 }
371
372                 public void TestEmptyCopyTo ()
373                 {
374                         Stack stack = new Stack ();
375                         string [] arr = new string [0];
376                         stack.CopyTo (arr, 0);
377                 }
378
379                 [Test]  
380                 public void TestICollectionCtorUsesEnum ()
381                 {
382                         BitArray x = new BitArray (10, true);
383                         Stack s = new Stack (x);
384                 }
385
386                 [SetUp]
387                 protected  void SetUp()
388                 {
389                         stack1 = new Stack();
390                         stack2 = new Stack();
391
392                         stackInt = new Stack();
393     
394                         for (int i = 0; i < 5; i++)
395                                 stackInt.Push(i);
396                 }
397         }
398 }