Remove excessive shortcut key matching in ToolStrip
[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                 class ValueAndSquare
119                 { 
120                         public float Value { get; set; }
121                         public float Square { get; set; }
122                 }
123
124                 [Test]
125                 public void ParallerForEach_UserType ()
126                 {
127                         var values = new[] {
128                                 new ValueAndSquare() { Value = 1f },
129                                 new ValueAndSquare() { Value = 2f },
130                                 new ValueAndSquare() { Value = 3f },
131                                 new ValueAndSquare() { Value = 4f },
132                                 new ValueAndSquare() { Value = 5f },
133                                 new ValueAndSquare() { Value = 6f },
134                                 new ValueAndSquare() { Value = 7f },
135                                 new ValueAndSquare() { Value = 8f },
136                                 new ValueAndSquare() { Value = 9f },
137                                 new ValueAndSquare() { Value = 10f }
138                         };
139
140                         Parallel.ForEach (Partitioner.Create (values), l => l.Square = l.Value * l.Value);
141
142                         foreach (var item in values) {
143                                 Assert.AreEqual (item.Square, item.Value * item.Value);
144                         }
145                 }
146                         
147                 [Test, ExpectedException (typeof (AggregateException))]
148                 public void ParallelForEachExceptionTestCase ()
149                 {
150                         IEnumerable<int> e = Enumerable.Repeat (1, 10);
151                         Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
152                 }
153
154                 [Test]
155                 public void BasicInvokeTest ()
156                 {
157                         int val1 = 0, val2 = 0;
158
159                         Parallel.Invoke (() => Interlocked.Increment (ref val1), () => Interlocked.Increment (ref val2));
160                         Assert.AreEqual (1, val1, "#1");
161                         Assert.AreEqual (1, val2, "#2");
162                 }
163
164                 [Test]
165                 public void InvokeWithOneNullActionTest ()
166                 {
167                         int val1 = 0, val2 = 0;
168
169                         try {
170                                 Parallel.Invoke (() => Interlocked.Increment (ref val1), null, () => Interlocked.Increment (ref val2));
171                         } catch (ArgumentException ex) {
172                                 Assert.AreEqual (0, val1, "#1");
173                                 Assert.AreEqual (0, val2, "#2");
174                                 return;
175                         }
176                         Assert.Fail ("Shouldn't be there");
177                 }
178
179                 [Test]
180                 public void OneActionInvokeTest ()
181                 {
182                         int val = 0;
183
184                         Parallel.Invoke (() => Interlocked.Increment (ref val));
185                         Assert.AreEqual (1, val, "#1");
186                 }
187
188                 [Test, ExpectedException (typeof (ArgumentNullException))]
189                 public void InvokeWithNullActions ()
190                 {
191                         Parallel.Invoke ((Action[])null);
192                 }
193
194                 [Test, ExpectedException (typeof (ArgumentNullException))]
195                 public void InvokeWithNullOptions ()
196                 {
197                         Parallel.Invoke ((ParallelOptions)null, () => Thread.Sleep (100));
198                 }
199
200                 [Test]
201                 public void InvokeWithExceptions ()
202                 {
203                         try {
204                                 Parallel.Invoke (() => { throw new ApplicationException ("foo"); }, () => { throw new IOException ("foo"); });
205                         } catch (AggregateException ex) {
206                                 Assert.AreEqual (2, ex.InnerExceptions.Count);
207                                 foreach (var e in ex.InnerExceptions)
208                                         Console.WriteLine (e.GetType ());
209                                 Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (ApplicationException)));
210                                 Assert.IsTrue (ex.InnerExceptions.Any (e => e.GetType () == typeof (IOException)));
211                                 return;
212                         }
213                         Assert.Fail ("Shouldn't go there");
214                 }
215         }
216 }
217 #endif