Merge pull request #5382 from kumpera/pedump_fix
[mono.git] / mono / tests / delegate-invoke.cs
1 using System;
2 using System.Reflection;
3 using System.Globalization;
4 using System.Diagnostics;
5
6 struct EmptyStruct {
7         public int value;
8
9         public int test() {
10                 return value + 10;
11         }
12
13         public static int test2 (ref EmptyStruct foo) {
14                 return foo.value + 20;
15         }
16 }
17
18 delegate int ActionRef (ref EmptyStruct _this);
19 delegate int ActionRef2 ();
20 delegate int ActionRef3 (EmptyStruct _this);
21
22 public struct Foo {
23         public int x,y,z;
24         public Foo (int i) {
25                 x = y = z = i;
26         }
27 }
28
29 delegate Foo NoArgDele ();
30 delegate Foo OneArgDele (Driver d);
31
32
33 public class Driver
34 {
35         public static Foo M0 () { return new Foo (10); }
36         public Foo M1 () { return new Foo (this == null ? 10 : 20); }
37         public static Foo M2 (string x) { return new Foo (30); }
38         public static Foo M2i (int x) { return new Foo (40); }
39         public static Foo M3 (object x) { return new Foo (50); }
40         public virtual Foo M4 () { return new Foo (this == null ? 60 : 70); }
41
42         public static int Main () {
43                 int r = test_0_refobj_invokes ();
44                 if (r != 0)
45                         return r;
46                 r = test_0_valuetype_invokes ();
47                 if (r != 0)
48                         return r + 20;
49                 return 0;
50         }
51
52         public static int test_0_refobj_invokes ()
53         {
54                 NoArgDele x;
55                 OneArgDele y;
56
57                 x = Driver.M0;
58                 if (x ().x != 10)
59                         return 1;
60
61                 x = new Driver ().M1;
62                 if (x ().x != 20)
63                         return 2;
64
65                 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), "10", typeof (Driver).GetMethod ("M2"));
66                 if (x ().x != 30)
67                         return 3;
68
69                 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), null, typeof (Driver).GetMethod ("M3"));
70                 if (x ().x != 50)
71                         return 4;
72
73                 y = (OneArgDele)Delegate.CreateDelegate (typeof (OneArgDele), null, typeof (Driver).GetMethod ("M4"));
74                 if (y (new Driver ()).x != 70)
75                         return 5;
76
77                 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), null, typeof (Driver).GetMethod ("M1"));
78                 if (x ().x != 10)
79                         return 6;
80
81                 x = (NoArgDele)Delegate.CreateDelegate (typeof (NoArgDele), null, typeof (Driver).GetMethod ("M4"));
82                 if (x ().x != 60)
83                         return 7;
84
85                 return 0;
86         }
87
88     public static int test_0_valuetype_invokes ()
89     {
90                 EmptyStruct es = default (EmptyStruct);
91                 es.value = 100;
92
93                 var ar1 = (ActionRef)Delegate.CreateDelegate(typeof (ActionRef), typeof (EmptyStruct).GetMethod("test"));
94                 if (ar1 (ref es) != 110) {
95                         Console.WriteLine ("expected 110, got {0}", ar1 (ref es));
96                         return 1;
97                 }
98
99                 var ar2 = (ActionRef2)Delegate.CreateDelegate (typeof (ActionRef2), null, typeof (EmptyStruct).GetMethod("test"));
100                 try {
101                         Console.WriteLine ("must not return, got {0}", ar2 ());
102                         return 2;
103                 } catch (NullReferenceException) {
104                 }
105
106                 ar1 = (ActionRef) Delegate.CreateDelegate(typeof (ActionRef), typeof (EmptyStruct).GetMethod("test2"));
107                 if (ar1 (ref es) != 120) {
108                         Console.WriteLine ("expected 120, got {0}", ar1 (ref es));
109                         return 3;
110                 }
111
112                 ar2 = (ActionRef2) Delegate.CreateDelegate(typeof (ActionRef2), es, typeof (EmptyStruct).GetMethod("test"));
113                 if (ar2 () != 110) {
114                         Console.WriteLine ("expected 110 got {0}", ar2 ());
115                         return 4;
116                 }
117
118                 try {
119                         Delegate.CreateDelegate(typeof (ActionRef2), new EmptyStruct (), typeof (EmptyStruct).GetMethod("test2"));
120                         Console.WriteLine ("must fail/2");
121                         return 5;
122                 } catch (ArgumentException) {}
123
124                 try {
125                         Delegate.CreateDelegate(typeof (ActionRef3), typeof (EmptyStruct).GetMethod("test"));
126                         Console.WriteLine ("must fail/2");
127                         return 6;
128                 } catch (ArgumentException) {}
129
130                 return 0;
131         }
132 }