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
29 using System.Threading;
30 using System.Threading.Tasks;
32 using System.Collections.Generic;
33 using System.Collections.Concurrent;
36 using NUnit.Framework;
37 using NUnit.Framework.Constraints;
39 namespace MonoTests.System.Threading.Tasks
42 public class ParallelTests
45 public void ParallelForTestCase ()
47 int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
49 ParallelTestHelper.Repeat (() => {
50 int[] actual = Enumerable.Range (1, 1000).ToArray ();
51 SpinWait sw = new SpinWait ();
53 Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
55 Assert.That (actual, new CollectionEquivalentConstraint (expected), "#1, same");
56 Assert.That (actual, new EqualConstraint (expected), "#2, in order");
60 [Test, ExpectedException (typeof (AggregateException))]
61 public void ParallelForExceptionTestCase ()
63 Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
67 public void ParallelForSmallRangeTest ()
69 ParallelTestHelper.Repeat (() => {
71 Parallel.For (0, 1, (i) => test = i);
73 Assert.AreEqual (0, test, "#1");
78 public void ParallelForNoOperationTest ()
80 bool launched = false;
81 Parallel.For (4, 1, (i) => launched = true);
82 Assert.IsFalse (launched, "#1");
86 public void ParallelForNestedTest ()
88 bool[] launched = new bool[6 * 20 * 10];
89 Parallel.For (0, 6, delegate (int i) {
90 Parallel.For (0, 20, delegate (int j) {
91 Parallel.For (0, 10, delegate (int k) {
92 launched[i * 20 * 10 + j * 10 + k] = true;
97 Assert.IsTrue (launched.All ((_) => _), "All true");
101 public void ParallelForEachTestCase ()
103 ParallelTestHelper.Repeat (() => {
104 IEnumerable<int> e = Enumerable.Repeat(1, 500);
105 ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
106 SpinWait sw = new SpinWait ();
109 Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
111 Assert.AreEqual (500, count, "#1");
113 Assert.That (queue, new CollectionEquivalentConstraint (e), "#2");
118 public void ParallelForEachTestCaseWithIndex ()
120 var list = new List<int> { 0, 1, 2, 3, 4 };
122 Parallel.ForEach (list, (l, s, i) => {
123 Assert.AreEqual (l, i, "#1");
129 public float Value { get; set; }
130 public float Square { get; set; }
134 public void ParallerForEach_UserType ()
137 new ValueAndSquare() { Value = 1f },
138 new ValueAndSquare() { Value = 2f },
139 new ValueAndSquare() { Value = 3f },
140 new ValueAndSquare() { Value = 4f },
141 new ValueAndSquare() { Value = 5f },
142 new ValueAndSquare() { Value = 6f },
143 new ValueAndSquare() { Value = 7f },
144 new ValueAndSquare() { Value = 8f },
145 new ValueAndSquare() { Value = 9f },
146 new ValueAndSquare() { Value = 10f }
149 Parallel.ForEach (Partitioner.Create (values), l => l.Square = l.Value * l.Value);
151 foreach (var item in values) {
152 Assert.AreEqual (item.Square, item.Value * item.Value);
156 [Test, ExpectedException (typeof (AggregateException))]
157 public void ParallelForEachExceptionTestCase ()
159 IEnumerable<int> e = Enumerable.Repeat (1, 10);
160 Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
164 public void BasicInvokeTest ()
166 int val1 = 0, val2 = 0;
168 Parallel.Invoke (() => Interlocked.Increment (ref val1), () => Interlocked.Increment (ref val2));
169 Assert.AreEqual (1, val1, "#1");
170 Assert.AreEqual (1, val2, "#2");
174 public void InvokeWithOneNullActionTest ()
176 int val1 = 0, val2 = 0;
179 Parallel.Invoke (() => Interlocked.Increment (ref val1), null, () => Interlocked.Increment (ref val2));
180 } catch (ArgumentException ex) {
181 Assert.AreEqual (0, val1, "#1");
182 Assert.AreEqual (0, val2, "#2");
185 Assert.Fail ("Shouldn't be there");
189 public void OneActionInvokeTest ()
193 Parallel.Invoke (() => Interlocked.Increment (ref val));
194 Assert.AreEqual (1, val, "#1");
197 [Test, ExpectedException (typeof (ArgumentNullException))]
198 public void InvokeWithNullActions ()
200 Parallel.Invoke ((Action[])null);
203 [Test, ExpectedException (typeof (ArgumentNullException))]
204 public void InvokeWithNullOptions ()
206 Parallel.Invoke ((ParallelOptions)null, () => Thread.Sleep (100));
210 public void InvokeWithExceptions ()
213 Parallel.Invoke (() => { throw new ApplicationException ("foo"); }, () => { throw new IOException ("foo"); });
214 } catch (AggregateException ex) {
215 Assert.AreEqual (2, ex.InnerExceptions.Count);
216 foreach (var e in ex.InnerExceptions)
217 Console.WriteLine (e.GetType ());
218 Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (ApplicationException)));
219 Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (IOException)));
222 Assert.Fail ("Shouldn't go there");