CountdownEventTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #if NET_4_0
  2. // CountdownEventTests.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.Threading;
  27. using NUnit.Framework;
  28. namespace ParallelFxTests
  29. {
  30. [TestFixtureAttribute]
  31. public class CountdownEventTests
  32. {
  33. CountdownEvent evt;
  34. [SetUpAttribute]
  35. public void Setup()
  36. {
  37. evt = new CountdownEvent(5);
  38. }
  39. [Test]
  40. public void InitialTestCase()
  41. {
  42. Assert.AreEqual(5, evt.InitialCount, "#1");
  43. evt.AddCount();
  44. evt.Signal(3);
  45. Assert.AreEqual(5, evt.InitialCount, "#2");
  46. }
  47. [Test]
  48. public void CurrentCountTestCase()
  49. {
  50. Assert.AreEqual(5, evt.CurrentCount, "#1");
  51. evt.AddCount();
  52. Assert.AreEqual(6, evt.CurrentCount, "#2");
  53. evt.TryAddCount(2);
  54. Assert.AreEqual(8, evt.CurrentCount, "#3");
  55. evt.Signal(4);
  56. Assert.AreEqual(4, evt.CurrentCount, "#4");
  57. evt.Reset();
  58. Assert.AreEqual(5, evt.CurrentCount, "#5");
  59. }
  60. [Test]
  61. public void IsSetTestCase()
  62. {
  63. Assert.IsFalse(evt.IsSet, "#1");
  64. evt.Signal(5);
  65. Assert.IsTrue(evt.IsSet, "#2");
  66. evt.Reset();
  67. Assert.IsFalse(evt.IsSet, "#3");
  68. }
  69. [Test]
  70. public void TryAddCountTestCase()
  71. {
  72. Assert.IsTrue(evt.TryAddCount(2), "#1");
  73. evt.Signal(7);
  74. Assert.IsFalse(evt.TryAddCount(), "#2");
  75. }
  76. [Test]
  77. public void WaitTestCase()
  78. {
  79. int count = 0;
  80. bool s = false;
  81. ParallelTestHelper.ParallelStressTest(evt, delegate (CountdownEvent e) {
  82. if (Interlocked.Increment(ref count) % 2 == 0) {
  83. Thread.Sleep(100);
  84. while(!e.IsSet)
  85. e.Signal();
  86. } else {
  87. e.Wait();
  88. s = true;
  89. }
  90. });
  91. Assert.IsTrue(s, "#1");
  92. Assert.IsTrue(evt.IsSet, "#2");
  93. }
  94. [Test]
  95. public void AddCountSignalStressTestCase()
  96. {
  97. int count = 0;
  98. ParallelTestHelper.ParallelStressTest(evt, delegate (CountdownEvent e) {
  99. int num = Interlocked.Increment(ref count);
  100. if (num % 2 == 0)
  101. e.AddCount();
  102. else
  103. e.Signal();
  104. }, 7);
  105. Assert.AreEqual(4, evt.CurrentCount, "#1");
  106. Assert.IsFalse(evt.IsSet, "#2");
  107. }
  108. }
  109. }
  110. #endif