Merge pull request #980 from StephenMcConnel/bug-18638
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / ParallelTests.cs
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
26 #if NET_4_0
27
28 using System;
29 using System.Linq;
30 using System.Threading;
31 using System.Threading.Tasks;
32 using System.IO;
33 using System.Collections.Generic;
34 using System.Collections.Concurrent;
35
36 using NUnit;
37 using NUnit.Framework;
38 using NUnit.Framework.Constraints;
39
40 namespace MonoTests.System.Threading.Tasks
41 {
42         [TestFixture]
43         public class ParallelTests
44         {
45                 [Test]
46                 public void ParallelForTestCase ()
47                 {
48                         int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
49                         
50                         ParallelTestHelper.Repeat (() => {      
51                                 int[] actual = Enumerable.Range (1, 1000).ToArray ();
52                                 SpinWait sw = new SpinWait ();
53                                 
54                                 Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
55
56                                 Assert.That (actual, new CollectionEquivalentConstraint (expected), "#1, same");
57                                 Assert.That (actual, new EqualConstraint (expected), "#2, in order");
58                         });
59                 }
60
61                 [Test, ExpectedException (typeof (AggregateException))]
62                 public void ParallelForExceptionTestCase ()
63                 {
64                         Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
65                 }
66                 
67                 [Test]
68                 public void ParallelForSmallRangeTest ()
69                 {
70                         ParallelTestHelper.Repeat (() => {
71                                 int test = -1;
72                                 Parallel.For (0, 1, (i) => test = i);
73                                 
74                                 Assert.AreEqual (0, test, "#1");
75                         });
76                 }
77                 
78                 [Test]
79                 public void ParallelForNoOperationTest ()
80                 {
81                         bool launched = false;
82                         Parallel.For (4, 1, (i) => launched = true);
83                         Assert.IsFalse (launched, "#1");
84                 }
85
86                 [Test]
87                 public void ParallelForNestedTest ()
88                 {
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;
94                                         });
95                                 });
96                     });
97
98                         Assert.IsTrue (launched.All ((_) => _), "All true");
99                 }
100                 
101                 [Test]
102                 public void ParallelForEachTestCase ()
103                 {
104                         ParallelTestHelper.Repeat (() => {
105                                 IEnumerable<int> e = Enumerable.Repeat(1, 500);
106                                 ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
107                                 SpinWait sw = new SpinWait ();
108                                 int count = 0;
109                                 
110                                 Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
111                                 
112                                 Assert.AreEqual (500, count, "#1");
113
114                                 Assert.That (queue, new CollectionEquivalentConstraint (e), "#2");
115                         });
116                 }
117
118                 [Test]
119                 public void ParallelForEachTestCaseWithIndex ()
120                 {
121                         var list = new List<int> { 0, 1, 2, 3, 4 };
122
123                         Parallel.ForEach (list, (l, s, i) => {
124                                 Assert.AreEqual (l, i, "#1");
125                         });
126                 }
127
128                 class ValueAndSquare
129                 { 
130                         public float Value { get; set; }
131                         public float Square { get; set; }
132                 }
133
134                 [Test]
135                 public void ParallerForEach_UserType ()
136                 {
137                         var values = new[] {
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 }
148                         };
149
150                         Parallel.ForEach (Partitioner.Create (values), l => l.Square = l.Value * l.Value);
151
152                         foreach (var item in values) {
153                                 Assert.AreEqual (item.Square, item.Value * item.Value);
154                         }
155                 }
156                         
157                 [Test, ExpectedException (typeof (AggregateException))]
158                 public void ParallelForEachExceptionTestCase ()
159                 {
160                         IEnumerable<int> e = Enumerable.Repeat (1, 10);
161                         Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
162                 }
163
164                 [Test]
165                 public void BasicInvokeTest ()
166                 {
167                         int val1 = 0, val2 = 0;
168
169                         Parallel.Invoke (() => Interlocked.Increment (ref val1), () => Interlocked.Increment (ref val2));
170                         Assert.AreEqual (1, val1, "#1");
171                         Assert.AreEqual (1, val2, "#2");
172                 }
173
174                 [Test]
175                 public void InvokeWithOneNullActionTest ()
176                 {
177                         int val1 = 0, val2 = 0;
178
179                         try {
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");
184                                 return;
185                         }
186                         Assert.Fail ("Shouldn't be there");
187                 }
188
189                 [Test]
190                 public void OneActionInvokeTest ()
191                 {
192                         int val = 0;
193
194                         Parallel.Invoke (() => Interlocked.Increment (ref val));
195                         Assert.AreEqual (1, val, "#1");
196                 }
197
198                 [Test, ExpectedException (typeof (ArgumentNullException))]
199                 public void InvokeWithNullActions ()
200                 {
201                         Parallel.Invoke ((Action[])null);
202                 }
203
204                 [Test, ExpectedException (typeof (ArgumentNullException))]
205                 public void InvokeWithNullOptions ()
206                 {
207                         Parallel.Invoke ((ParallelOptions)null, () => Thread.Sleep (100));
208                 }
209
210                 [Test]
211                 public void InvokeWithExceptions ()
212                 {
213                         try {
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)));
221                                 return;
222                         }
223                         Assert.Fail ("Shouldn't go there");
224                 }
225         }
226 }
227 #endif