ParallelTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. using System;
  26. using System.Linq;
  27. using System.Reflection;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using System.IO;
  31. using System.Xml.Serialization;
  32. using System.Collections.Generic;
  33. using System.Collections.Concurrent;
  34. using NUnit;
  35. using NUnit.Core;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Threading.Tasks
  38. {
  39. [TestFixture()]
  40. public class ParallelTests
  41. {
  42. [Test]
  43. public void ParallelForTestCase ()
  44. {
  45. int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
  46. ParallelTestHelper.Repeat (() => {
  47. int[] actual = Enumerable.Range (1, 1000).ToArray ();
  48. SpinWait sw = new SpinWait ();
  49. Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
  50. CollectionAssert.AreEquivalent (expected, actual, "#1, same");
  51. CollectionAssert.AreEqual (expected, actual, "#2, in order");
  52. });
  53. }
  54. [Test, ExpectedException (typeof (AggregateException))]
  55. public void ParallelForExceptionTestCase ()
  56. {
  57. Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
  58. }
  59. [Test]
  60. public void ParallelForSmallRangeTest ()
  61. {
  62. ParallelTestHelper.Repeat (() => {
  63. int test = -1;
  64. Parallel.For (0, 1, (i) => test = i);
  65. Assert.AreEqual (0, test, "#1");
  66. });
  67. }
  68. [Test]
  69. public void ParallelForNoOperationTest ()
  70. {
  71. bool launched = false;
  72. Parallel.For (4, 1, (i) => launched = true);
  73. Assert.IsFalse (launched, "#1");
  74. }
  75. [Test]
  76. public void ParallelForEachTestCase ()
  77. {
  78. ParallelTestHelper.Repeat (() => {
  79. IEnumerable<int> e = Enumerable.Repeat(1, 500);
  80. ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
  81. SpinWait sw = new SpinWait ();
  82. int count = 0;
  83. Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
  84. Assert.AreEqual (500, count, "#1");
  85. CollectionAssert.AreEquivalent (e, queue, "#2");
  86. });
  87. }
  88. [Test, ExpectedException (typeof (AggregateException))]
  89. public void ParallelForEachExceptionTestCase ()
  90. {
  91. IEnumerable<int> e = Enumerable.Repeat (1, 10);
  92. Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
  93. }
  94. }
  95. }
  96. #endif