5 // Marek Safar (marek.safar@gmail.com)
7 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Threading;
33 using NUnit.Framework;
35 namespace MonoTests.System.Threading
38 public class VolatileTest
41 public void ReadPrimitives ()
44 Assert.AreEqual (true, Volatile.Read (ref v1), "#v1");
47 Assert.AreEqual (4, Volatile.Read (ref v2), "#v2");
49 double v3 = double.MaxValue;
50 Assert.AreEqual (double.MaxValue, Volatile.Read (ref v3), "#v3");
52 float v4 = float.Epsilon;
53 Assert.AreEqual (float.Epsilon, Volatile.Read (ref v4), "#v4");
55 int v5 = int.MinValue;
56 Assert.AreEqual (int.MinValue, Volatile.Read (ref v5), "#v5");
58 IntPtr v6 = IntPtr.Zero;
59 Assert.AreEqual (IntPtr.Zero, Volatile.Read (ref v6), "#v6");
61 long v7 = long.MaxValue;
62 Assert.AreEqual (long.MaxValue, Volatile.Read (ref v7), "#v7");
65 Assert.AreEqual (44, Volatile.Read (ref v8), "#v8");
68 Assert.AreEqual (-999, Volatile.Read (ref v9), "#v9");
70 uint v10 = uint.MaxValue;
71 Assert.AreEqual (uint.MaxValue, Volatile.Read (ref v10), "#v10");
73 UIntPtr v11 = (UIntPtr) uint.MaxValue;
74 Assert.AreEqual (new UIntPtr (uint.MaxValue), Volatile.Read (ref v11), "#v11");
76 ulong v12 = ulong.MaxValue;
77 Assert.AreEqual (ulong.MaxValue, Volatile.Read (ref v12), "#v12");
79 ushort v13 = ushort.MaxValue;
80 Assert.AreEqual (ushort.MaxValue, Volatile.Read (ref v13), "#v13");
83 Assert.AreEqual (s, Volatile.Read (ref s));
87 public void WritePrimitives ()
90 Volatile.Write (ref v1, true);
91 Assert.AreEqual (true, v1, "#v1");
94 Volatile.Write (ref v2, 4);
95 Assert.AreEqual (4, v2, "#v2");
98 Volatile.Write (ref v3, double.MaxValue);
99 Assert.AreEqual (double.MaxValue, v3, "#v3");
102 Volatile.Write (ref v4, float.MaxValue);
103 Assert.AreEqual (float.MaxValue, v4, "#v4");
106 Volatile.Write (ref v5, int.MinValue);
107 Assert.AreEqual (int.MinValue, v5, "#v5");
109 IntPtr v6 = IntPtr.Zero;
110 Volatile.Write (ref v6, new IntPtr (5));
111 Assert.AreEqual (new IntPtr (5), v6, "#v6");
114 Volatile.Write (ref v7, long.MaxValue);
115 Assert.AreEqual (long.MaxValue, v7, "#v7");
118 Volatile.Write (ref v8, 44);
119 Assert.AreEqual (44, v8, "#v8");
122 Volatile.Write (ref v9, -999);
123 Assert.AreEqual (-999, v9, "#v9");
126 Volatile.Write (ref v10, uint.MaxValue);
127 Assert.AreEqual (uint.MaxValue, v10, "#v10");
129 UIntPtr v11 = UIntPtr.Zero;
130 Volatile.Write (ref v11, (UIntPtr) uint.MaxValue);
131 Assert.AreEqual (new UIntPtr (uint.MaxValue), v11, "#v11");
134 Volatile.Write (ref v12, ulong.MaxValue);
135 Assert.AreEqual (ulong.MaxValue, v12, "#v12");
138 Volatile.Write (ref v13, ushort.MaxValue);
139 Assert.AreEqual (ushort.MaxValue, v13, "#v13");
142 Volatile.Write (ref s, "DEF");
143 Assert.AreEqual ("DEF", s);