PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/Test/System.Collections.Concurrent/ConcurrentStackTests.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 194 lines | 106 code | 12 blank | 76 comment | 14 complexity | 7311220f0cea9b6392e065489c189078 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  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. namespace ParallelFxTests
  31. {
  32. [TestFixture()]
  33. public class ConcurrentStackTests
  34. {
  35. ConcurrentStack<int> stack;
  36. [SetUpAttribute]
  37. public void Setup()
  38. {
  39. stack = new ConcurrentStack<int>();
  40. for (int i = 0; i < 10; i++) {
  41. stack.Push(i);
  42. }
  43. }
  44. [Test]
  45. public void StressPushTestCase ()
  46. {
  47. /*ParallelTestHelper.Repeat (delegate {
  48. stack = new ConcurrentStack<int> ();
  49. int amount = -1;
  50. const int count = 10;
  51. const int threads = 5;
  52. ParallelTestHelper.ParallelStressTest (stack, (q) => {
  53. int t = Interlocked.Increment (ref amount);
  54. for (int i = 0; i < count; i++)
  55. stack.Push (t);
  56. }, threads);
  57. Assert.AreEqual (threads * count, stack.Count, "#-1");
  58. int[] values = new int[threads];
  59. int temp;
  60. while (stack.TryPop (out temp)) {
  61. values[temp]++;
  62. }
  63. for (int i = 0; i < threads; i++)
  64. Assert.AreEqual (count, values[i], "#" + i);
  65. });*/
  66. CollectionStressTestHelper.AddStressTest (new ConcurrentStack<int> ());
  67. }
  68. [Test]
  69. public void StressPopTestCase ()
  70. {
  71. /*ParallelTestHelper.Repeat (delegate {
  72. stack = new ConcurrentStack<int> ();
  73. const int count = 10;
  74. const int threads = 5;
  75. const int delta = 5;
  76. for (int i = 0; i < (count + delta) * threads; i++)
  77. stack.Push (i);
  78. bool state = true;
  79. ParallelTestHelper.ParallelStressTest (stack, (q) => {
  80. int t;
  81. for (int i = 0; i < count; i++)
  82. state &= stack.TryPop (out t);
  83. }, threads);
  84. Assert.IsTrue (state, "#1");
  85. Assert.AreEqual (delta * threads, stack.Count, "#2");
  86. string actual = string.Empty;
  87. int temp;
  88. while (stack.TryPop (out temp)) {
  89. actual += temp;
  90. }
  91. string expected = Enumerable.Range (0, delta * threads).Reverse()
  92. .Aggregate (string.Empty, (acc, v) => acc + v);
  93. Assert.AreEqual (expected, actual, "#3");
  94. });*/
  95. CollectionStressTestHelper.RemoveStressTest (new ConcurrentStack<int> (), CheckOrderingType.Reversed);
  96. }
  97. [Test]
  98. public void CountTestCase()
  99. {
  100. Assert.IsTrue(stack.Count == 10, "#1");
  101. int value;
  102. stack.TryPeek(out value);
  103. stack.TryPop(out value);
  104. stack.TryPop(out value);
  105. Assert.IsTrue(stack.Count == 8, "#2");
  106. stack.Clear();
  107. Assert.IsTrue(stack.Count == 0, "#3");
  108. Assert.IsTrue(stack.IsEmpty, "#4");
  109. }
  110. //[Ignore]
  111. [Test()]
  112. public void EnumerateTestCase()
  113. {
  114. string s = string.Empty;
  115. foreach (int i in stack) {
  116. s += i;
  117. }
  118. Assert.IsTrue(s == "9876543210", "#1 : " + s);
  119. }
  120. [Test()]
  121. public void TryPeekTestCase()
  122. {
  123. int value;
  124. stack.TryPeek(out value);
  125. Assert.IsTrue(value == 9, "#1 : " + value);
  126. stack.TryPop(out value);
  127. Assert.IsTrue(value == 9, "#2 : " + value);
  128. stack.TryPop(out value);
  129. Assert.IsTrue(value == 8, "#3 : " + value);
  130. stack.TryPeek(out value);
  131. Assert.IsTrue(value == 7, "#4 : " + value);
  132. stack.TryPeek(out value);
  133. Assert.IsTrue(value == 7, "#5 : " + value);
  134. }
  135. [Test()]
  136. public void TryPopTestCase()
  137. {
  138. int value;
  139. stack.TryPeek(out value);
  140. Assert.IsTrue(value == 9, "#1");
  141. stack.TryPop(out value);
  142. stack.TryPop(out value);
  143. Assert.IsTrue(value == 8, "#2 : " + value);
  144. }
  145. [Test()]
  146. public void TryPopEmptyTestCase()
  147. {
  148. int value;
  149. stack.Clear();
  150. stack.Push(1);
  151. Assert.IsTrue(stack.TryPop(out value), "#1");
  152. Assert.IsFalse(stack.TryPop(out value), "#2");
  153. Assert.IsTrue(stack.IsEmpty, "#3");
  154. }
  155. [Test]
  156. public void ToArrayTest()
  157. {
  158. int[] array = stack.ToArray();
  159. string s = string.Empty;
  160. foreach (int i in array) {
  161. s += i;
  162. }
  163. Assert.IsTrue(s == "9876543210", "#1 : " + s);
  164. stack.CopyTo(array, 0);
  165. s = string.Empty;
  166. foreach (int i in array) {
  167. s += i;
  168. }
  169. Assert.IsTrue(s == "9876543210", "#1 : " + s);
  170. }
  171. }
  172. }
  173. #endif