2 // ConcurrentStackTests.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
27 using System.Threading;
29 using System.Collections.Concurrent;
30 using NUnit.Framework;
31 using NUnit.Framework.Constraints;
33 namespace MonoTests.System.Collections.Concurrent
36 public class ConcurrentStackTests
38 ConcurrentStack<int> stack;
43 stack = new ConcurrentStack<int>();
44 for (int i = 0; i < 10; i++) {
50 public void StressPushTestCase ()
52 /*ParallelTestHelper.Repeat (delegate {
53 stack = new ConcurrentStack<int> ();
56 const int threads = 5;
58 ParallelTestHelper.ParallelStressTest (stack, (q) => {
59 int t = Interlocked.Increment (ref amount);
60 for (int i = 0; i < count; i++)
64 Assert.AreEqual (threads * count, stack.Count, "#-1");
65 int[] values = new int[threads];
67 while (stack.TryPop (out temp)) {
71 for (int i = 0; i < threads; i++)
72 Assert.AreEqual (count, values[i], "#" + i);
74 CollectionStressTestHelper.AddStressTest (new ConcurrentStack<int> ());
78 public void StressPopTestCase ()
80 /*ParallelTestHelper.Repeat (delegate {
81 stack = new ConcurrentStack<int> ();
83 const int threads = 5;
86 for (int i = 0; i < (count + delta) * threads; i++)
91 ParallelTestHelper.ParallelStressTest (stack, (q) => {
93 for (int i = 0; i < count; i++)
94 state &= stack.TryPop (out t);
97 Assert.IsTrue (state, "#1");
98 Assert.AreEqual (delta * threads, stack.Count, "#2");
100 string actual = string.Empty;
102 while (stack.TryPop (out temp)) {
105 string expected = Enumerable.Range (0, delta * threads).Reverse()
106 .Aggregate (string.Empty, (acc, v) => acc + v);
108 Assert.AreEqual (expected, actual, "#3");
111 CollectionStressTestHelper.RemoveStressTest (new ConcurrentStack<int> (), CheckOrderingType.Reversed);
115 public void CountTestCase()
117 Assert.IsTrue(stack.Count == 10, "#1");
119 stack.TryPeek(out value);
120 stack.TryPop(out value);
121 stack.TryPop(out value);
122 Assert.IsTrue(stack.Count == 8, "#2");
124 Assert.IsTrue(stack.Count == 0, "#3");
125 Assert.IsTrue(stack.IsEmpty, "#4");
129 public void EnumerateTestCase()
131 string s = string.Empty;
132 foreach (int i in stack) {
135 Assert.IsTrue(s == "9876543210", "#1 : " + s);
139 public void TryPeekTestCase()
142 stack.TryPeek(out value);
143 Assert.IsTrue(value == 9, "#1 : " + value);
144 stack.TryPop(out value);
145 Assert.IsTrue(value == 9, "#2 : " + value);
146 stack.TryPop(out value);
147 Assert.IsTrue(value == 8, "#3 : " + value);
148 stack.TryPeek(out value);
149 Assert.IsTrue(value == 7, "#4 : " + value);
150 stack.TryPeek(out value);
151 Assert.IsTrue(value == 7, "#5 : " + value);
155 public void TryPopTestCase()
158 stack.TryPeek(out value);
159 Assert.IsTrue(value == 9, "#1");
160 stack.TryPop(out value);
161 stack.TryPop(out value);
162 Assert.IsTrue(value == 8, "#2 : " + value);
166 public void TryPopEmptyTestCase()
171 Assert.IsTrue(stack.TryPop(out value), "#1");
172 Assert.IsFalse(stack.TryPop(out value), "#2");
173 Assert.IsTrue(stack.IsEmpty, "#3");
177 public void ToArrayTest()
179 int[] array = stack.ToArray();
180 string s = string.Empty;
181 foreach (int i in array) {
184 Assert.IsTrue(s == "9876543210", "#1 : " + s);
185 stack.CopyTo(array, 0);
187 foreach (int i in array) {
190 Assert.IsTrue(s == "9876543210", "#1 : " + s);
193 [Test, ExpectedException (typeof (ArgumentNullException))]
194 public void ToExistingArray_Null ()
196 stack.CopyTo (null, 0);
199 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
200 public void ToExistingArray_OutOfRange ()
202 stack.CopyTo (new int[3], -1);
205 [Test, ExpectedException (typeof (ArgumentException))]
206 public void ToExistingArray_IndexOverflow ()
208 stack.CopyTo (new int[3], 4);
211 [Test, ExpectedException (typeof (ArgumentException))]
212 public void ToExistingArray_Overflow ()
214 stack.CopyTo (new int[3], 0);
218 public void TryPopRangeTest ()
220 int[] values = new int[3];
221 Assert.AreEqual (3, stack.TryPopRange (values));
222 Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9, 8, 7 }));
223 Assert.AreEqual (10 - values.Length, stack.Count);
224 for (int i = 9 - values.Length; i >= 0; i--) {
226 Assert.IsTrue (stack.TryPop (out outValue));
227 Assert.AreEqual (i, outValue);
232 public void TryPopRangeEmpty ()
234 stack = new ConcurrentStack<int>();
235 Assert.AreEqual (0, stack.TryPopRange (new int [1]));
239 public void TryPopRangeTestWithOneElement ()
241 int[] values = new int[1];
242 Assert.AreEqual (1, stack.TryPopRange (values));
243 Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9 }));
244 Assert.AreEqual (10 - values.Length, stack.Count);
245 for (int i = 9 - values.Length; i >= 0; i--) {
247 Assert.IsTrue (stack.TryPop (out outValue));
248 Assert.AreEqual (i, outValue);
253 public void TryPopRangeFullTest ()
255 int[] values = new int[10];
256 Assert.AreEqual (10, stack.TryPopRange (values));
257 Assert.That (values, new CollectionEquivalentConstraint (Enumerable.Range (0, 10).Reverse ()));
258 Assert.AreEqual (0, stack.Count);
262 public void TryPopRangePartialFillTest ()
264 int[] values = new int[5];
265 Assert.AreEqual (2, stack.TryPopRange (values, 3, 2));
266 Assert.That (values, new CollectionEquivalentConstraint (new int[] { 0, 0, 0, 9, 8 }));
267 Assert.AreEqual (8, stack.Count);
270 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
271 public void TryPopRange_NegativeIndex ()
273 stack.TryPopRange (new int[3], -2, 3);
276 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
277 public void TryPopRange_LargeIndex ()
279 stack.TryPopRange (new int[3], 200, 3);
282 [Test, ExpectedException (typeof (ArgumentException))]
283 public void TryPopRange_LargeCount ()
285 stack.TryPopRange (new int[3], 2, 5);
288 [Test, ExpectedException (typeof (ArgumentNullException))]
289 public void TryPopRange_NullArray ()
291 stack.TryPopRange (null);