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