4 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
30 using System.Threading;
31 using System.Threading.Tasks;
33 using System.Collections.Generic;
34 using System.Collections.Concurrent;
37 using NUnit.Framework;
38 using NUnit.Framework.Constraints;
40 namespace MonoTests.System.Threading.Tasks
43 public class ParallelTests
46 public void ParallelForTestCase ()
48 int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
50 ParallelTestHelper.Repeat (() => {
51 int[] actual = Enumerable.Range (1, 1000).ToArray ();
52 SpinWait sw = new SpinWait ();
54 Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
56 Assert.That (actual, new CollectionEquivalentConstraint (expected), "#1, same");
57 Assert.That (actual, new EqualConstraint (expected), "#2, in order");
61 [Test, ExpectedException (typeof (AggregateException))]
62 public void ParallelForExceptionTestCase ()
64 Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
68 public void ParallelForSmallRangeTest ()
70 ParallelTestHelper.Repeat (() => {
72 Parallel.For (0, 1, (i) => test = i);
74 Assert.AreEqual (0, test, "#1");
79 public void ParallelForNoOperationTest ()
81 bool launched = false;
82 Parallel.For (4, 1, (i) => launched = true);
83 Assert.IsFalse (launched, "#1");
87 public void ParallelForNestedTest ()
89 bool[] launched = new bool[6 * 20 * 10];
90 Parallel.For (0, 6, delegate (int i) {
91 Parallel.For (0, 20, delegate (int j) {
92 Parallel.For (0, 10, delegate (int k) {
93 launched[i * 20 * 10 + j * 10 + k] = true;
98 Assert.IsTrue (launched.All ((_) => _), "All true");
102 public void ParallelForEachTestCase ()
104 ParallelTestHelper.Repeat (() => {
105 IEnumerable<int> e = Enumerable.Repeat(1, 500);
106 ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
107 SpinWait sw = new SpinWait ();
110 Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
112 Assert.AreEqual (500, count, "#1");
114 Assert.That (queue, new CollectionEquivalentConstraint (e), "#2");
119 public void ParallelForEachTestCaseWithIndex ()
121 var list = new List<int> { 0, 1, 2, 3, 4 };
123 Parallel.ForEach (list, (l, s, i) => {
124 Assert.AreEqual (l, i, "#1");
130 public float Value { get; set; }
131 public float Square { get; set; }
135 public void ParallerForEach_UserType ()
138 new ValueAndSquare() { Value = 1f },
139 new ValueAndSquare() { Value = 2f },
140 new ValueAndSquare() { Value = 3f },
141 new ValueAndSquare() { Value = 4f },
142 new ValueAndSquare() { Value = 5f },
143 new ValueAndSquare() { Value = 6f },
144 new ValueAndSquare() { Value = 7f },
145 new ValueAndSquare() { Value = 8f },
146 new ValueAndSquare() { Value = 9f },
147 new ValueAndSquare() { Value = 10f }
150 Parallel.ForEach (Partitioner.Create (values), l => l.Square = l.Value * l.Value);
152 foreach (var item in values) {
153 Assert.AreEqual (item.Square, item.Value * item.Value);
157 [Test, ExpectedException (typeof (AggregateException))]
158 public void ParallelForEachExceptionTestCase ()
160 IEnumerable<int> e = Enumerable.Repeat (1, 10);
161 Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
165 public void BasicInvokeTest ()
167 int val1 = 0, val2 = 0;
169 Parallel.Invoke (() => Interlocked.Increment (ref val1), () => Interlocked.Increment (ref val2));
170 Assert.AreEqual (1, val1, "#1");
171 Assert.AreEqual (1, val2, "#2");
175 public void InvokeWithOneNullActionTest ()
177 int val1 = 0, val2 = 0;
180 Parallel.Invoke (() => Interlocked.Increment (ref val1), null, () => Interlocked.Increment (ref val2));
181 } catch (ArgumentException ex) {
182 Assert.AreEqual (0, val1, "#1");
183 Assert.AreEqual (0, val2, "#2");
186 Assert.Fail ("Shouldn't be there");
190 public void OneActionInvokeTest ()
194 Parallel.Invoke (() => Interlocked.Increment (ref val));
195 Assert.AreEqual (1, val, "#1");
198 [Test, ExpectedException (typeof (ArgumentNullException))]
199 public void InvokeWithNullActions ()
201 Parallel.Invoke ((Action[])null);
204 [Test, ExpectedException (typeof (ArgumentNullException))]
205 public void InvokeWithNullOptions ()
207 Parallel.Invoke ((ParallelOptions)null, () => Thread.Sleep (100));
211 public void InvokeWithExceptions ()
214 Parallel.Invoke (() => { throw new ApplicationException ("foo"); }, () => { throw new IOException ("foo"); });
215 } catch (AggregateException ex) {
216 Assert.AreEqual (2, ex.InnerExceptions.Count);
217 foreach (var e in ex.InnerExceptions)
218 Console.WriteLine (e.GetType ());
219 Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (ApplicationException)));
220 Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (IOException)));
223 Assert.Fail ("Shouldn't go there");