[w32file] Move MonoIO.Find{First,Next,Close} to managed
[mono.git] / mcs / tests / test-async-11.cs
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
4 using System.Collections.Generic;
5
6 // Async stack spilling tests
7
8 struct S
9 {
10         public int value;
11 }
12
13 enum E : byte
14 {
15         E_1,
16         E_2
17 }
18
19 class G<T>
20 {
21         public async Task<int> TestStack_1 (T t)
22         {
23                 T[] a = new T[] { t };
24                 return Call (t, a[0], out t,
25                         await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
26         }
27         
28         int Call (T t1, T t2, out T t3, int i)
29         {
30                 t3 = t2;
31                 return 0;
32         }
33 }
34
35 class C
36 {
37         int field;
38         
39         int prop_value;
40         int get_called;
41         
42         int Prop {
43                 get {
44                         ++get_called;
45                         return prop_value;
46                 }
47                 set {
48                         prop_value += value;
49                 }
50         }
51
52         int TestCall (ref int a, Type type, object o, ulong ul, int b)
53         {
54                 field = a;
55                 
56                 if (type != typeof (string)) {
57                         return 1;
58                 }
59                 
60                 S s = (S) o;
61                 if (s.value != 4)
62                         return 2;
63                 
64                 if (ul != ulong.MaxValue)
65                         return 3;
66                 
67                 return 0;
68         }
69         
70         static async Task<int> TestStack_1 ()
71         {
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));
76         }
77
78         int TestCall2<T1, T2, T3, T4, T5, T6, T7> (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
79         {
80                 return 0;
81         }
82         
83         static async Task<int> TestStack_2 (ulong value)
84         {
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));
88         }
89
90         static async Task<int> TestStack_3 ()
91         {
92                 var s = new S [] { new S () };
93                 s[0].value = 6;
94                 
95                 var s2 = new S [,] { { new S () }, { new S () } };
96                 s2[0, 0].value = 3;
97                 
98                 TestCall3 (ref s [0], ref s2 [0, 0], s [0].value++,
99                         await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
100                 
101                 if (s [0].value != 10)
102                         return 1;
103                 
104                 if (s2 [0, 0].value != 20)
105                         return 2;
106
107                 return 0;
108         }
109         
110         static int TestCall3 (ref S value, ref S value2, int value3, int i)
111         {
112                 value.value = 10;
113                 value2.value = 20;
114                 return 0;
115         }
116
117         static async Task<int> TestStack_4 ()
118         {
119                 var a1 = new [] { E.E_2 };
120                 var a2 = new [] { new S () { value = 5 } };
121                 var a3 = new [] { new C () };
122                 
123                 return TestCall4 (a1[0], a2[0], a3[0],
124                         await Task.Factory.StartNew (() => 3).ConfigureAwait (false));
125         }
126         
127         static int TestCall4 (E e, S s, C c, int i)
128         {
129                 if (e != E.E_2)
130                         return 100;
131                 
132                 if (s.value != 5)
133                         return 101;
134                 
135                 if (i != 3)
136                         return 102;
137                 
138                 return 0;
139         }
140         
141         static async Task<int> TestStack_5 ()
142         {
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;
148                         
149                         c.prop_value = 99;
150                         return 3;
151                 }).ConfigureAwait (false);
152                 
153                 if (c.get_called != 1)
154                         return 1;
155                 
156                 if (c.prop_value != 109)
157                         return 2;
158
159                 return 0;
160         }
161
162         public static int Main ()
163         {
164                 Task<int> t;
165
166                 t = TestStack_1 ();
167                 if (!Task.WaitAll (new[] { t }, 1000))
168                         return 1;
169                 
170                 if (t.Result != 0)
171                         return 2;
172                 
173                 t = TestStack_2 (ulong.MaxValue);
174                 if (!Task.WaitAll (new[] { t }, 1000))
175                         return 3;
176                 
177                 if (t.Result != 0)
178                         return 4;
179                 
180                 t = TestStack_3 ();
181                 if (!Task.WaitAll (new[] { t }, 1000))
182                         return 4;
183                 
184                 if (t.Result != 0)
185                         return 5;
186                 
187                 t = TestStack_4 ();
188                 if (!Task.WaitAll (new[] { t }, 1000))
189                         return 6;
190                 
191                 if (t.Result != 0)
192                         return 7;
193                 
194                 var g = new G<sbyte> ();
195                 t = g.TestStack_1 (9);
196                 if (!Task.WaitAll (new[] { t }, 1000))
197                         return 8;
198                 
199                 if (t.Result != 0)
200                         return 9;
201                 
202                 t = TestStack_5 ();
203                 if (!Task.WaitAll (new[] { t }, 1000))
204                         return 10;
205                 
206                 if (t.Result != 0)
207                         return 11;
208                 
209                 Console.WriteLine ("ok");
210                 return 0;
211         }
212 }