Merge pull request #1068 from esdrubal/bug18421
[mono.git] / mcs / class / corlib / Test / System.Collections.Concurrent / ConcurrentQueueTests.cs
1 #if NET_4_0
2 // ConcurrentQueueTest.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 //
24 //
25
26 using System;
27 using System.Linq;
28 using System.Threading;
29 using System.Collections.Generic;
30 using System.Collections.Concurrent;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Collections.Concurrent
35 {
36         
37         
38         [TestFixture()]
39         public class ConcurrentQueueTests
40         {
41                 ConcurrentQueue<int> queue;
42                 
43                 [SetUpAttribute]
44                 public void Setup()
45                 {
46                         queue = new ConcurrentQueue<int>();
47                         for (int i = 0; i < 10; i++) {
48                                 queue.Enqueue(i);
49                         }
50                 }
51                 
52                 [Test]
53                 public void StressEnqueueTestCase ()
54                 {
55                         /*ParallelTestHelper.Repeat (delegate {
56                                 queue = new ConcurrentQueue<int> ();
57                                 int amount = -1;
58                                 const int count = 10;
59                                 const int threads = 5;
60                                 
61                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
62                                         int t = Interlocked.Increment (ref amount);
63                                         for (int i = 0; i < count; i++)
64                                                 queue.Enqueue (t);
65                                 }, threads);
66                                 
67                                 Assert.AreEqual (threads * count, queue.Count, "#-1");
68                                 int[] values = new int[threads];
69                                 int temp;
70                                 while (queue.TryDequeue (out temp)) {
71                                         values[temp]++;
72                                 }
73                                 
74                                 for (int i = 0; i < threads; i++)
75                                         Assert.AreEqual (count, values[i], "#" + i);
76                         });*/
77                         
78                         CollectionStressTestHelper.AddStressTest (new ConcurrentQueue<int> ());
79                 }
80                 
81                 [Test]
82                 public void StressDequeueTestCase ()
83                 {
84                         /*ParallelTestHelper.Repeat (delegate {
85                                 queue = new ConcurrentQueue<int> ();
86                                 const int count = 10;
87                                 const int threads = 5;
88                                 const int delta = 5;
89                                 
90                                 for (int i = 0; i < (count + delta) * threads; i++)
91                                         queue.Enqueue (i);
92                                 
93                                 bool state = true;
94                                 
95                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
96                                         int t;
97                                         for (int i = 0; i < count; i++)
98                                                 state &= queue.TryDequeue (out t);
99                                 }, threads);
100                                 
101                                 Assert.IsTrue (state, "#1");
102                                 Assert.AreEqual (delta * threads, queue.Count, "#2");
103                                 
104                                 string actual = string.Empty;
105                                 int temp;
106                                 while (queue.TryDequeue (out temp)) {
107                                         actual += temp;
108                                 }
109                                 string expected = Enumerable.Range (count * threads, delta * threads)
110                                         .Aggregate (string.Empty, (acc, v) => acc + v);
111                                 
112                                 Assert.AreEqual (expected, actual, "#3");
113                         });*/
114                         
115                         CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
116                 }
117                 
118                 [Test]
119                 public void CountTestCase()
120                 {
121                         Assert.AreEqual(10, queue.Count, "#1");
122                         int value;
123                         queue.TryPeek(out value);
124                         queue.TryDequeue(out value);
125                         queue.TryDequeue(out value);
126                         Assert.AreEqual(8, queue.Count, "#2");
127                 }
128                 
129                 //[Ignore]
130                 [Test]
131                 public void EnumerateTestCase()
132                 {
133                         string s = string.Empty;
134                         foreach (int i in queue) {
135                                 s += i;
136                         }
137                         Assert.AreEqual("0123456789", s, "#1 : " + s);
138                 }
139                 
140                 [Test()]
141                 public void TryPeekTestCase()
142                 {
143                         int value;
144                         queue.TryPeek(out value);
145                         Assert.AreEqual(0, value, "#1 : " + value);
146                         queue.TryDequeue(out value);
147                         Assert.AreEqual(0, value, "#2 : " + value);
148                         queue.TryDequeue(out value);
149                         Assert.AreEqual(1, value, "#3 : " + value);
150                         queue.TryPeek(out value);
151                         Assert.AreEqual(2, value, "#4 : " + value);
152                         queue.TryPeek(out value);
153                         Assert.AreEqual(2, value, "#5 : " + value);
154                 }
155                 
156                 [Test()]
157                 public void TryDequeueTestCase()
158                 {
159                         int value;
160                         queue.TryPeek(out value);
161                         Assert.AreEqual(0, value, "#1");
162                         Assert.IsTrue(queue.TryDequeue(out value), "#2");
163                         Assert.IsTrue(queue.TryDequeue(out value), "#3");
164                         Assert.AreEqual(1, value, "#4");
165                 }
166                 
167                 [Test()]
168                 public void TryDequeueEmptyTestCase()
169                 {
170                         int value;
171                         queue = new ConcurrentQueue<int> ();
172                         queue.Enqueue(1);
173                         Assert.IsTrue(queue.TryDequeue(out value), "#1");
174                         Assert.IsFalse(queue.TryDequeue(out value), "#2");
175                         Assert.IsTrue(queue.IsEmpty, "#3");
176                 }
177                 
178                 [Test]
179                 public void ToArrayTest()
180                 {
181                         int[] array = queue.ToArray();
182                         string s = string.Empty;
183                         foreach (int i in array) {
184                                 s += i;
185                         }
186                         Assert.AreEqual("0123456789", s, "#1 : " + s);
187                         queue.CopyTo(array, 0);
188                         s = string.Empty;
189                         foreach (int i in array) {
190                                 s += i;
191                         }
192                         Assert.AreEqual("0123456789", s, "#2 : " + s);
193                 }
194
195                 [Test, ExpectedException (typeof (ArgumentNullException))]
196                 public void ToExistingArray_Null ()
197                 {
198                         queue.CopyTo (null, 0);
199                 }
200
201                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
202                 public void ToExistingArray_OutOfRange ()
203                 {
204                         queue.CopyTo (new int[3], -1);
205                 }
206
207                 [Test, ExpectedException (typeof (ArgumentException))]
208                 public void ToExistingArray_IndexOverflow ()
209                 {
210                         queue.CopyTo (new int[3], 4);
211                 }
212
213                 [Test, ExpectedException (typeof (ArgumentException))]
214                 public void ToExistingArray_Overflow ()
215                 {
216                         queue.CopyTo (new int[3], 0);
217                 }
218                 
219                 static WeakReference CreateWeakReference (object obj)
220                 {
221                         return new WeakReference (obj);
222                 }
223                 
224                 [Test]
225                 public void TryDequeueReferenceTest ()
226                 {
227                         var obj = new Object ();
228                         var weakReference = CreateWeakReference(obj);
229                         var queue = new ConcurrentQueue<object> ();
230
231                         queue.Enqueue (obj);
232                         queue.TryDequeue (out obj);
233                         obj = null;
234
235                         GC.Collect ();
236                         GC.WaitForPendingFinalizers ();
237
238                         Assert.IsFalse (weakReference.IsAlive);
239                 }
240         }
241 }
242 #endif