InterlockedTest.cs 7.0 KB

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