PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/Test/System.Threading/ParallelTests.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 125 lines | 72 code | 16 blank | 37 comment | 0 complexity | 41f909b6f3ea75c62e81eb6dc642b33c MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  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.IO;
  30. using System.Xml.Serialization;
  31. using System.Collections.Generic;
  32. using System.Collections.Concurrent;
  33. using NUnit;
  34. using NUnit.Core;
  35. using NUnit.Framework;
  36. namespace ParallelFxTests
  37. {
  38. [TestFixture()]
  39. public class ParallelTests
  40. {
  41. [Test]
  42. public void ParallelForTestCase ()
  43. {
  44. ParallelTestHelper.Repeat (() => {
  45. int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
  46. int[] actual = Enumerable.Range (1, 1000).ToArray ();
  47. SpinWait sw = new SpinWait ();
  48. Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
  49. CollectionAssert.AreEquivalent (expected, actual, "#1, same");
  50. CollectionAssert.AreEqual (expected, actual, "#2, in order");
  51. });
  52. }
  53. [Test, ExpectedException (typeof (AggregateException))]
  54. public void ParallelForExceptionTestCase ()
  55. {
  56. Parallel.For(1, 100, delegate (int i) { throw new Exception("foo"); });
  57. }
  58. [Test]
  59. public void ParallelForSmallRangeTest ()
  60. {
  61. ParallelTestHelper.Repeat (() => {
  62. int test = -1;
  63. Parallel.For (0, 1, (i) => test = i);
  64. Assert.AreEqual (0, test, "#1");
  65. });
  66. }
  67. [Test]
  68. public void ParallelForNoOperationTest ()
  69. {
  70. bool launched = false;
  71. Parallel.For (4, 1, (i) => launched = true);
  72. Assert.IsFalse (launched, "#1");
  73. }
  74. [Test]
  75. public void ParallelForEachTestCase ()
  76. {
  77. ParallelTestHelper.Repeat (() => {
  78. IEnumerable<int> e = Enumerable.Repeat(1, 500);
  79. ConcurrentQueue<int> queue = new ConcurrentQueue<int> ();
  80. SpinWait sw = new SpinWait ();
  81. int count = 0;
  82. Parallel.ForEach (e, (element) => { Interlocked.Increment (ref count); queue.Enqueue (element); sw.SpinOnce (); });
  83. Assert.AreEqual (500, count, "#1");
  84. CollectionAssert.AreEquivalent (e, queue, "#2");
  85. });
  86. }
  87. [Test, ExpectedException (typeof (AggregateException))]
  88. public void ParallelForEachExceptionTestCase ()
  89. {
  90. IEnumerable<int> e = Enumerable.Repeat (1, 10);
  91. Parallel.ForEach (e, delegate (int element) { throw new Exception ("foo"); });
  92. }
  93. /* Disabled as this is an API addition
  94. [Test]
  95. public void ParallelWhileTestCase()
  96. {
  97. ParallelTestHelper.Repeat (() => {
  98. int i = 0;
  99. int count = 0;
  100. Parallel.While (() => Interlocked.Increment (ref i) <= 10, () => Interlocked.Increment (ref count));
  101. Assert.Greater(i, 10, "#1");
  102. Assert.AreEqual(10, count, "#2");
  103. });
  104. }*/
  105. }
  106. }
  107. #endif