2 // ConcurrentQueueTest.cs
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
28 using System.Threading;
29 using System.Collections.Generic;
30 using System.Collections.Concurrent;
32 using NUnit.Framework;
34 namespace MonoTests.System.Collections.Concurrent
39 public class ConcurrentQueueTests
41 ConcurrentQueue<int> queue;
46 queue = new ConcurrentQueue<int>();
47 for (int i = 0; i < 10; i++) {
53 public void StressEnqueueTestCase ()
55 /*ParallelTestHelper.Repeat (delegate {
56 queue = new ConcurrentQueue<int> ();
59 const int threads = 5;
61 ParallelTestHelper.ParallelStressTest (queue, (q) => {
62 int t = Interlocked.Increment (ref amount);
63 for (int i = 0; i < count; i++)
67 Assert.AreEqual (threads * count, queue.Count, "#-1");
68 int[] values = new int[threads];
70 while (queue.TryDequeue (out temp)) {
74 for (int i = 0; i < threads; i++)
75 Assert.AreEqual (count, values[i], "#" + i);
78 CollectionStressTestHelper.AddStressTest (new ConcurrentQueue<int> ());
82 public void StressDequeueTestCase ()
84 /*ParallelTestHelper.Repeat (delegate {
85 queue = new ConcurrentQueue<int> ();
87 const int threads = 5;
90 for (int i = 0; i < (count + delta) * threads; i++)
95 ParallelTestHelper.ParallelStressTest (queue, (q) => {
97 for (int i = 0; i < count; i++)
98 state &= queue.TryDequeue (out t);
101 Assert.IsTrue (state, "#1");
102 Assert.AreEqual (delta * threads, queue.Count, "#2");
104 string actual = string.Empty;
106 while (queue.TryDequeue (out temp)) {
109 string expected = Enumerable.Range (count * threads, delta * threads)
110 .Aggregate (string.Empty, (acc, v) => acc + v);
112 Assert.AreEqual (expected, actual, "#3");
115 CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
119 public void CountTestCase()
121 Assert.AreEqual(10, queue.Count, "#1");
123 queue.TryPeek(out value);
124 queue.TryDequeue(out value);
125 queue.TryDequeue(out value);
126 Assert.AreEqual(8, queue.Count, "#2");
131 public void EnumerateTestCase()
133 string s = string.Empty;
134 foreach (int i in queue) {
137 Assert.AreEqual("0123456789", s, "#1 : " + s);
141 public void TryPeekTestCase()
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);
157 public void TryDequeueTestCase()
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");
168 public void TryDequeueEmptyTestCase()
171 queue = new ConcurrentQueue<int> ();
173 Assert.IsTrue(queue.TryDequeue(out value), "#1");
174 Assert.IsFalse(queue.TryDequeue(out value), "#2");
175 Assert.IsTrue(queue.IsEmpty, "#3");
179 public void ToArrayTest()
181 int[] array = queue.ToArray();
182 string s = string.Empty;
183 foreach (int i in array) {
186 Assert.AreEqual("0123456789", s, "#1 : " + s);
187 queue.CopyTo(array, 0);
189 foreach (int i in array) {
192 Assert.AreEqual("0123456789", s, "#2 : " + s);
195 [Test, ExpectedException (typeof (ArgumentNullException))]
196 public void ToExistingArray_Null ()
198 queue.CopyTo (null, 0);
201 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
202 public void ToExistingArray_OutOfRange ()
204 queue.CopyTo (new int[3], -1);
207 [Test, ExpectedException (typeof (ArgumentException))]
208 public void ToExistingArray_IndexOverflow ()
210 queue.CopyTo (new int[3], 4);
213 [Test, ExpectedException (typeof (ArgumentException))]
214 public void ToExistingArray_Overflow ()
216 queue.CopyTo (new int[3], 0);
219 static WeakReference CreateWeakReference (object obj)
221 return new WeakReference (obj);
225 public void TryDequeueReferenceTest ()
227 var obj = new Object ();
228 var weakReference = CreateWeakReference(obj);
229 var queue = new ConcurrentQueue<object> ();
232 queue.TryDequeue (out obj);
236 GC.WaitForPendingFinalizers ();
238 Assert.IsFalse (weakReference.IsAlive);