Take stars out of types where they make more sense.
[mono-project.git] / mcs / tests / test-async-13.cs
blob33279ff09595988f1308bea6f370a134fa8349b4
1 using System;
2 using System.Threading.Tasks;
3 using System.Threading;
4 using System.Reflection;
5 using System.Linq;
6 using System.Runtime.CompilerServices;
8 struct S
10 public int Value;
11 public S2 s2;
13 public S (int a1, string a2)
15 Value = a1;
16 s2 = new S2 ();
19 public void SetValue (int value)
21 Value = value;
24 public static implicit operator S (Base b)
26 return new S (400, "a");
29 public static S operator + (S a, S b)
31 return new S () {
32 Value = a.Value + b.Value
37 struct S2
39 public int Value;
42 enum E
44 E_1 = 1
47 class Base
49 public volatile int field_int;
50 protected int field_this;
51 protected int property_this_counter;
53 public event Action Event;
55 public Base ()
59 public Base (int arg, int arg2)
61 field_int = arg;
64 public bool PropertyBool {
65 get {
66 return true;
70 public int PropertyInt {
71 get {
72 return field_int;
74 set {
75 field_int += value;
79 protected Base PropertyThis {
80 get {
81 ++property_this_counter;
82 return this;
86 public int this [int arg] {
87 get {
88 return field_this;
90 set {
91 field_this += value * arg;
95 public static bool operator true (Base a)
97 return true;
100 public static bool operator false (Base a)
102 return false;
105 public static Base operator & (Base a, Base b)
107 return new Base () {
108 field_int = 100
112 public static Base operator | (Base a, Base b)
114 return new Base () {
115 field_int = 200
119 protected int Call (int arg1, int arg2, int arg3)
121 if (arg1 != 5)
122 return 1;
124 if (arg2 != -3)
125 return 2;
127 if (arg3 != 6)
128 return 3;
130 return 0;
133 protected int Call(ref char ch, int arg)
135 ch = 'z';
136 return arg;
139 public void CallBool (bool b)
143 public int CallS (S s)
145 return s.Value;
148 public T[] CreateArray<T> (int size)
150 return new T [size];
153 protected static void CallRefParams (ref int i, params int[] i2)
155 i = 5;
158 protected int CallNamed (int a, int b)
160 return a - b;
163 protected void CallEvent ()
165 Event ();
169 class Tester : Base
171 async Task<bool> ArrayAccessTest_1 ()
173 bool[] b = new bool[1];
174 b [0] = await Task.Factory.StartNew (() => true);
175 return b[await Task.Factory.StartNew (() => 0)];
178 async Task<int> ArrayAccessTest_2 ()
180 double[] b = new double[2];
181 b [await Task.Factory.StartNew (() => 1)] = 5.5;
182 if (b [1] != 5.5)
183 return 1;
185 var d = b [await Task.Factory.StartNew (() => 1)] = 2.5;
186 if (b [1] != 2.5)
187 return 2;
189 if (d != 2.5)
190 return 3;
192 d = b [await Task.Factory.StartNew (() => 1)] = await Task.Factory.StartNew (() => 4.4);
193 if (d != 4.4)
194 return 4;
196 return 0;
199 async Task<int> ArrayAccessTest_3 ()
201 decimal[] d = new decimal [4];
202 d[1] = 4;
204 var r = ++d[await Task.Factory.StartNew (() => 1)];
205 if (r != 5)
206 return 1;
208 d [1] = 6;
209 d [await Task.Factory.StartNew (() => 1)] += await Task.Factory.StartNew (() => 9.9m);
210 if (d [1] != 15.9m)
211 return 2;
213 d [1] = 6;
214 r = d [await Task.Factory.StartNew (() => 1)] -= await Task.Factory.StartNew (() => 5.9m);
215 if (d [1] != 0.1m)
216 return 3;
218 return 0;
221 async Task<bool> ArrayAccessTest_4 ()
223 string[] s = new string [4];
224 s[1] = "a";
226 s [await Task.Factory.StartNew (() => 1)] += await Task.Factory.StartNew (() => "b");
227 return s [1] == "ab";
230 async Task<bool> ArrayAccessTest_5 ()
232 int[][] a = new int[3][];
233 a [1] = new int [5];
234 int index = 1;
235 CallRefParams (ref a[await Task.Factory.StartNew (() => index++)][0], await Task.Factory.StartNew (() => 3));
236 return a [1][0] == 5;
239 async Task<int> ArrayAccessTest_6 ()
241 int value = -6;
242 int[] a = new int[3] { 3, 6, 9 };
243 return a [await Task.Factory.StartNew (() => (long)1)] + value;
246 async Task<bool> ArrayAccessTest_7 ()
248 short?[] s = new short?[] { 3, 2, 1 };
249 var r = s [await Task.Factory.StartNew (() => 1)]++;
250 return r == 2;
253 async Task<int> ArrayAccessTest_8 ()
255 var s = new byte?[] { 3, 2, 1 };
256 var r = s [await Task.Factory.StartNew (() => 1)] += await Task.Factory.StartNew (() => (byte)30);
257 if (r != 32)
258 return 1;
260 if (s [1] != 32)
261 return 2;
263 return 0;
266 async Task<int> ArrayAccessTest_9 ()
268 var s = new Base ();
269 s.CreateArray<int> (await Task.Factory.StartNew (() => 5)) [2] += 9;
270 s.CreateArray<int> (await Task.Factory.StartNew (() => 5)) [2] += await Task.Factory.StartNew (() => 9);
272 var res = s.CreateArray<int> (await Task.Factory.StartNew (() => 5)) [2] += await Task.Factory.StartNew (() => 9);
273 if (res != 9)
274 return 1;
276 s.CreateArray<S> (await Task.Factory.StartNew (() => 5)) [2] += await Task.Factory.StartNew (() => new S () { Value = 4 });
277 return 0;
280 async Task<bool> ArrayAccessTest_10 ()
282 var b = new bool [1] { true };
284 var r = b [await Task.Factory.StartNew (() => 0)];
285 return r;
288 async Task<bool> ArrayAccessTest_11 ()
290 var a = new S [1];
291 a [await Task.Factory.StartNew (() => 0)].s2.Value += 5;
292 return a [await Task.Factory.StartNew(() => 0)].s2.Value == 5;
295 async Task<int> AssignTest_1 ()
297 field_int = await Task.Factory.StartNew (() => 0);
298 return field_int;
301 async Task<bool> AssignTest_2 ()
303 long? l = await Task.Factory.StartNew<sbyte?> (() => null);
304 return l == null;
307 async Task<int> AssignTest_3 ()
309 int a = await Task.Factory.StartNew (() => 1), b = await Task.Factory.StartNew (() => 2);
310 if (a != 1)
311 return 1;
312 if (b != 2)
313 return b;
315 return 0;
318 async Task<int> BinaryTest_1 ()
320 return await Task.Factory.StartNew (() => { Thread.Sleep (10); return 5; }) +
321 await Task.Factory.StartNew (() => -3) +
322 await Task.Factory.StartNew (() => -2);
325 async Task<int> BinaryTest_2 ()
327 int i = 1;
328 var b = await Task.Factory.StartNew (() => { i += 3; return true; }) &&
329 await Task.Factory.StartNew (() => { i += 4; return false; }) &&
330 await Task.Factory.StartNew (() => { i += 5; return true; });
332 return b ? -1 : i == 8 ? 0 : i;
335 async Task<int> BinaryTest_3 ()
337 var r = await Task.Factory.StartNew<bool?> (() => true) & await Task.Factory.StartNew<bool?> (() => null);
338 if (r != null)
339 return 1;
341 r = await Task.Factory.StartNew<bool?> (() => null) | await Task.Factory.StartNew<bool?> (() => true);
342 if (r != true)
343 return 2;
345 r = await Task.Factory.StartNew<bool?> (() => null) != await Task.Factory.StartNew<bool?> (() => true);
346 if (r != true)
347 return 3;
349 return 0;
352 async Task<int> BinaryTest_4 ()
354 var r1 = await Task.Factory.StartNew<short?> (() => 2) * await Task.Factory.StartNew<byte?> (() => null);
355 if (r1 != null)
356 return 1;
358 var r2 = await Task.Factory.StartNew<decimal?> (() => 100) / await Task.Factory.StartNew<decimal?> (() => null);
359 if (r2 != null)
360 return 2;
362 return 0;
365 async Task<int> BinaryTest_5 ()
367 var r1 = await Task.FromResult (1) == 9;
368 if (r1)
369 return 1;
371 var r2 = 1 == await Task.FromResult (1);
372 if (!r2)
373 return 2;
375 return 0;
378 async Task<bool> BinaryTest_6 ()
380 var t = Task.Delay (1);
381 if (t == await Task.WhenAny(new [] { t }))
382 return true;
384 return false;
387 async Task<int> CallTest_1 ()
389 return Call (
390 await Task.Factory.StartNew (() => { Thread.Sleep (10); return 5; }),
391 await Task.Factory.StartNew (() => -3),
392 await Task.Factory.StartNew (() => 6));
395 async Task<bool> CallTest_2 ()
397 char ch = 'a';
398 var r = Call (
399 ref ch,
400 await Task.Factory.StartNew (() => { Thread.Sleep (10); return 5; }));
402 return ch == 'z' && r == 5;
405 async Task<int> CallTest_3 ()
407 S s = new S ();
408 s.SetValue (await Task.Factory.StartNew (() => 10));
409 return s.Value - 10;
412 async Task<bool> CallTest_4 ()
414 return E.E_1.Equals (unchecked (await Task.Factory.StartNew (() => E.E_1)));
417 async Task<int> CallTest_5 ()
419 int value = 9;
420 return CallNamed (
421 b: await Task.Factory.StartNew (() => value++),
422 a: value) - 1;
425 async Task<bool> CastTest_1 ()
427 decimal value = 67;
428 return (value - await Task.Factory.StartNew (() => 66m)) == 1;
431 async Task<bool> CastTest_2 ()
433 var t = new Tester ();
434 return t.CallS (await Task.Factory.StartNew (() => this)) == 400;
437 async Task<bool> CoalescingTest_1 ()
439 var r = await Task.Factory.StartNew<string> (() => null) ?? await Task.Factory.StartNew (() => "x");
440 return r == "x";
443 async Task<bool> CoalescingTest_2 ()
445 var r = await Task.Factory.StartNew<short?> (() => null) ?? await Task.Factory.StartNew<byte> (() => 2);
446 return r == 2;
449 async Task<int> ConditionalTest_1 ()
451 return true ? await Task.Factory.StartNew (() => 0) : await Task.Factory.StartNew (() => 1);
454 async Task<int> ConditionalTest_2 ()
456 return PropertyBool ? await Task.Factory.StartNew (() => 0) : await Task.Factory.StartNew (() => 1);
459 async Task<int> ConditionalTest_3 ()
461 int v = 5;
462 return v * (await Task.Factory.StartNew (() => true) ? 0 : await Task.Factory.StartNew (() => 1));
465 async Task<int> ConditionalTest_4 ()
467 int v = 5;
468 return v * (v == 2 ? 3 : await Task.Factory.StartNew (() => 0));
471 async Task<int> DelegateInvoke_4 ()
473 Func<int, int> d = l => l - 3;
474 int value = 1;
475 return value + d (await Task.Factory.StartNew (() => 2));
478 async Task<int> EventInvoke_1 ()
480 int value = 0;
481 Event += await Task.Factory.StartNew (() => {
482 Action a = () => { value = 5; };
483 return a;
486 CallEvent ();
487 return value - 5;
490 async Task<int> FieldTest_1 ()
492 var s = new Base ();
493 s.field_int = 3;
494 s.field_int += await Task.Factory.StartNew (() => 6);
495 if (s.field_int != 9)
496 return 1;
498 var r = s.field_int -= await Task.Factory.StartNew (() => 5);
499 if (r != 4)
500 return 2;
502 if (s.field_int != 4)
503 return 3;
504 return 0;
507 async Task<bool> IndexerTest_1 ()
509 this[2] = await Task.Factory.StartNew (() => 6);
510 return this[2] == 12;
513 async Task<bool> IndexerTest_2 ()
515 this[await Task.Factory.StartNew (() => 3)] = await Task.Factory.StartNew (() => 6);
516 return this[3] == 18;
519 async Task<int> IndexerTest_3 ()
521 int value = -5;
522 this[await Task.Factory.StartNew (() => value++)] += await Task.Factory.StartNew (() => 5);
523 return this[3] + 25;
526 async Task<int> IndexerTest_4 ()
528 int value = 3;
529 PropertyThis[await Task.Factory.StartNew (() => value++)] += await Task.Factory.StartNew (() => -5);
530 return PropertyThis[3] + value + 11;
533 async Task<int> IndexerTest_5 ()
535 int value = 3;
536 field_this = 6;
537 int res = PropertyThis[await Task.Factory.StartNew (() => value++)]++;
538 if (res != 6)
539 return 1;
541 if (PropertyThis[0] != 27)
542 return 2;
544 return PropertyThis[5] -= await Task.Factory.StartNew (() => 27);
547 async Task<int> IndexerTest_6 ()
549 var r = this[3] = await Task.Factory.StartNew (() => 9);
550 if (r != 9)
551 return 1;
553 var r2 = this[await Task.Factory.StartNew (() => 55)] = await Task.Factory.StartNew (() => 8);
555 if (r2 != 8)
556 return 2;
558 return 0;
561 async Task<bool> IndexerTest_7 ()
563 int value = -5;
564 var res = ++this[await Task.Factory.StartNew (() => value++)];
565 return res == 1;
568 async Task<bool> IsTest_1 ()
570 new Tester ().CallBool (await Task.Factory.StartNew (() => new Tester ()) is Base);
571 return true;
574 async Task<bool> IsTest_2 ()
576 var r = await Task.Factory.StartNew<uint?> (() => 1) is uint;
577 return r;
580 async Task<bool> LogicalUserOperator_1 ()
582 var r = await Task.Factory.StartNew (() => new Base ()) && await Task.Factory.StartNew (() => new Base ());
583 return r.field_int == 100;
586 async Task<bool> LogicalUserOperator_2 ()
588 var r = new Base () && await Task.Factory.StartNew (() => new Base ());
589 return r.field_int == 100;
592 async Task<bool> LogicalUserOperator_3 ()
594 var r = await Task.Factory.StartNew (() => new Base ()) || await Task.Factory.StartNew (() => new Base ());
595 return r.field_int == 0;
598 async Task<bool> NewTest_1 ()
600 int value = 9;
601 var b = new Base (value, await Task.Factory.StartNew (() => 33));
602 return b.field_int == 9;
605 async Task<bool> NewTest_2 ()
607 var s = new S (await Task.Factory.StartNew (() => 77), await Task.Factory.StartNew (() => "b"));
608 return s.Value == 77;
611 async Task<int> NewDelegate_1 ()
613 var f = new Func<int> (await NewDelegate_1_0 ());
614 return f ();
617 static async Task<Func<int>> NewDelegate_1_0 ()
619 await Task.Factory.StartNew (() => { });
620 return () => 0;
623 async Task<int> NewInitTest_1 ()
625 int value = 9;
627 var b = new Base (value, await Task.Factory.StartNew (() => 33)) { };
628 if (b.field_int != 9)
629 return 1;
631 b = new Base (value, await Task.Factory.StartNew (() => 11)) {
632 field_int = await Task.Factory.StartNew (() => 12),
633 PropertyInt = await Task.Factory.StartNew (() => 13)
636 if (b.field_int != 25)
637 return 2;
639 b = new Base () {
640 field_int = await Task.Factory.StartNew (() => 12),
641 PropertyInt = await Task.Factory.StartNew (() => 13)
644 if (b.field_int != 25)
645 return 3;
647 return 0;
650 async Task<int> NewInitTest_2 ()
652 int value = 9;
654 var s = new S (value, await Task.Factory.StartNew (() => "x")) { };
655 if (s.Value != 9)
656 return 1;
658 s = new S (value, await Task.Factory.StartNew (() => "y")) {
659 Value = await Task.Factory.StartNew (() => 12)
662 if (s.Value != 12)
663 return 2;
665 s = new S () {
666 Value = await Task.Factory.StartNew (() => 13)
669 if (s.Value != 13)
670 return 3;
672 return 0;
675 async Task<bool> NewArrayInitTest_1 ()
677 var a = new int[await Task.Factory.StartNew (() => 5)];
678 return a.Length == 5;
681 async Task<bool> NewArrayInitTest_2 ()
683 var a = new short[await Task.Factory.StartNew (() => 3), await Task.Factory.StartNew (() => 4)];
684 return a.Length == 12;
687 async Task<int> NewArrayInitTest_3 ()
689 var a = new byte[] { await Task.Factory.StartNew (() => (byte)5) };
690 return a [0] - 5;
693 async Task<bool> NewArrayInitTest_4 ()
695 var a = new ushort[,] {
696 { await Task.Factory.StartNew (() => (ushort) 5), 50 },
697 { 30, await Task.Factory.StartNew (() => (ushort) 3) }
700 return a [0, 0] * a [1, 1] == 15;
703 async Task<int> NewArrayInitTest_5 ()
705 var a = new S[] { await Task.Factory.StartNew (() => new S () { Value = 4 }) };
706 return a [0].Value - 4;
709 async Task<bool> NewArrayInitTest_6 ()
711 var a = new int[] { 2, 3, 2, 3, 5, 6, 7, 2, 4, await Task.Factory.StartNew (() => 5), 11, 23, 45 };
712 return a.Length == 13;
715 async Task<bool> NewArrayInitTest_7 ()
717 var res = new [] { "a", new [] { "1", await Task.FromResult ("2") } [1], "b" };
718 return res [1] == "2";
721 async Task<bool> PropertyTest_1 ()
723 PropertyInt = await Task.Factory.StartNew (() => 6);
724 return PropertyInt == 6;
727 async Task<int> PropertyTest_2 ()
729 PropertyThis.PropertyInt += await Task.Factory.StartNew (() => 6);
730 if (property_this_counter != 1)
731 return 1;
733 return PropertyInt - 6;
736 async Task<int> PropertyTest_3 ()
738 var r = PropertyThis.PropertyInt = await Task.Factory.StartNew (() => 9);
739 if (r != 9)
740 return 1;
742 PropertyThis.PropertyInt = 4;
743 int[] a = new int[4];
744 a [await Task.Factory.StartNew (() => 1)] = PropertyThis.PropertyInt += await Task.Factory.StartNew (() => 8);
745 if (a[1] != 21)
746 return 2;
748 if (PropertyThis.PropertyInt != 34)
749 return 3;
751 return 0;
754 async Task<bool> StringConcatTest_1 ()
756 return (await Task.Factory.StartNew (() => "a") +
757 await Task.Factory.StartNew (() => "b") +
758 await Task.Factory.StartNew (() => (string) null) == "ab");
761 async Task<bool> UnaryTest_1 ()
763 long a = 1;
764 return (a + checked (-await Task.Factory.StartNew (() => 2))) == -1;
767 async Task<bool> UnaryTest_2 ()
769 short? s = 2;
770 int value = 2;
771 return (value * ~await Task.Factory.StartNew (() => s)) == -6;
774 async Task<bool> UnaryTest_3 ()
776 var d = new decimal? [2];
777 d[1] = 4;
778 var r = ++d[await Task.Factory.StartNew (() => 1)];
779 return r == 5;
782 async Task<bool> VariableInitializer_1 ()
784 int a = 2, b = await Task.Factory.StartNew (() => 1), c = await Task.Factory.StartNew (() => 1);
785 return a == (b + c);
788 static bool RunTest (MethodInfo test)
790 Console.Write ("Running test {0, -25}", test.Name);
791 try {
792 Task t = test.Invoke (new Tester (), null) as Task;
793 if (!Task.WaitAll (new[] { t }, 1000)) {
794 Console.WriteLine ("FAILED (Timeout)");
795 return false;
798 var ti = t as Task<int>;
799 if (ti != null) {
800 if (ti.Result != 0) {
801 Console.WriteLine ("FAILED (Result={0})", ti.Result);
802 return false;
804 } else {
805 var tb = t as Task<bool>;
806 if (tb != null) {
807 if (!tb.Result) {
808 Console.WriteLine ("FAILED (Result={0})", tb.Result);
809 return false;
814 Console.WriteLine ("OK");
815 return true;
816 } catch (Exception e) {
817 Console.WriteLine ("FAILED");
818 Console.WriteLine (e.ToString ());
819 return false;
823 public static int Main ()
825 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
826 where test.GetParameters ().Length == 0 && !test.IsDefined (typeof (CompilerGeneratedAttribute), false)
827 orderby test.Name
828 select RunTest (test);
830 int failures = tests.Count (a => !a);
831 Console.WriteLine (failures + " tests failed");
832 return failures;