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