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