[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / tests / test-async-16.cs
blobe4f239f1c06931b0724492281e5e37f7a6f5b382
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;
8 class Base : IDisposable
10 protected static int dispose_counter;
12 public void Dispose ()
14 ++dispose_counter;
18 class Tester : Base
20 async Task<int> SwitchTest_1 ()
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;
35 return 5;
38 async Task<int> Using_1 ()
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)) {
46 if (dispose_counter != 4)
47 return 1;
49 return 0;
52 async Task<int> Foreach_1 ()
54 int total = 0;
55 foreach (var e in await Task.Factory.StartNew (() => new List<int> () { 1, 2, 3 }).ConfigureAwait (false)) {
56 total += e;
59 return total - 6;
62 async Task<int> Foreach_2 ()
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;
70 return total - 6;
73 static bool RunTest (MethodInfo test)
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;
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;
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;
99 Console.WriteLine ("OK");
100 return true;
101 } catch (Exception e) {
102 Console.WriteLine ("FAILED");
103 Console.WriteLine (e.ToString ());
104 return false;
108 public static int Main ()
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);
115 int failures = tests.Count (a => !a);
116 Console.WriteLine (failures + " tests failed");
117 return failures;