ConcurrentStackTests.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #if NET_4_0
  2. // ConcurrentStackTests.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. using System;
  26. using System.Threading;
  27. using System.Linq;
  28. using System.Collections.Concurrent;
  29. using NUnit.Framework;
  30. using NUnit.Framework.Constraints;
  31. namespace MonoTests.System.Collections.Concurrent
  32. {
  33. [TestFixture()]
  34. public class ConcurrentStackTests
  35. {
  36. ConcurrentStack<int> stack;
  37. [SetUpAttribute]
  38. public void Setup()
  39. {
  40. stack = new ConcurrentStack<int>();
  41. for (int i = 0; i < 10; i++) {
  42. stack.Push(i);
  43. }
  44. }
  45. [Test]
  46. public void StressPushTestCase ()
  47. {
  48. /*ParallelTestHelper.Repeat (delegate {
  49. stack = new ConcurrentStack<int> ();
  50. int amount = -1;
  51. const int count = 10;
  52. const int threads = 5;
  53. ParallelTestHelper.ParallelStressTest (stack, (q) => {
  54. int t = Interlocked.Increment (ref amount);
  55. for (int i = 0; i < count; i++)
  56. stack.Push (t);
  57. }, threads);
  58. Assert.AreEqual (threads * count, stack.Count, "#-1");
  59. int[] values = new int[threads];
  60. int temp;
  61. while (stack.TryPop (out temp)) {
  62. values[temp]++;
  63. }
  64. for (int i = 0; i < threads; i++)
  65. Assert.AreEqual (count, values[i], "#" + i);
  66. });*/
  67. CollectionStressTestHelper.AddStressTest (new ConcurrentStack<int> ());
  68. }
  69. [Test]
  70. public void StressPopTestCase ()
  71. {
  72. /*ParallelTestHelper.Repeat (delegate {
  73. stack = new ConcurrentStack<int> ();
  74. const int count = 10;
  75. const int threads = 5;
  76. const int delta = 5;
  77. for (int i = 0; i < (count + delta) * threads; i++)
  78. stack.Push (i);
  79. bool state = true;
  80. ParallelTestHelper.ParallelStressTest (stack, (q) => {
  81. int t;
  82. for (int i = 0; i < count; i++)
  83. state &= stack.TryPop (out t);
  84. }, threads);
  85. Assert.IsTrue (state, "#1");
  86. Assert.AreEqual (delta * threads, stack.Count, "#2");
  87. string actual = string.Empty;
  88. int temp;
  89. while (stack.TryPop (out temp)) {
  90. actual += temp;
  91. }
  92. string expected = Enumerable.Range (0, delta * threads).Reverse()
  93. .Aggregate (string.Empty, (acc, v) => acc + v);
  94. Assert.AreEqual (expected, actual, "#3");
  95. });*/
  96. CollectionStressTestHelper.RemoveStressTest (new ConcurrentStack<int> (), CheckOrderingType.Reversed);
  97. }
  98. [Test]
  99. public void CountTestCase()
  100. {
  101. Assert.IsTrue(stack.Count == 10, "#1");
  102. int value;
  103. stack.TryPeek(out value);
  104. stack.TryPop(out value);
  105. stack.TryPop(out value);
  106. Assert.IsTrue(stack.Count == 8, "#2");
  107. stack.Clear();
  108. Assert.IsTrue(stack.Count == 0, "#3");
  109. Assert.IsTrue(stack.IsEmpty, "#4");
  110. }
  111. //[Ignore]
  112. [Test()]
  113. public void EnumerateTestCase()
  114. {
  115. string s = string.Empty;
  116. foreach (int i in stack) {
  117. s += i;
  118. }
  119. Assert.IsTrue(s == "9876543210", "#1 : " + s);
  120. }
  121. [Test()]
  122. public void TryPeekTestCase()
  123. {
  124. int value;
  125. stack.TryPeek(out value);
  126. Assert.IsTrue(value == 9, "#1 : " + value);
  127. stack.TryPop(out value);
  128. Assert.IsTrue(value == 9, "#2 : " + value);
  129. stack.TryPop(out value);
  130. Assert.IsTrue(value == 8, "#3 : " + value);
  131. stack.TryPeek(out value);
  132. Assert.IsTrue(value == 7, "#4 : " + value);
  133. stack.TryPeek(out value);
  134. Assert.IsTrue(value == 7, "#5 : " + value);
  135. }
  136. [Test()]
  137. public void TryPopTestCase()
  138. {
  139. int value;
  140. stack.TryPeek(out value);
  141. Assert.IsTrue(value == 9, "#1");
  142. stack.TryPop(out value);
  143. stack.TryPop(out value);
  144. Assert.IsTrue(value == 8, "#2 : " + value);
  145. }
  146. [Test()]
  147. public void TryPopEmptyTestCase()
  148. {
  149. int value;
  150. stack.Clear();
  151. stack.Push(1);
  152. Assert.IsTrue(stack.TryPop(out value), "#1");
  153. Assert.IsFalse(stack.TryPop(out value), "#2");
  154. Assert.IsTrue(stack.IsEmpty, "#3");
  155. }
  156. [Test]
  157. public void ToArrayTest()
  158. {
  159. int[] array = stack.ToArray();
  160. string s = string.Empty;
  161. foreach (int i in array) {
  162. s += i;
  163. }
  164. Assert.IsTrue(s == "9876543210", "#1 : " + s);
  165. stack.CopyTo(array, 0);
  166. s = string.Empty;
  167. foreach (int i in array) {
  168. s += i;
  169. }
  170. Assert.IsTrue(s == "9876543210", "#1 : " + s);
  171. }
  172. [Test, ExpectedException (typeof (ArgumentNullException))]
  173. public void ToExistingArray_Null ()
  174. {
  175. stack.CopyTo (null, 0);
  176. }
  177. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  178. public void ToExistingArray_OutOfRange ()
  179. {
  180. stack.CopyTo (new int[3], -1);
  181. }
  182. [Test, ExpectedException (typeof (ArgumentException))]
  183. public void ToExistingArray_IndexOverflow ()
  184. {
  185. stack.CopyTo (new int[3], 4);
  186. }
  187. [Test, ExpectedException (typeof (ArgumentException))]
  188. public void ToExistingArray_Overflow ()
  189. {
  190. stack.CopyTo (new int[3], 0);
  191. }
  192. [Test]
  193. public void TryPopRangeTest ()
  194. {
  195. int[] values = new int[3];
  196. Assert.AreEqual (3, stack.TryPopRange (values));
  197. Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9, 8, 7 }));
  198. Assert.AreEqual (10 - values.Length, stack.Count);
  199. for (int i = 9 - values.Length; i >= 0; i--) {
  200. int outValue;
  201. Assert.IsTrue (stack.TryPop (out outValue));
  202. Assert.AreEqual (i, outValue);
  203. }
  204. }
  205. [Test]
  206. public void TryPopRangeTestWithOneElement ()
  207. {
  208. int[] values = new int[1];
  209. Assert.AreEqual (1, stack.TryPopRange (values));
  210. Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9 }));
  211. Assert.AreEqual (10 - values.Length, stack.Count);
  212. for (int i = 9 - values.Length; i >= 0; i--) {
  213. int outValue;
  214. Assert.IsTrue (stack.TryPop (out outValue));
  215. Assert.AreEqual (i, outValue);
  216. }
  217. }
  218. [Test]
  219. public void TryPopRangeFullTest ()
  220. {
  221. int[] values = new int[10];
  222. Assert.AreEqual (10, stack.TryPopRange (values));
  223. Assert.That (values, new CollectionEquivalentConstraint (Enumerable.Range (0, 10).Reverse ()));
  224. Assert.AreEqual (0, stack.Count);
  225. }
  226. [Test]
  227. public void TryPopRangePartialFillTest ()
  228. {
  229. int[] values = new int[5];
  230. Assert.AreEqual (2, stack.TryPopRange (values, 3, 2));
  231. Assert.That (values, new CollectionEquivalentConstraint (new int[] { 0, 0, 0, 9, 8 }));
  232. Assert.AreEqual (8, stack.Count);
  233. }
  234. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  235. public void TryPopRange_NegativeIndex ()
  236. {
  237. stack.TryPopRange (new int[3], -2, 3);
  238. }
  239. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  240. public void TryPopRange_LargeIndex ()
  241. {
  242. stack.TryPopRange (new int[3], 200, 3);
  243. }
  244. [Test, ExpectedException (typeof (ArgumentException))]
  245. public void TryPopRange_LargeCount ()
  246. {
  247. stack.TryPopRange (new int[3], 2, 5);
  248. }
  249. [Test, ExpectedException (typeof (ArgumentNullException))]
  250. public void TryPopRange_NullArray ()
  251. {
  252. stack.TryPopRange (null);
  253. }
  254. }
  255. }
  256. #endif