[netcore] Ongoing work. (#13391)
[mono-project.git] / mono / tests / delegate.cs
blob4da5db391f2929b09cd5d7ca4d863a95f1a48672
1 using System;
2 using System.Runtime.InteropServices;
4 class A {
5 public static bool b_cctor_run = false;
8 class B {
9 static B () {
10 A.b_cctor_run = true;
12 public static void method () {
16 delegate void DoIt ();
18 namespace Bah {
19 class Tests {
20 [DllImport("cygwin1.dll", EntryPoint="puts", CharSet=CharSet.Ansi)]
21 public static extern int puts (string name);
23 delegate void SimpleDelegate ();
24 delegate string NotSimpleDelegate (int a);
25 delegate int AnotherDelegate (string s);
27 delegate string StringDelegate ();
29 public int data;
31 static void F () {
32 Console.WriteLine ("Test.F from delegate");
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 ();
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 ();
45 public virtual void VF () {
46 Console.WriteLine ("Test.VF from delegate");
49 public Tests () {
50 data = 5;
53 static int Main (String[] args) {
54 return TestDriver.RunTests (typeof (Tests), args);
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;
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);
79 if (d.Method.Name != "F")
80 return 2;
82 if (d3.Method == null)
83 return 3;
85 object [] args = {3};
86 Console.WriteLine (d3.DynamicInvoke (args));
88 AnotherDelegate d4 = new AnotherDelegate (puts);
89 if (d4.Method == null)
90 return 4;
92 Console.WriteLine (d4.Method);
93 Console.WriteLine (d4.Method.Name);
94 Console.WriteLine (d4.Method.DeclaringType);
96 return 0;
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;
105 delegate long LongDelegate (long l);
107 static long long_delegate (long l) {
108 return l + 1;
111 public static int test_56_long () {
112 LongDelegate l = new LongDelegate (long_delegate);
114 return (int)l (55);
117 delegate float FloatDelegate (float l);
119 static float float_delegate (float l) {
120 return l + 1;
123 public static int test_56_float () {
124 FloatDelegate l = new FloatDelegate (float_delegate);
126 return (int)l (55);
129 delegate double DoubleDelegate (double l);
131 static double double_delegate (double l) {
132 return l + 1;
135 public static int test_56_double () {
136 DoubleDelegate l = new DoubleDelegate (double_delegate);
138 return (int)l (55);
141 static int count = 0;
143 public static void inc_count () {
144 count ++;
147 public static int test_0_multicast () {
148 SimpleDelegate d = new SimpleDelegate (inc_count);
150 d += inc_count;
152 d ();
153 return count == 2 ? 0 : 1;
156 public delegate int Delegate0 ();
158 public delegate int Delegate1 (int i);
160 public delegate int Delegate2 (int i, int j);
162 public int int_field;
164 public int adder0 () {
165 return int_field;
168 public static int adder0_static () {
169 return 1;
172 public int adder1 (int i) {
173 return int_field + i;
176 public static int adder1_static (int i) {
177 return i;
180 public int adder2 (int i, int j) {
181 return int_field + i + j;
184 public static int adder2_static (int i, int j) {
185 return i + j;
188 public static int test_0_delegate_opt () {
189 Tests d = new Tests ();
190 d.int_field = 1;
192 if (new Delegate0 (d.adder0) () != 1)
193 return 1;
195 if (new Delegate1 (d.adder1) (2) != 3)
196 return 2;
198 if (new Delegate2 (d.adder2) (2, 3) != 6)
199 return 3;
201 if (new Delegate0 (adder0_static) () != 1)
202 return 4;
204 if (new Delegate1 (adder1_static) (2) != 2)
205 return 5;
207 if (new Delegate2 (adder2_static) (2, 3) != 5)
208 return 6;
210 return 0;