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