96c2e5f00f303ff867954d14ce2039929c264272
[mono.git] / mono / tests / 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 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
33 [DebuggerTypeProxy (typeof (Tests))]
34 public class Tests2 {
35         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
36         public int field_j;
37         public static int static_field_j;
38
39         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
40         public int AProperty {
41                 get {
42                         return 0;
43                 }
44         }
45
46         public void invoke () {
47         }
48 }
49
50 public struct AStruct {
51         public int i;
52         public string s;
53         public byte k;
54
55         [MethodImplAttribute (MethodImplOptions.NoInlining)]
56         public int foo (int val) {
57                 return val;
58         }
59
60         [MethodImplAttribute (MethodImplOptions.NoInlining)]
61         public static int static_foo (int val) {
62                 return val;
63         }
64
65         [MethodImplAttribute (MethodImplOptions.NoInlining)]
66         public int invoke_return_int () {
67                 return i;
68         }
69 }
70
71 public class GClass<T> {
72         public T field;
73         public static T static_field;
74
75         [MethodImplAttribute (MethodImplOptions.NoInlining)]
76         public GClass () {
77         }
78 }
79
80 public struct GStruct<T> {
81         public T i;
82 }
83
84 public class Tests : TestsBase
85 {
86 #pragma warning disable 0414
87         int field_i;
88         string field_s;
89         AnEnum field_enum;
90         bool field_bool1, field_bool2;
91         char field_char;
92         byte field_byte;
93         sbyte field_sbyte;
94         short field_short;
95         ushort field_ushort;
96         long field_long;
97         ulong field_ulong;
98         float field_float;
99         double field_double;
100         Thread field_class;
101         static int static_i = 55;
102         static string static_s = "A";
103         public const int literal_i = 56;
104         public const string literal_s = "B";
105         public object child;
106         public AStruct field_struct;
107         public object field_boxed_struct;
108         public GStruct<int> generic_field_struct;
109         [ThreadStatic]
110         public static int tls_i;
111         public static bool is_attached = Debugger.IsAttached;
112
113 #pragma warning restore 0414
114
115         public class NestedClass {
116         }
117
118         public int IntProperty {
119                 get {
120                         return field_i;
121                 }
122                 set {
123                         field_i = value;
124                 }
125         }
126
127         public int ReadOnlyProperty {
128                 get {
129                         return field_i;
130                 }
131         }
132
133         public int this [int index] {
134                 get {
135                         return field_i;
136                 }
137         }
138
139         public static int Main (String[] args) {
140                 if (args.Length > 0 && args [0] == "suspend-test")
141                         /* This contains an infinite loop, so execute it conditionally */
142                         suspend ();
143                 breakpoints ();
144                 single_stepping ();
145                 arguments ();
146                 objects ();
147                 objrefs ();
148                 vtypes ();
149                 locals ();
150                 line_numbers ();
151                 type_info ();
152                 assembly_load ();
153                 invoke ();
154                 exceptions ();
155                 threads ();
156                 dynamic_methods ();
157                 if (args.Length > 0 && args [0] == "domain-test")
158                         /* This takes a lot of time, so execute it conditionally */
159                         domains ();
160                 if (args.Length > 0 && args [0] == "ref-emit-test")
161                         ref_emit ();
162                 if (args.Length > 0 && args [0] == "frames-in-native")
163                         frames_in_native ();
164                 if (args.Length >0 && args [0] == "invoke-single-threaded")
165                         new Tests ().invoke_single_threaded ();
166                 return 3;
167         }
168
169         public static void breakpoints () {
170                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
171                 bp3 ();
172                 bp7<int> ();
173                 bp7<string> ();
174
175                 bp1 ();
176                 bp2 ();
177                 bp3 ();
178                 bp4 ();
179                 bp4 ();
180                 bp4 ();
181                 bp5 ();
182                 bp6<string> ();
183                 bp7<int> ();
184                 bp7<string> ();
185         }
186
187         [MethodImplAttribute (MethodImplOptions.NoInlining)]
188         public static void bp1 () {
189         }
190
191         [MethodImplAttribute (MethodImplOptions.NoInlining)]
192         public static void bp2 () {
193         }
194
195         [MethodImplAttribute (MethodImplOptions.NoInlining)]
196         public static void bp3 () {
197         }
198
199         [MethodImplAttribute (MethodImplOptions.NoInlining)]
200         public static void bp4 () {
201         }
202
203         [MethodImplAttribute (MethodImplOptions.NoInlining)]
204         public static void bp5 () {
205         }
206
207         [MethodImplAttribute (MethodImplOptions.NoInlining)]
208         public static void bp6<T> () {
209         }
210
211         [MethodImplAttribute (MethodImplOptions.NoInlining)]
212         public static void bp7<T> () {
213         }
214
215         [MethodImplAttribute (MethodImplOptions.NoInlining)]
216         public static void single_stepping () {
217                 ss1 ();
218                 ss2 ();
219                 ss3 ();
220                 ss4 ();
221                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
222         }
223
224         [MethodImplAttribute (MethodImplOptions.NoInlining)]
225         public static void ss1 () {
226         }
227
228         [MethodImplAttribute (MethodImplOptions.NoInlining)]
229         public static void ss2 () {
230         }
231
232         [MethodImplAttribute (MethodImplOptions.NoInlining)]
233         public static int ss3 () {
234                 int sum = 0;
235
236                 for (int i = 0; i < 10; ++i)
237                         sum += i;
238
239                 return sum;
240         }
241
242         [MethodImplAttribute (MethodImplOptions.NoInlining)]
243         public static int ss4 () {
244                 ss1 (); ss1 ();
245                 ss2 ();
246                 return 0;
247         }
248
249         [MethodImplAttribute (MethodImplOptions.NoInlining)]
250         public static void ss5 (int[] arr, Func<int, bool> selector) {
251                 // Call into linq which calls back into this assembly
252                 arr.Count (selector);
253         }
254
255         [MethodImplAttribute (MethodImplOptions.NoInlining)]
256         public static bool is_even (int i) {
257                 return i % 2 == 0;
258         }
259
260         /*
261                 lock (static_s) {
262                         Console.WriteLine ("HIT!");
263                 }
264                 return 0;
265         }
266         */
267
268         [MethodImplAttribute (MethodImplOptions.NoInlining)]
269         public static void arguments () {
270                 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));
271                 int i = 42;
272                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
273                 Tests t = new Tests () { field_i = 42, field_s = "S" };
274                 t.arg3 ("BLA");
275         }
276
277         [MethodImplAttribute (MethodImplOptions.NoInlining)]
278         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) {
279                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
280         }
281
282         [MethodImplAttribute (MethodImplOptions.NoInlining)]
283         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
284                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
285         }
286
287         [MethodImplAttribute (MethodImplOptions.NoInlining)]
288         public object arg3 (string s) {
289                 return s + s + s + s + this;
290         }
291
292         [MethodImplAttribute (MethodImplOptions.NoInlining)]
293         public static void objects () {
294                 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 };
295                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
296                 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 });
297         }
298
299         [MethodImplAttribute (MethodImplOptions.NoInlining)]
300         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
301                 if (t == null || gc1 == null || gc2 == null)
302                         return null;
303                 else
304                         return this;
305         }
306
307         [MethodImplAttribute (MethodImplOptions.NoInlining)]
308         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
309                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
310         }
311
312         [MethodImplAttribute (MethodImplOptions.NoInlining)]
313         public static void objrefs () {
314                 Tests t = new Tests () {};
315                 set_child (t);
316                 t.objrefs1 ();
317                 t.child = null;
318                 GC.Collect ();
319                 objrefs2 ();
320         }
321
322         [MethodImplAttribute (MethodImplOptions.NoInlining)]
323         public static void set_child (Tests t) {
324                 t.child = new Tests ();
325         }
326
327         [MethodImplAttribute (MethodImplOptions.NoInlining)]
328         public void objrefs1 () {
329         }
330
331         [MethodImplAttribute (MethodImplOptions.NoInlining)]
332         public static void objrefs2 () {
333         }
334
335         public static void vtypes () {
336                 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 }};
337                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
338                 AStruct[] arr = new AStruct[] { 
339                         new AStruct () { i = 1, s = "S1" },
340                         new AStruct () { i = 2, s = "S2" } };
341                 t.vtypes1 (s, arr);
342                 vtypes2 (s);
343                 vtypes3 (s);
344         }
345
346         [MethodImplAttribute (MethodImplOptions.NoInlining)]
347         public object vtypes1 (AStruct s, AStruct[] arr) {
348                 if (arr != null)
349                         return this;
350                 else
351                         return null;
352         }
353
354         [MethodImplAttribute (MethodImplOptions.NoInlining)]
355         public static void vtypes2 (AStruct s) {
356                 s.foo (5);
357         }
358
359         [MethodImplAttribute (MethodImplOptions.NoInlining)]
360         public static void vtypes3 (AStruct s) {
361                 AStruct.static_foo (5);
362         }
363
364         [MethodImplAttribute (MethodImplOptions.NoInlining)]
365         public static void locals () {
366                 locals1 (null);
367                 locals2 (null, 5);
368         }
369
370         [MethodImplAttribute (MethodImplOptions.NoInlining)]
371         public static void locals1 (string[] args) {
372                 long foo = 42;
373
374                 for (int j = 0; j < 10; ++j) {
375                         foo ++;
376                 }
377         }
378
379         [MethodImplAttribute (MethodImplOptions.NoInlining)]
380         public static void locals2 (string[] args, int arg) {
381                 long i = 42;
382                 string s = "AB";
383
384                 for (int j = 0; j < 10; ++j) {
385                         if (s != null)
386                                 i ++;
387                 }
388         }
389
390         [MethodImplAttribute (MethodImplOptions.NoInlining)]
391         public static void line_numbers () {
392                 LineNumbers.ln1 ();
393         }
394
395         [MethodImplAttribute (MethodImplOptions.NoInlining)]
396         public static void suspend () {
397                 long i = 5;
398
399                 while (true) {
400                         i ++;
401                 }
402         }
403
404         [MethodImplAttribute (MethodImplOptions.NoInlining)]
405         public static void type_info () {
406                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
407                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
408                 int val = 0;
409                 unsafe {
410                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
411
412                         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> ());
413                 }
414         }
415
416         [MethodImplAttribute (MethodImplOptions.NoInlining)]
417         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
418                 if (t == null || gc1 == null || gc2 == null)
419                         return null;
420                 else
421                         return this;
422         }
423
424         [MethodImplAttribute (MethodImplOptions.NoInlining)]
425         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) {
426                 return s2 [0] + s3 [0] + s4 [0, 0];
427         }
428
429         [MethodImplAttribute (MethodImplOptions.NoInlining)]
430         public static void assembly_load () {
431                 assembly_load_2 ();
432         }
433
434         [MethodImplAttribute (MethodImplOptions.NoInlining)]
435         public static void assembly_load_2 () {
436                 // This will load System.dll while holding the loader lock
437                 new Foo ();
438         }
439
440         [MethodImplAttribute (MethodImplOptions.NoInlining)]
441         public static void invoke () {
442                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42 });
443         }
444
445         [MethodImplAttribute (MethodImplOptions.NoInlining)]
446         public void invoke1 (Tests2 t, AStruct s) {
447                 invoke2 ();
448         }
449
450         [MethodImplAttribute (MethodImplOptions.NoInlining)]
451         public void invoke2 () {
452         }
453
454         int counter;
455
456         [MethodImplAttribute (MethodImplOptions.NoInlining)]
457         public void invoke_single_threaded () {
458                 // Spawn a thread incrementing a counter
459                 bool finished = false;
460
461                 new Thread (delegate () {
462                                 while (!finished)
463                                         counter ++;
464                 }).Start ();
465
466                 Thread.Sleep (100);
467
468                 invoke_single_threaded_2 ();
469
470                 finished = true;
471         }
472
473         [MethodImplAttribute (MethodImplOptions.NoInlining)]
474         public void invoke_single_threaded_2 () {
475         }
476
477         public void invoke_return_void () {
478         }
479
480         public string invoke_return_ref () {
481                 return "ABC";
482         }
483
484         public object invoke_return_null () {
485                 return null;
486         }
487
488         public int invoke_return_primitive () {
489                 return 42;
490         }
491
492         public void invoke_type_load () {
493                 new Class3 ();
494         }
495
496         class Class3 {
497         }
498
499         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) {
500                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
501         }
502
503         public int invoke_pass_primitive2 (bool b) {
504                 return b ? 1 : 0;
505         }
506
507         public string invoke_pass_ref (string s) {
508                 return s;
509         }
510
511         public static string invoke_static_pass_ref (string s) {
512                 return s;
513         }
514
515         public static void invoke_static_return_void () {
516         }
517
518         public static void invoke_throws () {
519                 throw new Exception ();
520         }
521
522         [MethodImplAttribute (MethodImplOptions.NoInlining)]
523         public static void exceptions () {
524                 try {
525                         throw new OverflowException ();
526                 } catch (Exception) {
527                 }
528                 try {
529                         throw new OverflowException ();
530                 } catch (Exception) {
531                 }
532                 try {
533                         throw new ArgumentException ();
534                 } catch (Exception) {
535                 }
536
537                 object o = null;
538                 try {
539                         o.GetType ();
540                 } catch (Exception) {
541                 }
542         }
543
544         [MethodImplAttribute (MethodImplOptions.NoInlining)]
545         public static void threads () {
546                 Thread t = new Thread (delegate () {});
547
548                 t.Start ();
549                 t.Join ();
550         }
551
552         [MethodImplAttribute (MethodImplOptions.NoInlining)]
553         public static void domains () {
554                 AppDomain domain = AppDomain.CreateDomain ("domain");
555
556                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
557                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
558
559                 o.invoke ();
560
561                 AppDomain.Unload (domain);
562
563                 domains_2 ();
564         }
565
566         [MethodImplAttribute (MethodImplOptions.NoInlining)]
567         public static void domains_2 () {
568         }
569
570         [MethodImplAttribute (MethodImplOptions.NoInlining)]
571         public static void invoke_in_domain () {
572         }
573
574         [MethodImplAttribute (MethodImplOptions.NoInlining)]
575         public static void dynamic_methods () {
576                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
577                 var ig = m.GetILGenerator ();
578
579                 ig.Emit (OpCodes.Ldstr, "FOO");
580                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
581                 ig.Emit (OpCodes.Ret);
582
583                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
584
585                 del (0);
586         }
587
588         public static void dyn_call (string s) {
589         }
590
591         [MethodImplAttribute (MethodImplOptions.NoInlining)]
592         public static void ref_emit () {
593                 AssemblyName assemblyName = new AssemblyName ();
594                 assemblyName.Name = "foo";
595
596                 AssemblyBuilder assembly =
597                         Thread.GetDomain ().DefineDynamicAssembly (
598                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
599
600                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
601
602                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
603                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
604                 ILGenerator ig = mb.GetILGenerator ();
605                 ig.Emit (OpCodes.Ldstr, "FOO");
606                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
607                 ig.Emit (OpCodes.Ret);
608
609                 Type t = tb.CreateType ();
610
611                 t.GetMethod ("ref_emit_method").Invoke (null, null);
612         }
613
614         [MethodImplAttribute (MethodImplOptions.NoInlining)]
615         public static void ref_emit_call (string s) {
616         }
617
618         [MethodImplAttribute (MethodImplOptions.NoInlining)]
619         public static void frames_in_native () {
620                 Thread.Sleep (500);
621         }
622 }
623
624 public class CrossDomain : MarshalByRefObject
625 {
626         public void invoke () {
627                 Tests.invoke_in_domain ();
628         }
629 }       
630
631 public class Foo
632 {
633         public ProcessStartInfo info;
634 }
635
636 // Class used for line number info testing, don't change its layout
637 public class LineNumbers
638 {
639         [MethodImplAttribute (MethodImplOptions.NoInlining)]
640         public static void ln1 () {
641                 ln2 ();
642                 ln3 ();
643         }
644
645         [MethodImplAttribute (MethodImplOptions.NoInlining)]
646         public static void ln2 () {
647         }
648
649         [MethodImplAttribute (MethodImplOptions.NoInlining)]
650         public static void ln3 () {
651         }
652 }