InterlockedTest.cs 7.3 KB

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