2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / Test / System.Threading.Tasks / ParallelTests.cs
1 #if NET_4_0
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 using System;
27 using System.Linq;
28 using System.Reflection;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.IO;
32 using System.Xml.Serialization;
33 using System.Collections.Generic;
34 using System.Collections.Concurrent;
35
36 using NUnit;
37 using NUnit.Core;
38 using NUnit.Framework;
39
40 namespace MonoTests.System.Threading.Tasks
41 {
42         
43         [TestFixture()]
44         public class ParallelTests
45         {
46                 
47                 [Test]
48                 public void ParallelForTestCase ()
49                 {
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 ();
54                                 
55                                 Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
56                                 
57                                 CollectionAssert.AreEquivalent (expected, actual, "#1, same");
58                                 CollectionAssert.AreEqual (expected, actual, "#2, in order");
59                         });
60                 }
61
62                 [Test, ExpectedException (typeof (AggregateException))]
63                 public void ParallelForExceptionTestCase ()
64                 {
65                         Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
66                 }
67                 
68                 [Test]
69                 public void ParallelForSmallRangeTest ()
70                 {
71                         ParallelTestHelper.Repeat (() => {
72                                 int test = -1;
73                                 Parallel.For (0, 1, (i) => test = i);
74                                 
75                                 Assert.AreEqual (0, test, "#1");
76                         });
77                 }
78                 
79                 [Test]
80                 public void ParallelForNoOperationTest ()
81                 {
82                         bool launched = false;
83                         Parallel.For (4, 1, (i) => launched = true);
84                         Assert.IsFalse (launched, "#1");
85                 }
86                 
87                 [Test]
88                 public void ParallelForEachTestCase ()
89                 {
90                         ParallelTestHelper.Repeat (() => {
91                                 IEnumerable<int> e = Enumerable.Repeat(1, 500);
92                                 ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
93                                 SpinWait sw = new SpinWait ();
94                                 int count = 0;
95                                 
96                                 Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
97                                 
98                                 Assert.AreEqual (500, count, "#1");
99                                 CollectionAssert.AreEquivalent (e, queue, "#2");
100                         });
101                 }
102                 
103                 [Test, ExpectedException (typeof (AggregateException))]
104                 public void ParallelForEachExceptionTestCase ()
105                 {
106                         IEnumerable<int> e = Enumerable.Repeat (1, 10);
107                         Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
108                 }
109                 
110                 /* Disabled as this is an API addition
111                 [Test]
112                 public void ParallelWhileTestCase()
113                 {
114                         ParallelTestHelper.Repeat (() => {
115                                 int i = 0;
116                                 int count = 0;
117                                 
118                                 Parallel.While (() => Interlocked.Increment (ref i) <= 10, () => Interlocked.Increment (ref count));
119                                 
120                                 Assert.Greater(i, 10, "#1");
121                                 Assert.AreEqual(10, count, "#2");
122                         });
123                 }*/
124         }
125 }
126 #endif