[interp] Reduce computation under calc_section mutex
[mono-project.git] / mcs / tests / test-async-11.cs
blob11a02cb83ddcf3e5a10cdedc91be6959e15f1e21
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
4 using System.Collections.Generic;
6 // Async stack spilling tests
8 struct S
10 public int value;
13 enum E : byte
15 E_1,
16 E_2
19 class G<T>
21 public async Task<int> TestStack_1 (T t)
23 T[] a = new T[] { t };
24 return Call (t, a[0], out t,
25 await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
28 int Call (T t1, T t2, out T t3, int i)
30 t3 = t2;
31 return 0;
35 class C
37 int field;
39 int prop_value;
40 int get_called;
42 int Prop {
43 get {
44 ++get_called;
45 return prop_value;
47 set {
48 prop_value += value;
52 int TestCall (ref int a, Type type, object o, ulong ul, int b)
54 field = a;
56 if (type != typeof (string)) {
57 return 1;
60 S s = (S) o;
61 if (s.value != 4)
62 return 2;
64 if (ul != ulong.MaxValue)
65 return 3;
67 return 0;
70 static async Task<int> TestStack_1 ()
72 int v = 9;
73 var array = new ulong[] { ulong.MaxValue };
74 return new C ().TestCall (ref v, typeof (string), new S () { value = 4 }, array [0],
75 await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
78 int TestCall2<T1, T2, T3, T4, T5, T6, T7> (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
80 return 0;
83 static async Task<int> TestStack_2 (ulong value)
85 short v = 2;
86 return new C ().TestCall2 ((byte) 1, v, value = 9999, float.MaxValue, double.MaxValue, decimal.MaxValue,
87 await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
90 static async Task<int> TestStack_3 ()
92 var s = new S [] { new S () };
93 s[0].value = 6;
95 var s2 = new S [,] { { new S () }, { new S () } };
96 s2[0, 0].value = 3;
98 TestCall3 (ref s [0], ref s2 [0, 0], s [0].value++,
99 await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
101 if (s [0].value != 10)
102 return 1;
104 if (s2 [0, 0].value != 20)
105 return 2;
107 return 0;
110 static int TestCall3 (ref S value, ref S value2, int value3, int i)
112 value.value = 10;
113 value2.value = 20;
114 return 0;
117 static async Task<int> TestStack_4 ()
119 var a1 = new [] { E.E_2 };
120 var a2 = new [] { new S () { value = 5 } };
121 var a3 = new [] { new C () };
123 return TestCall4 (a1[0], a2[0], a3[0],
124 await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
127 static int TestCall4 (E e, S s, C c, int i)
129 if (e != E.E_2)
130 return 100;
132 if (s.value != 5)
133 return 101;
135 if (i != 3)
136 return 102;
138 return 0;
141 static async Task<int> TestStack_5 ()
143 var c = new C ();
144 c.prop_value = 7;
145 c.Prop += await Task.Factory.StartNew (() => {
146 if (c.get_called != 1)
147 return -44;
149 c.prop_value = 99;
150 return 3;
151 }).ConfigureAwait (false);
153 if (c.get_called != 1)
154 return 1;
156 if (c.prop_value != 109)
157 return 2;
159 return 0;
162 public static int Main ()
164 Task<int> t;
166 t = TestStack_1 ();
167 if (!Task.WaitAll (new[] { t }, 1000))
168 return 1;
170 if (t.Result != 0)
171 return 2;
173 t = TestStack_2 (ulong.MaxValue);
174 if (!Task.WaitAll (new[] { t }, 1000))
175 return 3;
177 if (t.Result != 0)
178 return 4;
180 t = TestStack_3 ();
181 if (!Task.WaitAll (new[] { t }, 1000))
182 return 4;
184 if (t.Result != 0)
185 return 5;
187 t = TestStack_4 ();
188 if (!Task.WaitAll (new[] { t }, 1000))
189 return 6;
191 if (t.Result != 0)
192 return 7;
194 var g = new G<sbyte> ();
195 t = g.TestStack_1 (9);
196 if (!Task.WaitAll (new[] { t }, 1000))
197 return 8;
199 if (t.Result != 0)
200 return 9;
202 t = TestStack_5 ();
203 if (!Task.WaitAll (new[] { t }, 1000))
204 return 10;
206 if (t.Result != 0)
207 return 11;
209 Console.WriteLine ("ok");
210 return 0;