08d14bec59858267ee60972fdcab52ec9e9e4da5
[mono.git] / mcs / class / corlib / Test / System.Threading / InterlockedTest.cs
1 //
2 // InterlockedTest.cs - NUnit Test Cases for System.Threading.Interlocked
3 //
4 // Author:
5 //   Luca Barbieri (luca.barbieri@gmail.com)
6 //
7 // (C) 2004 Luca Barbieri
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Threading;
13
14 namespace MonoTests.System.Threading
15 {
16         [TestFixture]
17         public class InterlockedTest
18         {
19                 int int32;
20                 long int64;
21                 float flt;
22                 double dbl;
23                 object obj;
24                 IntPtr iptr;
25
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;
45 #endif // TARGET_JVM
46
47                 [Test]
48                 public void TestExchange_Int32 ()
49                 {
50                         int32 = int32_1;
51                         Assert.AreEqual(int32_1, Interlocked.Exchange(ref int32, int32_2));
52                         Assert.AreEqual(int32_2, int32);
53                 }
54
55                 [Test]
56                 public void TestExchange_Flt ()
57                 {
58                         flt = flt_1;
59                         Assert.AreEqual(flt_1, Interlocked.Exchange(ref flt, flt_2));
60                         Assert.AreEqual(flt_2, flt);
61                 }
62
63                 [Test]
64                 public void TestExchange_Obj ()
65                 {
66                         obj = obj_1;
67                         Assert.AreEqual(obj_1, Interlocked.Exchange(ref obj, obj_2));
68                         Assert.AreEqual(obj_2, obj);
69                 }
70
71 #if NET_2_0
72                 [Test]
73                 public void TestExchange_Int64 ()
74                 {
75                         int64 = int64_1;
76                         Assert.AreEqual(int64_1, Interlocked.Exchange(ref int64, int64_2));
77                         Assert.AreEqual(int64_2, int64);
78                 }
79
80                 [Test]
81                 public void TestExchange_Dbl ()
82                 {
83                         dbl = dbl_1;
84                         Assert.AreEqual(dbl_1, Interlocked.Exchange(ref dbl, dbl_2));
85                         Assert.AreEqual(dbl_2, dbl);
86                 }
87
88 #if !TARGET_JVM // No support for exchanging two IntPtrs
89                 [Test]
90                 public void TestExchange_Iptr ()
91                 {
92                         iptr = iptr_1;
93                         Assert.AreEqual(iptr_1, Interlocked.Exchange(ref iptr, iptr_2));
94                         Assert.AreEqual(iptr_2, iptr);
95                 }
96 #endif // TARGET_JVM
97 #endif
98
99                 [Test]
100                 public void TestCompareExchange_Int32 ()
101                 {
102                         int32 = int32_1;
103                         Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_1));
104                         Assert.AreEqual(int32_2, int32);
105                 }
106
107                 [Test]
108                 public void TestCompareExchange_Flt ()
109                 {
110                         flt = flt_1;
111                         Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_1));
112                         Assert.AreEqual(flt_2, flt);
113                 }
114
115                 [Test]
116                 public void TestCompareExchange_Obj ()
117                 {
118                         obj = obj_1;
119                         Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_1));
120                         Assert.AreEqual(obj_2, obj);
121                 }
122
123 #if NET_2_0
124                 [Test]
125                 public void TestCompareExchange_Int64 ()
126                 {
127                         int64 = int64_1;
128                         Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_1));
129                         Assert.AreEqual(int64_2, int64);
130                 }
131
132                 [Test]
133                 public void TestCompareExchange_Dbl ()
134                 {
135                         dbl = dbl_1;
136                         Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_1));
137                         Assert.AreEqual(dbl_2, dbl);
138                 }
139
140 #if !TARGET_JVM // No support for compare exchanging two IntPtrs
141                 [Test]
142                 public void TestCompareExchange_Iptr ()
143                 {
144                         iptr = iptr_1;
145                         Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_1));
146                         Assert.AreEqual(iptr_2, iptr);
147                 }
148 #endif // TARGET_JVM
149 #endif
150
151                 [Test]
152                 public void TestCompareExchange_Failed_Int32 ()
153                 {
154                         int32 = int32_1;
155                         Assert.AreEqual(int32_1, Interlocked.CompareExchange(ref int32, int32_2, int32_3));
156                         Assert.AreEqual(int32_1, int32);
157                 }
158
159                 [Test]
160                 public void TestCompareExchange_Failed_Flt ()
161                 {
162                         flt = flt_1;
163                         Assert.AreEqual(flt_1, Interlocked.CompareExchange(ref flt, flt_2, flt_3));
164                         Assert.AreEqual(flt_1, flt);
165                 }
166
167                 [Test]
168                 public void TestCompareExchange_Failed_Obj ()
169                 {
170                         obj = obj_1;
171                         Assert.AreEqual(obj_1, Interlocked.CompareExchange(ref obj, obj_2, obj_3));
172                         Assert.AreEqual(obj_1, obj);
173                 }
174
175 #if NET_2_0
176                 [Test]
177                 public void TestCompareExchange_Failed_Int64 ()
178                 {
179                         int64 = int64_1;
180                         Assert.AreEqual(int64_1, Interlocked.CompareExchange(ref int64, int64_2, int64_3));
181                         Assert.AreEqual(int64_1, int64);
182                 }
183
184                 [Test]
185                 public void TestCompareExchange_Failed_Dbl ()
186                 {
187                         dbl = dbl_1;
188                         Assert.AreEqual(dbl_1, Interlocked.CompareExchange(ref dbl, dbl_2, dbl_3));
189                         Assert.AreEqual(dbl_1, dbl);
190                 }
191
192 #if !TARGET_JVM // No support for compare exchanging two IntPtrs
193                 [Test]
194                 public void TestCompareExchange_Failed_Iptr ()
195                 {
196                         iptr = iptr_1;
197                         Assert.AreEqual(iptr_1, Interlocked.CompareExchange(ref iptr, iptr_2, iptr_3));
198                         Assert.AreEqual(iptr_1, iptr);
199                 }
200 #endif // TARGET_JVM
201 #endif
202
203                 [Test]
204                 public void TestIncrement_Int32 ()
205                 {
206                         int32 = int32_1;
207                         Assert.AreEqual(int32_1 + 1, Interlocked.Increment(ref int32));
208                         Assert.AreEqual(int32_1 + 1, int32);
209                 }
210
211                 [Test]
212                 public void TestIncrement_Int64 ()
213                 {
214                         int64 = int64_1;
215                         Assert.AreEqual(int64_1 + 1, Interlocked.Increment(ref int64), "func");
216                         Assert.AreEqual(int64_1 + 1, int64, "value");
217                 }
218
219                 [Test]
220                 public void TestDecrement_Int32 ()
221                 {
222                         int32 = int32_1;
223                         Assert.AreEqual(int32_1 - 1, Interlocked.Decrement(ref int32));
224                         Assert.AreEqual(int32_1 - 1, int32);
225                 }
226
227                 [Test]
228                 public void TestDecrement_Int64 ()
229                 {
230                         int64 = int64_1;
231                         Assert.AreEqual(int64_1 - 1, Interlocked.Decrement(ref int64));
232                         Assert.AreEqual(int64_1 - 1, int64);
233                 }
234
235 #if NET_2_0
236                 [Test]
237                 public void TestAdd_Int32 ()
238                 {
239                         int32 = int32_1;
240                         Assert.AreEqual(int32_1 + int32_2, Interlocked.Add(ref int32, int32_2));
241                         Assert.AreEqual(int32_1 + int32_2, int32);
242                 }
243                 
244                 [Test]
245                 public void TestAdd_Int64 ()
246                 {
247                         int64 = int64_1;
248                         Assert.AreEqual(int64_1 + int64_2, Interlocked.Add(ref int64, int64_2));
249                         Assert.AreEqual(int64_1 + int64_2, int64);
250                 }
251
252                 [Test]
253                 public void TestRead_Int64()
254                 {
255                         int64 = int64_1;
256                         Assert.AreEqual(int64_1, Interlocked.Read(ref int64));
257                         Assert.AreEqual(int64_1, int64);
258                 }
259
260                 [Test]
261                 public void CompareExchange_Generic ()
262                 {
263                         object a = null;
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");
269
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");
274                 }
275
276                 [Test]
277                 public void Exchange_Generic ()
278                 {
279                         object a = null;
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");
285                 }
286 #endif
287         }
288 }