Ver código fonte

2009-11-02 Jérémie Laval <[email protected]>

In class/corlib/System.Threading/:
   * Parallel.cs: Added fix for Bug #536919.

In class/corlib/Test/System.Threading/:
   * ParallelTests.cs: Added unit test for Bug #536919, cleaned up.

svn path=/trunk/mcs/; revision=145203
Jérémie Laval 16 anos atrás
pai
commit
84ffc71a15

+ 4 - 0
mcs/class/corlib/System.Threading/ChangeLog

@@ -1,3 +1,7 @@
+2009-11-02  Jérémie Laval  <[email protected]>
+
+	* Parallel.cs: Added fix for Bug #536919.
+
 2009-10-25  Sebastien Pouliot  <[email protected]>
 
 	* Thread.cs: Fix validations for Join and Sleep overloads

+ 2 - 0
mcs/class/corlib/System.Threading/Parallel.cs

@@ -120,6 +120,8 @@ namespace System.Threading
 			                    options != null && options.MaxDegreeOfParallelism != -1 ? options.MaxDegreeOfParallelism : int.MaxValue);
 			// Integer range that each task process
 			int step = Math.Min (5, (to - from) / num);
+			if (step <= 0)
+				step = 1;
 
 			// Each worker put the indexes it's responsible for here
 			// so that other worker may steal if they starve.

+ 4 - 0
mcs/class/corlib/Test/System.Threading/ChangeLog

@@ -1,3 +1,7 @@
+2009-11-02  Jérémie Laval  <[email protected]>
+
+	* ParallelTests.cs: Added unit test for Bug #536919, cleaned up.
+
 2009-10-26  Sebastien Pouliot  <[email protected]>
 
 	* InterlockedTest.cs: Add test case for generic methods

+ 29 - 21
mcs/class/corlib/Test/System.Threading/ParallelTests.cs

@@ -42,20 +42,9 @@ namespace ParallelFxTests
 	[TestFixture()]
 	public class ParallelTests
 	{
-		//int[] pixels;
-		//RayTracerApp rayTracer;
-		
-		public void Setup()
-		{
-			/*Stream stream = Assembly.GetAssembly(typeof(ParallelTests)).GetManifestResourceStream("raytracer-output.xml");
-			Console.WriteLine(stream == null);
-			XmlSerializer serializer = new XmlSerializer(typeof(int[]));
-			pixels = (int[])serializer.Deserialize(stream);
-			rayTracer = new RayTracerApp();*/
-		}
 		
 		[Test]
-		public void ParallelForTestCase()
+		public void ParallelForTestCase ()
 		{
 			ParallelTestHelper.Repeat (() => {
 				int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
@@ -64,19 +53,38 @@ namespace ParallelFxTests
 				
 				Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
 				
-				CollectionAssert.AreEquivalent (expected, actual, "#1, same pixels");
-				CollectionAssert.AreEqual (expected, actual, "#2, pixels in order");
+				CollectionAssert.AreEquivalent (expected, actual, "#1, same");
+				CollectionAssert.AreEqual (expected, actual, "#2, in order");
 			});
 		}
 
-		[Test, ExpectedException(typeof(AggregateException))]
-		public void ParallelForExceptionTestCase()
+		[Test, ExpectedException (typeof (AggregateException))]
+		public void ParallelForExceptionTestCase ()
 		{
 			Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
 		}
 		
 		[Test]
-		public void ParallelForEachTestCase()
+		public void ParallelForSmallRangeTest ()
+		{
+			ParallelTestHelper.Repeat (() => {
+				int test = -1;
+				Parallel.For (0, 1, (i) => test = i);
+				
+				Assert.AreEqual (0, test, "#1");
+			});
+		}
+		
+		[Test]
+		public void ParallelForNoOperationTest ()
+		{
+			bool launched = false;
+			Parallel.For (4, 1, (i) => launched = true);
+			Assert.IsFalse (launched, "#1");
+		}
+		
+		[Test]
+		public void ParallelForEachTestCase ()
 		{
 			ParallelTestHelper.Repeat (() => {
 				IEnumerable<int> e = Enumerable.Repeat(1, 500);
@@ -84,17 +92,17 @@ namespace ParallelFxTests
 				SpinWait sw = new SpinWait ();
 				int count = 0;
 				
-				Parallel.ForEach (e, (element) => { Interlocked.Increment(ref count); queue.Enqueue (element); sw.SpinOnce (); });
+				Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
 				
 				Assert.AreEqual (500, count, "#1");
 				CollectionAssert.AreEquivalent (e, queue, "#2");
 			});
 		}
 		
-		[Test, ExpectedException(typeof(AggregateException))]
-		public void ParallelForEachExceptionTestCse()
+		[Test, ExpectedException (typeof (AggregateException))]
+		public void ParallelForEachExceptionTestCase ()
 		{
-			IEnumerable<int> e = Enumerable.Repeat(1, 10);
+			IEnumerable<int> e = Enumerable.Repeat (1, 10);
 			Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
 		}