VolatileTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // VolatileTest.cs
  3. //
  4. // Authors:
  5. // Marek Safar ([email protected])
  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. #if NET_4_5
  29. using System;
  30. using System.Threading;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Threading
  33. {
  34. [TestFixture]
  35. public class VolatileTest
  36. {
  37. [Test]
  38. public void ReadPrimitives ()
  39. {
  40. bool v1 = true;
  41. Assert.AreEqual (true, Volatile.Read (ref v1), "#v1");
  42. byte v2 = 4;
  43. Assert.AreEqual (4, Volatile.Read (ref v2), "#v2");
  44. double v3 = double.MaxValue;
  45. Assert.AreEqual (double.MaxValue, Volatile.Read (ref v3), "#v3");
  46. float v4 = float.Epsilon;
  47. Assert.AreEqual (float.Epsilon, Volatile.Read (ref v4), "#v4");
  48. int v5 = int.MinValue;
  49. Assert.AreEqual (int.MinValue, Volatile.Read (ref v5), "#v5");
  50. IntPtr v6 = IntPtr.Zero;
  51. Assert.AreEqual (IntPtr.Zero, Volatile.Read (ref v6), "#v6");
  52. long v7 = long.MaxValue;
  53. Assert.AreEqual (long.MaxValue, Volatile.Read (ref v7), "#v7");
  54. sbyte v8 = 44;
  55. Assert.AreEqual (44, Volatile.Read (ref v8), "#v8");
  56. short v9 = -999;
  57. Assert.AreEqual (-999, Volatile.Read (ref v9), "#v9");
  58. uint v10 = uint.MaxValue;
  59. Assert.AreEqual (uint.MaxValue, Volatile.Read (ref v10), "#v10");
  60. UIntPtr v11 = (UIntPtr) uint.MaxValue;
  61. Assert.AreEqual (new UIntPtr (uint.MaxValue), Volatile.Read (ref v11), "#v11");
  62. ulong v12 = ulong.MaxValue;
  63. Assert.AreEqual (ulong.MaxValue, Volatile.Read (ref v12), "#v12");
  64. ushort v13 = ushort.MaxValue;
  65. Assert.AreEqual (ushort.MaxValue, Volatile.Read (ref v13), "#v13");
  66. string s = "ABC";
  67. Assert.AreEqual (s, Volatile.Read (ref s));
  68. }
  69. [Test]
  70. public void WritePrimitives ()
  71. {
  72. bool v1 = false;
  73. Volatile.Write (ref v1, true);
  74. Assert.AreEqual (true, v1, "#v1");
  75. byte v2 = 2;
  76. Volatile.Write (ref v2, 4);
  77. Assert.AreEqual (4, v2, "#v2");
  78. double v3 = 55667;
  79. Volatile.Write (ref v3, double.MaxValue);
  80. Assert.AreEqual (double.MaxValue, v3, "#v3");
  81. float v4 = 1;
  82. Volatile.Write (ref v4, float.MaxValue);
  83. Assert.AreEqual (float.MaxValue, v4, "#v4");
  84. int v5 = 0;
  85. Volatile.Write (ref v5, int.MinValue);
  86. Assert.AreEqual (int.MinValue, v5, "#v5");
  87. IntPtr v6 = IntPtr.Zero;
  88. Volatile.Write (ref v6, new IntPtr (5));
  89. Assert.AreEqual (new IntPtr (5), v6, "#v6");
  90. long v7 = 0;
  91. Volatile.Write (ref v7, long.MaxValue);
  92. Assert.AreEqual (long.MaxValue, v7, "#v7");
  93. sbyte v8 = 2;
  94. Volatile.Write (ref v8, 44);
  95. Assert.AreEqual (44, v8, "#v8");
  96. short v9 = 3;
  97. Volatile.Write (ref v9, -999);
  98. Assert.AreEqual (-999, v9, "#v9");
  99. uint v10 = 1;
  100. Volatile.Write (ref v10, uint.MaxValue);
  101. Assert.AreEqual (uint.MaxValue, v10, "#v10");
  102. UIntPtr v11 = UIntPtr.Zero;
  103. Volatile.Write (ref v11, (UIntPtr) uint.MaxValue);
  104. Assert.AreEqual (new UIntPtr (uint.MaxValue), v11, "#v11");
  105. ulong v12 = 0;
  106. Volatile.Write (ref v12, ulong.MaxValue);
  107. Assert.AreEqual (ulong.MaxValue, v12, "#v12");
  108. ushort v13 = 1;
  109. Volatile.Write (ref v13, ushort.MaxValue);
  110. Assert.AreEqual (ushort.MaxValue, v13, "#v13");
  111. string s = "ABC";
  112. Volatile.Write (ref s, "DEF");
  113. Assert.AreEqual ("DEF", s);
  114. }
  115. }
  116. }
  117. #endif