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)
229 // Test alignment of small structs
231 static Small get_small (byte v) {
232 Small r = new Small ();
235 r.b2 = (byte)(v + 1);
240 static Small return_small (Small s) {
244 static int receive_small (int a, Small v, int b) {
252 static int receive_small_sparc_many_args (int a, int a2, int a3, int a4, int a5, int a6, Small v, int b) {
260 static int test_5_pass_small_struct () {
261 Small v = get_small (1);
262 if (receive_small (7, v, 9) != 0)
264 if (receive_small (7, get_small (1), 9) != 0)
266 if (receive_small_sparc_many_args (1, 2, 3, 4, 5, 6, v, 9) != 0)
268 v = return_small (v);
279 public AStruct (int i) {
283 public override int GetHashCode () {
288 // Test that vtypes are unboxed during a virtual call
289 static int test_44_unbox_trampoline () {
290 AStruct s = new AStruct (44);
292 return o.GetHashCode ();
295 static int test_0_unbox_trampoline2 () {
299 if (i.ToString () != "12")
301 if (((Int32)o).ToString () != "12")
303 if (o.ToString () != "12")
308 // Test fields with big offsets
309 static int test_0_fields_with_big_offsets () {
310 StructWithBigOffsets s = new StructWithBigOffsets ();
311 StructWithBigOffsets s2 = new StructWithBigOffsets ();
337 if (s2.i != 0xdeadbeef)
339 if (s2.l != 0xcafebabe)
358 int buf_length, buf_offset;
366 public long Seek (long position) {
368 /* interaction between the register allocator and
369 * allocating arguments to registers */
370 if (pos >= buf_start && pos <= buf_start + buf_length) {
371 buf_offset = (int) (pos - buf_start);
379 static int test_0_seektest () {
380 TestRegA t = new TestRegA ();
381 return (int)t.Seek (0);
384 class Super : ICloneable {
385 public virtual object Clone () {
392 static int test_0_null_cast () {
400 static int test_0_super_cast () {
401 Duper d = new Duper ();
415 d = (Duper)(object)sup;
422 static int test_0_super_cast_array () {
423 Duper[] d = new Duper [0];
434 if (!(d is Object[]))
437 d = (Duper[])(object[])sup;
444 static int test_0_multi_array_cast () {
445 Duper[,] d = new Duper [1, 1];
449 o [0, 0] = new Super ();
452 catch (ArrayTypeMismatchException) {
458 static int test_0_vector_array_cast () {
459 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
460 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
462 if (arr1.GetType () != typeof (int[]))
465 if (arr2.GetType () == typeof (int[]))
476 catch (InvalidCastException) {
485 static int test_0_enum_array_cast () {
486 TypeCode[] tc = new TypeCode [0];
495 if (a is ValueType[])
509 vta = (ValueType[])a;
526 static int test_0_more_cast_corner_cases () {
527 ValueType[] vta = new ValueType [0];
528 Enum[] ea = new Enum [0];
533 if (!(a is object[]))
535 if (!(a is ValueType[]))
540 if (!(a is object[]))
542 if (!(a is ValueType[]))
604 static int test_0_cast_iface_array () {
605 object o = new ICloneable [0];
606 object o2 = new Duper [0];
610 if (!(o is object[]))
612 if (!(o2 is ICloneable[]))
626 t = (ICloneable[])o2;
642 if (!(o is ICloneable[]))
645 /* add tests for interfaces that 'inherit' interfaces */
649 private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
651 private static int AbsoluteDays (int year, int month, int day)
654 int[] days = daysmonthleap;
657 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
660 static int test_719162_complex_div () {
661 int adays = AbsoluteDays (1970, 1, 1);
665 delegate int GetIntDel ();
667 static int return4 () {
675 static int test_2_static_delegate () {
676 GetIntDel del = new GetIntDel (return4);
683 static int test_2_instance_delegate () {
684 Tests t = new Tests ();
685 GetIntDel del = new GetIntDel (t.return5);
692 static int test_1_store_decimal () {
693 decimal[,] a = {{1}};
700 static int test_2_intptr_stobj () {
701 System.IntPtr [] arr = { new System.IntPtr () };
703 if (arr [0] != (System.IntPtr)0)
708 static int llmult (int a, int b, int c, int d) {
709 return a + b + c + d;
713 * Test that evaluation of complex arguments does not overwrite the
714 * arguments already in outgoing registers.
716 static int test_155_regalloc () {
722 int[] arr = new int [5];
724 return llmult (arr [c + d], 150, 5, 0);
727 static bool large_struct_test (Large a, Large b, Large c, Large d)
729 if (!a.check ()) return false;
730 if (!b.check ()) return false;
731 if (!c.check ()) return false;
732 if (!d.check ()) return false;
736 static int test_2_large_struct_pass ()
747 if (large_struct_test (a, b, c, d))
752 public static unsafe int test_0_pin_string () {
754 fixed (char *c = x) {
761 public static int my_flags;
762 public static int test_0_and_cmp_static ()
765 /* various forms of test [mem], imm */
767 my_flags = 0x01020304;
769 if ((my_flags & 0x01020304) == 0)
772 if ((my_flags & 0x00000304) == 0)
775 if ((my_flags & 0x00000004) == 0)
778 if ((my_flags & 0x00000300) == 0)
781 if ((my_flags & 0x00020000) == 0)
784 if ((my_flags & 0x01000000) == 0)
791 public static int test_0_byte_compares ()
797 if (!(b < System.Byte.MaxValue))
800 if (!(b <= System.Byte.MaxValue))
806 public static int test_71_long_shift_right () {
807 ulong value = 38654838087;
809 byte [] buffer = new byte [1];
810 buffer [x] = ((byte)(value >> x));
815 public static int test_0_addsub_mem ()
832 public static int test_0_sh32_mem ()
834 y = 0x0102130405060708;
840 y = 0x0102130405060708;
843 if (y != 0x0506070800000000)
846 x = 0x0102130405060708;
849 if (x != 0x0506070800000000)
856 static uint dum_de_dum = 1;
857 static int test_0_long_arg_opt ()
859 return Foo (0x1234567887654321, dum_de_dum);
862 static int Foo (ulong x, ulong y)
864 if (x != 0x1234567887654321)
873 static int test_0_long_ret_opt ()
876 if (x != 0x1234567887654321)
887 return 0x1234567887654321;
895 /* from bug# 71515 */
896 static int counter = 0;
897 static bool WriteStuff () {
901 static int test_0_cond_branch_side_effects () {
910 public static int arg_only_written (string file_name, int[]
912 if (file_name == null)
921 public static int[] foo () {
925 public static void bar (int i) {
929 public static int test_0_arg_only_written ()
931 return arg_only_written ("md.in", null);
934 static long position = 0;
936 public static int test_4_static_inc_long () {
944 return (int)position;
949 public FooStruct (long l) {
953 static int test_0_calls_opcode_emulation () {
954 // Test that emulated opcodes do not clobber arguments already in
958 new FooStruct (val * 10000);
963 static int test_0_intrins_string_length () {
966 return (s.Length == 3) ? 0 : 1;
969 static int test_0_intrins_string_chars () {
972 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
975 static int test_0_intrins_object_gettype () {
978 return (o.GetType () == typeof (int)) ? 0 : 1;
981 static int test_0_intrins_object_gethashcode () {
982 object o = new Object ();
984 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
990 static int test_0_intrins_object_ctor () {
991 object o = new FooClass ();
993 return (o != null) ? 0 : 1;
996 static int test_0_intrins_array_rank () {
997 int[,] a = new int [10, 10];
999 return (a.Rank == 2) ? 0 : 1;
1002 static int test_0_intrins_array_length () {
1003 int[,] a = new int [10, 10];
1006 return (a2.Length == 100) ? 0 : 1;
1009 static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1010 int i = RuntimeHelpers.OffsetToStringData;
1015 static int test_0_intrins_string_setchar () {
1016 StringBuilder sb = new StringBuilder ("ABC");
1020 return sb.ToString () == "ADC" ? 0 : 1;
1024 bool allowLocation = true;
1028 static int test_0_regress_78990_unaligned_structs () {
1034 static unsafe int test_97_negative_index () {
1035 char[] arr = new char[] {'a', 'b'};
1036 fixed (char *p = arr) {
1044 static int test_0_unsigned_right_shift_imm0 () {
1046 byte[] data = new byte[256];
1047 for (int i = 0; i < 1; i ++)
1048 temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1053 static int test_0_array_access_64_bit () {
1054 int[] arr2 = new int [10];
1055 for (int i = 0; i < 10; ++i)
1057 string s = "ABCDEFGH";
1059 byte[] arr = new byte [4];
1065 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1066 int len2 = - (len + 2);
1068 // Test array and string access with a 32 bit value whose upper 32 bits are
1071 if (arr2 [len2] != 2)
1073 if (s [len2] != 'C')