ParallelTests.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #if NET_4_0
  2. // ParallelTests.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.Reflection;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using System.IO;
  31. using System.Collections.Generic;
  32. using System.Collections.Concurrent;
  33. using NUnit;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Threading.Tasks
  36. {
  37. [TestFixture()]
  38. public class ParallelTests
  39. {
  40. [Test]
  41. public void ParallelForTestCase ()
  42. {
  43. int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
  44. ParallelTestHelper.Repeat (() => {
  45. int[] actual = Enumerable.Range (1, 1000).ToArray ();
  46. SpinWait sw = new SpinWait ();
  47. Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
  48. CollectionAssert.AreEquivalent (expected, actual, "#1, same");
  49. CollectionAssert.AreEqual (expected, actual, "#2, in order");
  50. });
  51. }
  52. [Test, ExpectedException (typeof (AggregateException))]
  53. public void ParallelForExceptionTestCase ()
  54. {
  55. Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
  56. }
  57. [Test]
  58. public void ParallelForSmallRangeTest ()
  59. {
  60. ParallelTestHelper.Repeat (() => {
  61. int test = -1;
  62. Parallel.For (0, 1, (i) => test = i);
  63. Assert.AreEqual (0, test, "#1");
  64. });
  65. }
  66. [Test]
  67. public void ParallelForNoOperationTest ()
  68. {
  69. bool launched = false;
  70. Parallel.For (4, 1, (i) => launched = true);
  71. Assert.IsFalse (launched, "#1");
  72. }
  73. [Test]
  74. public void ParallelForNestedTest ()
  75. {
  76. bool[] launched = new bool[6 * 20 * 10];
  77. Parallel.For (0, 6, delegate (int i) {
  78. Parallel.For (0, 20, delegate (int j) {
  79. Parallel.For (0, 10, delegate (int k) {
  80. launched[i * 20 * 10 + j * 10 + k] = true;
  81. });
  82. });
  83. });
  84. Assert.IsTrue (launched.All ((_) => _), "All true");
  85. }
  86. [Test]
  87. public void ParallelForEachTestCase ()
  88. {
  89. ParallelTestHelper.Repeat (() => {
  90. IEnumerable<int> e = Enumerable.Repeat(1, 500);
  91. ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
  92. SpinWait sw = new SpinWait ();
  93. int count = 0;
  94. Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
  95. Assert.AreEqual (500, count, "#1");
  96. CollectionAssert.AreEquivalent (e, queue, "#2");
  97. });
  98. }
  99. [Test, ExpectedException (typeof (AggregateException))]
  100. public void ParallelForEachExceptionTestCase ()
  101. {
  102. IEnumerable<int> e = Enumerable.Repeat (1, 10);
  103. Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
  104. }
  105. [Test]
  106. public void BasicInvokeTest ()
  107. {
  108. int val1 = 0, val2 = 0;
  109. Parallel.Invoke (() => Interlocked.Increment (ref val1), () => Interlocked.Increment (ref val2));
  110. Assert.AreEqual (1, val1, "#1");
  111. Assert.AreEqual (1, val2, "#2");
  112. }
  113. [Test]
  114. public void InvokeWithOneNullActionTest ()
  115. {
  116. int val1 = 0, val2 = 0;
  117. try {
  118. Parallel.Invoke (() => Interlocked.Increment (ref val1), null, () => Interlocked.Increment (ref val2));
  119. } catch (ArgumentException ex) {
  120. Assert.AreEqual (0, val1, "#1");
  121. Assert.AreEqual (0, val2, "#2");
  122. return;
  123. }
  124. Assert.Fail ("Shouldn't be there");
  125. }
  126. [Test]
  127. public void OneActionInvokeTest ()
  128. {
  129. int val = 0;
  130. Parallel.Invoke (() => Interlocked.Increment (ref val));
  131. Assert.AreEqual (1, val, "#1");
  132. }
  133. [Test, ExpectedException (typeof (ArgumentNullException))]
  134. public void InvokeWithNullActions ()
  135. {
  136. Parallel.Invoke ((Action[])null);
  137. }
  138. [Test, ExpectedException (typeof (ArgumentNullException))]
  139. public void InvokeWithNullOptions ()
  140. {
  141. Parallel.Invoke ((ParallelOptions)null, () => Thread.Sleep (100));
  142. }
  143. [Test]
  144. public void InvokeWithExceptions ()
  145. {
  146. try {
  147. Parallel.Invoke (() => { throw new ApplicationException ("foo"); }, () => { throw new IOException ("foo"); });
  148. } catch (AggregateException ex) {
  149. Assert.AreEqual (2, ex.InnerExceptions.Count);
  150. foreach (var e in ex.InnerExceptions)
  151. Console.WriteLine (e.GetType ());
  152. Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (ApplicationException)));
  153. Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (IOException)));
  154. return;
  155. }
  156. Assert.Fail ("Shouldn't go there");
  157. }
  158. }
  159. }
  160. #endif