3 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Runtime.CompilerServices;
8 * Regression tests for the mono JIT.
10 * Each test needs to be of the form:
12 * static int test_<result>_<name> ();
14 * where <result> is an integer (the value that needs to be returned by
15 * the method to make it pass.
16 * <name> is a user-displayed name used to identify the test.
18 * The tests can be driven in two ways:
19 * *) running the program directly: Main() uses reflection to find and invoke
20 * the test methods (this is useful mostly to check that the tests are correct)
21 * *) with the --regression switch of the jit (this is the preferred way since
22 * all the tests will be run with optimizations on and off)
24 * The reflection logic could be moved to a .dll since we need at least another
25 * regression test file written in IL code to have better control on how
59 public void populate ()
69 return one == 1 && two == 2 &&
70 three == 3 && four == 4 &&
71 five == 5 && six == 6 &&
72 seven == 7 && eight == 8 &&
73 nine == 9 && ten == 10;
79 public Sample (int v) {
84 [StructLayout ( LayoutKind.Explicit )]
85 struct StructWithBigOffsets {
86 [ FieldOffset(10000) ] public byte b;
87 [ FieldOffset(10001) ] public sbyte sb;
88 [ FieldOffset(11000) ] public short s;
89 [ FieldOffset(11002) ] public ushort us;
90 [ FieldOffset(12000) ] public uint i;
91 [ FieldOffset(12004) ] public int si;
92 [ FieldOffset(13000) ] public long l;
93 [ FieldOffset(14000) ] public float f;
94 [ FieldOffset(15000) ] public double d;
106 return TestDriver.RunTests (typeof (Tests));
109 static int test_0_return () {
113 s.c = (short)(s.a + s.b);
118 static int test_0_string_access () {
125 static int test_0_string_virtual_call () {
127 string s2 = s.ToString ();
133 static int test_0_iface_call () {
135 object o = ((ICloneable)s).Clone ();
139 static int test_5_newobj () {
140 Sample s = new Sample (5);
144 static int test_4_box () {
149 static int test_0_enum_unbox () {
150 SampleEnum x = SampleEnum.A;
160 static Simple get_simple (int v) {
161 Simple r = new Simple ();
164 r.c = (short)(v + 2);
170 static int test_3_return_struct () {
171 Simple v = get_simple (1);
184 public virtual Simple v_get_simple (int v)
186 return get_simple (v);
189 static int test_2_return_struct_virtual () {
190 Tests t = new Tests ();
191 Simple v = t.v_get_simple (2);
204 static int receive_simple (int a, Simple v, int b) {
220 static int test_5_pass_struct () {
221 Simple v = get_simple (1);
222 if (receive_simple (7, v, 9) != 0)
224 if (receive_simple (7, get_simple (1), 9) != 0)
230 static int test_5_pass_static_struct () {
231 s_v = get_simple (1);
232 if (receive_simple (7, s_v, 9) != 0)
237 // Test alignment of small structs
239 static Small get_small (byte v) {
240 Small r = new Small ();
243 r.b2 = (byte)(v + 1);
248 static Small return_small (Small s) {
252 static int receive_small (int a, Small v, int b) {
260 static int receive_small_sparc_many_args (int a, int a2, int a3, int a4, int a5, int a6, Small v, int b) {
268 static int test_5_pass_small_struct () {
269 Small v = get_small (1);
270 if (receive_small (7, v, 9) != 0)
272 if (receive_small (7, get_small (1), 9) != 0)
274 if (receive_small_sparc_many_args (1, 2, 3, 4, 5, 6, v, 9) != 0)
276 v = return_small (v);
284 // 64-bits, 32-bit aligned
290 static int check_struct1(struct1 x) {
298 static int pass_struct1(int a, int b, struct1 x) {
303 return check_struct1(x);
306 static int pass_struct1(int a, struct1 x) {
309 return check_struct1(x);
312 static int pass_struct1(struct1 x) {
313 return check_struct1(x);
316 static int test_0_struct1_args () {
322 if ((r = check_struct1(x)) != 0)
324 if ((r = pass_struct1(x)) != 0)
326 if ((r = pass_struct1(3, x)) != 0)
328 if ((r = pass_struct1(3, 4, x)) != 0)
333 // 64-bits, 64-bit aligned
338 static int check_struct2(struct2 x) {
344 static int pass_struct2(int a, int b, int c, struct2 x) {
351 return check_struct2(x);
354 static int pass_struct2(int a, int b, struct2 x) {
359 return check_struct2(x);
362 static int pass_struct2(int a, struct2 x) {
365 return check_struct2(x);
368 static int pass_struct2(struct2 x) {
369 return check_struct2(x);
372 static int test_0_struct2_args () {
377 if ((r = check_struct2(x)) != 0)
379 if ((r = pass_struct2(x)) != 0)
381 if ((r = pass_struct2(3, x)) != 0)
383 if ((r = pass_struct2(3, 4, x)) != 0)
385 if ((r = pass_struct2(3, 4, 5, x)) != 0)
392 public long i, j, k, l;
395 static int pass_struct3 (int i, int j, int k, int l, int m, int n, int o, int p, Struct3 s, int q) {
396 if (s.i + s.j + s.k + s.l != 10)
402 static int test_0_struct3_args () {
403 Struct3 s = new Struct3 ();
409 return pass_struct3 (1, 2, 3, 4, 5, 6, 7, 8, s, 9);
415 public AStruct (int i) {
419 public override int GetHashCode () {
424 // Test that vtypes are unboxed during a virtual call
425 static int test_44_unbox_trampoline () {
426 AStruct s = new AStruct (44);
428 return o.GetHashCode ();
431 static int test_0_unbox_trampoline2 () {
435 if (i.ToString () != "12")
437 if (((Int32)o).ToString () != "12")
439 if (o.ToString () != "12")
444 // Test fields with big offsets
445 static int test_0_fields_with_big_offsets () {
446 StructWithBigOffsets s = new StructWithBigOffsets ();
447 StructWithBigOffsets s2 = new StructWithBigOffsets ();
473 if (s2.i != 0xdeadbeef)
475 if (s2.l != 0xcafebabe)
494 int buf_length, buf_offset;
502 public long Seek (long position) {
504 /* interaction between the register allocator and
505 * allocating arguments to registers */
506 if (pos >= buf_start && pos <= buf_start + buf_length) {
507 buf_offset = (int) (pos - buf_start);
515 static int test_0_seektest () {
516 TestRegA t = new TestRegA ();
517 return (int)t.Seek (0);
520 class Super : ICloneable {
521 public virtual object Clone () {
528 static int test_0_null_cast () {
536 static int test_0_super_cast () {
537 Duper d = new Duper ();
551 d = (Duper)(object)sup;
558 static int test_0_super_cast_array () {
559 Duper[] d = new Duper [0];
570 if (!(d is Object[]))
573 d = (Duper[])(object[])sup;
580 static int test_0_multi_array_cast () {
581 Duper[,] d = new Duper [1, 1];
585 o [0, 0] = new Super ();
588 catch (ArrayTypeMismatchException) {
594 static int test_0_vector_array_cast () {
595 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
596 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
598 if (arr1.GetType () != typeof (int[]))
601 if (arr2.GetType () == typeof (int[]))
612 catch (InvalidCastException) {
618 int [,] [] arr3 = new int [1, 1] [];
620 int [,] [] arr4 = (int [,] [])o;
625 static int test_0_enum_array_cast () {
626 TypeCode[] tc = new TypeCode [0];
635 if (a is ValueType[])
649 vta = (ValueType[])a;
666 static int test_0_more_cast_corner_cases () {
667 ValueType[] vta = new ValueType [0];
668 Enum[] ea = new Enum [0];
673 if (!(a is object[]))
675 if (!(a is ValueType[]))
680 if (!(a is object[]))
682 if (!(a is ValueType[]))
744 static int test_0_cast_iface_array () {
745 object o = new ICloneable [0];
746 object o2 = new Duper [0];
750 if (!(o is object[]))
752 if (!(o2 is ICloneable[]))
766 t = (ICloneable[])o2;
782 if (!(o is ICloneable[]))
785 /* add tests for interfaces that 'inherit' interfaces */
789 private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
791 private static int AbsoluteDays (int year, int month, int day)
794 int[] days = daysmonthleap;
797 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
800 static int test_719162_complex_div () {
801 int adays = AbsoluteDays (1970, 1, 1);
805 delegate int GetIntDel ();
807 static int return4 () {
815 static int test_2_static_delegate () {
816 GetIntDel del = new GetIntDel (return4);
823 static int test_2_instance_delegate () {
824 Tests t = new Tests ();
825 GetIntDel del = new GetIntDel (t.return5);
832 static int test_1_store_decimal () {
833 decimal[,] a = {{1}};
840 static int test_2_intptr_stobj () {
841 System.IntPtr [] arr = { new System.IntPtr () };
843 if (arr [0] != (System.IntPtr)0)
848 static int llmult (int a, int b, int c, int d) {
849 return a + b + c + d;
853 * Test that evaluation of complex arguments does not overwrite the
854 * arguments already in outgoing registers.
856 static int test_155_regalloc () {
862 int[] arr = new int [5];
864 return llmult (arr [c + d], 150, 5, 0);
867 static bool large_struct_test (Large a, Large b, Large c, Large d)
869 if (!a.check ()) return false;
870 if (!b.check ()) return false;
871 if (!c.check ()) return false;
872 if (!d.check ()) return false;
876 static int test_2_large_struct_pass ()
887 if (large_struct_test (a, b, c, d))
892 public static unsafe int test_0_pin_string () {
894 fixed (char *c = x) {
901 public static int my_flags;
902 public static int test_0_and_cmp_static ()
905 /* various forms of test [mem], imm */
907 my_flags = 0x01020304;
909 if ((my_flags & 0x01020304) == 0)
912 if ((my_flags & 0x00000304) == 0)
915 if ((my_flags & 0x00000004) == 0)
918 if ((my_flags & 0x00000300) == 0)
921 if ((my_flags & 0x00020000) == 0)
924 if ((my_flags & 0x01000000) == 0)
931 public static int test_0_byte_compares ()
937 if (!(b < System.Byte.MaxValue))
940 if (!(b <= System.Byte.MaxValue))
946 public static int test_71_long_shift_right () {
947 ulong value = 38654838087;
949 byte [] buffer = new byte [1];
950 buffer [x] = ((byte)(value >> x));
955 public static int test_0_addsub_mem ()
972 public static int test_0_sh32_mem ()
974 y = 0x0102130405060708;
980 y = 0x0102130405060708;
983 if (y != 0x0506070800000000)
986 x = 0x0102130405060708;
989 if (x != 0x0506070800000000)
996 static uint dum_de_dum = 1;
997 static int test_0_long_arg_opt ()
999 return Foo (0x1234567887654321, dum_de_dum);
1002 static int Foo (ulong x, ulong y)
1004 if (x != 0x1234567887654321)
1013 static int test_0_long_ret_opt ()
1016 if (x != 0x1234567887654321)
1027 return 0x1234567887654321;
1035 /* from bug# 71515 */
1036 static int counter = 0;
1037 static bool WriteStuff () {
1041 static int test_0_cond_branch_side_effects () {
1050 public static int arg_only_written (string file_name, int[]
1052 if (file_name == null)
1061 public static int[] foo () {
1065 public static void bar (int i) {
1069 public static int test_0_arg_only_written ()
1071 return arg_only_written ("md.in", null);
1074 static long position = 0;
1076 public static int test_4_static_inc_long () {
1084 return (int)position;
1089 public FooStruct (long l) {
1093 static int test_0_calls_opcode_emulation () {
1094 // Test that emulated opcodes do not clobber arguments already in
1098 new FooStruct (val * 10000);
1103 static int test_0_intrins_string_length () {
1106 return (s.Length == 3) ? 0 : 1;
1109 static int test_0_intrins_string_chars () {
1112 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1115 static int test_0_intrins_object_gettype () {
1118 return (o.GetType () == typeof (int)) ? 0 : 1;
1121 static int test_0_intrins_object_gethashcode () {
1122 object o = new Object ();
1124 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1130 static int test_0_intrins_object_ctor () {
1131 object o = new FooClass ();
1133 return (o != null) ? 0 : 1;
1136 static int test_0_intrins_array_rank () {
1137 int[,] a = new int [10, 10];
1139 return (a.Rank == 2) ? 0 : 1;
1142 static int test_0_intrins_array_length () {
1143 int[,] a = new int [10, 10];
1146 return (a2.Length == 100) ? 0 : 1;
1149 static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1150 int i = RuntimeHelpers.OffsetToStringData;
1155 static int test_0_intrins_string_setchar () {
1156 StringBuilder sb = new StringBuilder ("ABC");
1160 return sb.ToString () == "ADC" ? 0 : 1;
1164 bool allowLocation = true;
1168 static int test_0_regress_78990_unaligned_structs () {
1174 static unsafe int test_97_negative_index () {
1175 char[] arr = new char[] {'a', 'b'};
1176 fixed (char *p = arr) {
1184 static int test_0_unsigned_right_shift_imm0 () {
1186 byte[] data = new byte[256];
1187 for (int i = 0; i < 1; i ++)
1188 temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1193 public virtual int foo () {
1198 sealed class Bar2 : Foo2 {
1199 public override int foo () {
1204 static int test_0_abcrem_check_this_removal () {
1205 Bar2 b = new Bar2 ();
1207 // The check_this generated here by the JIT should be removed
1213 static int invoke_twice (Bar2 b) {
1215 // The check_this generated here by the JIT should be removed
1221 static int test_0_abcrem_check_this_removal2 () {
1222 Bar2 b = new Bar2 ();
1230 static int test_0_array_access_64_bit () {
1231 int[] arr2 = new int [10];
1232 for (int i = 0; i < 10; ++i)
1234 string s = "ABCDEFGH";
1236 byte[] arr = new byte [4];
1242 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1243 int len2 = - (len + 2);
1245 // Test array and string access with a 32 bit value whose upper 32 bits are
1248 if (arr2 [len2] != 2)
1250 if (s [len2] != 'C')
1256 public static float pi = 3.14f;
1258 public float float_field;
1261 static int test_0_ldsfld_soft_float () {
1262 if (R4Holder.pi == 3.14f)
1268 static int test_0_ldfld_stfld_soft_float () {
1269 R4Holder h = new R4Holder ();
1270 h.float_field = 3.14f;
1272 if (h.float_field == 3.14f)
1278 class R4HolderRemote : MarshalByRefObject {
1279 public static float pi = 3.14f;
1281 public float float_field;
1284 static int test_0_ldfld_stfld_soft_float_remote () {
1285 R4HolderRemote h = new R4HolderRemote ();
1286 h.float_field = 3.14f;
1288 if (h.float_field == 3.14f)
1294 static int test_0_locals_soft_float () {