2 // RuntimeHelpersTest.cs - NUnit Test Cases for the System.Runtime.CompilerServices.RuntimeHelpers class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
9 using System.Runtime.CompilerServices;
10 using System.Threading;
12 using NUnit.Framework;
14 namespace MonoTests.System.Runtime.CompilerServices {
17 public class RuntimeHelpersTest {
22 public override int GetHashCode () {
26 public override bool Equals (object o) {
33 public static int counter = 0;
36 counter = counter + 1;
39 public override int GetHashCode () {
43 public override bool Equals (object o) {
50 public void TestOffsetToStringData ()
53 RuntimeHelpers.OffsetToStringData,
54 RuntimeHelpers.OffsetToStringData, "OffsetToStringData is not constant");
58 public void TestGetObjectValue ()
64 Assert.AreEqual (RuntimeHelpers.GetObjectValue (null),
68 Assert.AreEqual (RuntimeHelpers.GetObjectValue (this),
74 s2 = (FooStruct)RuntimeHelpers.GetObjectValue(s1);
77 Assert.AreEqual (s2.i, 42);
78 Assert.AreEqual (s2.j, "FOO");
82 public void TestRunClassConstructor ()
84 RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
85 Assert.AreEqual (FooClass.counter, 1);
87 // Each static constructor should only be run once
88 RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
89 Assert.AreEqual (FooClass.counter, 1);
93 [ExpectedException (typeof (ArgumentException))]
94 public void RunClassConstructor_Default ()
96 RuntimeTypeHandle rth = new RuntimeTypeHandle ();
97 Assert.AreEqual (IntPtr.Zero, rth.Value, "Value");
98 RuntimeHelpers.RunClassConstructor (rth);
101 // Expected the handle here is that way, because we are going to test for an ArgumentException being thrown
102 #pragma warning disable 649
103 static RuntimeTypeHandle handle;
104 #pragma warning restore 649
106 [ExpectedException (typeof (ArgumentException))]
107 public void RunClassConstructor_Uninitialized ()
109 RuntimeHelpers.RunClassConstructor (handle);
115 throw new NotFiniteNumberException ();
120 [ExpectedException (typeof (TypeInitializationException))]
121 public void RunClassConstructor_Throw ()
123 RuntimeHelpers.RunClassConstructor (typeof (Thrower).TypeHandle);
131 public void RunClassConstructor_Generic ()
133 RuntimeHelpers.RunClassConstructor (typeof (GClass<>).TypeHandle);
137 public byte [] array = new byte [1];
140 static RuntimeFieldHandle rfh = typeof (Fielder).GetField ("array").FieldHandle;
142 // Disable expected warning: the point of the test is to validate that an exception is thrown for something with a null (the default value in this case)
143 #pragma warning disable 649
144 static RuntimeFieldHandle static_rfh;
145 #pragma warning restore 649
148 [ExpectedException (typeof (ArgumentNullException))]
149 public void InitializeArray_Null ()
151 RuntimeHelpers.InitializeArray (null, rfh);
155 [ExpectedException (typeof (ArgumentNullException))]
156 public void InitializeArray_Default ()
158 RuntimeFieldHandle h = new RuntimeFieldHandle ();
159 RuntimeHelpers.InitializeArray (new Fielder ().array, h);
163 [ExpectedException (typeof (ArgumentNullException))]
164 public void InitializeArray_Uninitialized ()
166 RuntimeHelpers.InitializeArray (new Fielder ().array, static_rfh);
170 [ExpectedException (typeof (ArgumentException))]
171 public void InitializeArray ()
173 RuntimeHelpers.InitializeArray (new Fielder ().array, rfh);
177 public void TestGetHashCode ()
179 Assert.AreEqual (0, RuntimeHelpers.GetHashCode (null));
180 object o = new object ();
181 Assert.AreEqual (o.GetHashCode (), RuntimeHelpers.GetHashCode (o));
182 Assert.IsTrue (5 != RuntimeHelpers.GetHashCode (new FooClass ()));
186 public void TestEquals ()
188 Assert.IsTrue (RuntimeHelpers.Equals (null, null));
189 Assert.IsTrue (!RuntimeHelpers.Equals (new object (), null));
190 Assert.IsTrue (!RuntimeHelpers.Equals (null, new object ()));
192 FooStruct f1 = new FooStruct ();
194 FooStruct f2 = new FooStruct ();
200 Assert.IsTrue (RuntimeHelpers.Equals (o1, o2));
202 // This should do a bit-by-bit comparison for valuetypes
203 Assert.IsTrue (RuntimeHelpers.Equals (o1, o3));
204 Assert.IsTrue (!RuntimeHelpers.Equals (o1, o4));