5 // Ben Maurer (bmaurer@ximian.com)
10 using System.Collections;
11 using System.Collections.Generic;
13 using System.Runtime.Serialization.Formatters.Binary;
15 using NUnit.Framework;
17 namespace MonoTests.System.Collections.Generic
20 public class StackTest
23 public void TestCtor ()
25 Stack <int> a = new Stack <int> ();
26 Stack <int> b = new Stack <int> (1);
27 Stack <object> c = new Stack <object> ();
28 Stack <object> d = new Stack <object> (1);
29 Stack <object> e = new Stack <object> (0);
33 [ExpectedException (typeof (ArgumentOutOfRangeException))]
34 public void TestCtorEx ()
40 public void TestCtorEnum ()
42 List <int> l = new List <int> ();
47 Stack <int> s = new Stack <int> (l);
49 // Things get pop'd in reverse
56 [ExpectedException (typeof (ArgumentNullException))]
57 public void TestCtorEnumNull ()
59 new Stack <int> (null);
63 public void TestClear()
65 Stack <int> s = new Stack <int> ();
68 Assert.AreEqual (0, s.Count, "#1");
73 Assert.AreEqual (2, s.Count, "#2");
77 Assert.AreEqual (0, s.Count, "#3");
81 public void TestContains ()
83 Stack <int> s = new Stack <int> ();
85 Assert.IsFalse (s.Contains (1), "#1");
89 Assert.IsTrue (s.Contains (1), "#2");
90 Assert.IsFalse (s.Contains (0), "#3");
94 public void TestCopyTo ()
96 int [] x = new int [3];
97 Stack <int> z = new Stack <int> ();
103 Assert.AreEqual (10, x [0], "#1");
104 Assert.AreEqual (2, x [1], "#2");
105 Assert.AreEqual (1, x [2], "#3");
107 z = new Stack <int> ();
108 x = new int [z.Count];
111 ICollection c = new Stack <int> ();
112 x = new int [c.Count];
117 public void TestPeek ()
119 Stack <int> s = new Stack <int> ();
122 Assert.AreEqual (1, s.Peek (), "#1");
123 Assert.AreEqual (1, s.Count, "#2");
125 IEnumerator enumerator = s.GetEnumerator();
131 [ExpectedException (typeof (InvalidOperationException))]
132 public void TestPeekEx ()
134 Stack <int> s = new Stack <int> ();
139 [ExpectedException (typeof (InvalidOperationException))]
140 public void TestPeekEx2 ()
142 Stack <int> s = new Stack <int> ();
149 public void TestPop ()
151 Stack <int> s = new Stack <int> ();
154 Assert.AreEqual (1, s.Pop (), "#1");
155 Assert.AreEqual (0, s.Count, "#2");
159 [ExpectedException (typeof (InvalidOperationException))]
160 public void TestPopEx ()
162 Stack <int> s = new Stack <int> ();
167 [ExpectedException (typeof (InvalidOperationException))]
168 public void TestPopEx2 ()
170 Stack <int> s = new Stack <int> ();
177 public void TestPush ()
179 Stack <int> s = new Stack <int> ();
181 Assert.AreEqual (1, s.Count, "#1");
183 Assert.AreEqual (2, s.Count, "#2");
185 for (int i = 0; i < 100; i ++)
188 Assert.AreEqual (102, s.Count, "#3");
192 public void TestToArray ()
194 Stack <int> s = new Stack <int> ();
196 int [] x = s.ToArray ();
198 Assert.AreEqual (0, x.Length, "#1");
202 Assert.AreEqual (1, x.Length, "#2");
203 Assert.AreEqual (1, x [0], "#3");
207 public void TestEnumerator ()
209 Stack <int> s = new Stack <int> ();
212 Assert.Fail ("#1:" + x);
218 foreach (int x in s) {
219 Assert.AreEqual (0, i, "#2");
220 Assert.AreEqual (1, x, "#3");
229 foreach (int x in s) {
230 Assert.AreEqual (3 - i, x, "#4");
231 Assert.IsTrue (i < 3, "#5");
237 public void DisposeEnumerator ()
239 var stack = new Stack<int> ();
243 var enumerator = stack.GetEnumerator ();
244 Assert.IsTrue (enumerator.MoveNext ());
245 enumerator.Dispose ();
246 Assert.IsFalse (enumerator.MoveNext ());
250 public void TrimExcessTest ()
252 Stack <int> s = new Stack <int> ();
254 Assert.AreEqual (0, s.Count, "#1");
258 Assert.AreEqual (3, s.Pop (), "#2");
259 Assert.AreEqual (1, s.Peek (), "#3");
262 Assert.AreEqual (1, s.Count, "#4");
263 Assert.AreEqual (1, s.Peek (), "#5");
266 Assert.AreEqual (2, s.Pop (), "#6");
267 Assert.AreEqual (1, s.Pop (), "#7");
270 Assert.AreEqual (0, s.Count, "#8");
274 [Category ("NotWorking")] // bug #80649
275 public void SerializeTest ()
277 Stack <int> s = new Stack <int> ();
283 BinaryFormatter bf = new BinaryFormatter ();
284 MemoryStream ms = new MemoryStream ();
285 bf.Serialize (ms, s);
287 byte [] buffer = new byte [ms.Length];
289 ms.Read (buffer, 0, buffer.Length);
291 Assert.AreEqual (_serializedStack, buffer);
295 public void DeserializeTest ()
297 MemoryStream ms = new MemoryStream ();
298 ms.Write (_serializedStack, 0, _serializedStack.Length);
301 BinaryFormatter bf = new BinaryFormatter ();
302 Stack<int> s = (Stack<int>) bf.Deserialize (ms);
303 Assert.AreEqual (2, s.Count, "#1");
304 Assert.AreEqual (3, s.Pop (), "#2");
305 Assert.AreEqual (1, s.Pop (), "#3");
308 void AssertPop <T> (Stack <T> s, T t)
310 Assert.AreEqual (t, s.Pop ());
313 static byte [] _serializedStack = new byte [] {
314 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
315 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
316 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
317 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
318 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
319 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
320 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
321 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
322 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
323 0x00, 0x00, 0x7f, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43,
324 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
325 0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x74, 0x61,
326 0x63, 0x6b, 0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65,
327 0x6d, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73,
328 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72,
329 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
330 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d,
331 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75,
332 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65,
333 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31,
334 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00,
335 0x00, 0x00, 0x06, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x05, 0x5f,
336 0x73, 0x69, 0x7a, 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69,
337 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x08, 0x08, 0x08, 0x02, 0x00, 0x00,
338 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04,
339 0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
340 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
341 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b };