ParallelTests.cs 6.1 KB

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