2005-08-23 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / tests / delegate.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 class A {
5         public static bool b_cctor_run = false;
6 }
7
8 class B {
9         static B () {
10                 A.b_cctor_run = true;
11         }
12         public static void method () {
13         }
14 }
15
16 delegate void DoIt ();
17
18 namespace Bah {
19 class Tests {
20         [DllImport("cygwin1.dll", EntryPoint="puts", CharSet=CharSet.Ansi)]
21         public static extern int puts (string name);
22
23         delegate void SimpleDelegate ();
24         delegate string NotSimpleDelegate (int a);
25         delegate int AnotherDelegate (string s);
26
27         delegate string StringDelegate (); 
28         
29         public int data;
30         
31         static void F () {
32                 Console.WriteLine ("Test.F from delegate");
33         }
34         public static string G (int a) {
35                 if (a != 2)
36                         throw new Exception ("Something went wrong in G");
37                 return "G got: " + a.ToString ();
38         }
39         public string H (int a) {
40                 if (a != 3)
41                         throw new Exception ("Something went wrong in H");
42                 return "H got: " + a.ToString () + " and " + data.ToString ();
43         }
44
45         public virtual void VF () {
46                 Console.WriteLine ("Test.VF from delegate");
47         }
48         
49         public Tests () {
50                 data = 5;
51         }
52
53         static int Main (String[] args) {
54                 return TestDriver.RunTests (typeof (Tests), args);
55         }
56
57         public static int test_0_tests () {
58                 // Check that creation of delegates do not runs the class cctor
59                 DoIt doit = new DoIt (B.method);
60                 if (A.b_cctor_run)
61                         return 1;
62
63                 Tests test = new Tests ();
64                 SimpleDelegate d = new SimpleDelegate (F);
65                 SimpleDelegate d1 = new SimpleDelegate (test.VF);
66                 NotSimpleDelegate d2 = new NotSimpleDelegate (G);
67                 NotSimpleDelegate d3 = new NotSimpleDelegate (test.H);
68                 d ();
69                 d1 ();
70                 // we run G() and H() before and after using them as delegates
71                 // to be sure we don't corrupt them.
72                 G (2);
73                 test.H (3);
74                 Console.WriteLine (d2 (2));
75                 Console.WriteLine (d3 (3));
76                 G (2);
77                 test.H (3);
78
79                 if (d.Method.Name != "F")
80                         return 1;
81
82                 if (d3.Method == null)
83                         return 1;
84                 
85                 object [] args = {3};
86                 Console.WriteLine (d3.DynamicInvoke (args));
87
88                 AnotherDelegate d4 = new AnotherDelegate (puts);
89                 if (d4.Method == null)
90                         return 1;
91
92                 Console.WriteLine (d4.Method);
93                 Console.WriteLine (d4.Method.Name);
94                 Console.WriteLine (d4.Method.DeclaringType);
95                 
96                 return 0;
97         }
98
99         public static int test_0_unbox_this () {
100                 int x = 10;
101                 StringDelegate d5 = new StringDelegate (x.ToString);
102                 return d5 () == "10" ? 0 : 1;
103         }
104
105         delegate long LongDelegate (long l);
106
107         static long long_delegate (long l) {
108                 return l + 1;
109         }
110
111         public static int test_56_long () {
112                 LongDelegate l = new LongDelegate (long_delegate);
113
114                 return (int)l (55);
115         }
116
117         delegate float FloatDelegate (float l);
118
119         static float float_delegate (float l) {
120                 return l + 1;
121         }
122
123         public static int test_56_float () {
124                 FloatDelegate l = new FloatDelegate (float_delegate);
125
126                 return (int)l (55);
127         }
128
129         delegate double DoubleDelegate (double l);
130
131         static double double_delegate (double l) {
132                 return l + 1;
133         }
134
135         public static int test_56_double () {
136                 DoubleDelegate l = new DoubleDelegate (double_delegate);
137
138                 return (int)l (55);
139         }
140
141 }
142 }