2 // InterlockedTest.cs - NUnit Test Cases for System.Threading.Interlocked
5 // Luca Barbieri (luca.barbieri@gmail.com)
7 // (C) 2004 Luca Barbieri
10 using NUnit.Framework;
12 using System.Threading;
14 namespace MonoTests.System.Threading
17 public class InterlockedTest
26 const int int32_1 = 0x12490082;
27 const int int32_2 = 0x24981071;
28 const int int32_3 = 0x36078912;
29 const long int64_1 = 0x1412803412472901L;
30 const long int64_2 = 0x2470232089701124L;
31 const long int64_3 = 0x3056432945919433L;
32 const float flt_1 = 141287.109874f;
33 const float flt_2 = 234108.324113f;
34 const float flt_3 = 342419.752395f;
35 const double dbl_1 = 141287.109874;
36 const double dbl_2 = 234108.324113;
37 const double dbl_3 = 342419.752395;
38 readonly object obj_1 = "obj_1";
39 readonly object obj_2 = "obj_2";
40 readonly object obj_3 = "obj_3";
41 #if !TARGET_JVM // No support for exchanging two IntPtrs
42 readonly IntPtr iptr_1 = (IntPtr)int32_1;
43 readonly IntPtr iptr_2 = (IntPtr)int32_2;
44 readonly IntPtr iptr_3 = (IntPtr)int32_3;
48 public void TestExchange_Int32 ()
51 Assert.AreEqual(int32_1, Interlocked.Exchange(ref int32, int32_2));
52 Assert.AreEqual(int32_2, int32);
56 public void TestExchange_Flt ()
59 Assert.AreEqual(flt_1, Interlocked.Exchange(ref flt, flt_2));
60 Assert.AreEqual(flt_2, flt);
64 public void TestExchange_Obj ()
67 Assert.AreEqual(obj_1, Interlocked.Exchange(ref obj, obj_2));
68 Assert.AreEqual(obj_2, obj);
73 public void TestExchange_Int64 ()
76 Assert.AreEqual(int64_1, Interlocked.Exchange(ref int64, int64_2));
77 Assert.AreEqual(int64_2, int64);
81 public void TestExchange_Dbl ()
84 Assert.AreEqual(dbl_1, Interlocked.Exchange(ref dbl, dbl_2));
85 Assert.AreEqual(dbl_2, dbl);
88 #if !TARGET_JVM // No support for exchanging two IntPtrs
90 public void TestExchange_Iptr ()
93 Assert.AreEqual(iptr_1, Interlocked.Exchange(ref iptr, iptr_2));
94 Assert.AreEqual(iptr_2, iptr);
100 public void TestCompareExchange_Int32 ()
103 Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_1));
104 Assert.AreEqual(int32_2, int32);
108 public void TestCompareExchange_Flt ()
111 Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_1));
112 Assert.AreEqual(flt_2, flt);
116 public void TestCompareExchange_Obj ()
119 Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_1));
120 Assert.AreEqual(obj_2, obj);
125 public void TestCompareExchange_Int64 ()
128 Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_1));
129 Assert.AreEqual(int64_2, int64);
133 public void TestCompareExchange_Dbl ()
136 Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_1));
137 Assert.AreEqual(dbl_2, dbl);
140 #if !TARGET_JVM // No support for compare exchanging two IntPtrs
142 public void TestCompareExchange_Iptr ()
145 Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_1));
146 Assert.AreEqual(iptr_2, iptr);
152 public void TestCompareExchange_Failed_Int32 ()
155 Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_3));
156 Assert.AreEqual(int32_1, int32);
160 public void TestCompareExchange_Failed_Flt ()
163 Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_3));
164 Assert.AreEqual(flt_1, flt);
168 public void TestCompareExchange_Failed_Obj ()
171 Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_3));
172 Assert.AreEqual(obj_1, obj);
177 public void TestCompareExchange_Failed_Int64 ()
180 Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_3));
181 Assert.AreEqual(int64_1, int64);
185 public void TestCompareExchange_Failed_Dbl ()
188 Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_3));
189 Assert.AreEqual(dbl_1, dbl);
192 #if !TARGET_JVM // No support for compare exchanging two IntPtrs
194 public void TestCompareExchange_Failed_Iptr ()
197 Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_3));
198 Assert.AreEqual(iptr_1, iptr);
204 public void TestIncrement_Int32 ()
207 Assert.AreEqual(int32_1 + 1, Interlocked.Increment(ref int32));
208 Assert.AreEqual(int32_1 + 1, int32);
212 public void TestIncrement_Int64 ()
215 Assert.AreEqual(int64_1 + 1, Interlocked.Increment(ref int64), "func");
216 Assert.AreEqual(int64_1 + 1, int64, "value");
220 public void TestDecrement_Int32 ()
223 Assert.AreEqual(int32_1 - 1, Interlocked.Decrement(ref int32));
224 Assert.AreEqual(int32_1 - 1, int32);
228 public void TestDecrement_Int64 ()
231 Assert.AreEqual(int64_1 - 1, Interlocked.Decrement(ref int64));
232 Assert.AreEqual(int64_1 - 1, int64);
237 public void TestAdd_Int32 ()
240 Assert.AreEqual(int32_1 + int32_2, Interlocked.Add(ref int32, int32_2));
241 Assert.AreEqual(int32_1 + int32_2, int32);
245 public void TestAdd_Int64 ()
248 Assert.AreEqual(int64_1 + int64_2, Interlocked.Add(ref int64, int64_2));
249 Assert.AreEqual(int64_1 + int64_2, int64);
253 public void TestRead_Int64()
256 Assert.AreEqual(int64_1, Interlocked.Read(ref int64));
257 Assert.AreEqual(int64_1, int64);
261 public void CompareExchange_Generic ()
264 Assert.IsNull (Interlocked.CompareExchange<object> (ref a, a, a), "null,null,null");
265 object b = new object ();
266 Assert.IsNull (Interlocked.CompareExchange<object> (ref a, a, b), "null,non-null,non-null");
267 Assert.IsNull (Interlocked.CompareExchange<object> (ref a, b, a), "null,non-null,null");
268 Assert.AreSame (b, Interlocked.CompareExchange<object> (ref a, b, b), "null,null,non-null");
270 Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, a, a), "non-null,null,null");
271 Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, a, b), "non-null,null,non-null");
272 Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, b, a), "non-null,non-null,null");
273 Assert.AreSame (b, Interlocked.CompareExchange<object> (ref b, b, b), "non-null,non-null,non-null");
277 public void Exchange_Generic ()
280 Assert.IsNull (Interlocked.Exchange<object> (ref a, a), "null,null");
281 object b = new object ();
282 Assert.IsNull (Interlocked.Exchange<object> (ref a, b), "null,non-null");
283 Assert.AreSame (b, Interlocked.Exchange<object> (ref b, a), "non-null,null");
284 Assert.AreSame (b, Interlocked.Exchange<object> (ref b, b), "non-null,non-null");