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 {
19 public class QueueTest
22 public void TestCtor ()
24 Queue <int> a = new Queue <int> ();
25 Queue <int> b = new Queue <int> (1);
26 Queue <object> c = new Queue <object> ();
27 Queue <object> d = new Queue <object> (1);
28 Queue <object> e = new Queue <object> (0);
32 [ExpectedException (typeof (ArgumentOutOfRangeException))]
33 public void TestCtorEx ()
35 Queue <int> a = new Queue <int> (-1);
39 public void TestCtorEnum ()
41 List <int> l = new List <int> ();
46 Queue <int> s = new Queue <int> (l);
54 [ExpectedException (typeof (ArgumentNullException))]
55 public void TestCtorEnumNull ()
57 Queue <int> s = new Queue <int> (null);
61 public void TestClear()
63 Queue <int> s = new Queue <int> ();
66 Assert.AreEqual (0, s.Count, "#1");
71 Assert.AreEqual (2, s.Count, "#2");
75 Assert.AreEqual (0, s.Count, "#3");
77 IEnumerator enumerator = s.GetEnumerator();
83 } catch(InvalidOperationException) {
88 public void TestContains ()
90 Stack <int> s = new Stack <int> ();
92 Assert.IsFalse (s.Contains (1), "#1");
96 Assert.IsTrue (s.Contains (1), "#2");
97 Assert.IsFalse (s.Contains (0), "#3");
101 public void TestCopyTo ()
103 int [] x = new int [3];
104 Queue <int> z = new Queue <int> ();
110 Assert.AreEqual (10, x [0], "#1");
111 Assert.AreEqual (1, x [1], "#2");
112 Assert.AreEqual (2, x [2], "#3");
114 z = new Queue <int> ();
115 x = new int [z.Count];
120 public void TestICollectionCopyTo ()
122 var queue = new Queue<int> ();
124 ((ICollection) queue).CopyTo (new int [0], 0);
129 var array = new int [queue.Count];
131 ((ICollection) queue).CopyTo (array, 0);
133 Assert.AreEqual (1, array [0]);
134 Assert.AreEqual (2, array [1]);
136 array = new int [queue.Count + 1];
139 ((ICollection) queue).CopyTo (array, 1);
141 Assert.AreEqual (42, array [0]);
142 Assert.AreEqual (1, array [1]);
143 Assert.AreEqual (2, array [2]);
147 public void TestPeek ()
149 Queue <int> s = new Queue <int> ();
152 Assert.AreEqual (1, s.Peek (), "#1");
153 Assert.AreEqual (1, s.Count, "#2");
157 [ExpectedException (typeof (InvalidOperationException))]
158 public void TestPeekEx ()
160 Queue <int> s = new Queue <int> ();
165 [ExpectedException (typeof (InvalidOperationException))]
166 public void TestPeekEx2 ()
168 Queue <int> s = new Queue <int> ();
175 public void TestDequeue ()
177 Queue <int> s = new Queue <int> ();
180 Assert.AreEqual (1, s.Dequeue (), "#1");
181 Assert.AreEqual (0, s.Count, "#2");
185 [ExpectedException (typeof (InvalidOperationException))]
186 public void TestDequeueEx ()
188 Queue <int> s = new Queue <int> ();
193 [ExpectedException (typeof (InvalidOperationException))]
194 public void TestDequeueEx2 ()
196 Queue <int> s = new Queue <int> ();
203 public void TestEnqueue ()
205 Queue <int> s = new Queue <int> ();
207 Assert.AreEqual (1, s.Count, "#1");
209 Assert.AreEqual (2, s.Count, "#2");
211 for (int i = 0; i < 100; i ++)
214 Assert.AreEqual (102, s.Count, "#3");
218 public void TestToArray ()
220 Queue <int> s = new Queue <int> ();
222 int [] x = s.ToArray ();
224 Assert.AreEqual (0, x.Length, "#1");
228 Assert.AreEqual (1, x.Length, "#2");
229 Assert.AreEqual (1, x [0], "#3");
233 public void TestEnumerator ()
235 Queue <int> s = new Queue <int> ();
238 Assert.Fail ("#1" + x);
244 foreach (int x in s) {
245 Assert.AreEqual (0, i, "#2");
246 Assert.AreEqual (1, x, "#3");
250 for (i = 2; i < 100; i ++)
255 foreach (int x in s) {
256 Assert.AreEqual (i, x, "#4");
262 public void TrimExcessTest ()
264 Queue <int> s = new Queue <int> ();
266 Assert.AreEqual (0, s.Count, "#1");
270 Assert.AreEqual (1, s.Dequeue (), "#2");
271 Assert.AreEqual (3, s.Peek (), "#3");
274 Assert.AreEqual (1, s.Count, "#4");
275 Assert.AreEqual (3, s.Peek (), "#5");
278 Assert.AreEqual (3, s.Dequeue (), "#6");
279 Assert.AreEqual (2, s.Dequeue (), "#7");
282 Assert.AreEqual (0, s.Count, "#8");
286 public void TrimExcessDequeueEnqueue ()
288 var queue = new Queue<int> ();
295 Assert.AreEqual (1, queue.Dequeue ());
299 Assert.AreEqual (2, queue.Dequeue ());
300 Assert.AreEqual (3, queue.Dequeue ());
301 Assert.AreEqual (4, queue.Dequeue ());
303 Assert.AreEqual (0, queue.Count);
307 [Category ("NotWorking")] // bug #80649
308 public void SerializeTest ()
310 Queue <int> s = new Queue <int> ();
316 BinaryFormatter bf = new BinaryFormatter ();
317 MemoryStream ms = new MemoryStream ();
318 bf.Serialize (ms, s);
320 byte [] buffer = new byte [ms.Length];
322 ms.Read (buffer, 0, buffer.Length);
324 Assert.AreEqual (_serializedQueue, buffer);
329 [Category ("NotWorking")]
331 public void DeserializeTest ()
333 MemoryStream ms = new MemoryStream ();
334 ms.Write (_serializedQueue, 0, _serializedQueue.Length);
337 BinaryFormatter bf = new BinaryFormatter ();
338 Queue<int> s = (Queue<int>) bf.Deserialize (ms);
339 Assert.AreEqual (2, s.Count, "#1");
340 Assert.AreEqual (3, s.Dequeue (), "#2");
341 Assert.AreEqual (2, s.Dequeue (), "#3");
344 void AssertDequeue <T> (Queue <T> s, T t)
346 Assert.AreEqual (t, s.Dequeue ());
349 static byte [] _serializedQueue = new byte [] {
350 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
351 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
352 0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
353 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
354 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
355 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
356 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
357 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
358 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
359 0x00, 0x00, 0x7f, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43,
360 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
361 0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x51, 0x75, 0x65,
362 0x75, 0x65, 0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65,
363 0x6d, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73,
364 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72,
365 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
366 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d,
367 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75,
368 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65,
369 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31,
370 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x05, 0x00,
371 0x00, 0x00, 0x06, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x05, 0x5f,
372 0x68, 0x65, 0x61, 0x64, 0x05, 0x5f, 0x74, 0x61, 0x69, 0x6c, 0x05,
373 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73,
374 0x69, 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08,
375 0x08, 0x08, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00,
376 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
377 0x00, 0x05, 0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x04,
378 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
379 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b };