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 30 using System; 31 using System.Threading; 32 using NUnit.Framework; 33 34 namespace MonoTests.System.Threading 35 { 36 [TestFixture] 37 public class VolatileTest 38 { 39 [Test] ReadPrimitives()40 public void ReadPrimitives () 41 { 42 bool v1 = true; 43 Assert.AreEqual (true, Volatile.Read (ref v1), "#v1"); 44 45 byte v2 = 4; 46 Assert.AreEqual (4, Volatile.Read (ref v2), "#v2"); 47 48 double v3 = double.MaxValue; 49 Assert.AreEqual (double.MaxValue, Volatile.Read (ref v3), "#v3"); 50 51 float v4 = float.Epsilon; 52 Assert.AreEqual (float.Epsilon, Volatile.Read (ref v4), "#v4"); 53 54 int v5 = int.MinValue; 55 Assert.AreEqual (int.MinValue, Volatile.Read (ref v5), "#v5"); 56 57 IntPtr v6 = IntPtr.Zero; 58 Assert.AreEqual (IntPtr.Zero, Volatile.Read (ref v6), "#v6"); 59 60 long v7 = long.MaxValue; 61 Assert.AreEqual (long.MaxValue, Volatile.Read (ref v7), "#v7"); 62 63 sbyte v8 = 44; 64 Assert.AreEqual (44, Volatile.Read (ref v8), "#v8"); 65 66 short v9 = -999; 67 Assert.AreEqual (-999, Volatile.Read (ref v9), "#v9"); 68 69 uint v10 = uint.MaxValue; 70 Assert.AreEqual (uint.MaxValue, Volatile.Read (ref v10), "#v10"); 71 72 UIntPtr v11 = (UIntPtr) uint.MaxValue; 73 Assert.AreEqual (new UIntPtr (uint.MaxValue), Volatile.Read (ref v11), "#v11"); 74 75 ulong v12 = ulong.MaxValue; 76 Assert.AreEqual (ulong.MaxValue, Volatile.Read (ref v12), "#v12"); 77 78 ushort v13 = ushort.MaxValue; 79 Assert.AreEqual (ushort.MaxValue, Volatile.Read (ref v13), "#v13"); 80 81 string s = "ABC"; 82 Assert.AreEqual (s, Volatile.Read (ref s)); 83 } 84 85 [Test] WritePrimitives()86 public void WritePrimitives () 87 { 88 bool v1 = false; 89 Volatile.Write (ref v1, true); 90 Assert.AreEqual (true, v1, "#v1"); 91 92 byte v2 = 2; 93 Volatile.Write (ref v2, 4); 94 Assert.AreEqual (4, v2, "#v2"); 95 96 double v3 = 55667; 97 Volatile.Write (ref v3, double.MaxValue); 98 Assert.AreEqual (double.MaxValue, v3, "#v3"); 99 100 float v4 = 1; 101 Volatile.Write (ref v4, float.MaxValue); 102 Assert.AreEqual (float.MaxValue, v4, "#v4"); 103 104 int v5 = 0; 105 Volatile.Write (ref v5, int.MinValue); 106 Assert.AreEqual (int.MinValue, v5, "#v5"); 107 108 IntPtr v6 = IntPtr.Zero; 109 Volatile.Write (ref v6, new IntPtr (5)); 110 Assert.AreEqual (new IntPtr (5), v6, "#v6"); 111 112 long v7 = 0; 113 Volatile.Write (ref v7, long.MaxValue); 114 Assert.AreEqual (long.MaxValue, v7, "#v7"); 115 116 sbyte v8 = 2; 117 Volatile.Write (ref v8, 44); 118 Assert.AreEqual (44, v8, "#v8"); 119 120 short v9 = 3; 121 Volatile.Write (ref v9, -999); 122 Assert.AreEqual (-999, v9, "#v9"); 123 124 uint v10 = 1; 125 Volatile.Write (ref v10, uint.MaxValue); 126 Assert.AreEqual (uint.MaxValue, v10, "#v10"); 127 128 UIntPtr v11 = UIntPtr.Zero; 129 Volatile.Write (ref v11, (UIntPtr) uint.MaxValue); 130 Assert.AreEqual (new UIntPtr (uint.MaxValue), v11, "#v11"); 131 132 ulong v12 = 0; 133 Volatile.Write (ref v12, ulong.MaxValue); 134 Assert.AreEqual (ulong.MaxValue, v12, "#v12"); 135 136 ushort v13 = 1; 137 Volatile.Write (ref v13, ushort.MaxValue); 138 Assert.AreEqual (ushort.MaxValue, v13, "#v13"); 139 140 string s = "ABC"; 141 Volatile.Write (ref s, "DEF"); 142 Assert.AreEqual ("DEF", s); 143 } 144 145 } 146 } 147 148 149