SnziTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #if NET_4_0
  2. //
  3. // SnziTests.cs
  4. //
  5. // Author:
  6. // Jérémie "Garuma" Laval <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. using System;
  28. using System.Threading;
  29. using NUnit.Framework;
  30. namespace ParallelFxTests
  31. {
  32. [TestFixtureAttribute]
  33. public class SnziTests
  34. {
  35. Snzi snzi;
  36. [SetUpAttribute]
  37. public void Setup ()
  38. {
  39. snzi = new Snzi ();
  40. }
  41. [Test]
  42. public void InitialTest ()
  43. {
  44. Assert.IsTrue (snzi.IsSet, "#1");
  45. }
  46. [Test]
  47. public void SimpleOperationTest ()
  48. {
  49. snzi.Increment ();
  50. snzi.Decrement ();
  51. Assert.IsTrue (snzi.IsSet, "#1");
  52. }
  53. [Test]
  54. public void SimpleZeroTest ()
  55. {
  56. for (int i = 0; i < 10; i++) {
  57. if (i % 2 == 0)
  58. snzi.Increment ();
  59. else
  60. snzi.Decrement ();
  61. }
  62. Assert.IsTrue (snzi.IsSet, "#1");
  63. }
  64. [Test]
  65. public void SimpleNonZeroTest ()
  66. {
  67. snzi.Increment ();
  68. for (int i = 0; i < 20; i++) {
  69. if (i % 2 == 0)
  70. snzi.Increment ();
  71. else
  72. snzi.Decrement ();
  73. if (i % 5 == 0)
  74. Thread.Sleep (0);
  75. }
  76. Assert.IsFalse (snzi.IsSet, "#1");
  77. }
  78. [Test]
  79. public void StressZeroTest ()
  80. {
  81. ParallelTestHelper.Repeat (delegate {
  82. int times = 0;
  83. ParallelTestHelper.ParallelStressTest (snzi, (s) => {
  84. int t = Interlocked.Increment (ref times);
  85. for (int i = 0; i < 20; i++) {
  86. if (i % 2 == 0)
  87. snzi.Increment ();
  88. else
  89. snzi.Decrement ();
  90. if (i % (3 * t) == 0)
  91. Thread.Sleep (0);
  92. }
  93. });
  94. Assert.IsTrue (snzi.IsSet, "#1");
  95. });
  96. }
  97. [Test]
  98. public void StressNonZeroTest ()
  99. {
  100. ParallelTestHelper.Repeat (delegate {
  101. ParallelTestHelper.ParallelStressTest (snzi, (s) => {
  102. snzi.Increment ();
  103. for (int i = 0; i < 1; i++) {
  104. if (i % 2 == 0)
  105. snzi.Increment ();
  106. else
  107. snzi.Decrement ();
  108. }
  109. });
  110. Assert.IsFalse (snzi.IsSet, "#1");
  111. });
  112. }
  113. }
  114. }
  115. #endif