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);
296 [Category ("NotWorking")]
298 public void DeserializeTest ()
300 MemoryStream ms = new MemoryStream ();
301 ms.Write (_serializedStack, 0, _serializedStack.Length);
304 BinaryFormatter bf = new BinaryFormatter ();
305 Stack<int> s = (Stack<int>) bf.Deserialize (ms);
306 Assert.AreEqual (2, s.Count, "#1");
307 Assert.AreEqual (3, s.Pop (), "#2");
308 Assert.AreEqual (1, s.Pop (), "#3");
311 void AssertPop <T> (Stack <T> s, T t)
313 Assert.AreEqual (t, s.Pop ());
316 static byte [] _serializedStack = new byte [] {
317 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
319 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
320 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
321 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
322 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
323 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
324 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
325 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
326 0x00, 0x00, 0x7f, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43,
327 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
328 0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x74, 0x61,
329 0x63, 0x6b, 0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65,
330 0x6d, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73,
331 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72,
332 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
333 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d,
334 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75,
335 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65,
336 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31,
337 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00,
338 0x00, 0x00, 0x06, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x05, 0x5f,
339 0x73, 0x69, 0x7a, 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69,
340 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x08, 0x08, 0x08, 0x02, 0x00, 0x00,
341 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04,
342 0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
343 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b };