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;
11 using NUnit.Framework;
13 namespace MonoTests.System.Runtime.CompilerServices {
16 public class RuntimeHelpersTest {
21 public override int GetHashCode () {
25 public override bool Equals (object o) {
32 public static int counter = 0;
35 counter = counter + 1;
38 public override int GetHashCode () {
42 public override bool Equals (object o) {
48 public void TestOffsetToStringData ()
51 RuntimeHelpers.OffsetToStringData,
52 RuntimeHelpers.OffsetToStringData, "OffsetToStringData is not constant");
55 public void TestGetObjectValue ()
61 Assert.AreEqual (RuntimeHelpers.GetObjectValue (null),
65 Assert.AreEqual (RuntimeHelpers.GetObjectValue (this),
71 s2 = (FooStruct)RuntimeHelpers.GetObjectValue(s1);
74 Assert.AreEqual (s2.i, 42);
75 Assert.AreEqual (s2.j, "FOO");
78 public void TestRunClassConstructor ()
80 RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
81 Assert.AreEqual (FooClass.counter, 1);
83 // Each static constructor should only be run once
84 RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
85 Assert.AreEqual (FooClass.counter, 1);
89 [ExpectedException (typeof (ArgumentException))]
90 public void RunClassConstructor_Default ()
92 RuntimeTypeHandle rth = new RuntimeTypeHandle ();
93 Assert.AreEqual (IntPtr.Zero, rth.Value, "Value");
94 RuntimeHelpers.RunClassConstructor (rth);
97 // Expected the handle here is that way, because we are going to test for an ArgumentException being thrown
98 #pragma warning disable 649
99 static RuntimeTypeHandle handle;
100 #pragma warning restore 649
102 [ExpectedException (typeof (ArgumentException))]
103 public void RunClassConstructor_Uninitialized ()
105 RuntimeHelpers.RunClassConstructor (handle);
111 throw new NotFiniteNumberException ();
116 [ExpectedException (typeof (TypeInitializationException))]
117 public void RunClassConstructor_Throw ()
119 RuntimeHelpers.RunClassConstructor (typeof (Thrower).TypeHandle);
123 public byte [] array = new byte [1];
126 static RuntimeFieldHandle rfh = typeof (Fielder).GetField ("array").FieldHandle;
128 // 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)
129 #pragma warning disable 649
130 static RuntimeFieldHandle static_rfh;
131 #pragma warning restore 649
134 [ExpectedException (typeof (ArgumentNullException))]
135 public void InitializeArray_Null ()
137 RuntimeHelpers.InitializeArray (null, rfh);
141 [ExpectedException (typeof (ArgumentNullException))]
142 public void InitializeArray_Default ()
144 RuntimeFieldHandle h = new RuntimeFieldHandle ();
145 RuntimeHelpers.InitializeArray (new Fielder ().array, h);
149 [ExpectedException (typeof (ArgumentNullException))]
150 public void InitializeArray_Uninitialized ()
152 RuntimeHelpers.InitializeArray (new Fielder ().array, static_rfh);
156 [ExpectedException (typeof (ArgumentException))]
157 public void InitializeArray ()
159 RuntimeHelpers.InitializeArray (new Fielder ().array, rfh);
162 public void TestGetHashCode ()
164 Assert.AreEqual (0, RuntimeHelpers.GetHashCode (null));
165 object o = new object ();
166 Assert.AreEqual (o.GetHashCode (), RuntimeHelpers.GetHashCode (o));
167 Assert.IsTrue (5 != RuntimeHelpers.GetHashCode (new FooClass ()));
170 public void TestEquals ()
172 Assert.IsTrue (RuntimeHelpers.Equals (null, null));
173 Assert.IsTrue (!RuntimeHelpers.Equals (new object (), null));
174 Assert.IsTrue (!RuntimeHelpers.Equals (null, new object ()));
176 FooStruct f1 = new FooStruct ();
178 FooStruct f2 = new FooStruct ();
184 Assert.IsTrue (RuntimeHelpers.Equals (o1, o2));
186 // This should do a bit-by-bit comparison for valuetypes
187 Assert.IsTrue (RuntimeHelpers.Equals (o1, o3));
188 Assert.IsTrue (!RuntimeHelpers.Equals (o1, o4));