[sgen] Make sure we don't sweep a block if we're not supposed to
[mono.git] / mcs / class / corlib / Test / System.Threading / VolatileTest.cs
1 //
2 // VolatileTest.cs
3 //
4 // Authors:
5 //       Marek Safar (marek.safar@gmail.com)
6 //
7 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_4_5
30
31 using System;
32 using System.Threading;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Threading
36 {
37         [TestFixture]
38         public class VolatileTest
39         {
40                 [Test]
41                 public void ReadPrimitives ()
42                 {
43                         bool v1 = true;
44                         Assert.AreEqual (true, Volatile.Read (ref v1), "#v1");
45
46                         byte v2 = 4;
47                         Assert.AreEqual (4, Volatile.Read (ref v2), "#v2");
48
49                         double v3 = double.MaxValue;
50                         Assert.AreEqual (double.MaxValue, Volatile.Read (ref v3), "#v3");
51
52                         float v4 = float.Epsilon;
53                         Assert.AreEqual (float.Epsilon, Volatile.Read (ref v4), "#v4");
54
55                         int v5 = int.MinValue;
56                         Assert.AreEqual (int.MinValue, Volatile.Read (ref v5), "#v5");
57
58                         IntPtr v6 = IntPtr.Zero;
59                         Assert.AreEqual (IntPtr.Zero, Volatile.Read (ref v6), "#v6");
60
61                         long v7 = long.MaxValue;
62                         Assert.AreEqual (long.MaxValue, Volatile.Read (ref v7), "#v7");
63
64                         sbyte v8 = 44;
65                         Assert.AreEqual (44, Volatile.Read (ref v8), "#v8");
66
67                         short v9 = -999;
68                         Assert.AreEqual (-999, Volatile.Read (ref v9), "#v9");
69
70                         uint v10 = uint.MaxValue;
71                         Assert.AreEqual (uint.MaxValue, Volatile.Read (ref v10), "#v10");
72
73                         UIntPtr v11 = (UIntPtr) uint.MaxValue;
74                         Assert.AreEqual (new UIntPtr (uint.MaxValue), Volatile.Read (ref v11), "#v11");
75
76                         ulong v12 = ulong.MaxValue;
77                         Assert.AreEqual (ulong.MaxValue, Volatile.Read (ref v12), "#v12");
78
79                         ushort v13 = ushort.MaxValue;
80                         Assert.AreEqual (ushort.MaxValue, Volatile.Read (ref v13), "#v13");
81
82                         string s = "ABC";
83                         Assert.AreEqual (s, Volatile.Read (ref s));
84                 }
85
86                 [Test]
87                 public void WritePrimitives ()
88                 {
89                         bool v1 = false;
90                         Volatile.Write (ref v1, true);
91                         Assert.AreEqual (true, v1, "#v1");
92
93                         byte v2 = 2;
94                         Volatile.Write (ref v2, 4);
95                         Assert.AreEqual (4, v2, "#v2");
96
97                         double v3 = 55667;
98                         Volatile.Write (ref v3, double.MaxValue);
99                         Assert.AreEqual (double.MaxValue, v3, "#v3");
100
101                         float v4 = 1;
102                         Volatile.Write (ref v4, float.MaxValue);
103                         Assert.AreEqual (float.MaxValue, v4, "#v4");
104
105                         int v5 = 0;
106                         Volatile.Write (ref v5, int.MinValue);
107                         Assert.AreEqual (int.MinValue, v5, "#v5");
108
109                         IntPtr v6 = IntPtr.Zero;
110                         Volatile.Write (ref v6, new IntPtr (5));
111                         Assert.AreEqual (new IntPtr (5), v6, "#v6");
112
113                         long v7 = 0;
114                         Volatile.Write (ref v7, long.MaxValue);
115                         Assert.AreEqual (long.MaxValue, v7, "#v7");
116
117                         sbyte v8 = 2;
118                         Volatile.Write (ref v8, 44);
119                         Assert.AreEqual (44, v8, "#v8");
120
121                         short v9 = 3;
122                         Volatile.Write (ref v9, -999);
123                         Assert.AreEqual (-999, v9, "#v9");
124
125                         uint v10 = 1;
126                         Volatile.Write (ref v10, uint.MaxValue);
127                         Assert.AreEqual (uint.MaxValue, v10, "#v10");
128
129                         UIntPtr v11 = UIntPtr.Zero;
130                         Volatile.Write (ref v11, (UIntPtr) uint.MaxValue);
131                         Assert.AreEqual (new UIntPtr (uint.MaxValue), v11, "#v11");
132
133                         ulong v12 = 0;
134                         Volatile.Write (ref v12, ulong.MaxValue);
135                         Assert.AreEqual (ulong.MaxValue, v12, "#v12");
136
137                         ushort v13 = 1;
138                         Volatile.Write (ref v13, ushort.MaxValue);
139                         Assert.AreEqual (ushort.MaxValue, v13, "#v13");
140
141                         string s = "ABC";
142                         Volatile.Write (ref s, "DEF");
143                         Assert.AreEqual ("DEF", s);
144                 }
145
146         }
147 }
148
149 #endif
150