PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 196 lines | 104 code | 16 blank | 76 comment | 1 complexity | 17bce6f5895f33c1ef0a26105f1d6c6b 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. // ConcurrentQueueTest.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.Linq;
  27. using System.Threading;
  28. using System.Collections.Generic;
  29. using System.Collections.Concurrent;
  30. using NUnit.Framework;
  31. namespace ParallelFxTests
  32. {
  33. [TestFixture()]
  34. public class ConcurrentQueueTests
  35. {
  36. ConcurrentQueue<int> queue;
  37. [SetUpAttribute]
  38. public void Setup()
  39. {
  40. queue = new ConcurrentQueue<int>();
  41. for (int i = 0; i < 10; i++) {
  42. queue.Enqueue(i);
  43. }
  44. }
  45. [Test]
  46. public void StressEnqueueTestCase ()
  47. {
  48. /*ParallelTestHelper.Repeat (delegate {
  49. queue = new ConcurrentQueue<int> ();
  50. int amount = -1;
  51. const int count = 10;
  52. const int threads = 5;
  53. ParallelTestHelper.ParallelStressTest (queue, (q) => {
  54. int t = Interlocked.Increment (ref amount);
  55. for (int i = 0; i < count; i++)
  56. queue.Enqueue (t);
  57. }, threads);
  58. Assert.AreEqual (threads * count, queue.Count, "#-1");
  59. int[] values = new int[threads];
  60. int temp;
  61. while (queue.TryDequeue (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 ConcurrentQueue<int> ());
  68. }
  69. [Test]
  70. public void StressDequeueTestCase ()
  71. {
  72. /*ParallelTestHelper.Repeat (delegate {
  73. queue = new ConcurrentQueue<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. queue.Enqueue (i);
  79. bool state = true;
  80. ParallelTestHelper.ParallelStressTest (queue, (q) => {
  81. int t;
  82. for (int i = 0; i < count; i++)
  83. state &= queue.TryDequeue (out t);
  84. }, threads);
  85. Assert.IsTrue (state, "#1");
  86. Assert.AreEqual (delta * threads, queue.Count, "#2");
  87. string actual = string.Empty;
  88. int temp;
  89. while (queue.TryDequeue (out temp)) {
  90. actual += temp;
  91. }
  92. string expected = Enumerable.Range (count * threads, delta * threads)
  93. .Aggregate (string.Empty, (acc, v) => acc + v);
  94. Assert.AreEqual (expected, actual, "#3");
  95. });*/
  96. CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
  97. }
  98. [Test]
  99. public void CountTestCase()
  100. {
  101. Assert.AreEqual(10, queue.Count, "#1");
  102. int value;
  103. queue.TryPeek(out value);
  104. queue.TryDequeue(out value);
  105. queue.TryDequeue(out value);
  106. Assert.AreEqual(8, queue.Count, "#2");
  107. }
  108. //[Ignore]
  109. [Test]
  110. public void EnumerateTestCase()
  111. {
  112. string s = string.Empty;
  113. foreach (int i in queue) {
  114. s += i;
  115. }
  116. Assert.AreEqual("0123456789", s, "#1 : " + s);
  117. }
  118. [Test()]
  119. public void TryPeekTestCase()
  120. {
  121. int value;
  122. queue.TryPeek(out value);
  123. Assert.AreEqual(0, value, "#1 : " + value);
  124. queue.TryDequeue(out value);
  125. Assert.AreEqual(0, value, "#2 : " + value);
  126. queue.TryDequeue(out value);
  127. Assert.AreEqual(1, value, "#3 : " + value);
  128. queue.TryPeek(out value);
  129. Assert.AreEqual(2, value, "#4 : " + value);
  130. queue.TryPeek(out value);
  131. Assert.AreEqual(2, value, "#5 : " + value);
  132. }
  133. [Test()]
  134. public void TryDequeueTestCase()
  135. {
  136. int value;
  137. queue.TryPeek(out value);
  138. Assert.AreEqual(0, value, "#1");
  139. Assert.IsTrue(queue.TryDequeue(out value), "#2");
  140. Assert.IsTrue(queue.TryDequeue(out value), "#3");
  141. Assert.AreEqual(1, value, "#4");
  142. }
  143. [Test()]
  144. public void TryDequeueEmptyTestCase()
  145. {
  146. int value;
  147. queue = new ConcurrentQueue<int> ();
  148. queue.Enqueue(1);
  149. Assert.IsTrue(queue.TryDequeue(out value), "#1");
  150. Assert.IsFalse(queue.TryDequeue(out value), "#2");
  151. Assert.IsTrue(queue.IsEmpty, "#3");
  152. }
  153. [Test]
  154. public void ToArrayTest()
  155. {
  156. int[] array = queue.ToArray();
  157. string s = string.Empty;
  158. foreach (int i in array) {
  159. s += i;
  160. }
  161. Assert.AreEqual("0123456789", s, "#1 : " + s);
  162. queue.CopyTo(array, 0);
  163. s = string.Empty;
  164. foreach (int i in array) {
  165. s += i;
  166. }
  167. Assert.AreEqual("0123456789", s, "#2 : " + s);
  168. }
  169. }
  170. }
  171. #endif