Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Delegates.cs « Delegates « Simple « src « tests - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d17133dfe615b440290f4a35ee1a75b37905bb35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

using Pointer = System.Reflection.Pointer;

public class BringUpTests
{
    const int Pass = 100;
    const int Fail = -1;

    public static int Main()
    {
        int result = Pass;

        if (!TestValueTypeDelegates())
        {
            Console.WriteLine("Failed");
            result = Fail;
        }

        if (!TestVirtualDelegates())
        {
            Console.WriteLine("Failed");
            result = Fail;
        }

        if (!TestInterfaceDelegates())
        {
            Console.WriteLine("Failed");
            result = Fail;
        }

        if (!TestStaticOpenClosedDelegates())
        {
            Console.WriteLine("Failed");
            result = Fail;
        }

        if (!TestMulticastDelegates())
        {
            Console.WriteLine("Failed");
            result = Fail;
        }

        if (!TestDynamicInvoke())
        {
            Console.WriteLine("Failed");
            result = Fail;
        }

        return result;
    }

    public static bool TestValueTypeDelegates()
    {
        Console.Write("Testing delegates to value types...");

        {
            TestValueType t = new TestValueType { X = 123 };
            Func<string, string> d = t.GiveX;
            string result = d("MyPrefix");
            if (result != "MyPrefix123")
                return false;
        }

        {
            object t = new TestValueType { X = 456 };
            Func<string> d = t.ToString;
            string result = d();
            if (result != "456")
                return false;
        }

        {
            Func<int, TestValueType> d = TestValueType.MakeValueType;
            TestValueType result = d(789);
            if (result.X != 789)
                return false;
        }

        Console.WriteLine("OK");
        return true;
    }

    public static bool TestVirtualDelegates()
    {
        Console.Write("Testing delegates to virtual methods...");

        {
            Mid t = new Mid();
            if (t.GetBaseDo()() != "Base")
                return false;

            if (t.GetDerivedDo()() != "Mid")
                return false;
        }

        {
            Mid t = new Derived();
            if (t.GetBaseDo()() != "Base")
                return false;

            if (t.GetDerivedDo()() != "Derived")
                return false;
        }

        {
            // This will end up being a delegate to a sealed virtual method.
            ClassWithIFoo t = new ClassWithIFoo("Class");
            Func<int, string> d = t.DoFoo;
            if (d(987) != "Class987")
                return false;       
        }

        Console.WriteLine("OK");
        return true;
    }

    public static bool TestInterfaceDelegates()
    {
        Console.Write("Testing delegates to interface methods...");

        {
            IFoo t = new ClassWithIFoo("Class");
            Func<int, string> d = t.DoFoo;
            if (d(987) != "Class987")
                return false;
        }

        {
            IFoo t = new StructWithIFoo("Struct");
            Func<int, string> d = t.DoFoo;
            if (d(654) != "Struct654")
                return false;
        }

        Console.WriteLine("OK");
        return true;
    }

    public static bool TestStaticOpenClosedDelegates()
    {
        Console.Write("Testing static open and closed delegates...");

        {
            Func<string, string, string> d = ExtensionClass.Combine;
            if (d("Hello", "World") != "HelloWorld")
                return false;
        }

        {
            Func<string, string> d = "Hi".Combine;
            if (d("There") != "HiThere")
                return false;
        }

        Console.WriteLine("OK");
        return true;
    }

    public static bool TestMulticastDelegates()
    {
        Console.Write("Testing multicast delegates...");

        {
            ClassThatMutates t = new ClassThatMutates();

            Action d = t.AddOne;
            d();

            if (t.State != 1)
                return false;
            t.State = 0;

            d += t.AddTwo;
            d();

            if (t.State != 3)
                return false;
            t.State = 0;

            d += t.AddOne;
            d();

            if (t.State != 4)
                return false;
        }

        Console.WriteLine("OK");
        return true;
    }

    public static bool TestDynamicInvoke()
    {
        Console.Write("Testing dynamic invoke...");

        {
            TestValueType t = new TestValueType { X = 123 };
            Func<string, string> d = t.GiveX;
            string result = (string)d.DynamicInvoke(new object[] { "MyPrefix" });
            if (result != "MyPrefix123")
                return false;
        }

        {
            Func<int, TestValueType> d = TestValueType.MakeValueType;
            TestValueType result = (TestValueType)d.DynamicInvoke(new object[] { 789 });
            if (result.X != 789)
                return false;
        }

        {
            IFoo t = new ClassWithIFoo("Class");
            Func<int, string> d = t.DoFoo;
            if ((string)d.DynamicInvoke(new object[] { 987 }) != "Class987")
                return false;
        }

        {
            IFoo t = new StructWithIFoo("Struct");
            Func<int, string> d = t.DoFoo;
            if ((string)d.DynamicInvoke(new object[] { 654 }) != "Struct654")
                return false;
        }

        {
            Func<string, string, string> d = ExtensionClass.Combine;
            if ((string)d.DynamicInvoke(new object[] { "Hello", "World" }) != "HelloWorld")
                return false;
        }

        {
            Func<string, string> d = "Hi".Combine;
            if ((string)d.DynamicInvoke(new object[] { "There" }) != "HiThere")
                return false;
        }

        {
            Mutate<int> d = ClassWithByRefs.Mutate;
            object[] args = new object[] { 8 };
            d.DynamicInvoke(args);
            if ((int)args[0] != 50)
                return false;
        }

        {
            Mutate<string> d = ClassWithByRefs.Mutate;
            object[] args = new object[] { "Hello" };
            d.DynamicInvoke(args);
            if ((string)args[0] != "HelloMutated")
                return false;
        }

        unsafe
        {
            GetAndReturnPointerDelegate d = ClassWithPointers.GetAndReturnPointer;
            if (Pointer.Unbox(d.DynamicInvoke(new object[] { (IntPtr)8 })) != (void*)50)
                return false;

            if (Pointer.Unbox(d.DynamicInvoke(new object[] { Pointer.Box((void*)9, typeof(void*)) })) != (void*)51)
                return false;
        }

#if false
        // This is hitting an EH bug around throw/rethrow from a catch block (pass is not set properly)
        unsafe
        {
            PassPointerByRefDelegate d = ClassWithPointers.PassPointerByRef;
            var args = new object[] { (IntPtr)8 };

            bool caught = false;
            try
            {
                d.DynamicInvoke(args);
            }
            catch (ArgumentException)
            {
                caught = true;
            }

            if (!caught)
                return false;
        }
#endif

        Console.WriteLine("OK");
        return true;
    }

    struct TestValueType
    {
        public int X;

        public string GiveX(string prefix)
        {
            return prefix + X.ToString();
        }

        public static TestValueType MakeValueType(int value)
        {
            return new TestValueType { X = value };
        }

        public override string ToString()
        {
            return X.ToString();
        }
    }

    class Base
    {
        public virtual string Do()
        {
            return "Base";
        }
    }

    class Mid : Base
    {
        public override string Do()
        {
            return "Mid";
        }

        public Func<string> GetBaseDo()
        {
            return base.Do;
        }

        public Func<string> GetDerivedDo()
        {
            return Do;
        }
    }

    class Derived : Mid
    {
        public override string Do()
        {
            return "Derived";
        }
    }

    interface IFoo
    {
        string DoFoo(int x);
    }

    class ClassWithIFoo : IFoo
    {
        string _prefix;

        public ClassWithIFoo(string prefix)
        {
            _prefix = prefix;
        }

        public string DoFoo(int x)
        {
            return _prefix + x.ToString();
        }
    }

    struct StructWithIFoo : IFoo
    {
        string _prefix;

        public StructWithIFoo(string prefix)
        {
            _prefix = prefix;
        }

        public string DoFoo(int x)
        {
            return _prefix + x.ToString();
        }
    }

    class ClassThatMutates
    {
        public int State;

        public void AddOne()
        {
            State++;
        }

        public void AddTwo()
        {
            State += 2;
        }
    }
}

static class ExtensionClass
{
    public static string Combine(this string s1, string s2)
    {
        return s1 + s2;
    }
}

unsafe delegate byte* GetAndReturnPointerDelegate(void* ptr);
unsafe delegate void PassPointerByRefDelegate(ref void* ptr);

unsafe static class ClassWithPointers
{
    public static byte* GetAndReturnPointer(void* ptr)
    {
        return (byte*)ptr + 42;
    }

    public static void PassPointerByRef(ref void* ptr)
    {
        ptr = (byte*)ptr + 42;
    }
}

delegate void Mutate<T>(ref T x);

class ClassWithByRefs
{
    public static void Mutate(ref int x)
    {
        x += 42;
    }

    public static void Mutate(ref string x)
    {
        x += "Mutated";
    }
}