Removed DeflateStream.UnmanagedRead Read loop. Fixes #19313.
[mono.git] / mcs / class / Mono.Debugger.Soft / Test / dtest-app.cs
1 /*
2  * dtest-app.cs:
3  *
4  *   Application program used by the debugger tests.
5  */
6 using System;
7 using System.Runtime.CompilerServices;
8 using System.Reflection;
9 using System.Reflection.Emit;
10 using System.Diagnostics;
11 using System.Threading;
12 using System.Collections.Generic;
13 using System.Linq;
14
15 public class TestsBase
16 {
17 #pragma warning disable 0414
18 #pragma warning disable 0169
19         public int base_field_i;
20         public string base_field_s;
21         static int base_static_i = 57;
22         static string base_static_s = "C";
23 #pragma warning restore 0414
24 #pragma warning restore 0169
25 }
26
27 public enum AnEnum {
28         A = 0,
29         B= 1
30 }
31
32 public sealed class Tests3 {
33         public static void M1 () {
34         }
35
36         static void M2 () {
37         }
38
39         public void M3 () {
40         }
41
42         void M4 () {
43         }
44
45 }
46
47 public static class Tests4 {
48         static Tests4 () {
49         }
50 }
51
52 public class AAttribute : Attribute {
53         public int afield;
54 }
55
56 public class BAttribute : AAttribute {
57         public int bfield;
58 }
59
60 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
61 [DebuggerTypeProxy (typeof (Tests))]
62 [BAttribute (afield = 1, bfield = 2)]
63 public class Tests2 {
64         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
65         public int field_j;
66         public static int static_field_j;
67
68         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
69         public int AProperty {
70                 get {
71                         return 0;
72                 }
73         }
74
75         public void invoke () {
76         }
77 }
78
79 public struct AStruct {
80         public int i;
81         public string s;
82         public byte k;
83         public IntPtr j;
84
85         [MethodImplAttribute (MethodImplOptions.NoInlining)]
86         public int foo (int val) {
87                 return val;
88         }
89
90         [MethodImplAttribute (MethodImplOptions.NoInlining)]
91         public static int static_foo (int val) {
92                 return val;
93         }
94
95         [MethodImplAttribute (MethodImplOptions.NoInlining)]
96         public int invoke_return_int () {
97                 return i;
98         }
99
100         [MethodImplAttribute (MethodImplOptions.NoInlining)]
101         public static int invoke_static () {
102                 return 5;
103         }
104
105         [MethodImplAttribute (MethodImplOptions.NoInlining)]
106         public IntPtr invoke_return_intptr () {
107                 return j;
108         }
109 }
110
111 public class GClass<T> {
112         public T field;
113         public static T static_field;
114
115         [MethodImplAttribute (MethodImplOptions.NoInlining)]
116         public GClass () {
117         }
118
119         [MethodImplAttribute (MethodImplOptions.NoInlining)]
120         public void bp<T2> () {
121         }
122 }
123
124 public struct GStruct<T> {
125         public T i;
126
127         public int j;
128
129         [MethodImplAttribute (MethodImplOptions.NoInlining)]
130         public int invoke_return_int () {
131                 return j;
132         }
133 }
134
135 public struct NestedStruct {
136         NestedInner nested1, nested2;
137 }
138
139 public struct NestedInner {
140 }
141
142 public interface IRecStruct {
143         void foo (object o);
144 }
145
146 struct RecStruct : IRecStruct {
147         public object o;
148
149         public void foo (object o) {
150                 this.o = o;
151         }
152 }
153
154 interface ITest
155 {
156         void Foo ();
157         void Bar ();
158 }
159
160 interface ITest<T>
161 {
162         void Foo ();
163         void Bar ();
164 }
165
166 class TestIfaces : ITest
167 {
168         void ITest.Foo () {
169         }
170
171         void ITest.Bar () {
172         }
173
174         TestIfaces<int> Baz () {
175                 return null;
176         }
177 }
178
179 class TestIfaces<T> : ITest<T>
180 {
181         void ITest<T>.Foo () {
182         }
183
184         void ITest<T>.Bar () {
185         }
186 }
187
188 public interface ITest2
189 {
190         int invoke_iface ();
191 }
192
193 public class Tests : TestsBase, ITest2
194 {
195 #pragma warning disable 0414
196         int field_i;
197         string field_s;
198         AnEnum field_enum;
199         bool field_bool1, field_bool2;
200         char field_char;
201         byte field_byte;
202         sbyte field_sbyte;
203         short field_short;
204         ushort field_ushort;
205         long field_long;
206         ulong field_ulong;
207         float field_float;
208         double field_double;
209         Thread field_class;
210         IntPtr field_intptr;
211         int? field_nullable;
212         static int static_i = 55;
213         static string static_s = "A";
214         public const int literal_i = 56;
215         public const string literal_s = "B";
216         public object child;
217         public AStruct field_struct;
218         public object field_boxed_struct;
219         public GStruct<int> generic_field_struct;
220         public KeyValuePair<int, object> boxed_struct_field;
221         [ThreadStatic]
222         public static int tls_i;
223         public static bool is_attached = Debugger.IsAttached;
224         public NestedStruct nested_struct;
225
226 #pragma warning restore 0414
227
228         public class NestedClass {
229         }
230
231         public int IntProperty {
232                 get {
233                         return field_i;
234                 }
235                 set {
236                         field_i = value;
237                 }
238         }
239
240         public int ReadOnlyProperty {
241                 get {
242                         return field_i;
243                 }
244         }
245
246         public int this [int index] {
247                 get {
248                         return field_i;
249                 }
250         }
251
252         public static void wait_one ()
253         {
254                 ManualResetEvent evt = new ManualResetEvent (false);
255                 evt.WaitOne ();
256         }
257
258         public static int Main (String[] args) {
259                 tls_i = 42;
260
261                 if (args.Length > 0 && args [0] == "suspend-test")
262                         /* This contains an infinite loop, so execute it conditionally */
263                         suspend ();
264                 if (args.Length >0 && args [0] == "unhandled-exception") {
265                         unhandled_exception ();
266                         return 0;
267                 }
268                 if (args.Length >0 && args [0] == "unhandled-exception-endinvoke") {
269                         unhandled_exception_endinvoke ();
270                         return 0;
271                 }
272                 if (args.Length >0 && args [0] == "unhandled-exception-user") {
273                         unhandled_exception_user ();
274                         return 0;
275                 }
276                 if (args.Length >0 && args [0] == "wait-one") {
277                         wait_one ();
278                         return 0;
279                 }
280                 breakpoints ();
281                 single_stepping ();
282                 arguments ();
283                 objects ();
284                 objrefs ();
285                 vtypes ();
286                 locals ();
287                 line_numbers ();
288                 type_info ();
289                 assembly_load ();
290                 invoke ();
291                 exceptions ();
292                 exception_filter ();
293                 threads ();
294                 dynamic_methods ();
295                 user ();
296                 type_load ();
297                 regress ();
298                 gc_suspend ();
299                 set_ip ();
300                 if (args.Length > 0 && args [0] == "domain-test")
301                         /* This takes a lot of time, so execute it conditionally */
302                         domains ();
303                 if (args.Length > 0 && args [0] == "ref-emit-test")
304                         ref_emit ();
305                 if (args.Length > 0 && args [0] == "frames-in-native")
306                         frames_in_native ();
307                 if (args.Length > 0 && args [0] == "invoke-single-threaded")
308                         new Tests ().invoke_single_threaded ();
309                 new Tests ().evaluate_method ();
310                 return 3;
311         }
312
313         public static void breakpoints () {
314                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
315                 bp3 ();
316                 bp7<int> ();
317                 bp7<string> ();
318
319                 bp1 ();
320                 bp2 ();
321                 bp3 ();
322                 bp4 ();
323                 bp4 ();
324                 bp4 ();
325                 bp5 ();
326                 bp6<string> (new GClass <int> ());
327                 bp7<int> ();
328                 bp7<string> ();
329         }
330
331         [MethodImplAttribute (MethodImplOptions.NoInlining)]
332         public static void bp1 () {
333         }
334
335         [MethodImplAttribute (MethodImplOptions.NoInlining)]
336         public static void bp2 () {
337         }
338
339         [MethodImplAttribute (MethodImplOptions.NoInlining)]
340         public static void bp3 () {
341         }
342
343         [MethodImplAttribute (MethodImplOptions.NoInlining)]
344         public static void bp4 () {
345         }
346
347         [MethodImplAttribute (MethodImplOptions.NoInlining)]
348         public static void bp5 () {
349         }
350
351         [MethodImplAttribute (MethodImplOptions.NoInlining)]
352         public static void bp6<T> (GClass<int> gc) {
353                 gc.bp<int> ();
354         }
355
356         [MethodImplAttribute (MethodImplOptions.NoInlining)]
357         public static void bp7<T> () {
358         }
359
360         [MethodImplAttribute (MethodImplOptions.NoInlining)]
361         public static void single_stepping () {
362                 bool b = true;
363                 ss1 ();
364                 ss2 ();
365                 ss3 ();
366                 ss3_2 ();
367                 ss4 ();
368                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
369                 try {
370                         ss6 (b);
371                 } catch {
372                 }
373                 ss7 ();
374                 ss_nested ();
375                 ss_regress_654694 ();
376                 ss_step_through ();
377                 ss_non_user_code ();
378                 ss_recursive (1);
379                 ss_fp_clobber ();
380         }
381
382         [MethodImplAttribute (MethodImplOptions.NoInlining)]
383         public static void ss1 () {
384         }
385
386         [MethodImplAttribute (MethodImplOptions.NoInlining)]
387         public static void ss2 () {
388         }
389
390         [MethodImplAttribute (MethodImplOptions.NoInlining)]
391         public static int ss3 () {
392                 int sum = 0;
393
394                 for (int i = 0; i < 10; ++i)
395                         sum += i;
396
397                 return sum;
398         }
399
400         [MethodImplAttribute (MethodImplOptions.NoInlining)]
401         public static void ss3_2 () {
402                 ss3_2_2 ();
403         }
404
405         [MethodImplAttribute (MethodImplOptions.NoInlining)]
406         public static void ss3_2_2 () {
407         }
408
409         [MethodImplAttribute (MethodImplOptions.NoInlining)]
410         public static int ss4 () {
411                 ss1 (); ss1 ();
412                 ss2 ();
413                 return 0;
414         }
415
416         [MethodImplAttribute (MethodImplOptions.NoInlining)]
417         public static void ss5 (int[] arr, Func<int, bool> selector) {
418                 // Call into linq which calls back into this assembly
419                 arr.Count (selector);
420         }
421
422         [MethodImplAttribute (MethodImplOptions.NoInlining)]
423         public static void ss6 (bool b) {
424                 if (b) {
425                         ss6_2 ();
426                         throw new Exception ();
427                 }
428         }
429
430         [MethodImplAttribute (MethodImplOptions.NoInlining)]
431         public static void ss6_2 () {
432         }
433
434         [MethodImplAttribute (MethodImplOptions.NoInlining)]
435         public static void ss7 () {
436                 try {
437                         ss7_2 ();
438                         ss7_3 ();
439                 } catch {
440                 }
441                 ss7_2 ();
442         }
443
444         [MethodImplAttribute (MethodImplOptions.NoInlining)]
445         public static void ss7_2 () {
446         }
447
448         [MethodImplAttribute (MethodImplOptions.NoInlining)]
449         public static void ss7_3 () {
450                 throw new Exception ();
451         }
452
453         [MethodImplAttribute (MethodImplOptions.NoInlining)]
454         public static void ss_nested () {
455                 ss_nested_1 (ss_nested_2 ());
456                 ss_nested_1 (ss_nested_2 ());
457                 ss_nested_3 ();
458         }
459
460         [MethodImplAttribute (MethodImplOptions.NoInlining)]
461         public static void ss_nested_1 (int i) {
462         }
463
464         [MethodImplAttribute (MethodImplOptions.NoInlining)]
465         public static int ss_nested_2 () {
466                 return 0;
467         }
468
469         [MethodImplAttribute (MethodImplOptions.NoInlining)]
470         public static void ss_nested_3 () {
471         }
472
473         [MethodImplAttribute (MethodImplOptions.NoInlining)]
474         public static void ss_step_through () {
475                 step_through_1 ();
476                 StepThroughClass.step_through_2 ();
477                 step_through_3 ();
478         }
479
480         [DebuggerStepThrough]
481         [MethodImplAttribute (MethodImplOptions.NoInlining)]
482         public static void step_through_1 () {
483         }
484
485         [DebuggerStepThrough]
486         class StepThroughClass {
487                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
488                 public static void step_through_2 () {
489                 }
490         }
491
492         [DebuggerStepThrough]
493         [MethodImplAttribute (MethodImplOptions.NoInlining)]
494         public static void step_through_3 () {
495         }
496
497         [MethodImplAttribute (MethodImplOptions.NoInlining)]
498         public static void ss_non_user_code () {
499                 non_user_code_1 ();
500                 StepNonUserCodeClass.non_user_code_2 ();
501                 non_user_code_3 ();
502         }
503
504         [DebuggerNonUserCode]
505         [MethodImplAttribute (MethodImplOptions.NoInlining)]
506         public static void non_user_code_1 () {
507         }
508
509         [DebuggerNonUserCode]
510         class StepNonUserCodeClass {
511                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
512                 public static void non_user_code_2 () {
513                 }
514         }
515
516         [DebuggerNonUserCode]
517         [MethodImplAttribute (MethodImplOptions.NoInlining)]
518         public static void non_user_code_3 () {
519         }
520
521         [MethodImplAttribute (MethodImplOptions.NoInlining)]
522         public static void ss_recursive (int n) {
523                 if (n == 10)
524                         return;
525                 ss_recursive (n + 1);
526         }
527
528         [MethodImplAttribute (MethodImplOptions.NoInlining)]
529         public static void ss_fp_clobber () {
530                 double v = ss_fp_clobber_1 (5.0);
531                 ss_fp_clobber_2 (v);
532         }
533
534         [MethodImplAttribute (MethodImplOptions.NoInlining)]
535         public static double ss_fp_clobber_1 (double d) {
536                 return d + 2.0;
537         }
538
539         [MethodImplAttribute (MethodImplOptions.NoInlining)]
540         public static void ss_fp_clobber_2 (double d) {
541         }
542
543         [MethodImplAttribute (MethodImplOptions.NoInlining)]
544         public static bool is_even (int i) {
545                 return i % 2 == 0;
546         }
547
548         /*
549                 lock (static_s) {
550                         Console.WriteLine ("HIT!");
551                 }
552                 return 0;
553         }
554         */
555
556         [MethodImplAttribute (MethodImplOptions.NoInlining)]
557         public static void arguments () {
558                 arg1 (SByte.MaxValue - 5, Byte.MaxValue - 5, true, Int16.MaxValue - 5, UInt16.MaxValue - 5, 'F', Int32.MaxValue - 5, UInt32.MaxValue - 5, Int64.MaxValue - 5, UInt64.MaxValue - 5, 1.2345f, 6.78910, new IntPtr (Int32.MaxValue - 5), new UIntPtr (UInt32.MaxValue - 5));
559                 int i = 42;
560                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
561                 Tests t = new Tests () { field_i = 42, field_s = "S" };
562                 t.arg3 ("BLA");
563         }
564
565         [MethodImplAttribute (MethodImplOptions.NoInlining)]
566         public static int arg1 (sbyte sb, byte b, bool bl, short s, ushort us, char c, int i, uint ui, long l, ulong ul, float f, double d, IntPtr ip, UIntPtr uip) {
567                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
568         }
569
570         [MethodImplAttribute (MethodImplOptions.NoInlining)]
571         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
572                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
573         }
574
575         [MethodImplAttribute (MethodImplOptions.NoInlining)]
576         public object arg3 (string s) {
577                 return s + s + s + s + this;
578         }
579
580         [MethodImplAttribute (MethodImplOptions.NoInlining)]
581         public static void objects () {
582                 Tests t = new Tests () { field_i = 42, field_bool1 = true, field_bool2 = false, field_char = 'A', field_byte = 129, field_sbyte = -33, field_short = Int16.MaxValue - 5, field_ushort = UInt16.MaxValue - 5, field_long = Int64.MaxValue - 5, field_ulong = UInt64.MaxValue - 5, field_float = 3.14f, field_double = 3.14f, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B, field_class = null, field_intptr = new IntPtr (Int32.MaxValue - 5), field_nullable = null };
583                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
584                 o2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, (int[,])Array.CreateInstance (typeof (int), new int [] { 2, 2}, new int [] { 1, 3}), new int[] { 0 });
585         }
586
587         [MethodImplAttribute (MethodImplOptions.NoInlining)]
588         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
589                 if (t == null || gc1 == null || gc2 == null)
590                         return null;
591                 else
592                         return this;
593         }
594
595         [MethodImplAttribute (MethodImplOptions.NoInlining)]
596         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
597                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
598         }
599
600         [MethodImplAttribute (MethodImplOptions.NoInlining)]
601         public static void objrefs () {
602                 Tests t = new Tests () {};
603                 set_child (t);
604                 t.objrefs1 ();
605                 t.child = null;
606                 GC.Collect ();
607                 objrefs2 ();
608         }
609
610         [MethodImplAttribute (MethodImplOptions.NoInlining)]
611         public static void set_child (Tests t) {
612                 t.child = new Tests ();
613         }
614
615         [MethodImplAttribute (MethodImplOptions.NoInlining)]
616         public void objrefs1 () {
617         }
618
619         [MethodImplAttribute (MethodImplOptions.NoInlining)]
620         public static void objrefs2 () {
621         }
622
623         public static void vtypes () {
624                 Tests t = new Tests () { field_struct = new AStruct () { i = 42, s = "S", k = 43 }, generic_field_struct = new GStruct<int> () { i = 42 }, field_boxed_struct = new AStruct () { i = 42 }, boxed_struct_field = new KeyValuePair<int, object> (1, (long)42 ) };
625                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
626                 AStruct[] arr = new AStruct[] { 
627                         new AStruct () { i = 1, s = "S1" },
628                         new AStruct () { i = 2, s = "S2" } };
629                 t.vtypes1 (s, arr);
630                 vtypes2 (s);
631                 vtypes3 (s);
632                 vtypes4 ();
633         }
634
635         [MethodImplAttribute (MethodImplOptions.NoInlining)]
636         public object vtypes1 (AStruct s, AStruct[] arr) {
637                 if (arr != null)
638                         return this;
639                 else
640                         return null;
641         }
642
643         [MethodImplAttribute (MethodImplOptions.NoInlining)]
644         public static void vtypes2 (AStruct s) {
645                 s.foo (5);
646         }
647
648         [MethodImplAttribute (MethodImplOptions.NoInlining)]
649         public static void vtypes3 (AStruct s) {
650                 AStruct.static_foo (5);
651         }
652
653         [MethodImplAttribute (MethodImplOptions.NoInlining)]
654         public static void vtypes4_2 (IRecStruct o) {
655         }
656
657         [MethodImplAttribute (MethodImplOptions.NoInlining)]
658         public static void vtypes4 () {
659                 IRecStruct s = new RecStruct ();
660                 s.foo (s);
661                 vtypes4_2 (s);
662         }
663
664         [MethodImplAttribute (MethodImplOptions.NoInlining)]
665         public static void locals () {
666                 string s = null;
667                 var astruct = new AStruct () { i = 42 };
668                 locals1 (null);
669                 locals2<string> (null, 5, "ABC", ref s, ref astruct);
670                 locals3 ();
671                 locals6 ();
672                 locals7<int> (22);
673         }
674
675         [MethodImplAttribute (MethodImplOptions.NoInlining)]
676         static void locals11 (double a, ref double b) {
677         }
678
679         [MethodImplAttribute (MethodImplOptions.NoInlining)]
680         public static void locals1 (string[] args) {
681                 long foo = 42;
682
683                 double ri = 1;
684                 locals11 (b: ref ri, a: ri);
685
686                 for (int j = 0; j < 10; ++j) {
687                         foo ++;
688                 }
689         }
690
691         [MethodImplAttribute (MethodImplOptions.NoInlining)]
692 #if NET_4_5
693         [StateMachine (typeof (int))]
694 #endif
695         public static void locals2<T> (string[] args, int arg, T t, ref string rs, ref AStruct astruct) {
696                 long i = 42;
697                 string s = "AB";
698
699                 for (int j = 0; j < 10; ++j) {
700                         if (s != null)
701                                 i ++;
702                         if (t != null)
703                                 i ++;
704                         astruct = new AStruct ();
705                 }
706                 rs = "A";
707         }
708
709
710         [MethodImplAttribute (MethodImplOptions.NoInlining)]
711         public static void locals3 () {
712                 string s = "B";
713                 s.ToString ();
714
715                 {
716                         long i = 42;
717                         i ++;
718                         locals4 ();
719                 }
720                 {
721                         string i = "A";
722                         i.ToString ();
723                         locals5 ();
724                 }
725                 {
726                         long j = 42;
727                         j ++;
728                 }
729         }
730
731         [MethodImplAttribute (MethodImplOptions.NoInlining)]
732         public static void locals4 () {
733         }
734
735         [MethodImplAttribute (MethodImplOptions.NoInlining)]
736         public static void locals5 () {
737         }
738
739         [MethodImplAttribute (MethodImplOptions.NoInlining)]
740         public static void locals6 () {
741                 int i = 0;
742                 int j = 0;
743                 for (i = 0; i < 10; ++i)
744                         j ++;
745                 sbyte sb = 0;
746                 for (i = 0; i < 10; ++i)
747                         sb ++;
748                 locals6_1 ();
749                 locals6_2 (j);
750                 locals6_3 ();
751                 locals6_4 (j);
752                 locals6_5 ();
753                 locals6_6 (sb);
754         }
755
756         [MethodImplAttribute (MethodImplOptions.NoInlining)]
757         public static void locals6_1 () {
758         }
759
760         [MethodImplAttribute (MethodImplOptions.NoInlining)]
761         public static void locals6_2 (int arg) {
762         }
763
764         [MethodImplAttribute (MethodImplOptions.NoInlining)]
765         public static void locals6_3 () {
766                 // Clobber all registers
767                 int sum = 0, i, j, k, l, m;
768                 for (i = 0; i < 100; ++i)
769                         sum ++;
770                 for (j = 0; j < 100; ++j)
771                         sum ++;
772                 for (k = 0; k < 100; ++k)
773                         sum ++;
774                 for (l = 0; l < 100; ++l)
775                         sum ++;
776                 for (m = 0; m < 100; ++m)
777                         sum ++;
778         }
779
780         [MethodImplAttribute (MethodImplOptions.NoInlining)]
781         public static void locals6_4 (int arg) {
782         }
783
784         [MethodImplAttribute (MethodImplOptions.NoInlining)]
785         public static void locals6_5 () {
786         }
787
788         [MethodImplAttribute (MethodImplOptions.NoInlining)]
789         public static void locals6_6 (int arg) {
790         }
791
792         [MethodImplAttribute (MethodImplOptions.NoInlining)]
793         public static void locals7<T> (T arg) {
794                 T t = arg;
795                 T t2 = t;
796         }
797
798         [MethodImplAttribute (MethodImplOptions.NoInlining)]
799         public static void line_numbers () {
800                 LineNumbers.ln1 ();
801         }
802
803         [MethodImplAttribute (MethodImplOptions.NoInlining)]
804         public static void suspend () {
805                 long i = 5;
806
807                 while (true) {
808                         i ++;
809                 }
810         }
811
812         [MethodImplAttribute (MethodImplOptions.NoInlining)]
813         public static void type_info () {
814                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
815                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
816                 int val = 0;
817                 unsafe {
818                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
819
820                         ti2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, ref val, (int*)IntPtr.Zero, 5, s, new Tests (), new Tests2 (), new GClass <int> (), AnEnum.B);
821                 }
822         }
823
824         [MethodImplAttribute (MethodImplOptions.NoInlining)]
825         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
826                 if (t == null || gc1 == null || gc2 == null)
827                         return null;
828                 else
829                         return this;
830         }
831
832         [MethodImplAttribute (MethodImplOptions.NoInlining)]
833         public static unsafe string ti2 (string[] s2, int[] s3, int[,] s4, ref int ri, int* ptr, int i, AStruct s, Tests t, Tests2 t2, GClass<int> g, AnEnum ae) {
834                 return s2 [0] + s3 [0] + s4 [0, 0];
835         }
836
837         [MethodImplAttribute (MethodImplOptions.NoInlining)]
838         public static void assembly_load () {
839                 assembly_load_2 ();
840         }
841
842         [MethodImplAttribute (MethodImplOptions.NoInlining)]
843         public static void assembly_load_2 () {
844                 // This will load System.dll while holding the loader lock
845                 new Foo ();
846         }
847
848         [MethodImplAttribute (MethodImplOptions.NoInlining)]
849         public static void invoke () {
850                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
851                 new Tests ().invoke_ex ();
852         }
853
854         [MethodImplAttribute (MethodImplOptions.NoInlining)]
855         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
856                 invoke2 ();
857         }
858
859         [MethodImplAttribute (MethodImplOptions.NoInlining)]
860         public void invoke2 () {
861         }
862
863         [MethodImplAttribute (MethodImplOptions.NoInlining)]
864         public void invoke_ex () {
865                 invoke_ex_inner ();
866         }
867
868         [MethodImplAttribute (MethodImplOptions.NoInlining)]
869         public void invoke_ex_inner () {
870                 try {
871                         throw new Exception ();
872                 } catch {
873                 }
874         }
875
876         int counter;
877
878         [MethodImplAttribute (MethodImplOptions.NoInlining)]
879         public void invoke_single_threaded () {
880                 // Spawn a thread incrementing a counter
881                 bool finished = false;
882
883                 new Thread (delegate () {
884                                 while (!finished)
885                                         counter ++;
886                 }).Start ();
887
888                 Thread.Sleep (100);
889
890                 invoke_single_threaded_2 ();
891
892                 finished = true;
893         }
894
895         [MethodImplAttribute (MethodImplOptions.NoInlining)]
896         public void invoke_single_threaded_2 () {
897         }
898
899         public void invoke_return_void () {
900         }
901
902         public string invoke_return_ref () {
903                 return "ABC";
904         }
905
906         public object invoke_return_null () {
907                 return null;
908         }
909
910         public int invoke_return_primitive () {
911                 return 42;
912         }
913
914         public int? invoke_return_nullable () {
915                 return 42;
916         }
917
918         public int? invoke_return_nullable_null () {
919                 return null;
920         }
921
922         public void invoke_type_load () {
923                 new Class3 ();
924         }
925
926         class Class3 {
927         }
928
929         public long invoke_pass_primitive (byte ub, sbyte sb, short ss, ushort us, int i, uint ui, long l, ulong ul, char c, bool b, float f, double d) {
930                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
931         }
932
933         public int invoke_pass_primitive2 (bool b) {
934                 return b ? 1 : 0;
935         }
936
937         public string invoke_pass_ref (string s) {
938                 return s;
939         }
940
941         public static string invoke_static_pass_ref (string s) {
942                 return s;
943         }
944
945         public static void invoke_static_return_void () {
946         }
947
948         public static void invoke_throws () {
949                 throw new Exception ();
950         }
951
952         public int invoke_iface () {
953                 return 42;
954         }
955
956         [MethodImplAttribute (MethodImplOptions.NoInlining)]
957         public static void exceptions () {
958                 try {
959                         throw new OverflowException ();
960                 } catch (Exception) {
961                 }
962                 try {
963                         throw new OverflowException ();
964                 } catch (Exception) {
965                 }
966                 try {
967                         throw new ArgumentException ();
968                 } catch (Exception) {
969                 }
970                 try {
971                         throw new OverflowException ();
972                 } catch (Exception) {
973                 }
974                 // no subclasses
975                 try {
976                         throw new OverflowException ();
977                 } catch (Exception) {
978                 }
979                 try {
980                         throw new Exception ();
981                 } catch (Exception) {
982                 }
983
984                 object o = null;
985                 try {
986                         o.GetType ();
987                 } catch (Exception) {
988                 }
989
990                 try {
991                         exceptions2 ();
992                 } catch (Exception) {
993                 }
994         }
995
996         [MethodImplAttribute (MethodImplOptions.NoInlining)]
997         public static void unhandled_exception () {
998                 ThreadPool.QueueUserWorkItem (delegate {
999                                 throw new InvalidOperationException ();
1000                         });
1001                 Thread.Sleep (10000);
1002         }
1003
1004         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1005         public static void unhandled_exception_endinvoke_2 () {
1006         }
1007
1008         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1009         public static void unhandled_exception_endinvoke () {
1010                         Action action = new Action (() => 
1011                         {
1012                                 throw new Exception ("thrown");
1013                         });
1014                         action.BeginInvoke ((ar) => {
1015                                 try {
1016                                         action.EndInvoke (ar);
1017                                 } catch (Exception ex) {
1018                                         //Console.WriteLine (ex);
1019                                 }
1020                         }, null);
1021                 Thread.Sleep (1000);
1022                 unhandled_exception_endinvoke_2 ();
1023         }
1024
1025         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1026         public static void unhandled_exception_user () {
1027 #if NET_4_5
1028                 System.Threading.Tasks.Task.Factory.StartNew (() => {
1029                                 Throw ();
1030                         });
1031                 Thread.Sleep (10000);
1032 #endif
1033         }
1034
1035         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1036         public static void Throw () {
1037                 throw new Exception ();
1038         }
1039
1040         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
1041         {
1042                 if (dlg == null)
1043                         throw new ArgumentNullException ();
1044                 if (dlg.Target != null)
1045                         throw new ArgumentException ();
1046                 if (dlg.Method == null)
1047                         throw new ArgumentException ();
1048
1049                 var ret_type = dlg.Method.ReturnType;
1050                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
1051
1052                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
1053                 var ig = dynamic.GetILGenerator ();
1054
1055                 LocalBuilder retval = null;
1056                 if (ret_type != typeof (void))
1057                         retval = ig.DeclareLocal (ret_type);
1058
1059                 var label = ig.BeginExceptionBlock ();
1060
1061                 for (int i = 0; i < param_types.Length; i++)
1062                         ig.Emit (OpCodes.Ldarg, i);
1063                 ig.Emit (OpCodes.Call, dlg.Method);
1064
1065                 if (retval != null)
1066                         ig.Emit (OpCodes.Stloc, retval);
1067
1068                 ig.Emit (OpCodes.Leave, label);
1069
1070                 ig.BeginExceptFilterBlock ();
1071
1072                 ig.Emit (OpCodes.Call, filter_method);
1073
1074                 ig.BeginCatchBlock (null);
1075
1076                 ig.Emit (OpCodes.Pop);
1077
1078                 ig.EndExceptionBlock ();
1079
1080                 if (retval != null)
1081                         ig.Emit (OpCodes.Ldloc, retval);
1082
1083                 ig.Emit (OpCodes.Ret);
1084
1085                 return dynamic.CreateDelegate (dlg.GetType ());
1086         }
1087
1088         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1089         static void exception_filter_method () {
1090                 throw new InvalidOperationException ();
1091         }
1092
1093         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1094         static int exception_filter_filter (Exception exc) {
1095                 return 1;
1096         }
1097
1098         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1099         public static void exception_filter () {
1100                 var method = typeof (Tests).GetMethod (
1101                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
1102                 var filter_method = typeof (Tests).GetMethod (
1103                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
1104
1105                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
1106
1107                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
1108
1109                 wrapper ();
1110         }
1111
1112         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1113         public static bool return_true () {
1114                 return true;
1115         }
1116
1117         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1118         public static void exceptions2 () {
1119                 if (return_true ())
1120                         throw new Exception ();
1121                 Console.WriteLine ();
1122         }
1123
1124         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1125         public static void threads () {
1126                 Thread t = new Thread (delegate () {});
1127
1128                 t.Start ();
1129                 t.Join ();
1130         }
1131
1132         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1133         public static void domains () {
1134                 AppDomain domain = AppDomain.CreateDomain ("domain");
1135
1136                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
1137                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
1138
1139                 domains_2 (o, new CrossDomain ());
1140
1141                 o.invoke_2 ();
1142
1143                 o.invoke ();
1144
1145                 o.invoke_2 ();
1146
1147                 AppDomain.Unload (domain);
1148
1149                 domains_3 ();
1150         }
1151
1152         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1153         public static void domains_2 (object o, object o2) {
1154         }
1155
1156         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1157         public static void domains_3 () {
1158         }
1159
1160         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1161         public static void invoke_in_domain () {
1162         }
1163
1164         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1165         public static void invoke_in_domain_2 () {
1166         }
1167
1168         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1169         public static void dynamic_methods () {
1170                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
1171                 var ig = m.GetILGenerator ();
1172
1173                 ig.Emit (OpCodes.Ldstr, "FOO");
1174                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
1175                 ig.Emit (OpCodes.Ret);
1176
1177                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
1178
1179                 del (0);
1180         }
1181
1182         public static void dyn_call (string s) {
1183         }
1184
1185         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1186         public static void ref_emit () {
1187                 AssemblyName assemblyName = new AssemblyName ();
1188                 assemblyName.Name = "foo";
1189
1190                 AssemblyBuilder assembly =
1191                         Thread.GetDomain ().DefineDynamicAssembly (
1192                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
1193
1194                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
1195
1196                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
1197                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
1198                 ILGenerator ig = mb.GetILGenerator ();
1199                 ig.Emit (OpCodes.Ldstr, "FOO");
1200                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
1201                 ig.Emit (OpCodes.Ret);
1202
1203                 Type t = tb.CreateType ();
1204
1205                 t.GetMethod ("ref_emit_method").Invoke (null, null);
1206         }
1207
1208         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1209         public static void ref_emit_call (string s) {
1210         }
1211
1212         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1213         public static void frames_in_native () {
1214                 Thread.Sleep (500);
1215                 var evt = new ManualResetEvent (false);
1216                 
1217                 object mon = new object ();
1218                 ThreadPool.QueueUserWorkItem (delegate {
1219                                 frames_in_native_2 ();
1220                                 evt.Set ();
1221                         });
1222                 evt.WaitOne ();
1223         }
1224
1225         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1226         static void frames_in_native_2 () {
1227                 frames_in_native_3 ();
1228         }
1229
1230         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1231         static void frames_in_native_3 () {
1232         }
1233
1234         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1235         public static void string_call (string s) {
1236         }
1237
1238         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1239         public static void ss_regress_654694 () {
1240                 if (true) {
1241                         string h = "hi";
1242                         string_call (h);
1243                 }
1244         }
1245
1246         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1247         public static void user () {
1248                 Debugger.Break ();
1249
1250                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
1251         }
1252
1253         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1254         public static void type_load () {
1255                 type_load_2 ();
1256         }
1257
1258         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1259         static void type_load_2 () {
1260                 var c1 = new Dictionary<int, int> ();
1261                 c1.ToString ();
1262                 var c = new TypeLoadClass ();
1263                 c.ToString ();
1264                 var c2 = new TypeLoadClass2 ();
1265                 c2.ToString ();
1266         }
1267
1268         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1269         public static void regress () {
1270                 regress_2755 (DateTime.Now);
1271         }
1272
1273         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1274         public static unsafe void regress_2755 (DateTime d) {
1275                 int* buffer = stackalloc int [128];
1276
1277                 regress_2755_2 ();
1278
1279                 int sum = 0;
1280                 for (int i = 0; i < 128; ++i)
1281                         sum += buffer [i];
1282
1283                 regress_2755_3 (sum);
1284         }
1285
1286         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1287         public static void regress_2755_2 () {
1288         }
1289
1290         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1291         public static void regress_2755_3 (int sum) {
1292         }
1293
1294         static object gc_suspend_field;
1295
1296         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1297         static unsafe void set_gc_suspend_field () {
1298                 set_gc_suspend_field_2 ();
1299                 // Clear stack
1300                 int* buffer = stackalloc int [4096];
1301         }
1302
1303         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1304         static void set_gc_suspend_field_2 () {
1305                 gc_suspend_field = new object ();
1306         }
1307
1308         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1309         static void gc_suspend_1 () {
1310         }
1311
1312         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1313         public static void gc_suspend_invoke () {
1314                 gc_suspend_field = null;
1315                 GC.Collect ();
1316                 GC.WaitForPendingFinalizers ();
1317         }
1318
1319         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1320         public static void gc_suspend () {
1321                 set_gc_suspend_field ();
1322                 gc_suspend_1 ();
1323         }
1324
1325         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1326         public static void generic_method<T> () where T : class {
1327         }
1328
1329         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1330         public void evaluate_method_2 () {
1331         }
1332
1333         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1334         public void evaluate_method () {
1335                 field_i = 42;
1336                 evaluate_method_2 ();
1337         }
1338
1339         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1340         static void set_ip_1 () {
1341         }
1342
1343         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1344         static void set_ip_2 () {
1345         }
1346
1347         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1348         public static void set_ip () {
1349                 int i, j;
1350
1351                 i = 1;
1352                 set_ip_1 ();
1353                 i = 5;
1354                 j = 5;
1355                 set_ip_2 ();
1356         }
1357 }
1358
1359 class TypeLoadClass {
1360 }
1361
1362 class TypeLoadClass2 {
1363 }
1364
1365 public class CrossDomain : MarshalByRefObject
1366 {
1367         public void invoke () {
1368                 Tests.invoke_in_domain ();
1369         }
1370
1371         public void invoke_2 () {
1372                 Tests.invoke_in_domain_2 ();
1373         }
1374
1375         public int invoke_3 () {
1376                 return 42;
1377         }
1378 }       
1379
1380 public class Foo
1381 {
1382         public ProcessStartInfo info;
1383 }
1384
1385 // Class used for line number info testing, don't change its layout
1386 public class LineNumbers
1387 {
1388         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1389         public static void ln1 () {
1390                 // Column 3
1391                 ln2 ();
1392                 ln3 ();
1393         }
1394
1395         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1396         public static void ln2 () {
1397         }
1398
1399         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1400         public static void ln3 () {
1401 #pragma warning disable 0219
1402                 int i = 5;
1403 #pragma warning restore 0219
1404                 #line 55 "FOO"
1405         }
1406 }