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