Merge pull request #4327 from vkargov/vk-abcremedy
[mono.git] / mcs / tests / test-async-15.cs
1 using System;
2 using System.Threading.Tasks;
3 using System.Threading;
4 using System.Collections;
5 using System.Collections.Generic;
6
7 interface IFoo
8 {
9         int Value { get; set; }
10 }
11
12 struct S : IFoo
13 {
14         public S (int a1, string a2)
15                 : this ()
16         {
17                 Value = a1;
18         }
19         
20         public int Value { get; set; }
21         
22         public void SetValue (int value)
23         {
24                 Value = value;
25         }
26 }
27
28 struct S2 : IEnumerable
29 {
30         public List<int> Values;
31
32         public void Add (int x)
33         {
34                 if (Values == null)
35                         Values = new List<int> ();
36
37                 Values.Add(x);
38         }
39
40         public IEnumerator GetEnumerator()
41         {
42                 return Values as IEnumerator;
43         }
44 }
45
46 class Tester
47 {
48         async Task<T> NewInitTestGen<T> () where T : struct, IFoo
49         {
50                 var s = new T () {
51                         Value = await Task.Factory.StartNew (() => 13).ConfigureAwait (false)
52                 };
53                 
54                 if (s.Value != 13)
55                         return new T ();
56                 
57                 return s;
58         }
59
60         static async Task<int> NewInitCol ()
61         {
62                 var s = new S2 { 
63                         await Task.FromResult (1),
64                         await Task.Factory.StartNew (() => 2)
65                 };
66
67                 return s.Values [0] + s.Values [1];
68         }
69         
70         public static int Main ()
71         {
72                 var t = new Tester().NewInitTestGen<S> ();
73                 
74                 if (!Task.WaitAll (new[] { t }, 1000)) {
75                         return 1;
76                 }
77                 
78                 if (t.Result.Value != 13)
79                         return 2;
80
81                 var v = NewInitCol ().Result;
82                 if (v != 3)
83                         return 3;
84                 
85                 return 0;
86         }
87 }