2004-02-14 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 Test {
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         public int data;
28         
29         static void F () {
30                 Console.WriteLine ("Test.F from delegate");
31         }
32         public static string G (int a) {
33                 if (a != 2)
34                         throw new Exception ("Something went wrong in G");
35                 return "G got: " + a.ToString ();
36         }
37         public string H (int a) {
38                 if (a != 3)
39                         throw new Exception ("Something went wrong in H");
40                 return "H got: " + a.ToString () + " and " + data.ToString ();
41         }
42
43         public virtual void VF () {
44                 Console.WriteLine ("Test.VF from delegate");
45         }
46         
47         public Test () {
48                 data = 5;
49         }
50         static int Main () {
51                 // Check that creation of delegates do not runs the class cctor
52                 DoIt doit = new DoIt (B.method);
53                 if (A.b_cctor_run)
54                         return 1;
55
56                 Test test = new Test ();
57                 SimpleDelegate d = new SimpleDelegate (F);
58                 SimpleDelegate d1 = new SimpleDelegate (test.VF);
59                 NotSimpleDelegate d2 = new NotSimpleDelegate (G);
60                 NotSimpleDelegate d3 = new NotSimpleDelegate (test.H);
61                 d ();
62                 d1 ();
63                 // we run G() and H() before and after using them as delegates
64                 // to be sure we don't corrupt them.
65                 G (2);
66                 test.H (3);
67                 Console.WriteLine (d2 (2));
68                 Console.WriteLine (d3 (3));
69                 G (2);
70                 test.H (3);
71
72                 if (d.Method.Name != "F")
73                         return 1;
74
75                 if (d3.Method == null)
76                         return 1;
77                 
78                 object [] args = {3};
79                 Console.WriteLine (d3.DynamicInvoke (args));
80
81                 AnotherDelegate d4 = new AnotherDelegate (puts);
82                 if (d4.Method == null)
83                         return 1;
84
85                 Console.WriteLine (d4.Method);
86                 Console.WriteLine (d4.Method.Name);
87                 Console.WriteLine (d4.Method.DeclaringType);
88                 
89                 return 0;
90
91
92         }
93 }
94 }