2010-05-11 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / dtest-app.cs
blobfe6e4529495f9faea9b3dfa48bd0afcd64ee851b
1 /*
2 * dtest-app.cs:
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;
15 public class TestsBase
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
27 public enum AnEnum {
28 A = 0,
29 B= 1
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;
39 [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
40 public int AProperty {
41 get {
42 return 0;
46 public void invoke () {
50 public struct AStruct {
51 public int i;
52 public string s;
53 public byte k;
54 public IntPtr j;
56 [MethodImplAttribute (MethodImplOptions.NoInlining)]
57 public int foo (int val) {
58 return val;
61 [MethodImplAttribute (MethodImplOptions.NoInlining)]
62 public static int static_foo (int val) {
63 return val;
66 [MethodImplAttribute (MethodImplOptions.NoInlining)]
67 public int invoke_return_int () {
68 return i;
71 [MethodImplAttribute (MethodImplOptions.NoInlining)]
72 public static int invoke_static () {
73 return 5;
76 [MethodImplAttribute (MethodImplOptions.NoInlining)]
77 public IntPtr invoke_return_intptr () {
78 return j;
82 public class GClass<T> {
83 public T field;
84 public static T static_field;
86 [MethodImplAttribute (MethodImplOptions.NoInlining)]
87 public GClass () {
91 public struct GStruct<T> {
92 public T i;
94 public int j;
96 [MethodImplAttribute (MethodImplOptions.NoInlining)]
97 public int invoke_return_int () {
98 return j;
102 public class Tests : TestsBase
104 #pragma warning disable 0414
105 int field_i;
106 string field_s;
107 AnEnum field_enum;
108 bool field_bool1, field_bool2;
109 char field_char;
110 byte field_byte;
111 sbyte field_sbyte;
112 short field_short;
113 ushort field_ushort;
114 long field_long;
115 ulong field_ulong;
116 float field_float;
117 double field_double;
118 Thread field_class;
119 IntPtr field_intptr;
120 static int static_i = 55;
121 static string static_s = "A";
122 public const int literal_i = 56;
123 public const string literal_s = "B";
124 public object child;
125 public AStruct field_struct;
126 public object field_boxed_struct;
127 public GStruct<int> generic_field_struct;
128 [ThreadStatic]
129 public static int tls_i;
130 public static bool is_attached = Debugger.IsAttached;
132 #pragma warning restore 0414
134 public class NestedClass {
137 public int IntProperty {
138 get {
139 return field_i;
141 set {
142 field_i = value;
146 public int ReadOnlyProperty {
147 get {
148 return field_i;
152 public int this [int index] {
153 get {
154 return field_i;
158 public static int Main (String[] args) {
159 if (args.Length > 0 && args [0] == "suspend-test")
160 /* This contains an infinite loop, so execute it conditionally */
161 suspend ();
162 breakpoints ();
163 single_stepping ();
164 arguments ();
165 objects ();
166 objrefs ();
167 vtypes ();
168 locals ();
169 line_numbers ();
170 type_info ();
171 assembly_load ();
172 invoke ();
173 exceptions ();
174 threads ();
175 dynamic_methods ();
176 if (args.Length > 0 && args [0] == "domain-test")
177 /* This takes a lot of time, so execute it conditionally */
178 domains ();
179 if (args.Length > 0 && args [0] == "ref-emit-test")
180 ref_emit ();
181 if (args.Length > 0 && args [0] == "frames-in-native")
182 frames_in_native ();
183 if (args.Length >0 && args [0] == "invoke-single-threaded")
184 new Tests ().invoke_single_threaded ();
185 return 3;
188 public static void breakpoints () {
189 /* Call these early so it is JITted by the time a breakpoint is placed on it */
190 bp3 ();
191 bp7<int> ();
192 bp7<string> ();
194 bp1 ();
195 bp2 ();
196 bp3 ();
197 bp4 ();
198 bp4 ();
199 bp4 ();
200 bp5 ();
201 bp6<string> ();
202 bp7<int> ();
203 bp7<string> ();
206 [MethodImplAttribute (MethodImplOptions.NoInlining)]
207 public static void bp1 () {
210 [MethodImplAttribute (MethodImplOptions.NoInlining)]
211 public static void bp2 () {
214 [MethodImplAttribute (MethodImplOptions.NoInlining)]
215 public static void bp3 () {
218 [MethodImplAttribute (MethodImplOptions.NoInlining)]
219 public static void bp4 () {
222 [MethodImplAttribute (MethodImplOptions.NoInlining)]
223 public static void bp5 () {
226 [MethodImplAttribute (MethodImplOptions.NoInlining)]
227 public static void bp6<T> () {
230 [MethodImplAttribute (MethodImplOptions.NoInlining)]
231 public static void bp7<T> () {
234 [MethodImplAttribute (MethodImplOptions.NoInlining)]
235 public static void single_stepping () {
236 ss1 ();
237 ss2 ();
238 ss3 ();
239 ss3_2 ();
240 ss4 ();
241 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
242 try {
243 ss6 (true);
244 } catch {
248 [MethodImplAttribute (MethodImplOptions.NoInlining)]
249 public static void ss1 () {
252 [MethodImplAttribute (MethodImplOptions.NoInlining)]
253 public static void ss2 () {
256 [MethodImplAttribute (MethodImplOptions.NoInlining)]
257 public static int ss3 () {
258 int sum = 0;
260 for (int i = 0; i < 10; ++i)
261 sum += i;
263 return sum;
266 [MethodImplAttribute (MethodImplOptions.NoInlining)]
267 public static void ss3_2 () {
268 ss3_2_2 ();
271 [MethodImplAttribute (MethodImplOptions.NoInlining)]
272 public static void ss3_2_2 () {
275 [MethodImplAttribute (MethodImplOptions.NoInlining)]
276 public static int ss4 () {
277 ss1 (); ss1 ();
278 ss2 ();
279 return 0;
282 [MethodImplAttribute (MethodImplOptions.NoInlining)]
283 public static void ss5 (int[] arr, Func<int, bool> selector) {
284 // Call into linq which calls back into this assembly
285 arr.Count (selector);
288 [MethodImplAttribute (MethodImplOptions.NoInlining)]
289 public static void ss6 (bool b) {
290 if (b) {
291 ss7 ();
292 throw new Exception ();
296 [MethodImplAttribute (MethodImplOptions.NoInlining)]
297 public static void ss7 () {
300 [MethodImplAttribute (MethodImplOptions.NoInlining)]
301 public static bool is_even (int i) {
302 return i % 2 == 0;
306 lock (static_s) {
307 Console.WriteLine ("HIT!");
309 return 0;
313 [MethodImplAttribute (MethodImplOptions.NoInlining)]
314 public static void arguments () {
315 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));
316 int i = 42;
317 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
318 Tests t = new Tests () { field_i = 42, field_s = "S" };
319 t.arg3 ("BLA");
322 [MethodImplAttribute (MethodImplOptions.NoInlining)]
323 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) {
324 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
327 [MethodImplAttribute (MethodImplOptions.NoInlining)]
328 public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
329 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
332 [MethodImplAttribute (MethodImplOptions.NoInlining)]
333 public object arg3 (string s) {
334 return s + s + s + s + this;
337 [MethodImplAttribute (MethodImplOptions.NoInlining)]
338 public static void objects () {
339 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) };
340 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
341 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 });
344 [MethodImplAttribute (MethodImplOptions.NoInlining)]
345 public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
346 if (t == null || gc1 == null || gc2 == null)
347 return null;
348 else
349 return this;
352 [MethodImplAttribute (MethodImplOptions.NoInlining)]
353 public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
354 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
357 [MethodImplAttribute (MethodImplOptions.NoInlining)]
358 public static void objrefs () {
359 Tests t = new Tests () {};
360 set_child (t);
361 t.objrefs1 ();
362 t.child = null;
363 GC.Collect ();
364 objrefs2 ();
367 [MethodImplAttribute (MethodImplOptions.NoInlining)]
368 public static void set_child (Tests t) {
369 t.child = new Tests ();
372 [MethodImplAttribute (MethodImplOptions.NoInlining)]
373 public void objrefs1 () {
376 [MethodImplAttribute (MethodImplOptions.NoInlining)]
377 public static void objrefs2 () {
380 public static void vtypes () {
381 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 }};
382 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
383 AStruct[] arr = new AStruct[] {
384 new AStruct () { i = 1, s = "S1" },
385 new AStruct () { i = 2, s = "S2" } };
386 t.vtypes1 (s, arr);
387 vtypes2 (s);
388 vtypes3 (s);
391 [MethodImplAttribute (MethodImplOptions.NoInlining)]
392 public object vtypes1 (AStruct s, AStruct[] arr) {
393 if (arr != null)
394 return this;
395 else
396 return null;
399 [MethodImplAttribute (MethodImplOptions.NoInlining)]
400 public static void vtypes2 (AStruct s) {
401 s.foo (5);
404 [MethodImplAttribute (MethodImplOptions.NoInlining)]
405 public static void vtypes3 (AStruct s) {
406 AStruct.static_foo (5);
409 [MethodImplAttribute (MethodImplOptions.NoInlining)]
410 public static void locals () {
411 locals1 (null);
412 locals2 (null, 5);
415 [MethodImplAttribute (MethodImplOptions.NoInlining)]
416 public static void locals1 (string[] args) {
417 long foo = 42;
419 for (int j = 0; j < 10; ++j) {
420 foo ++;
424 [MethodImplAttribute (MethodImplOptions.NoInlining)]
425 public static void locals2 (string[] args, int arg) {
426 long i = 42;
427 string s = "AB";
429 for (int j = 0; j < 10; ++j) {
430 if (s != null)
431 i ++;
435 [MethodImplAttribute (MethodImplOptions.NoInlining)]
436 public static void line_numbers () {
437 LineNumbers.ln1 ();
440 [MethodImplAttribute (MethodImplOptions.NoInlining)]
441 public static void suspend () {
442 long i = 5;
444 while (true) {
445 i ++;
449 [MethodImplAttribute (MethodImplOptions.NoInlining)]
450 public static void type_info () {
451 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
452 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
453 int val = 0;
454 unsafe {
455 AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
457 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);
461 [MethodImplAttribute (MethodImplOptions.NoInlining)]
462 public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
463 if (t == null || gc1 == null || gc2 == null)
464 return null;
465 else
466 return this;
469 [MethodImplAttribute (MethodImplOptions.NoInlining)]
470 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) {
471 return s2 [0] + s3 [0] + s4 [0, 0];
474 [MethodImplAttribute (MethodImplOptions.NoInlining)]
475 public static void assembly_load () {
476 assembly_load_2 ();
479 [MethodImplAttribute (MethodImplOptions.NoInlining)]
480 public static void assembly_load_2 () {
481 // This will load System.dll while holding the loader lock
482 new Foo ();
485 [MethodImplAttribute (MethodImplOptions.NoInlining)]
486 public static void invoke () {
487 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
490 [MethodImplAttribute (MethodImplOptions.NoInlining)]
491 public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
492 invoke2 ();
495 [MethodImplAttribute (MethodImplOptions.NoInlining)]
496 public void invoke2 () {
499 int counter;
501 [MethodImplAttribute (MethodImplOptions.NoInlining)]
502 public void invoke_single_threaded () {
503 // Spawn a thread incrementing a counter
504 bool finished = false;
506 new Thread (delegate () {
507 while (!finished)
508 counter ++;
509 }).Start ();
511 Thread.Sleep (100);
513 invoke_single_threaded_2 ();
515 finished = true;
518 [MethodImplAttribute (MethodImplOptions.NoInlining)]
519 public void invoke_single_threaded_2 () {
522 public void invoke_return_void () {
525 public string invoke_return_ref () {
526 return "ABC";
529 public object invoke_return_null () {
530 return null;
533 public int invoke_return_primitive () {
534 return 42;
537 public void invoke_type_load () {
538 new Class3 ();
541 class Class3 {
544 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) {
545 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
548 public int invoke_pass_primitive2 (bool b) {
549 return b ? 1 : 0;
552 public string invoke_pass_ref (string s) {
553 return s;
556 public static string invoke_static_pass_ref (string s) {
557 return s;
560 public static void invoke_static_return_void () {
563 public static void invoke_throws () {
564 throw new Exception ();
567 [MethodImplAttribute (MethodImplOptions.NoInlining)]
568 public static void exceptions () {
569 try {
570 throw new OverflowException ();
571 } catch (Exception) {
573 try {
574 throw new OverflowException ();
575 } catch (Exception) {
577 try {
578 throw new ArgumentException ();
579 } catch (Exception) {
581 try {
582 throw new OverflowException ();
583 } catch (Exception) {
586 object o = null;
587 try {
588 o.GetType ();
589 } catch (Exception) {
593 [MethodImplAttribute (MethodImplOptions.NoInlining)]
594 public static void threads () {
595 Thread t = new Thread (delegate () {});
597 t.Start ();
598 t.Join ();
601 [MethodImplAttribute (MethodImplOptions.NoInlining)]
602 public static void domains () {
603 AppDomain domain = AppDomain.CreateDomain ("domain");
605 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
606 typeof (CrossDomain).Assembly.FullName, "CrossDomain");
608 o.invoke ();
610 AppDomain.Unload (domain);
612 domains_2 ();
615 [MethodImplAttribute (MethodImplOptions.NoInlining)]
616 public static void domains_2 () {
619 [MethodImplAttribute (MethodImplOptions.NoInlining)]
620 public static void invoke_in_domain () {
623 [MethodImplAttribute (MethodImplOptions.NoInlining)]
624 public static void dynamic_methods () {
625 var m = new DynamicMethod ("dyn_method", typeof (void), new Type [] { typeof (int) }, typeof (object).Module);
626 var ig = m.GetILGenerator ();
628 ig.Emit (OpCodes.Ldstr, "FOO");
629 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
630 ig.Emit (OpCodes.Ret);
632 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
634 del (0);
637 public static void dyn_call (string s) {
640 [MethodImplAttribute (MethodImplOptions.NoInlining)]
641 public static void ref_emit () {
642 AssemblyName assemblyName = new AssemblyName ();
643 assemblyName.Name = "foo";
645 AssemblyBuilder assembly =
646 Thread.GetDomain ().DefineDynamicAssembly (
647 assemblyName, AssemblyBuilderAccess.RunAndSave);
649 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
651 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
652 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
653 ILGenerator ig = mb.GetILGenerator ();
654 ig.Emit (OpCodes.Ldstr, "FOO");
655 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
656 ig.Emit (OpCodes.Ret);
658 Type t = tb.CreateType ();
660 t.GetMethod ("ref_emit_method").Invoke (null, null);
663 [MethodImplAttribute (MethodImplOptions.NoInlining)]
664 public static void ref_emit_call (string s) {
667 [MethodImplAttribute (MethodImplOptions.NoInlining)]
668 public static void frames_in_native () {
669 Thread.Sleep (500);
673 public class CrossDomain : MarshalByRefObject
675 public void invoke () {
676 Tests.invoke_in_domain ();
680 public class Foo
682 public ProcessStartInfo info;
685 // Class used for line number info testing, don't change its layout
686 public class LineNumbers
688 [MethodImplAttribute (MethodImplOptions.NoInlining)]
689 public static void ln1 () {
690 ln2 ();
691 ln3 ();
694 [MethodImplAttribute (MethodImplOptions.NoInlining)]
695 public static void ln2 () {
698 [MethodImplAttribute (MethodImplOptions.NoInlining)]
699 public static void ln3 () {