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
28 using System.Reflection;
29 using System.Threading;
30 using System.Threading.Tasks;
32 using System.Xml.Serialization;
33 using System.Collections.Generic;
34 using System.Collections.Concurrent;
38 using NUnit.Framework;
40 namespace MonoTests.System.Threading.Tasks
44 public class ParallelTests
48 public void ParallelForTestCase ()
50 ParallelTestHelper.Repeat (() => {
51 int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
52 int[] actual = Enumerable.Range (1, 1000).ToArray ();
53 SpinWait sw = new SpinWait ();
55 Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
57 CollectionAssert.AreEquivalent (expected, actual, "#1, same");
58 CollectionAssert.AreEqual (expected, actual, "#2, in order");
62 [Test, ExpectedException (typeof (AggregateException))]
63 public void ParallelForExceptionTestCase ()
65 Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
69 public void ParallelForSmallRangeTest ()
71 ParallelTestHelper.Repeat (() => {
73 Parallel.For (0, 1, (i) => test = i);
75 Assert.AreEqual (0, test, "#1");
80 public void ParallelForNoOperationTest ()
82 bool launched = false;
83 Parallel.For (4, 1, (i) => launched = true);
84 Assert.IsFalse (launched, "#1");
88 public void ParallelForEachTestCase ()
90 ParallelTestHelper.Repeat (() => {
91 IEnumerable<int> e = Enumerable.Repeat(1, 500);
92 ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
93 SpinWait sw = new SpinWait ();
96 Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
98 Assert.AreEqual (500, count, "#1");
99 CollectionAssert.AreEquivalent (e, queue, "#2");
103 [Test, ExpectedException (typeof (AggregateException))]
104 public void ParallelForEachExceptionTestCase ()
106 IEnumerable<int> e = Enumerable.Repeat (1, 10);
107 Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
110 /* Disabled as this is an API addition
112 public void ParallelWhileTestCase()
114 ParallelTestHelper.Repeat (() => {
118 Parallel.While (() => Interlocked.Increment (ref i) <= 10, () => Interlocked.Increment (ref count));
120 Assert.Greater(i, 10, "#1");
121 Assert.AreEqual(10, count, "#2");