2 // ConcurrentStackRe.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.Collections;
28 using NUnit.Framework;
30 namespace ParallelFxTests
33 public class ConcurrentStackTests
35 ConcurrentStack<int> stack;
40 stack = new ConcurrentStack<int>();
41 for (int i = 0; i < 10; i++) {
47 public void CountTestCase()
49 Assert.IsTrue(stack.Count == 10, "#1");
51 stack.TryPeek(out value);
52 stack.TryPop(out value);
53 stack.TryPop(out value);
54 Assert.IsTrue(stack.Count == 8, "#2");
56 Assert.IsTrue(stack.Count == 0, "#3");
57 Assert.IsTrue(stack.IsEmpty, "#4");
62 public void EnumerateTestCase()
64 string s = string.Empty;
65 foreach (int i in stack) {
68 Assert.IsTrue(s == "9876543210", "#1 : " + s);
72 public void TryPeekTestCase()
75 stack.TryPeek(out value);
76 Assert.IsTrue(value == 9, "#1 : " + value);
77 stack.TryPop(out value);
78 Assert.IsTrue(value == 9, "#2 : " + value);
79 stack.TryPop(out value);
80 Assert.IsTrue(value == 8, "#3 : " + value);
81 stack.TryPeek(out value);
82 Assert.IsTrue(value == 7, "#4 : " + value);
83 stack.TryPeek(out value);
84 Assert.IsTrue(value == 7, "#5 : " + value);
88 public void TryPopTestCase()
91 stack.TryPeek(out value);
92 Assert.IsTrue(value == 9, "#1");
93 stack.TryPop(out value);
94 stack.TryPop(out value);
95 Assert.IsTrue(value == 8, "#2 : " + value);
99 public void TryPopEmptyTestCase()
104 Assert.IsTrue(stack.TryPop(out value), "#1");
105 Assert.IsFalse(stack.TryPop(out value), "#2");
106 Assert.IsTrue(stack.IsEmpty, "#3");
110 public void ToArrayTest()
112 int[] array = stack.ToArray();
113 string s = string.Empty;
114 foreach (int i in array) {
117 Assert.IsTrue(s == "9876543210", "#1 : " + s);
118 stack.CopyTo(array, 0);
120 foreach (int i in array) {
123 Assert.IsTrue(s == "9876543210", "#1 : " + s);