VolatileTest.cs 4.2 KB

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