Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / test-async-16.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 class Base : IDisposable
9 {
10         protected static int dispose_counter;
11
12         public void Dispose ()
13         {
14                 ++dispose_counter;
15         }
16 }
17
18 class Tester : Base
19 {
20         async Task<int> SwitchTest_1 ()
21         {
22                 switch (await Task.Factory.StartNew (() => "X").ConfigureAwait (false)) {
23                 case "A":
24                         return 1;
25                 case "B":
26                         return 2;
27                 case "C":
28                         return 3;
29                 case "D":
30                         return 4;
31                 case "X":
32                         return 0;
33                 }
34
35                 return 5;
36         }
37
38         async Task<int> Using_1 ()
39         {
40                 using (Base a = await Task.Factory.StartNew (() => new Base ()).ConfigureAwait (false),
41                                 b = await Task.Factory.StartNew (() => new Tester ()).ConfigureAwait (false),
42                                 c = await Task.Factory.StartNew (() => new Base ()).ConfigureAwait (false),
43                                 d = await Task.Factory.StartNew (() => new Base ()).ConfigureAwait (false)) {
44                 }
45
46                 if (dispose_counter != 4)
47                         return 1;
48
49                 return 0;
50         }
51
52         async Task<int> Foreach_1 ()
53         {
54                 int total = 0;
55                 foreach (var e in await Task.Factory.StartNew (() => new List<int> () { 1, 2, 3 }).ConfigureAwait (false)) {
56                         total += e;
57                 }
58
59                 return total - 6;
60         }
61
62         async Task<int> Foreach_2 ()
63         {
64                 int total = 0;
65                 foreach (var e in await Task.Factory.StartNew (() => new List<int> () { 1, 2, 3 }).ConfigureAwait (false)) {
66                         await Task.Yield ();
67                         total += e;
68                 }
69
70                 return total - 6;
71         }
72
73         static bool RunTest (MethodInfo test)
74         {
75                 Console.Write ("Running test {0, -25}", test.Name);
76                 try {
77                         Task t = test.Invoke (new Tester (), null) as Task;
78                         if (!Task.WaitAll (new[] { t }, 1000)) {
79                                 Console.WriteLine ("FAILED (Timeout)");
80                                 return false;
81                         }
82
83                         var ti = t as Task<int>;
84                         if (ti != null) {
85                                 if (ti.Result != 0) {
86                                         Console.WriteLine ("FAILED (Result={0})", ti.Result);
87                                         return false;
88                                 }
89                         } else {
90                                 var tb = t as Task<bool>;
91                                 if (tb != null) {
92                                         if (!tb.Result) {
93                                                 Console.WriteLine ("FAILED (Result={0})", tb.Result);
94                                                 return false;
95                                         }
96                                 }
97                         }
98
99                         Console.WriteLine ("OK");
100                         return true;
101                 } catch (Exception e) {
102                         Console.WriteLine ("FAILED");
103                         Console.WriteLine (e.ToString ());
104                         return false;
105                 }
106         }
107
108         public static int Main ()
109         {
110                 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
111                                         where test.GetParameters ().Length == 0
112                                         orderby test.Name
113                                         select RunTest (test);
114
115                 int failures = tests.Count (a => !a);
116                 Console.WriteLine (failures + " tests failed");
117                 return failures;
118         }
119 }