5 // Ben Maurer (bmaurer@ximian.com)
9 using System.Collections;
10 using System.Collections.Generic;
12 using System.Runtime.Serialization.Formatters.Binary;
14 using NUnit.Framework;
16 namespace MonoTests.System.Collections.Generic
19 public class StackTest
22 public void TestCtor ()
24 Stack <int> a = new Stack <int> ();
25 Stack <int> b = new Stack <int> (1);
26 Stack <object> c = new Stack <object> ();
27 Stack <object> d = new Stack <object> (1);
28 Stack <object> e = new Stack <object> (0);
32 [ExpectedException (typeof (ArgumentOutOfRangeException))]
33 public void TestCtorEx ()
39 public void TestCtorEnum ()
41 List <int> l = new List <int> ();
46 Stack <int> s = new Stack <int> (l);
48 // Things get pop'd in reverse
55 [ExpectedException (typeof (ArgumentNullException))]
56 public void TestCtorEnumNull ()
58 new Stack <int> (null);
62 public void TestClear()
64 Stack <int> s = new Stack <int> ();
67 Assert.AreEqual (0, s.Count, "#1");
72 Assert.AreEqual (2, s.Count, "#2");
76 Assert.AreEqual (0, s.Count, "#3");
80 public void TestContains ()
82 Stack <int> s = new Stack <int> ();
84 Assert.IsFalse (s.Contains (1), "#1");
88 Assert.IsTrue (s.Contains (1), "#2");
89 Assert.IsFalse (s.Contains (0), "#3");
93 public void TestCopyTo ()
95 int [] x = new int [3];
96 Stack <int> z = new Stack <int> ();
102 Assert.AreEqual (10, x [0], "#1");
103 Assert.AreEqual (2, x [1], "#2");
104 Assert.AreEqual (1, x [2], "#3");
106 z = new Stack <int> ();
107 x = new int [z.Count];
110 ICollection c = new Stack <int> ();
111 x = new int [c.Count];
116 public void TestPeek ()
118 Stack <int> s = new Stack <int> ();
121 Assert.AreEqual (1, s.Peek (), "#1");
122 Assert.AreEqual (1, s.Count, "#2");
124 IEnumerator enumerator = s.GetEnumerator();
130 [ExpectedException (typeof (InvalidOperationException))]
131 public void TestPeekEx ()
133 Stack <int> s = new Stack <int> ();
138 [ExpectedException (typeof (InvalidOperationException))]
139 public void TestPeekEx2 ()
141 Stack <int> s = new Stack <int> ();
148 public void TestPop ()
150 Stack <int> s = new Stack <int> ();
153 Assert.AreEqual (1, s.Pop (), "#1");
154 Assert.AreEqual (0, s.Count, "#2");
158 [ExpectedException (typeof (InvalidOperationException))]
159 public void TestPopEx ()
161 Stack <int> s = new Stack <int> ();
166 [ExpectedException (typeof (InvalidOperationException))]
167 public void TestPopEx2 ()
169 Stack <int> s = new Stack <int> ();
176 public void TestPush ()
178 Stack <int> s = new Stack <int> ();
180 Assert.AreEqual (1, s.Count, "#1");
182 Assert.AreEqual (2, s.Count, "#2");
184 for (int i = 0; i < 100; i ++)
187 Assert.AreEqual (102, s.Count, "#3");
191 public void TestToArray ()
193 Stack <int> s = new Stack <int> ();
195 int [] x = s.ToArray ();
197 Assert.AreEqual (0, x.Length, "#1");
201 Assert.AreEqual (1, x.Length, "#2");
202 Assert.AreEqual (1, x [0], "#3");
206 public void TestEnumerator ()
208 Stack <int> s = new Stack <int> ();
211 Assert.Fail ("#1:" + x);
217 foreach (int x in s) {
218 Assert.AreEqual (0, i, "#2");
219 Assert.AreEqual (1, x, "#3");
228 foreach (int x in s) {
229 Assert.AreEqual (3 - i, x, "#4");
230 Assert.IsTrue (i < 3, "#5");
236 public void DisposeEnumerator ()
238 var stack = new Stack<int> ();
242 var enumerator = stack.GetEnumerator ();
243 Assert.IsTrue (enumerator.MoveNext ());
244 enumerator.Dispose ();
245 Assert.IsFalse (enumerator.MoveNext ());
249 public void TrimExcessTest ()
251 Stack <int> s = new Stack <int> ();
253 Assert.AreEqual (0, s.Count, "#1");
257 Assert.AreEqual (3, s.Pop (), "#2");
258 Assert.AreEqual (1, s.Peek (), "#3");
261 Assert.AreEqual (1, s.Count, "#4");
262 Assert.AreEqual (1, s.Peek (), "#5");
265 Assert.AreEqual (2, s.Pop (), "#6");
266 Assert.AreEqual (1, s.Pop (), "#7");
269 Assert.AreEqual (0, s.Count, "#8");
273 [Category ("NotWorking")] // bug #80649
274 public void SerializeTest ()
276 Stack <int> s = new Stack <int> ();
282 BinaryFormatter bf = new BinaryFormatter ();
283 MemoryStream ms = new MemoryStream ();
284 bf.Serialize (ms, s);
286 byte [] buffer = new byte [ms.Length];
288 ms.Read (buffer, 0, buffer.Length);
290 Assert.AreEqual (_serializedStack, buffer);
294 public void DeserializeTest ()
296 MemoryStream ms = new MemoryStream ();
297 ms.Write (_serializedStack, 0, _serializedStack.Length);
300 BinaryFormatter bf = new BinaryFormatter ();
301 Stack<int> s = (Stack<int>) bf.Deserialize (ms);
302 Assert.AreEqual (2, s.Count, "#1");
303 Assert.AreEqual (3, s.Pop (), "#2");
304 Assert.AreEqual (1, s.Pop (), "#3");
307 void AssertPop <T> (Stack <T> s, T t)
309 Assert.AreEqual (t, s.Pop ());
312 static byte [] _serializedStack = new byte [] {
313 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
315 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
316 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
317 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
318 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
319 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
320 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
321 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
322 0x00, 0x00, 0x7f, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43,
323 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
324 0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x74, 0x61,
325 0x63, 0x6b, 0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65,
326 0x6d, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73,
327 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72,
328 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
329 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d,
330 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75,
331 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65,
332 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31,
333 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00,
334 0x00, 0x00, 0x06, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x05, 0x5f,
335 0x73, 0x69, 0x7a, 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69,
336 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x08, 0x08, 0x08, 0x02, 0x00, 0x00,
337 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04,
338 0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
339 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
340 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b };