2 using System.Reflection;
3 using System.Runtime.InteropServices;
4 using System.Runtime.CompilerServices;
7 * Regression tests for the mono JIT.
9 * Each test needs to be of the form:
11 * static int test_<result>_<name> ();
13 * where <result> is an integer (the value that needs to be returned by
14 * the method to make it pass.
15 * <name> is a user-displayed name used to identify the test.
17 * The tests can be driven in two ways:
18 * *) running the program directly: Main() uses reflection to find and invoke
19 * the test methods (this is useful mostly to check that the tests are correct)
20 * *) with the --regression switch of the jit (this is the preferred way since
21 * all the tests will be run with optimizations on and off)
23 * The reflection logic could be moved to a .dll since we need at least another
24 * regression test file written in IL code to have better control on how
58 public void populate ()
68 return one == 1 && two == 2 &&
69 three == 3 && four == 4 &&
70 five == 5 && six == 6 &&
71 seven == 7 && eight == 8 &&
72 nine == 9 && ten == 10;
78 public Sample (int v) {
83 [StructLayout ( LayoutKind.Explicit )]
84 struct StructWithBigOffsets {
85 [ FieldOffset(10000) ] public byte b;
86 [ FieldOffset(10001) ] public sbyte sb;
87 [ FieldOffset(11000) ] public short s;
88 [ FieldOffset(11002) ] public ushort us;
89 [ FieldOffset(12000) ] public uint i;
90 [ FieldOffset(12004) ] public int si;
91 [ FieldOffset(13000) ] public long l;
92 [ FieldOffset(14000) ] public float f;
93 [ FieldOffset(15000) ] public double d;
105 return TestDriver.RunTests (typeof (Tests));
108 static int test_0_return () {
112 s.c = (short)(s.a + s.b);
117 static int test_0_string_access () {
124 static int test_0_string_virtual_call () {
126 string s2 = s.ToString ();
132 static int test_0_iface_call () {
134 object o = ((ICloneable)s).Clone ();
138 static int test_5_newobj () {
139 Sample s = new Sample (5);
143 static int test_4_box () {
148 static int test_0_enum_unbox () {
149 SampleEnum x = SampleEnum.A;
159 static Simple get_simple (int v) {
160 Simple r = new Simple ();
163 r.c = (short)(v + 2);
169 static int test_3_return_struct () {
170 Simple v = get_simple (1);
183 public virtual Simple v_get_simple (int v)
185 return get_simple (v);
188 static int test_2_return_struct_virtual () {
189 Tests t = new Tests ();
190 Simple v = t.v_get_simple (2);
203 static int receive_simple (int a, Simple v, int b) {
219 static int test_5_pass_struct () {
220 Simple v = get_simple (1);
221 if (receive_simple (7, v, 9) != 0)
223 if (receive_simple (7, get_simple (1), 9) != 0)
228 // Test alignment of small structs
230 static Small get_small (byte v) {
231 Small r = new Small ();
234 r.b2 = (byte)(v + 1);
239 static Small return_small (Small s) {
243 static int receive_small (int a, Small v, int b) {
251 static int test_5_pass_small_struct () {
252 Small v = get_small (1);
253 if (receive_small (7, v, 9) != 0)
255 if (receive_small (7, get_small (1), 9) != 0)
257 v = return_small (v);
268 public AStruct (int i) {
272 public override int GetHashCode () {
277 // Test that vtypes are unboxed during a virtual call
278 static int test_44_unbox_trampoline () {
279 AStruct s = new AStruct (44);
281 return o.GetHashCode ();
284 static int test_0_unbox_trampoline2 () {
288 if (i.ToString () != "12")
290 if (((Int32)o).ToString () != "12")
292 if (o.ToString () != "12")
297 // Test fields with big offsets
298 static int test_0_fields_with_big_offsets () {
299 StructWithBigOffsets s = new StructWithBigOffsets ();
300 StructWithBigOffsets s2 = new StructWithBigOffsets ();
326 if (s2.i != 0xdeadbeef)
328 if (s2.l != 0xcafebabe)
347 int buf_length, buf_offset;
355 public long Seek (long position) {
357 /* interaction between the register allocator and
358 * allocating arguments to registers */
359 if (pos >= buf_start && pos <= buf_start + buf_length) {
360 buf_offset = (int) (pos - buf_start);
368 static int test_0_seektest () {
369 TestRegA t = new TestRegA ();
370 return (int)t.Seek (0);
373 class Super : ICloneable {
374 public virtual object Clone () {
381 static int test_0_null_cast () {
389 static int test_0_super_cast () {
390 Duper d = new Duper ();
404 d = (Duper)(object)sup;
411 static int test_0_super_cast_array () {
412 Duper[] d = new Duper [0];
423 if (!(d is Object[]))
426 d = (Duper[])(object[])sup;
433 static int test_0_multi_array_cast () {
434 Duper[,] d = new Duper [1, 1];
438 o [0, 0] = new Super ();
441 catch (ArrayTypeMismatchException) {
447 static int test_0_vector_array_cast () {
448 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
449 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
451 if (arr1.GetType () != typeof (int[]))
454 if (arr2.GetType () == typeof (int[]))
465 catch (InvalidCastException) {
474 static int test_0_enum_array_cast () {
475 TypeCode[] tc = new TypeCode [0];
484 if (a is ValueType[])
498 vta = (ValueType[])a;
515 static int test_0_more_cast_corner_cases () {
516 ValueType[] vta = new ValueType [0];
517 Enum[] ea = new Enum [0];
522 if (!(a is object[]))
524 if (!(a is ValueType[]))
529 if (!(a is object[]))
531 if (!(a is ValueType[]))
593 static int test_0_cast_iface_array () {
594 object o = new ICloneable [0];
595 object o2 = new Duper [0];
599 if (!(o is object[]))
601 if (!(o2 is ICloneable[]))
615 t = (ICloneable[])o2;
631 if (!(o is ICloneable[]))
634 /* add tests for interfaces that 'inherit' interfaces */
638 private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
640 private static int AbsoluteDays (int year, int month, int day)
643 int[] days = daysmonthleap;
646 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
649 static int test_719162_complex_div () {
650 int adays = AbsoluteDays (1970, 1, 1);
654 delegate int GetIntDel ();
656 static int return4 () {
664 static int test_2_static_delegate () {
665 GetIntDel del = new GetIntDel (return4);
672 static int test_2_instance_delegate () {
673 Tests t = new Tests ();
674 GetIntDel del = new GetIntDel (t.return5);
681 static int test_1_store_decimal () {
682 decimal[,] a = {{1}};
689 static int test_2_intptr_stobj () {
690 System.IntPtr [] arr = { new System.IntPtr () };
692 if (arr [0] != (System.IntPtr)0)
697 static int llmult (int a, int b, int c, int d) {
698 return a + b + c + d;
702 * Test that evaluation of complex arguments does not overwrite the
703 * arguments already in outgoing registers.
705 static int test_155_regalloc () {
711 int[] arr = new int [5];
713 return llmult (arr [c + d], 150, 5, 0);
716 static bool large_struct_test (Large a, Large b, Large c, Large d)
718 if (!a.check ()) return false;
719 if (!b.check ()) return false;
720 if (!c.check ()) return false;
721 if (!d.check ()) return false;
725 static int test_2_large_struct_pass ()
736 if (large_struct_test (a, b, c, d))
741 public static unsafe int test_0_pin_string () {
743 fixed (char *c = x) {
750 public static int my_flags;
751 public static int test_0_and_cmp_static ()
754 /* various forms of test [mem], imm */
756 my_flags = 0x01020304;
758 if ((my_flags & 0x01020304) == 0)
761 if ((my_flags & 0x00000304) == 0)
764 if ((my_flags & 0x00000004) == 0)
767 if ((my_flags & 0x00000300) == 0)
770 if ((my_flags & 0x00020000) == 0)
773 if ((my_flags & 0x01000000) == 0)
780 public static int test_0_byte_compares ()
786 if (!(b < System.Byte.MaxValue))
789 if (!(b <= System.Byte.MaxValue))
795 public static int test_71_long_shift_right () {
796 ulong value = 38654838087;
798 byte [] buffer = new byte [1];
799 buffer [x] = ((byte)(value >> x));
804 public static int test_0_addsub_mem ()
821 public static int test_0_sh32_mem ()
823 y = 0x0102130405060708;
829 y = 0x0102130405060708;
832 if (y != 0x0506070800000000)
835 x = 0x0102130405060708;
838 if (x != 0x0506070800000000)
845 static uint dum_de_dum = 1;
846 static int test_0_long_arg_opt ()
848 return Foo (0x1234567887654321, dum_de_dum);
851 static int Foo (ulong x, ulong y)
853 if (x != 0x1234567887654321)
862 static int test_0_long_ret_opt ()
865 if (x != 0x1234567887654321)
876 return 0x1234567887654321;
884 /* from bug# 71515 */
885 static int counter = 0;
886 static bool WriteStuff () {
890 static int test_0_cond_branch_side_effects () {
899 public static int arg_only_written (string file_name, int[]
901 if (file_name == null)
910 public static int[] foo () {
914 public static void bar (int i) {
918 public static int test_0_arg_only_written ()
920 return arg_only_written ("md.in", null);
923 static long position = 0;
925 public static int test_4_static_inc_long () {
933 return (int)position;
938 public FooStruct (long l) {
942 static int test_0_calls_opcode_emulation () {
943 // Test that emulated opcodes do not clobber arguments already in
947 new FooStruct (val * 10000);
952 static int test_0_intrins_string_length () {
955 return (s.Length == 3) ? 0 : 1;
958 static int test_0_intrins_string_chars () {
961 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
964 static int test_0_intrins_object_gettype () {
967 return (o.GetType () == typeof (int)) ? 0 : 1;
970 static int test_0_intrins_object_gethashcode () {
971 object o = new Object ();
973 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
979 static int test_0_intrins_object_ctor () {
980 object o = new FooClass ();
982 return (o != null) ? 0 : 1;
985 static int test_0_intrins_array_rank () {
986 int[,] a = new int [10, 10];
988 return (a.Rank == 2) ? 0 : 1;
991 static int test_0_intrins_array_length () {
992 int[,] a = new int [10, 10];
995 return (a2.Length == 100) ? 0 : 1;
998 static int test_0_intrins_runtimehelpers_offset_to_string_data () {
999 int i = RuntimeHelpers.OffsetToStringData;
1005 bool allowLocation = true;
1009 static int test_0_regress_78990_unaligned_structs () {