InterlockedTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // InterlockedTest.cs - NUnit Test Cases for System.Threading.Interlocked
  3. //
  4. // Author:
  5. // Luca Barbieri ([email protected])
  6. //
  7. // (C) 2004 Luca Barbieri
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Threading;
  12. namespace MonoTests.System.Threading
  13. {
  14. [TestFixture]
  15. public class InterlockedTest
  16. {
  17. int int32;
  18. long int64;
  19. float flt;
  20. double dbl;
  21. object obj;
  22. IntPtr iptr;
  23. const int int32_1 = 0x12490082;
  24. const int int32_2 = 0x24981071;
  25. const int int32_3 = 0x36078912;
  26. const long int64_1 = 0x1412803412472901L;
  27. const long int64_2 = 0x2470232089701124L;
  28. const long int64_3 = 0x3056432945919433L;
  29. const float flt_1 = 141287.109874f;
  30. const float flt_2 = 234108.324113f;
  31. const float flt_3 = 342419.752395f;
  32. const double dbl_1 = 141287.109874;
  33. const double dbl_2 = 234108.324113;
  34. const double dbl_3 = 342419.752395;
  35. readonly object obj_1 = "obj_1";
  36. readonly object obj_2 = "obj_2";
  37. readonly object obj_3 = "obj_3";
  38. readonly IntPtr iptr_1 = (IntPtr)int32_1;
  39. readonly IntPtr iptr_2 = (IntPtr)int32_2;
  40. readonly IntPtr iptr_3 = (IntPtr)int32_3;
  41. [Test]
  42. public void TestExchange_Int32 ()
  43. {
  44. int32 = int32_1;
  45. Assert.AreEqual(int32_1, Interlocked.Exchange(ref int32, int32_2));
  46. Assert.AreEqual(int32_2, int32);
  47. }
  48. [Test]
  49. public void TestExchange_Flt ()
  50. {
  51. flt = flt_1;
  52. Assert.AreEqual(flt_1, Interlocked.Exchange(ref flt, flt_2));
  53. Assert.AreEqual(flt_2, flt);
  54. }
  55. [Test]
  56. public void TestExchange_Obj ()
  57. {
  58. obj = obj_1;
  59. Assert.AreEqual(obj_1, Interlocked.Exchange(ref obj, obj_2));
  60. Assert.AreEqual(obj_2, obj);
  61. }
  62. #if NET_2_0
  63. [Test]
  64. public void TestExchange_Int64 ()
  65. {
  66. int64 = int64_1;
  67. Assert.AreEqual(int64_1, Interlocked.Exchange(ref int64, int64_2));
  68. Assert.AreEqual(int64_2, int64);
  69. }
  70. [Test]
  71. public void TestExchange_Dbl ()
  72. {
  73. dbl = dbl_1;
  74. Assert.AreEqual(dbl_1, Interlocked.Exchange(ref dbl, dbl_2));
  75. Assert.AreEqual(dbl_2, dbl);
  76. }
  77. [Test]
  78. public void TestExchange_Iptr ()
  79. {
  80. iptr = iptr_1;
  81. Assert.AreEqual(iptr_1, Interlocked.Exchange(ref iptr, iptr_2));
  82. Assert.AreEqual(iptr_2, iptr);
  83. }
  84. #endif
  85. [Test]
  86. public void TestCompareExchange_Int32 ()
  87. {
  88. int32 = int32_1;
  89. Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_1));
  90. Assert.AreEqual(int32_2, int32);
  91. }
  92. [Test]
  93. public void TestCompareExchange_Flt ()
  94. {
  95. flt = flt_1;
  96. Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_1));
  97. Assert.AreEqual(flt_2, flt);
  98. }
  99. [Test]
  100. public void TestCompareExchange_Obj ()
  101. {
  102. obj = obj_1;
  103. Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_1));
  104. Assert.AreEqual(obj_2, obj);
  105. }
  106. #if NET_2_0
  107. [Test]
  108. public void TestCompareExchange_Int64 ()
  109. {
  110. int64 = int64_1;
  111. Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_1));
  112. Assert.AreEqual(int64_2, int64);
  113. }
  114. [Test]
  115. public void TestCompareExchange_Dbl ()
  116. {
  117. dbl = dbl_1;
  118. Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_1));
  119. Assert.AreEqual(dbl_2, dbl);
  120. }
  121. [Test]
  122. public void TestCompareExchange_Iptr ()
  123. {
  124. iptr = iptr_1;
  125. Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_1));
  126. Assert.AreEqual(iptr_2, iptr);
  127. }
  128. #endif
  129. [Test]
  130. public void TestCompareExchange_Failed_Int32 ()
  131. {
  132. int32 = int32_1;
  133. Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_3));
  134. Assert.AreEqual(int32_1, int32);
  135. }
  136. [Test]
  137. public void TestCompareExchange_Failed_Flt ()
  138. {
  139. flt = flt_1;
  140. Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_3));
  141. Assert.AreEqual(flt_1, flt);
  142. }
  143. [Test]
  144. public void TestCompareExchange_Failed_Obj ()
  145. {
  146. obj = obj_1;
  147. Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_3));
  148. Assert.AreEqual(obj_1, obj);
  149. }
  150. #if NET_2_0
  151. [Test]
  152. public void TestCompareExchange_Failed_Int64 ()
  153. {
  154. int64 = int64_1;
  155. Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_3));
  156. Assert.AreEqual(int64_1, int64);
  157. }
  158. [Test]
  159. public void TestCompareExchange_Failed_Dbl ()
  160. {
  161. dbl = dbl_1;
  162. Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_3));
  163. Assert.AreEqual(dbl_1, dbl);
  164. }
  165. [Test]
  166. public void TestCompareExchange_Failed_Iptr ()
  167. {
  168. iptr = iptr_1;
  169. Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_3));
  170. Assert.AreEqual(iptr_1, iptr);
  171. }
  172. #endif
  173. [Test]
  174. public void TestIncrement_Int32 ()
  175. {
  176. int32 = int32_1;
  177. Assert.AreEqual(int32_1 + 1, Interlocked.Increment(ref int32));
  178. Assert.AreEqual(int32_1 + 1, int32);
  179. }
  180. [Test]
  181. public void TestIncrement_Int64 ()
  182. {
  183. int64 = int64_1;
  184. Assert.AreEqual(int64_1 + 1, Interlocked.Increment(ref int64), "func");
  185. Assert.AreEqual(int64_1 + 1, int64, "value");
  186. }
  187. [Test]
  188. public void TestDecrement_Int32 ()
  189. {
  190. int32 = int32_1;
  191. Assert.AreEqual(int32_1 - 1, Interlocked.Decrement(ref int32));
  192. Assert.AreEqual(int32_1 - 1, int32);
  193. }
  194. [Test]
  195. public void TestDecrement_Int64 ()
  196. {
  197. int64 = int64_1;
  198. Assert.AreEqual(int64_1 - 1, Interlocked.Decrement(ref int64));
  199. Assert.AreEqual(int64_1 - 1, int64);
  200. }
  201. #if NET_2_0
  202. [Test]
  203. public void TestAdd_Int32 ()
  204. {
  205. int32 = int32_1;
  206. Assert.AreEqual(int32_1, Interlocked.Add(ref int32, int32_2));
  207. Assert.AreEqual(int32_1 + int32_2, int32);
  208. }
  209. [Test]
  210. public void TestAdd_Int64 ()
  211. {
  212. int64 = int64_1;
  213. Assert.AreEqual(int64_1, Interlocked.Add(ref int64, int64_2));
  214. Assert.AreEqual(int64_1 + int64_2, int64);
  215. }
  216. [Test]
  217. public void TestRead_Int64()
  218. {
  219. int64 = int64_1;
  220. Assert.AreEqual(int64_1, Interlocked.Read(ref int64));
  221. Assert.AreEqual(int64_1, int64);
  222. }
  223. #endif
  224. }
  225. }