Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System / Test / System.Collections.Generic / QueueTest.cs
1 //
2 // Queue.cs
3 //
4 // Author:
5 //  Ben Maurer (bmaurer@ximian.com)
6 //
7
8 #if NET_2_0
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.IO;
13 using System.Runtime.Serialization.Formatters.Binary;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Collections.Generic {
18         [TestFixture]
19         public class QueueTest
20         {
21                 [Test]
22                 public void TestCtor ()
23                 {
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);
29                 }
30                 
31                 [Test]
32                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
33                 public void TestCtorEx ()
34                 {
35                         Queue <int> a = new Queue <int> (-1);
36                 }
37                 
38                 [Test]
39                 public void TestCtorEnum ()
40                 {
41                         List <int> l = new List <int> ();
42                         l.Add (1);
43                         l.Add (2);
44                         l.Add (3);
45                         
46                         Queue <int> s = new Queue <int> (l);
47                         
48                         AssertDequeue (s, 1);
49                         AssertDequeue (s, 2);
50                         AssertDequeue (s, 3);
51                 }
52                 
53                 [Test]
54                 [ExpectedException (typeof (ArgumentNullException))]
55                 public void TestCtorEnumNull ()
56                 {
57                         Queue <int> s = new Queue <int> (null);
58                 }
59                 
60                 [Test]
61                 public void TestClear()
62                 {
63                         Queue <int> s = new Queue <int> ();
64                         s.Clear ();
65                         
66                         Assert.AreEqual (0, s.Count, "#1");
67                         
68                         s.Enqueue (1);
69                         s.Enqueue (2);
70                         
71                         Assert.AreEqual (2, s.Count, "#2");
72                         
73                         s.Clear ();
74                         
75                         Assert.AreEqual (0, s.Count, "#3");
76
77                         IEnumerator enumerator = s.GetEnumerator();
78                         s.Clear();
79
80                         try {
81                                 enumerator.Reset();
82                                 Assert.Fail ("#4");
83                         } catch(InvalidOperationException) {
84                         }
85                 }
86                 
87                 [Test]
88                 public void TestContains ()
89                 {
90                         Stack <int> s = new Stack <int> ();
91                         
92                         Assert.IsFalse (s.Contains (1), "#1");
93                         
94                         s.Push (1);
95                         
96                         Assert.IsTrue (s.Contains (1), "#2");
97                         Assert.IsFalse (s.Contains (0), "#3");
98                 }
99
100                 [Test]
101                 public void TestCopyTo ()
102                 {
103                         int [] x = new int [3];
104                         Queue <int> z = new Queue <int> ();
105                         z.Enqueue (1);
106                         z.Enqueue (2);
107                         x [0] = 10;
108                         z.CopyTo (x, 1);
109                         
110                         Assert.AreEqual (10, x [0], "#1");
111                         Assert.AreEqual (1, x [1], "#2");
112                         Assert.AreEqual (2, x [2], "#3");
113                         
114                         z = new Queue <int> ();
115                         x = new int [z.Count];
116                         z.CopyTo (x, 0);
117                 }
118
119                 [Test]
120                 public void TestICollectionCopyTo ()
121                 {
122                         var queue = new Queue<int> ();
123
124                         ((ICollection) queue).CopyTo (new int [0], 0);
125
126                         queue.Enqueue (1);
127                         queue.Enqueue (2);
128
129                         var array = new int [queue.Count];
130
131                         ((ICollection) queue).CopyTo (array, 0);
132
133                         Assert.AreEqual (1, array [0]);
134                         Assert.AreEqual (2, array [1]);
135
136                         array = new int [queue.Count + 1];
137                         array [0] = 42;
138
139                         ((ICollection) queue).CopyTo (array, 1);
140
141                         Assert.AreEqual (42, array [0]);
142                         Assert.AreEqual (1, array [1]);
143                         Assert.AreEqual (2, array [2]);
144                 }
145
146                 [Test]
147                 public void TestPeek ()
148                 {
149                         Queue <int> s = new Queue <int> ();
150                         s.Enqueue (1);
151                         
152                         Assert.AreEqual (1, s.Peek (), "#1");
153                         Assert.AreEqual (1, s.Count, "#2");
154                 }
155                 
156                 [Test]
157                 [ExpectedException (typeof (InvalidOperationException))]
158                 public void TestPeekEx ()
159                 {
160                         Queue <int> s = new Queue <int> ();
161                         s.Peek ();
162                 }
163                 
164                 [Test]
165                 [ExpectedException (typeof (InvalidOperationException))]
166                 public void TestPeekEx2 ()
167                 {
168                         Queue <int> s = new Queue <int> ();
169                         s.Enqueue (1);
170                         s.Dequeue ();
171                         s.Peek ();
172                 }
173                 
174                 [Test]
175                 public void TestDequeue ()
176                 {
177                         Queue <int> s = new Queue <int> ();
178                         s.Enqueue (1);
179                         
180                         Assert.AreEqual (1, s.Dequeue (), "#1");
181                         Assert.AreEqual (0, s.Count, "#2");
182                 }
183                 
184                 [Test]
185                 [ExpectedException (typeof (InvalidOperationException))]
186                 public void TestDequeueEx ()
187                 {
188                         Queue <int> s = new Queue <int> ();
189                         s.Dequeue ();
190                 }
191                 
192                 [Test]
193                 [ExpectedException (typeof (InvalidOperationException))]
194                 public void TestDequeueEx2 ()
195                 {
196                         Queue <int> s = new Queue <int> ();
197                         s.Enqueue (1);
198                         s.Dequeue ();
199                         s.Dequeue ();
200                 }
201                 
202                 [Test]
203                 public void TestEnqueue ()
204                 {
205                         Queue <int> s = new Queue <int> ();
206                         s.Enqueue (1);
207                         Assert.AreEqual (1, s.Count, "#1");
208                         s.Enqueue (2);
209                         Assert.AreEqual (2, s.Count, "#2");
210                         
211                         for (int i = 0; i < 100; i ++)
212                                 s.Enqueue (i);
213                         
214                         Assert.AreEqual (102, s.Count, "#3");
215                 }
216                 
217                 [Test]
218                 public void TestToArray ()
219                 {
220                         Queue <int> s = new Queue <int> ();
221                         
222                         int [] x = s.ToArray ();
223                         
224                         Assert.AreEqual (0, x.Length, "#1");
225                         
226                         s.Enqueue (1);
227                         x = s.ToArray ();
228                         Assert.AreEqual (1, x.Length, "#2");
229                         Assert.AreEqual (1, x [0], "#3");
230                 }
231
232                 [Test]
233                 public void TestEnumerator ()
234                 {
235                         Queue <int> s = new Queue <int> ();
236                         
237                         foreach (int x in s)
238                                 Assert.Fail ("#1" + x);
239                         
240                         s.Enqueue (1);
241                         
242                         int i = 0;
243                         
244                         foreach (int x in s) {
245                                 Assert.AreEqual  (0, i, "#2");
246                                 Assert.AreEqual  (1, x, "#3");
247                                 i ++;
248                         }
249                         
250                         for (i = 2; i < 100; i ++)
251                                 s.Enqueue (i);
252                         
253                         i = 1;
254                         
255                         foreach (int x in s) {
256                                 Assert.AreEqual (i, x, "#4");
257                                 i ++;
258                         }
259                 }
260
261                 [Test]
262                 public void TrimExcessTest ()
263                 {
264                         Queue <int> s = new Queue <int> ();
265                         s.TrimExcess ();
266                         Assert.AreEqual (0, s.Count, "#1");
267
268                         s.Enqueue (1);
269                         s.Enqueue (3);
270                         Assert.AreEqual (1, s.Dequeue (), "#2");
271                         Assert.AreEqual (3, s.Peek (), "#3");
272
273                         s.TrimExcess ();
274                         Assert.AreEqual (1, s.Count, "#4");
275                         Assert.AreEqual (3, s.Peek (), "#5");
276
277                         s.Enqueue (2);
278                         Assert.AreEqual (3, s.Dequeue (), "#6");
279                         Assert.AreEqual (2, s.Dequeue (), "#7");
280
281                         s.TrimExcess ();
282                         Assert.AreEqual (0, s.Count, "#8");
283                 }
284
285                 [Test]
286                 public void TrimExcessDequeueEnqueue ()
287                 {
288                         var queue = new Queue<int> ();
289                         queue.Enqueue (1);
290                         queue.Enqueue (2);
291                         queue.Enqueue (3);
292
293                         queue.TrimExcess ();
294
295                         Assert.AreEqual (1, queue.Dequeue ());
296
297                         queue.Enqueue (4);
298
299                         Assert.AreEqual (2, queue.Dequeue ());
300                         Assert.AreEqual (3, queue.Dequeue ());
301                         Assert.AreEqual (4, queue.Dequeue ());
302
303                         Assert.AreEqual (0, queue.Count);
304                 }
305
306                 [Test]
307                 [Category ("NotWorking")] // bug #80649
308                 public void SerializeTest ()
309                 {
310                         Queue <int> s = new Queue <int> ();
311                         s.Enqueue (1);
312                         s.Enqueue (3);
313                         s.Enqueue (2);
314                         s.Dequeue ();
315
316                         BinaryFormatter bf = new BinaryFormatter ();
317                         MemoryStream ms = new MemoryStream ();
318                         bf.Serialize (ms, s);
319
320                         byte [] buffer = new byte [ms.Length];
321                         ms.Position = 0;
322                         ms.Read (buffer, 0, buffer.Length);
323
324                         Assert.AreEqual (_serializedQueue, buffer);
325                 }
326
327                 [Test]
328                 public void DeserializeTest ()
329                 {
330                         MemoryStream ms = new MemoryStream ();
331                         ms.Write (_serializedQueue, 0, _serializedQueue.Length);
332                         ms.Position = 0;
333
334                         BinaryFormatter bf = new BinaryFormatter ();
335                         Queue<int> s = (Queue<int>) bf.Deserialize (ms);
336                         Assert.AreEqual (2, s.Count, "#1");
337                         Assert.AreEqual (3, s.Dequeue (), "#2");
338                         Assert.AreEqual (2, s.Dequeue (), "#3");
339                 }
340
341                 void AssertDequeue <T> (Queue <T> s, T t)
342                 {
343                         Assert.AreEqual (t, s.Dequeue ());
344                 }
345
346                 static byte [] _serializedQueue = new byte [] {
347                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
348                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
349                         0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
350                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
351                         0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
352                         0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
353                         0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
354                         0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
355                         0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
356                         0x00, 0x00, 0x7f, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43,
357                         0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
358                         0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x51, 0x75, 0x65,
359                         0x75, 0x65, 0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65,
360                         0x6d, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73,
361                         0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72,
362                         0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
363                         0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d,
364                         0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75,
365                         0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65,
366                         0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31,
367                         0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x05, 0x00,
368                         0x00, 0x00, 0x06, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x05, 0x5f,
369                         0x68, 0x65, 0x61, 0x64, 0x05, 0x5f, 0x74, 0x61, 0x69, 0x6c, 0x05,
370                         0x5f, 0x73, 0x69, 0x7a, 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73,
371                         0x69, 0x6f, 0x6e, 0x07, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08,
372                         0x08, 0x08, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00,
373                         0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
374                         0x00, 0x05, 0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x04,
375                         0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
376                         0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b };
377         }
378 }
379 #endif