2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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
11 using NUnit.Framework;
12
13 namespace MonoTests.System.Runtime.CompilerServices {
14
15         [TestFixture]
16         public class RuntimeHelpersTest : Assertion {
17             struct FooStruct {
18                         public int i;
19                         public string j;
20
21                         public override int GetHashCode () {
22                                 return 5;
23                         }
24
25                         public override bool Equals (object o) {
26                                 Fail ();
27                                 return false;
28                         }
29                 }
30
31                 class FooClass {
32                         public static int counter = 0;
33
34                         static FooClass () {
35                                 counter = counter + 1;
36                         }
37
38                         public override int GetHashCode () {
39                                 return 5;
40                         }
41
42                         public override bool Equals (object o) {
43                                 Fail ();
44                                 return true;
45                         }
46                 }
47
48                 public void TestOffsetToStringData () 
49                 {
50                         AssertEquals ("OffsetToStringData is not constant",
51                                                   RuntimeHelpers.OffsetToStringData,
52                                                   RuntimeHelpers.OffsetToStringData);
53                 }
54
55                 public void TestGetObjectValue ()
56                 {
57                         FooStruct s1;
58                         FooStruct s2;
59
60                         // Test null
61                         AssertEquals ("",
62                                                   RuntimeHelpers.GetObjectValue (null),
63                                                   null);
64                         
65                         // Test non-valuetype
66                         AssertEquals ("",
67                                                   RuntimeHelpers.GetObjectValue (this),
68                                                   this);
69
70                         // Test valuetype
71                         s1.i = 42;
72                         s1.j = "FOO";
73                         s2 = (FooStruct)RuntimeHelpers.GetObjectValue(s1);
74                         s1.i = 43;
75                         s1.j = "BAR";
76                         AssertEquals ("", s2.i, 42);
77                         AssertEquals ("", s2.j, "FOO");
78                 }
79
80                 public void TestRunClassConstructor ()
81                 {
82                         RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
83                         AssertEquals ("", FooClass.counter, 1);
84
85                         // Each static constructor should only be run once
86                         RuntimeHelpers.RunClassConstructor (typeof(FooClass).TypeHandle);
87                         AssertEquals ("", FooClass.counter, 1);
88                 }
89
90 #if NET_1_1
91                 public void TestGetHashCode ()
92                 {
93                         AssertEquals ("Null has hash code 0", 0, RuntimeHelpers.GetHashCode (null));
94                         object o = new object ();
95                         AssertEquals ("", o.GetHashCode (), RuntimeHelpers.GetHashCode (o));
96                         Assert ("", 5 != RuntimeHelpers.GetHashCode (new FooClass ()));
97                 }                       
98
99                 public void TestEquals ()
100                 {
101                         Assert (RuntimeHelpers.Equals (null, null));
102                         Assert (!RuntimeHelpers.Equals (new object (), null));
103                         Assert (!RuntimeHelpers.Equals (null, new object ()));
104
105                         FooStruct f1 = new FooStruct ();
106                         f1.i = 5;
107                         FooStruct f2 = new FooStruct ();
108                         f2.i = 5;
109                         object o1 = f1;
110                         object o2 = o1;
111                         object o3 = f2;
112                         object o4 = "AAA";
113                         Assert (RuntimeHelpers.Equals (o1, o2));
114
115                         // This should do a bit-by-bit comparison for valuetypes
116                         Assert (RuntimeHelpers.Equals (o1, o3));
117                         Assert (!RuntimeHelpers.Equals (o1, o4));
118                 }
119 #endif
120         }
121 }