1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
//
// RuntimeHelpersTest.cs - NUnit Test Cases for the System.Runtime.CompilerServices.RuntimeHelpers class
//
// Zoltan Varga (vargaz@freemail.hu)
//
// (C) Ximian, Inc. http://www.ximian.com
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using NUnit.Framework;
namespace MonoTests.System.Runtime.CompilerServices {
[TestFixture]
public class RuntimeHelpersTest {
struct FooStruct {
public int i;
public string j;
public override int GetHashCode () {
return 5;
}
public override bool Equals (object o) {
Assert.Fail ();
return false;
}
}
class FooClass {
public static int counter = 0;
static FooClass () {
counter = counter + 1;
}
public override int GetHashCode () {
return 5;
}
public override bool Equals (object o) {
Assert.Fail ();
return true;
}
}
[Test]
public void TestOffsetToStringData ()
{
Assert.AreEqual (
RuntimeHelpers.OffsetToStringData,
RuntimeHelpers.OffsetToStringData, "OffsetToStringData is not constant");
}
[Test]
public void TestGetObjectValue ()
{
FooStruct s1;
FooStruct s2;
// Test null
Assert.AreEqual (RuntimeHelpers.GetObjectValue (null),
null);
// Test non-valuetype
Assert.AreEqual (RuntimeHelpers.GetObjectValue (this),
this);
// Test valuetype
s1.i = 42;
s1.j = "FOO";
s2 = (FooStruct)RuntimeHelpers.GetObjectValue(s1);
s1.i = 43;
s1.j = "BAR";
Assert.AreEqual (s2.i, 42);
Assert.AreEqual (s2.j, "FOO");
}
[Test]
public void TestRunClassConstructor ()
{
RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
Assert.AreEqual (FooClass.counter, 1);
// Each static constructor should only be run once
RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
Assert.AreEqual (FooClass.counter, 1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void RunClassConstructor_Default ()
{
RuntimeTypeHandle rth = new RuntimeTypeHandle ();
Assert.AreEqual (IntPtr.Zero, rth.Value, "Value");
RuntimeHelpers.RunClassConstructor (rth);
}
// Expected the handle here is that way, because we are going to test for an ArgumentException being thrown
#pragma warning disable 649
static RuntimeTypeHandle handle;
#pragma warning restore 649
[Test]
[ExpectedException (typeof (ArgumentException))]
public void RunClassConstructor_Uninitialized ()
{
RuntimeHelpers.RunClassConstructor (handle);
}
class Thrower {
static Thrower ()
{
throw new NotFiniteNumberException ();
}
}
[Test]
[ExpectedException (typeof (TypeInitializationException))]
public void RunClassConstructor_Throw ()
{
RuntimeHelpers.RunClassConstructor (typeof (Thrower).TypeHandle);
}
class GClass<T> {
protected T Field;
}
[Test]
public void RunClassConstructor_Generic ()
{
RuntimeHelpers.RunClassConstructor (typeof (GClass<>).TypeHandle);
}
class Fielder {
public byte [] array = new byte [1];
}
static RuntimeFieldHandle rfh = typeof (Fielder).GetField ("array").FieldHandle;
// 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)
#pragma warning disable 649
static RuntimeFieldHandle static_rfh;
#pragma warning restore 649
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void InitializeArray_Null ()
{
RuntimeHelpers.InitializeArray (null, rfh);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void InitializeArray_Default ()
{
RuntimeFieldHandle h = new RuntimeFieldHandle ();
RuntimeHelpers.InitializeArray (new Fielder ().array, h);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void InitializeArray_Uninitialized ()
{
RuntimeHelpers.InitializeArray (new Fielder ().array, static_rfh);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void InitializeArray ()
{
RuntimeHelpers.InitializeArray (new Fielder ().array, rfh);
}
[Test]
public void TestGetHashCode ()
{
Assert.AreEqual (0, RuntimeHelpers.GetHashCode (null));
object o = new object ();
Assert.AreEqual (o.GetHashCode (), RuntimeHelpers.GetHashCode (o));
Assert.IsTrue (5 != RuntimeHelpers.GetHashCode (new FooClass ()));
}
[Test]
public void TestEquals ()
{
Assert.IsTrue (RuntimeHelpers.Equals (null, null));
Assert.IsTrue (!RuntimeHelpers.Equals (new object (), null));
Assert.IsTrue (!RuntimeHelpers.Equals (null, new object ()));
FooStruct f1 = new FooStruct ();
f1.i = 5;
FooStruct f2 = new FooStruct ();
f2.i = 5;
object o1 = f1;
object o2 = o1;
object o3 = f2;
object o4 = "AAA";
Assert.IsTrue (RuntimeHelpers.Equals (o1, o2));
// This should do a bit-by-bit comparison for valuetypes
Assert.IsTrue (RuntimeHelpers.Equals (o1, o3));
Assert.IsTrue (!RuntimeHelpers.Equals (o1, o4));
}
[Test]
[Category ("MultiThreaded")]
public void TestEnsureSufficientExecutionStack ()
{
var t = new Thread (() => {
unsafe {
var s = stackalloc byte [1024];
}
RuntimeHelpers.EnsureSufficientExecutionStack ();
});
t.Start ();
t.Join ();
}
}
}
|