2002-12-21 Nick Drochak <ndrochak@gol.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         public class StackTest: TestCase
19         {
20                 private Stack stack1;
21                 private Stack stack2;
22                 private Stack stackInt;
23
24                 public void TestConstructor()
25                 {
26                         AssertEquals(false, stack1 == null);
27                 }
28                 
29                 public void TestICollectionConstructor()
30                 {
31                         Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
32
33                         for (int i = 4; i >= 0; i--)
34                                 AssertEquals(i, stackTest.Pop());
35
36                         AssertEquals(0, stackTest.Count);
37                 }
38
39                 public void TestIntConstructor()
40                 {
41                         Stack stackTest = new Stack(50);
42
43                         AssertEquals(false, stackTest == null);
44                 }
45
46                 public void TestCount()
47                 {
48                         Stack stackTest = new Stack();
49
50                         stackTest.Push(50);
51                         stackTest.Push(5);
52                         stackTest.Push(0);
53                         stackTest.Push(50);
54
55                         AssertEquals(4, stackTest.Count);
56                 }
57
58                 public void TestIsSyncronized()
59                 {
60                         AssertEquals(false, stack1.IsSynchronized);
61                         AssertEquals(true, Stack.Synchronized(stack1).IsSynchronized);
62                 }
63
64                 public void TestSyncRoot()
65                 {
66                         AssertEquals(false, stack1.SyncRoot == null);
67                 }
68
69                 public void TestGetEnumerator()
70                 {
71                         stackInt.Pop();
72
73                         int j = 3;
74
75                         foreach (int i in stackInt)
76                         {
77                                 AssertEquals(j--, i);
78                         }
79
80                         stackInt.Clear();
81
82                         IEnumerator e = stackInt.GetEnumerator();
83
84                         AssertEquals(false, e.MoveNext());
85                 }
86
87                 public void TestClear()
88                 {
89                         stackInt.Clear();
90
91                         AssertEquals(0, stackInt.Count);
92                 }
93
94                 public void TestClone()
95                 {
96                         Stack clone = (Stack)stackInt.Clone();
97
98                         while (stackInt.Count > 0)
99                         {
100                                 AssertEquals(stackInt.Pop(), clone.Pop());
101                         }
102                 }
103
104                 public void TestContains()
105                 {
106                         string toLocate = "test";
107
108                         stackInt.Push(toLocate);
109
110                         stackInt.Push("chaff");
111
112                         Assert(stackInt.Contains(toLocate));
113
114                         stackInt.Pop();
115
116                         Assert(stackInt.Contains(toLocate));
117
118                         stackInt.Pop();
119
120                         Assert(!stackInt.Contains(toLocate));
121                 }
122
123                 public void TestCopyTo()
124                 {
125                         int[] arr = new int[stackInt.Count - 1];
126                         int[,] arrMulti;
127
128                         try 
129                         {
130                                 stackInt.CopyTo(null, 0);
131                                 Fail("Should throw an ArgumentNullException");
132                         } 
133                         catch (ArgumentNullException) {}
134
135                         try
136                         {
137                                 stackInt.CopyTo(arr, -1);
138                                 Fail("Should throw an ArgumentOutOfRangeException");
139                         } 
140                         catch (ArgumentOutOfRangeException) {}
141
142                         try
143                         {
144                                 stackInt.CopyTo(arrMulti = new int[1, 1], 1);
145                                 Fail("Should throw an ArgumentException");
146                         } 
147                         catch (ArgumentException) {}
148
149                         try
150                         {
151                                 stackInt.CopyTo(arr = new int[2], 3);
152                                 Fail("Should throw an ArgumentException");
153                         } 
154                         catch (ArgumentException) {}
155
156                         try
157                         {
158                                 stackInt.CopyTo(arr = new int[3], 2);
159                                 Fail("Should throw an ArgumentException");
160                         } 
161                         catch (ArgumentException) {}
162
163                         try
164                         {
165                                 stackInt.CopyTo(arr = new int[2], 3);
166                                 Fail("Should throw an ArgumentException");
167                         } 
168                         catch (ArgumentException) {}
169
170                         arr = new int[stackInt.Count];
171
172                         stackInt.CopyTo(arr, 0);
173
174                         int j = 4;
175
176                         for (int i = 0; i < 4; i++)
177                         {
178                                AssertEquals(j--, arr[i]);
179                         }
180                 }
181
182                 public void TestSyncronized()
183                 {
184                         Stack syncStack = Stack.Synchronized(stackInt);
185
186                         syncStack.Push(5);
187
188                         for (int i = 5; i >= 0; i--)
189                                 AssertEquals(i, syncStack.Pop());
190                 }
191
192                 public void TestPushPeekPop()
193                 {
194                         stackInt.Pop();
195
196                         int topVal = (int)stackInt.Peek();
197
198                         AssertEquals(3, topVal);
199
200                         AssertEquals(4, stackInt.Count);
201
202                         AssertEquals(topVal, stackInt.Pop());
203
204                         AssertEquals(2, stackInt.Pop());
205
206                         Stack test = new Stack();
207                         test.Push(null);
208
209                         AssertEquals(null, test.Pop());
210                 }
211
212                 public void TestToArray()
213                 {
214                         object[] arr = stackInt.ToArray();
215
216                         AssertEquals(stackInt.Count, arr.Length);                       
217
218                         for (int i = 0; i < 5; i++)
219                                 AssertEquals(arr[i], stackInt.Pop());
220                 }
221
222                 protected override void SetUp()
223                 {
224                         stack1 = new Stack();
225                         stack2 = new Stack();
226
227                         stackInt = new Stack();
228     
229                         for (int i = 0; i < 5; i++)
230                                 stackInt.Push(i);
231                 }
232         }
233 }