InterlockedTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. [Test]
  63. public void TestExchange_Int64 ()
  64. {
  65. int64 = int64_1;
  66. Assert.AreEqual(int64_1, Interlocked.Exchange(ref int64, int64_2));
  67. Assert.AreEqual(int64_2, int64);
  68. }
  69. [Test]
  70. public void TestExchange_Dbl ()
  71. {
  72. dbl = dbl_1;
  73. Assert.AreEqual(dbl_1, Interlocked.Exchange(ref dbl, dbl_2));
  74. Assert.AreEqual(dbl_2, dbl);
  75. }
  76. [Test]
  77. public void TestExchange_Iptr ()
  78. {
  79. iptr = iptr_1;
  80. Assert.AreEqual(iptr_1, Interlocked.Exchange(ref iptr, iptr_2));
  81. Assert.AreEqual(iptr_2, iptr);
  82. }
  83. [Test]
  84. public void TestCompareExchange_Int32 ()
  85. {
  86. int32 = int32_1;
  87. Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_1));
  88. Assert.AreEqual(int32_2, int32);
  89. }
  90. [Test]
  91. public void TestCompareExchange_Flt ()
  92. {
  93. flt = flt_1;
  94. Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_1));
  95. Assert.AreEqual(flt_2, flt);
  96. }
  97. [Test]
  98. public void TestCompareExchange_Obj ()
  99. {
  100. obj = obj_1;
  101. Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_1));
  102. Assert.AreEqual(obj_2, obj);
  103. }
  104. [Test]
  105. public void TestCompareExchange_Int64 ()
  106. {
  107. int64 = int64_1;
  108. Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_1));
  109. Assert.AreEqual(int64_2, int64);
  110. }
  111. [Test]
  112. public void TestCompareExchange_Dbl ()
  113. {
  114. dbl = dbl_1;
  115. Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_1));
  116. Assert.AreEqual(dbl_2, dbl);
  117. }
  118. [Test]
  119. public void TestCompareExchange_Iptr ()
  120. {
  121. iptr = iptr_1;
  122. Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_1));
  123. Assert.AreEqual(iptr_2, iptr);
  124. }
  125. [Test]
  126. public void TestCompareExchange_Failed_Int32 ()
  127. {
  128. int32 = int32_1;
  129. Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_3));
  130. Assert.AreEqual(int32_1, int32);
  131. }
  132. [Test]
  133. public void TestCompareExchange_Failed_Flt ()
  134. {
  135. flt = flt_1;
  136. Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_3));
  137. Assert.AreEqual(flt_1, flt);
  138. }
  139. [Test]
  140. public void TestCompareExchange_Failed_Obj ()
  141. {
  142. obj = obj_1;
  143. Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_3));
  144. Assert.AreEqual(obj_1, obj);
  145. }
  146. [Test]
  147. public void TestCompareExchange_Failed_Int64 ()
  148. {
  149. int64 = int64_1;
  150. Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_3));
  151. Assert.AreEqual(int64_1, int64);
  152. }
  153. [Test]
  154. public void TestCompareExchange_Failed_Dbl ()
  155. {
  156. dbl = dbl_1;
  157. Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_3));
  158. Assert.AreEqual(dbl_1, dbl);
  159. }
  160. [Test]
  161. public void TestCompareExchange_Failed_Iptr ()
  162. {
  163. iptr = iptr_1;
  164. Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_3));
  165. Assert.AreEqual(iptr_1, iptr);
  166. }
  167. [Test]
  168. public void TestIncrement_Int32 ()
  169. {
  170. int32 = int32_1;
  171. Assert.AreEqual(int32_1 + 1, Interlocked.Increment(ref int32));
  172. Assert.AreEqual(int32_1 + 1, int32);
  173. }
  174. [Test]
  175. public void TestIncrement_Int64 ()
  176. {
  177. int64 = int64_1;
  178. Assert.AreEqual(int64_1 + 1, Interlocked.Increment(ref int64), "func");
  179. Assert.AreEqual(int64_1 + 1, int64, "value");
  180. }
  181. [Test]
  182. public void TestDecrement_Int32 ()
  183. {
  184. int32 = int32_1;
  185. Assert.AreEqual(int32_1 - 1, Interlocked.Decrement(ref int32));
  186. Assert.AreEqual(int32_1 - 1, int32);
  187. }
  188. [Test]
  189. public void TestDecrement_Int64 ()
  190. {
  191. int64 = int64_1;
  192. Assert.AreEqual(int64_1 - 1, Interlocked.Decrement(ref int64));
  193. Assert.AreEqual(int64_1 - 1, int64);
  194. }
  195. [Test]
  196. public void TestAdd_Int32 ()
  197. {
  198. int32 = int32_1;
  199. Assert.AreEqual(int32_1 + int32_2, Interlocked.Add(ref int32, int32_2));
  200. Assert.AreEqual(int32_1 + int32_2, int32);
  201. }
  202. [Test]
  203. public void TestAdd_Int64 ()
  204. {
  205. int64 = int64_1;
  206. Assert.AreEqual(int64_1 + int64_2, Interlocked.Add(ref int64, int64_2));
  207. Assert.AreEqual(int64_1 + int64_2, int64);
  208. }
  209. [Test]
  210. public void TestRead_Int64()
  211. {
  212. int64 = int64_1;
  213. Assert.AreEqual(int64_1, Interlocked.Read(ref int64));
  214. Assert.AreEqual(int64_1, int64);
  215. }
  216. [Test]
  217. public void CompareExchange_Generic ()
  218. {
  219. object a = null;
  220. Assert.IsNull (Interlocked.CompareExchange<object> (ref a, a, a), "null,null,null");
  221. object b = new object ();
  222. Assert.IsNull (Interlocked.CompareExchange<object> (ref a, a, b), "null,non-null,non-null");
  223. Assert.IsNull (Interlocked.CompareExchange<object> (ref a, b, a), "null,non-null,null");
  224. Assert.AreSame (b, Interlocked.CompareExchange<object> (ref a, b, b), "null,null,non-null");
  225. Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, a, a), "non-null,null,null");
  226. Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, a, b), "non-null,null,non-null");
  227. Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, b, a), "non-null,non-null,null");
  228. Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, b, b), "non-null,non-null,non-null");
  229. }
  230. [Test]
  231. public void Exchange_Generic ()
  232. {
  233. object a = null;
  234. Assert.IsNull (Interlocked.Exchange<object> (ref a, a), "null,null");
  235. object b = new object ();
  236. Assert.IsNull (Interlocked.Exchange<object> (ref a, b), "null,non-null");
  237. Assert.AreSame (b, Interlocked.Exchange<object> (ref b, a), "non-null,null");
  238. Assert.AreSame (b, Interlocked.Exchange<object> (ref b, b), "non-null,non-null");
  239. }
  240. }
  241. }