Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / test-async-20.cs
1 using System;
2 using System.Threading.Tasks;
3 using System.Threading;
4 using System.Reflection;
5 using System.Linq;
6 using System.Collections.Generic;
7
8 // Dynamic tests
9
10 class Base
11 {
12         public int Value;
13 }
14
15 class Tester : Base
16 {
17         async Task<bool> Add_1 ()
18         {
19                 dynamic d = 1;
20                 int total = d + await Task.Factory.StartNew (() => 2);
21                 return total == 3;
22         }
23
24         async Task<bool> AssignCompound_1 ()
25         {
26                 dynamic d = new Base ();
27                 d.Value = 3;
28                 d.Value += await Task.Factory.StartNew (() => 2);
29                 return d.Value == 5;
30         }
31
32         async Task<bool> Convert_1 ()
33         {
34                 string s = await Task.Factory.StartNew (() => (dynamic) "x");
35                 return s == "x";
36         }
37
38         async Task<bool> Invocation_1 ()
39         {
40                 var r = (await Task.Factory.StartNew (() => (dynamic) "x|y|z")).Split ('|');
41                 return r[2] == "z";
42         }
43
44         static bool RunTest (MethodInfo test)
45         {
46                 Console.Write ("Running test {0, -25}", test.Name);
47                 try {
48                         Task t = test.Invoke (new Tester (), null) as Task;
49                         if (!Task.WaitAll (new[] { t }, 1000)) {
50                                 Console.WriteLine ("FAILED (Timeout)");
51                                 return false;
52                         }
53
54                         var tb = t as Task<bool>;
55                         if (tb != null) {
56                                 if (!tb.Result) {
57                                         Console.WriteLine ("FAILED (Result={0})", tb.Result);
58                                         return false;
59                                 }
60                         }
61
62                         Console.WriteLine ("OK");
63                         return true;
64                 } catch (Exception e) {
65                         Console.WriteLine ("FAILED");
66                         Console.WriteLine (e.ToString ());
67                         return false;
68                 }
69         }
70
71         public static int Main ()
72         {
73                 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
74                                         where test.GetParameters ().Length == 0
75                                         orderby test.Name
76                                         select RunTest (test);
77
78                 int failures = tests.Count (a => !a);
79                 Console.WriteLine (failures + " tests failed");
80                 return failures;
81         }
82 }