Merge pull request #1074 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 using MonoTests.System.Threading.Tasks;
34
35 namespace MonoTests.System.Collections.Concurrent
36 {
37         
38         
39         [TestFixture()]
40         public class ConcurrentQueueTests
41         {
42                 ConcurrentQueue<int> queue;
43                 
44                 [SetUpAttribute]
45                 public void Setup()
46                 {
47                         queue = new ConcurrentQueue<int>();
48                         for (int i = 0; i < 10; i++) {
49                                 queue.Enqueue(i);
50                         }
51                 }
52                 
53                 [Test]
54                 public void StressEnqueueTestCase ()
55                 {
56                         /*ParallelTestHelper.Repeat (delegate {
57                                 queue = new ConcurrentQueue<int> ();
58                                 int amount = -1;
59                                 const int count = 10;
60                                 const int threads = 5;
61                                 
62                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
63                                         int t = Interlocked.Increment (ref amount);
64                                         for (int i = 0; i < count; i++)
65                                                 queue.Enqueue (t);
66                                 }, threads);
67                                 
68                                 Assert.AreEqual (threads * count, queue.Count, "#-1");
69                                 int[] values = new int[threads];
70                                 int temp;
71                                 while (queue.TryDequeue (out temp)) {
72                                         values[temp]++;
73                                 }
74                                 
75                                 for (int i = 0; i < threads; i++)
76                                         Assert.AreEqual (count, values[i], "#" + i);
77                         });*/
78                         
79                         CollectionStressTestHelper.AddStressTest (new ConcurrentQueue<int> ());
80                 }
81                 
82                 [Test]
83                 public void StressDequeueTestCase ()
84                 {
85                         /*ParallelTestHelper.Repeat (delegate {
86                                 queue = new ConcurrentQueue<int> ();
87                                 const int count = 10;
88                                 const int threads = 5;
89                                 const int delta = 5;
90                                 
91                                 for (int i = 0; i < (count + delta) * threads; i++)
92                                         queue.Enqueue (i);
93                                 
94                                 bool state = true;
95                                 
96                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
97                                         int t;
98                                         for (int i = 0; i < count; i++)
99                                                 state &= queue.TryDequeue (out t);
100                                 }, threads);
101                                 
102                                 Assert.IsTrue (state, "#1");
103                                 Assert.AreEqual (delta * threads, queue.Count, "#2");
104                                 
105                                 string actual = string.Empty;
106                                 int temp;
107                                 while (queue.TryDequeue (out temp)) {
108                                         actual += temp;
109                                 }
110                                 string expected = Enumerable.Range (count * threads, delta * threads)
111                                         .Aggregate (string.Empty, (acc, v) => acc + v);
112                                 
113                                 Assert.AreEqual (expected, actual, "#3");
114                         });*/
115                         
116                         CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
117                 }
118                 
119                 [Test]
120                 public void StressTryPeekTestCase ()
121                 {
122                         ParallelTestHelper.Repeat (delegate {
123                                 var queue = new ConcurrentQueue<object> ();
124                                 queue.Enqueue (new object());
125                                 
126                                 const int threads = 10;
127                                 int threadCounter = 0;
128                                 bool success = true;
129                                 
130                                 ParallelTestHelper.ParallelStressTest (queue, (q) => {
131                                         int threadId = Interlocked.Increment (ref threadCounter);
132                                         object temp;
133                                         if (threadId < threads)
134                                         {
135                                                 while (queue.TryPeek (out temp))
136                                                         if (temp == null)
137                                                                 success = false;
138                                         } else {
139                                                 queue.TryDequeue (out temp);
140                                         }
141                                 }, threads);
142                                 
143                                 Assert.IsTrue (success, "TryPeek returned unexpected null value.");
144                         }, 10);
145                 }
146                 
147                 [Test]
148                 public void CountTestCase()
149                 {
150                         Assert.AreEqual(10, queue.Count, "#1");
151                         int value;
152                         queue.TryPeek(out value);
153                         queue.TryDequeue(out value);
154                         queue.TryDequeue(out value);
155                         Assert.AreEqual(8, queue.Count, "#2");
156                 }
157                 
158                 //[Ignore]
159                 [Test]
160                 public void EnumerateTestCase()
161                 {
162                         string s = string.Empty;
163                         foreach (int i in queue) {
164                                 s += i;
165                         }
166                         Assert.AreEqual("0123456789", s, "#1 : " + s);
167                 }
168                 
169                 [Test()]
170                 public void TryPeekTestCase()
171                 {
172                         int value;
173                         queue.TryPeek(out value);
174                         Assert.AreEqual(0, value, "#1 : " + value);
175                         queue.TryDequeue(out value);
176                         Assert.AreEqual(0, value, "#2 : " + value);
177                         queue.TryDequeue(out value);
178                         Assert.AreEqual(1, value, "#3 : " + value);
179                         queue.TryPeek(out value);
180                         Assert.AreEqual(2, value, "#4 : " + value);
181                         queue.TryPeek(out value);
182                         Assert.AreEqual(2, value, "#5 : " + value);
183                 }
184                 
185                 [Test()]
186                 public void TryDequeueTestCase()
187                 {
188                         int value;
189                         queue.TryPeek(out value);
190                         Assert.AreEqual(0, value, "#1");
191                         Assert.IsTrue(queue.TryDequeue(out value), "#2");
192                         Assert.IsTrue(queue.TryDequeue(out value), "#3");
193                         Assert.AreEqual(1, value, "#4");
194                 }
195                 
196                 [Test()]
197                 public void TryDequeueEmptyTestCase()
198                 {
199                         int value;
200                         queue = new ConcurrentQueue<int> ();
201                         queue.Enqueue(1);
202                         Assert.IsTrue(queue.TryDequeue(out value), "#1");
203                         Assert.IsFalse(queue.TryDequeue(out value), "#2");
204                         Assert.IsTrue(queue.IsEmpty, "#3");
205                 }
206                 
207                 [Test]
208                 public void ToArrayTest()
209                 {
210                         int[] array = queue.ToArray();
211                         string s = string.Empty;
212                         foreach (int i in array) {
213                                 s += i;
214                         }
215                         Assert.AreEqual("0123456789", s, "#1 : " + s);
216                         queue.CopyTo(array, 0);
217                         s = string.Empty;
218                         foreach (int i in array) {
219                                 s += i;
220                         }
221                         Assert.AreEqual("0123456789", s, "#2 : " + s);
222                 }
223
224                 [Test, ExpectedException (typeof (ArgumentNullException))]
225                 public void ToExistingArray_Null ()
226                 {
227                         queue.CopyTo (null, 0);
228                 }
229
230                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
231                 public void ToExistingArray_OutOfRange ()
232                 {
233                         queue.CopyTo (new int[3], -1);
234                 }
235
236                 [Test, ExpectedException (typeof (ArgumentException))]
237                 public void ToExistingArray_IndexOverflow ()
238                 {
239                         queue.CopyTo (new int[3], 4);
240                 }
241
242                 [Test, ExpectedException (typeof (ArgumentException))]
243                 public void ToExistingArray_Overflow ()
244                 {
245                         queue.CopyTo (new int[3], 0);
246                 }
247
248                 static WeakReference CreateWeakReference (object obj)
249                 {
250                         return new WeakReference (obj);
251                 }
252
253                 [Test]
254                 // This depends on precise stack scanning
255                 [Category ("NotWorking")]
256                 public void TryDequeueReferenceTest ()
257                 {
258                         var obj = new Object ();
259                         var weakReference = CreateWeakReference(obj);
260                         var queue = new ConcurrentQueue<object> ();
261
262                         queue.Enqueue (obj);
263                         queue.TryDequeue (out obj);
264                         obj = null;
265
266                         GC.Collect ();
267                         GC.WaitForPendingFinalizers ();
268
269                         Assert.IsFalse (weakReference.IsAlive);
270                 }
271         }
272 }
273 #endif