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