In System:
[mono.git] / mcs / class / corlib / Test / System / RuntimeMethodHandleTest.cs
1 //
2 // RuntimeMethodHandleTest.cs - Unit tests for System.RuntimeMethodHandle
3 //
4 // Author:
5 //      Gert Driesen  <drieseng@users.sourceforge.net>
6 //
7 // Copyright (C) 2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31 using System.Runtime.Serialization;
32 using System.Runtime.Serialization.Formatters.Binary;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System
37 {
38         [TestFixture]
39         public class RuntimeMethodHandleTest
40         {
41                 [Test]
42                 public void Equals_Object ()
43                 {
44                         Type type = typeof (RuntimeMethodHandleTest);
45                         RuntimeMethodHandle rmhA1 = type.GetMethod ("Equals_Object").MethodHandle;
46                         RuntimeMethodHandle rmhA2 = type.GetMethod ("Equals_Object").MethodHandle;
47                         RuntimeMethodHandle rmhB1 = type.GetMethod ("Equals_RuntimeMethodHandle").MethodHandle;
48                         RuntimeMethodHandle rmhB2 = type.GetMethod ("Equals_RuntimeMethodHandle").MethodHandle;
49
50                         Assert.IsTrue (rmhA1.Equals((object) rmhA1), "#1");
51                         Assert.IsTrue (rmhA1.Equals ((object) rmhA2), "#2");
52                         Assert.IsTrue (rmhB1.Equals ((object) rmhB1), "#3");
53                         Assert.IsTrue (rmhB2.Equals ((object) rmhB1), "#4");
54                         Assert.IsFalse (rmhA1.Equals ((object) rmhB1), "#5");
55                         Assert.IsFalse (rmhB1.Equals ((object) rmhA1), "#6");
56                         Assert.IsFalse (rmhA1.Equals (null), "#7");
57                         Assert.IsFalse (rmhA1.Equals (5), "#8");
58                 }
59
60                 [Test]
61                 public void Equals_RuntimeMethodHandle ()
62                 {
63                         Type type = typeof (RuntimeMethodHandleTest);
64                         RuntimeMethodHandle rmhA1 = type.GetMethod ("Equals_Object").MethodHandle;
65                         RuntimeMethodHandle rmhA2 = type.GetMethod ("Equals_Object").MethodHandle;
66                         RuntimeMethodHandle rmhB1 = type.GetMethod ("Equals_RuntimeMethodHandle").MethodHandle;
67                         RuntimeMethodHandle rmhB2 = type.GetMethod ("Equals_RuntimeMethodHandle").MethodHandle;
68
69                         Assert.IsTrue (rmhA1.Equals (rmhA1), "#1");
70                         Assert.IsTrue (rmhA1.Equals (rmhA2), "#2");
71                         Assert.IsTrue (rmhB1.Equals (rmhB1), "#3");
72                         Assert.IsTrue (rmhB2.Equals (rmhB1), "#4");
73                         Assert.IsFalse (rmhA1.Equals (rmhB1), "#5");
74                         Assert.IsFalse (rmhB1.Equals (rmhA1), "#6");
75                 }
76
77 #if NET_2_0
78                 [Test]
79                 public void Operators ()
80                 {
81                         Type type = typeof (RuntimeMethodHandleTest);
82                         RuntimeMethodHandle rmhA1 = type.GetMethod ("Operators").MethodHandle;
83                         RuntimeMethodHandle rmhA2 = type.GetMethod ("Operators").MethodHandle;
84                         RuntimeMethodHandle rmhB1 = type.GetMethod ("Equals_Object").MethodHandle;
85                         RuntimeMethodHandle rmhB2 = type.GetMethod ("Equals_Object").MethodHandle;
86
87                         // equality
88                         Assert.IsTrue (rmhA1 == rmhA1, "#A1");
89                         Assert.IsTrue (rmhA1 == rmhA2, "#A2");
90                         Assert.IsTrue (rmhB1 == rmhB1, "#A3");
91                         Assert.IsTrue (rmhB2 == rmhB1, "#A4");
92                         Assert.IsFalse (rmhA1 == rmhB1, "#A5");
93                         Assert.IsFalse (rmhB1 == rmhA1, "#A6");
94
95                         // inequality
96                         Assert.IsFalse (rmhA1 != rmhA1, "#B1");
97                         Assert.IsFalse (rmhA1 != rmhA2, "#B2");
98                         Assert.IsFalse (rmhB1 != rmhB1, "#B3");
99                         Assert.IsFalse (rmhB2 != rmhB1, "#B4");
100                         Assert.IsTrue (rmhA1 != rmhB1, "#B5");
101                         Assert.IsTrue (rmhB1 != rmhA1, "#B6");
102                 }
103 #endif
104
105                 [Test]
106                 [ExpectedException (typeof (SerializationException))]
107                 public void Serialization_Of_Empty_Handle ()
108                 {
109                         RuntimeMethodHandle handle = new RuntimeMethodHandle ();
110                         new BinaryFormatter ().Serialize (Stream.Null, handle);
111                 }
112         }
113 }