[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / corlib / Test / System.Collections / QueueTest.cs
1 //
2 // System.Collections.QueueTest
3 // Test suite for System.Collections.Queue
4 //
5 // Author:
6 //    Ricardo Fernández Pascual
7 //
8 // (C) 2001 Ricardo Fernández Pascual
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using System;
13 using System.Collections;
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Collections {
17
18         [TestFixture]
19         public class QueueTest {
20
21                 protected Queue q1;
22                 protected Queue q2;
23                 protected Queue emptyQueue;
24
25                 [SetUp]
26                 protected void SetUp () 
27                 {
28                         q1 = new Queue (10);
29                         for (int i = 0; i < 100; i++)
30                                 q1.Enqueue (i);
31                         
32                         q2 = new Queue (50, 1.5f);
33                         for (int i = 50; i < 100; i++)
34                                 q2.Enqueue (i);
35
36                         emptyQueue = new Queue ();
37                 }
38
39                 [Test]
40                 public void TestConstructorException1 () 
41                 {
42                         try 
43                         {
44                                 Queue q = new Queue(-1, 2);
45                                 Assert.Fail ("Should throw an exception");
46                         } catch (ArgumentOutOfRangeException e) {
47                                 Assert.AreEqual ("capacity", e.ParamName, "Exception's ParamName must be \"capacity\"");
48                         }
49                 }
50
51                 [Test]
52                 public void TestConstructorException2 () 
53                 {
54                         try 
55                         {
56                                 Queue q = new Queue(10, 0);
57                                 Assert.Fail ("Should throw an exception because growFactor < 1");
58                         } 
59                         catch (ArgumentOutOfRangeException e) 
60                         {
61                                 Assert.AreEqual ("growFactor", e.ParamName, "Exception's ParamName must be \"growFactor\"");
62                         }
63                 }
64
65                 [Test]
66                 public void TestConstructorException3 () 
67                 {
68                         try 
69                         {
70                                 Queue q = new Queue(10, 11);
71                                 Assert.Fail ("Should throw an exception because growFactor > 10");
72                         } 
73                         catch (ArgumentOutOfRangeException e) 
74                         {
75                                 Assert.AreEqual ("growFactor", e.ParamName, "Exception's ParamName must be \"growFactor\"");
76                         }
77                 }
78
79                 [Test]
80                 public void TestConstructorException4 () 
81                 {
82                         try 
83                         {
84                                 Queue q = new Queue(null);
85                                 Assert.Fail ("Should throw an exception because col == null");
86                         } 
87                         catch (ArgumentNullException e) 
88                         {
89                                 Assert.AreEqual ("col", e.ParamName, "Exception's ParamName must be \"col\"");
90                         }
91                 }
92
93                 [Test]
94                 public void TestICollectionConstructor () 
95                 {
96                         Queue q = new Queue(new int[] {1, 2, 3, 4, 5});
97                         Assert.AreEqual (5, q.Count, "count");
98                         for (int i=1; i <=5; i++) 
99                         {
100                                 Assert.AreEqual (q.Dequeue(), i);
101                         }
102                 }
103
104                 [Test]
105                 public void TestConstructors () 
106                 {
107                         Assert.IsTrue (q1.Count == 100);
108                         Assert.IsTrue (q2.Count == 50);
109                         Assert.IsTrue (emptyQueue.Count == 0);
110                 }
111
112                 [Test]
113                 public void TestCount() 
114                 {
115                         Assert.AreEqual (100, q1.Count, "Count #1");
116                         for (int i = 1; i <=50; i ++) 
117                         {
118                                 q1.Dequeue();
119                         }
120                         Assert.AreEqual (50, q1.Count, "Count #2");
121                         for (int i = 1; i <=50; i ++) 
122                         {
123                                 q1.Enqueue(i);
124                         }
125                         Assert.AreEqual (100, q1.Count, "Count #3");
126
127                         Assert.AreEqual (50, q2.Count, "Count #4");
128
129                         Assert.AreEqual (0, emptyQueue.Count, "Count #5");
130                 }
131
132                 [Test]
133                 public void TestIsSynchronized() 
134                 {
135                         Assert.IsTrue (!q1.IsSynchronized, "IsSynchronized should be false");
136                         Assert.IsTrue (!q2.IsSynchronized, "IsSynchronized should be false");
137                         Assert.IsTrue (!emptyQueue.IsSynchronized, "IsSynchronized should be false");
138                 }
139
140                 [Test]
141                 public void TestSyncRoot() 
142                 {
143
144                         Queue q1sync = Queue.Synchronized(q1);
145                         Assert.AreNotSame (q1, q1sync.SyncRoot, "SyncRoot value of a synchronized queue");
146                 }
147
148                 [Test]
149                 public void TestCopyToException1 () 
150                 {
151                         try 
152                         {
153                                 q1.CopyTo(null, 1);
154                                 Assert.Fail ("must throw ArgumentNullException");
155                         } catch (ArgumentNullException e) {
156                                 Assert.AreEqual ("array", e.ParamName, "Exception's ParamName must be \"array\"");
157                         }
158                 }
159
160
161                 [Test]
162                 public void TestCopyToException2 () 
163                 {
164                         try 
165                         {
166                                 q1.CopyTo(new int[2,2], 1);
167                                 Assert.Fail ("must throw ArgumentException");
168                         } 
169                         catch (ArgumentException) 
170                         {
171                         }
172                 }
173
174                 [Test]
175                 public void TestCopyToException3 () 
176                 {
177                         try 
178                         {
179                                 q1.CopyTo(new int[3], -1);
180                                 Assert.Fail ("must throw ArgumentOutOfRangeException");
181                         } 
182                         catch (ArgumentOutOfRangeException e) 
183                         {
184                                 Assert.AreEqual ("index", e.ParamName, "Exception's ParamName must be \"index\"");
185                         }
186                 }
187
188                 [Test]
189                 public void TestCopyToException4 () 
190                 {
191                         try 
192                         {
193                                 q1.CopyTo(new int[3], 1);
194                                 Assert.Fail ("must throw ArgumentException");
195                         } 
196                         catch (ArgumentException) {}
197                 }
198
199
200                 [Test]
201                 public void TestCopyTo () 
202                 {
203                         int[] a1 = new int[100];
204                         int[] a2 = new int[60];
205
206                         string progress_marker = "";
207                         try {
208                                 progress_marker = "before first CopyTo";
209                                 q1.CopyTo (a1, 0);
210                                 for (int i = 0; i < 100; i++)
211                                         Assert.AreEqual (a1[i], i);
212
213                                 // Remove some items from q2 and add other 
214                                 // items, to avoid having  an "easy" just created
215                                 // Queue
216                                 for (int i = 50; i < 60; i++)
217                                         Assert.IsTrue (i == (int) q2.Dequeue ());
218                                 for (int i = 100; i < 110; i++)
219                                         q2.Enqueue (i);
220                                 
221                                 progress_marker = "before second CopyTo";
222                                 q2.CopyTo (a2, 10);
223                                 for (int i = 60; i < 110; i++)
224                                         Assert.IsTrue (i == a2[i - 60 + 10]);
225                                 
226                                 // Copying an empty Queue should not modify the array
227                                 progress_marker = "before third CopyTo";
228                                 emptyQueue.CopyTo (a2, 10);
229                                 for (int i = 60; i < 110; i++)
230                                         Assert.IsTrue (i == a2[i - 60 + 10]);
231                         } catch (Exception e) {
232                                 Assert.Fail ("Unexpected exception at marker <" + progress_marker + ">: e = " + e);
233                         }
234
235                 }
236
237                 [Test]
238                 public void TestEnumerator () {
239                         int i;
240                         IEnumerator e;
241                         e = q1.GetEnumerator ();
242                         i = 0;
243                         while (e.MoveNext ()) {
244                                 Assert.AreEqual (i, ((int) e.Current), "q1 at i=" + i);
245                                 i++;
246                         }
247                         e = q2.GetEnumerator ();
248                         i = 50;
249                         while (e.MoveNext ()) {
250                                 Assert.AreEqual (((int) e.Current), i);
251                                 i++;
252                         }
253                         e = emptyQueue.GetEnumerator ();
254                         if (e.MoveNext ())
255                                 Assert.Fail ("Empty Queue enumerator returning elements!");
256
257                         e = q1.GetEnumerator ();
258                         try {
259                                 e.MoveNext ();
260                                 q1.Enqueue (0);
261                                 e.MoveNext ();
262                                 Assert.Fail ("#1 Should have thrown InvalidOperationException");
263                         } catch (InvalidOperationException) { }
264                         e = q1.GetEnumerator ();
265                 }
266
267                 [Test]
268                 public void TestEnumeratorException1 () 
269                 {
270                         IEnumerator e;
271
272                         e = q1.GetEnumerator();
273                         q1.Enqueue(6);
274                         try {
275                                 e.MoveNext();
276                                 Assert.Fail ("MoveNext must throw InvalidOperationException after Enqueue");
277                         } catch (InvalidOperationException) {}
278
279
280                         e = q1.GetEnumerator();
281                         q1.Enqueue(6);
282                         try 
283                         {
284                                 e.Reset();
285                                 Assert.Fail ("Reset must throw InvalidOperationException after Enqueue");
286                         } 
287                         catch (InvalidOperationException) {}
288
289                         e = q1.GetEnumerator();
290                         q1.TrimToSize();
291                         try 
292                         {
293                                 e.Reset();
294                                 Assert.Fail ("Reset must throw InvalidOperationException after TrimToSize");
295                         } 
296                         catch (InvalidOperationException) {}
297
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (InvalidOperationException))]
302                 public void EnumeratorCurrentAfterMoveNextAll () 
303                 {
304                         IEnumerator e = q1.GetEnumerator();
305                         while (e.MoveNext ()) {
306                         }
307                         Assert.IsNotNull (e.Current);
308                 }
309
310                 [Test]
311                 public void EnumeratorFalseAfterMoveNextAll () 
312                 {
313                         IEnumerator e = q1.GetEnumerator();
314                         while (e.MoveNext ()) {
315                         }
316                         Assert.IsTrue (!e.MoveNext ());
317                 }
318
319                 [Test]
320                 public void TestClone () {
321                         Queue q3 = (Queue) q2.Clone ();
322                         Assert.IsTrue (q3.Count == q2.Count);
323                         for (int i = 0; i < 50; i++)
324                                 Assert.IsTrue (q2.Dequeue ().Equals (q3.Dequeue ()));
325                         Assert.IsTrue (q3.Count == 0);
326                         Assert.IsTrue (q2.Count == 0);
327                 }
328
329                 [Test]
330                 public void TestClear () {
331                         q1.Clear ();
332                         Assert.IsTrue (q1.Count == 0);
333                         q2.Clear ();
334                         Assert.IsTrue (q2.Count == 0);
335                         emptyQueue.Clear ();
336                         Assert.IsTrue (emptyQueue.Count == 0);
337                 }
338
339                 [Test]
340                 public void TestContains () {
341                         for (int i = 0; i < 100; i++) {
342                                 Assert.IsTrue (q1.Contains (i));
343                                 Assert.IsTrue (!emptyQueue.Contains (i));
344                                 if (i < 50)
345                                         Assert.IsTrue (!q2.Contains (i));
346                                 else
347                                         Assert.IsTrue (q2.Contains (i));
348                         }
349                         
350                         Assert.IsTrue (!q1.Contains(null), "q1 does not contain null");
351                         q1.Enqueue(null);
352                         Assert.IsTrue (q1.Contains(null), "q1 contains null");
353                 }
354                 
355                 [Test]
356                 public void TestEnqueueDequeuePeek () {
357                         int q1size = q1.Count;
358                         int q2size = q2.Count;
359                         q2.Enqueue (null);
360                         Assert.IsTrue (q2.Count == ++q2size);
361                         for (int i = 0; i < 50; i++) {
362                                 int k = (int) q1.Peek ();
363                                 Assert.IsTrue (q1.Count == q1size);
364                                 int j = (int) q1.Dequeue ();
365                                 Assert.IsTrue (q1.Count == --q1size);
366                                 Assert.IsTrue (i == j);
367                                 Assert.IsTrue (j == k);
368                                 q2.Enqueue (j);
369                                 Assert.IsTrue (q2.Count == ++q2size);
370                         }
371                         for (int i = 50; i < 100; i++) {
372                                 Assert.IsTrue (((int) q2.Dequeue ()) == i);
373                                 Assert.IsTrue (q2.Count == --q2size);
374                         }
375                         Assert.IsTrue (q2.Peek () == null);
376                         Assert.IsTrue (q2.Dequeue () == null);
377                         Assert.IsTrue (q2.Count == --q2size);
378                         for (int i = 0; i < 50; i++) {
379                                 Assert.IsTrue (((int) q2.Dequeue ()) == i);
380                                 Assert.IsTrue (q2.Count == --q2size);
381                         }
382                 }
383                 
384                 [Test]
385                 public void TestDequeue() {
386                         Queue queue = new Queue();
387                         string[] tmp = new string[50];
388                         int i;
389                         for (i=0;i<50;i++) {
390                                 tmp[i] = "Data #" + i;
391                                 queue.Enqueue(tmp[i]);
392                         }
393                         
394                         i = 0;
395                         while(queue.Count>0){
396                                 string z = (string) queue.Dequeue();
397                                 Assert.AreEqual (tmp[i], z, tmp[i]);
398                                 i++;
399                         }
400                 }
401
402                 [Test]
403                 [ExpectedException(typeof(InvalidOperationException))]
404                 public void TestDequeueEmpty() 
405                 {
406                         Queue q= new Queue();
407                         q.Dequeue();
408                 }
409
410                 [Test]
411                 public void TestToArray() 
412                 {
413                         object[] a = q1.ToArray();
414                         for (int i = 0; i < 100; i++) 
415                         {
416                                 Assert.AreEqual (q1.Dequeue(), (int) a[i], "Queue-Array mismatch");
417                         }
418
419                         object[] b = emptyQueue.ToArray();
420                         Assert.AreEqual (0, b.Length, "b should be a zero-lenght array"); 
421                 }
422
423                 [Test]
424                 public void TestTrimToSize() 
425                 {
426                         for (int i=0; i < 50; i++) 
427                         {
428                                 q1.Dequeue();
429                         }
430                         q1.TrimToSize();
431                         // FIXME: I can't figure out how to test if TrimToSize actually worked!
432                 }
433
434                 // TODO: test Syncronized operation
435
436                 [Test]
437                 public void TestSynchronizedException() 
438                 {
439                         try 
440                         {
441                                 Queue.Synchronized(null);
442                                 Assert.Fail ("Must throw ArgumentNullException");
443                         } 
444                         catch (ArgumentNullException e)
445                         {
446                                 Assert.AreEqual ("queue", e.ParamName, "Exception's ParamName must be \"queue\"");
447                         }
448                 }
449                 
450                 [Test]
451                 public void TestAlwaysGrows() 
452                 {
453                         // In bug #61919 the grow () method might not always grow (if the size
454                         // was 0, or due to rounding).
455                         Queue queue = new Queue (new Queue());
456                         queue.Enqueue(1);
457                 }
458
459                 [Test]
460                 public void SynchronizedClone () 
461                 {
462                         Queue q1sync = Queue.Synchronized (q1);
463                         Assert.IsTrue (q1sync.IsSynchronized, "q1sync.IsSyncronized"); 
464                         Assert.AreEqual (q1.Count, q1sync.Count, "q1sync.Count");
465
466                         Queue q1syncsync = Queue.Synchronized (q1sync);
467                         Assert.IsTrue (q1syncsync.IsSynchronized, "q1syncsync must be synchronized too");
468                         Assert.AreEqual (q1.Count, q1syncsync.Count, "q1syncsync.Count");
469
470                         Queue q1syncclone = (Queue) q1sync.Clone();
471                         Assert.IsTrue (q1syncclone.IsSynchronized, "clone must be synchronized too");
472                         Assert.AreEqual (q1.Count, q1syncclone.Count, "q1syncclone.Count");
473                 }
474
475                 [Test]          
476                 public void TestICollectionCtorUsesEnum ()
477                 {
478                         BitArray x = new BitArray (10, true);
479                         Queue s = new Queue (x);
480                 }
481
482                 // https://bugzilla.novell.com/show_bug.cgi?id=321657
483                 [Test]
484                 public void TrimToSize_Dequeue_Enqueue ()
485                 {
486                         Queue queue = new Queue (32, 1.0F);
487                         for (int i = 0; i < 31; i++)
488                                 queue.Enqueue (i);
489
490                         queue.TrimToSize ();
491                         Assert.AreEqual (0, queue.Dequeue (), "0");
492                         queue.Enqueue (411);
493
494                         Assert.AreEqual (31, queue.Count, "Count-1");
495                         for (int i = 1; i < 31; i++) {
496                                 Assert.AreEqual (i, queue.Peek (), "Peek" + i.ToString ());
497                                 Assert.AreEqual (i, queue.Dequeue (), "Dequeue" + i.ToString ());
498                         }
499                         Assert.AreEqual (1, queue.Count, "Count-2");
500                         Assert.AreEqual (411, queue.Dequeue (), "411");
501                 }
502
503                 [Test]
504                 public void TrimToSize_Enqueue_Dequeue ()
505                 {
506                         Queue queue = new Queue (32, 1.0F);
507                         for (int i = 0; i < 31; i++)
508                                 queue.Enqueue (i);
509
510                         queue.TrimToSize ();
511                         queue.Enqueue (411);
512                         Assert.AreEqual (32, queue.Count, "Count-1");
513                         Assert.AreEqual (0, queue.Dequeue (), "0");
514                 }
515         }
516 }
517