2 using System.Runtime.CompilerServices;
6 * Regression tests for the mono JIT.
8 * Each test needs to be of the form:
10 * static int test_<result>_<name> ();
12 * where <result> is an integer (the value that needs to be returned by
13 * the method to make it pass.
14 * <name> is a user-displayed name used to identify the test.
16 * The tests can be driven in two ways:
17 * *) running the program directly: Main() uses reflection to find and invoke
18 * the test methods (this is useful mostly to check that the tests are correct)
19 * *) with the --regression switch of the jit (this is the preferred way since
20 * all the tests will be run with optimizations on and off)
22 * The reflection logic could be moved to a .dll since we need at least another
23 * regression test file written in IL code to have better control on how
28 namespace UnalignedTests
36 public static int Main (string[] args) {
37 return TestDriver.RunTests (typeof (Tests), args);
42 public static unsafe int test_0_ldobj_r4 ()
44 byte *ptr = stackalloc byte [32];
45 float f = (float)123.44f;
46 *(float*)ptr = (float)f;
48 int expected = *(int*)ptr;
50 Intrinsics.UnalignedStobj<int> (ptr + 1, expected);
51 /* we can loose some precision due to r4<->r8 conversions */
52 if (Math.Abs (Intrinsics.UnalignedLdobj<float> (ptr + 1) - f) > 0.01f)
58 public static unsafe int test_0_ldobj_r8 ()
60 byte *ptr = stackalloc byte [32];
62 *(double*)ptr = (double)f;
64 long expected = *(long*)ptr;
66 Intrinsics.UnalignedStobj<long> (ptr + 3, expected);
67 if (Intrinsics.UnalignedLdobj<double> (ptr + 3) != f)
74 public static unsafe int test_0_ldobj ()
76 byte *ptr = stackalloc byte [20];
77 for (int i = 0; i < 20; ++i)
80 if (BitConverter.IsLittleEndian) {
82 if (Intrinsics.UnalignedLdobj<short> (ptr + 0) != 0x0100)
85 if (Intrinsics.UnalignedLdobj<short> (ptr + 1) != 0x0201)
88 if (Intrinsics.UnalignedLdobj<short> (ptr + 2) != 0x0302)
91 if (Intrinsics.UnalignedLdobj<int> (ptr + 1) != 0x04030201)
94 if (Intrinsics.UnalignedLdobj<int> (ptr + 2) != 0x05040302)
97 if (Intrinsics.UnalignedLdobj<long> (ptr + 1) != 0x0807060504030201)
100 if (Intrinsics.UnalignedLdobj<long> (ptr + 6) != 0xD0C0B0A09080706)
104 if (Intrinsics.UnalignedLdobj<short> (ptr + 0) != 0x0001)
107 if (Intrinsics.UnalignedLdobj<short> (ptr + 1) != 0x0102)
110 if (Intrinsics.UnalignedLdobj<short> (ptr + 2) != 0x0203)
113 if (Intrinsics.UnalignedLdobj<int> (ptr + 1) != 0x01020304)
116 if (Intrinsics.UnalignedLdobj<int> (ptr + 2) != 0x02030405)
119 if (Intrinsics.UnalignedLdobj<long> (ptr + 1) != 0x0102030405060708)
122 if (Intrinsics.UnalignedLdobj<long> (ptr + 6) != 0x60708090A0B0C0D)
129 public static unsafe int test_0_ldind ()
131 byte *ptr = stackalloc byte [20];
132 for (int i = 0; i < 20; ++i)
136 if (BitConverter.IsLittleEndian) {
138 if (Intrinsics.UnalignedLdInd2 (ptr + 0) != 0x0100)
141 if (Intrinsics.UnalignedLdInd2 (ptr + 1) != 0x0201)
144 if (Intrinsics.UnalignedLdInd2 (ptr + 2) != 0x0302)
147 if (Intrinsics.UnalignedLdInd4 (ptr + 1) != 0x04030201)
150 if (Intrinsics.UnalignedLdInd4 (ptr + 2) != 0x05040302)
153 if (Intrinsics.UnalignedLdInd8 (ptr + 1) != 0x0807060504030201)
156 if (Intrinsics.UnalignedLdInd8 (ptr + 6) != 0xD0C0B0A09080706)
160 if (Intrinsics.UnalignedLdInd2 (ptr + 0) != 0x0001)
163 if (Intrinsics.UnalignedLdInd2 (ptr + 1) != 0x0102)
166 if (Intrinsics.UnalignedLdInd2 (ptr + 2) != 0x0203)
169 if (Intrinsics.UnalignedLdInd4 (ptr + 1) != 0x01020304)
172 if (Intrinsics.UnalignedLdInd4 (ptr + 2) != 0x02030405)
175 if (Intrinsics.UnalignedLdInd8 (ptr + 1) != 0x0102030405060708)
178 if (Intrinsics.UnalignedLdInd8 (ptr + 6) != 0x60708090A0B0C0D)
184 public static unsafe int test_0_cpobj ()
186 byte *dest = stackalloc byte [20];
187 byte *src = stackalloc byte [20];
188 for (int i = 0; i < 20; ++i)
191 Intrinsics.UnalignedCpobj<short> (dest + 0, src + 0);
192 if (dest [0] != src [0] || dest [1] != src [1])
195 Intrinsics.UnalignedCpobj<short> (dest + 1, src + 0);
196 if (dest [1] != src [0] || dest [2] != src [1])
199 Intrinsics.UnalignedCpobj<short> (dest + 0, src + 1);
200 if (dest [0] != src [1] || dest [1] != src [2])
203 Intrinsics.UnalignedCpobj<short> (dest + 1, src + 1);
204 if (dest [1] != src [1] || dest [2] != src [2])
207 Intrinsics.UnalignedCpobj<int> (dest + 3, src);
208 for (int i = 0; i < 4; ++i) {
209 if (dest [i + 3] != src [i])
213 Intrinsics.UnalignedCpobj<int> (dest + 1, src + 2);
214 for (int i = 0; i < 4; ++i) {
215 if (dest [i + 1] != src [i + 2])
219 Intrinsics.UnalignedCpobj<long> (dest + 1, src + 2);
220 for (int i = 0; i < 8; ++i) {
221 if (dest [i + 1] != src [i + 2])
225 Intrinsics.UnalignedCpobj<long> (dest + 7, src + 2);
226 for (int i = 0; i < 8; ++i) {
227 if (dest [i + 7] != src [i + 2])
234 public static unsafe int test_0_stobj ()
236 byte *ptr = stackalloc byte [20];
238 if (BitConverter.IsLittleEndian) {
239 Intrinsics.UnalignedStobj <short> (ptr + 0, 0x6688);
240 if (ptr [0] != 0x88 || ptr [1] != 0x66)
243 Intrinsics.UnalignedStobj <short> (ptr + 1, 0x6589);
244 if (ptr [1] != 0x89 || ptr [2] != 0x65)
247 Intrinsics.UnalignedStobj <int> (ptr + 1, 0x60708090);
248 if (ptr [1] != 0x90 || ptr [2] != 0x80 || ptr [3] != 0x70 || ptr [4] != 0x60)
251 Intrinsics.UnalignedStobj <long> (ptr + 1, 0x405060708090);
252 if (ptr [1] != 0x90 || ptr [2] != 0x80 || ptr [3] != 0x70 ||
253 ptr [4] != 0x60 || ptr [5] != 0x50 || ptr [6] != 0x40)
256 Intrinsics.UnalignedStobj <short> (ptr + 0, 0x6688);
257 if (ptr [0] != 0x66 || ptr [1] != 0x88)
260 Intrinsics.UnalignedStobj <short> (ptr + 1, 0x6589);
261 if (ptr [1] != 0x65 || ptr [2] != 0x89)
264 Intrinsics.UnalignedStobj <int> (ptr + 1, 0x60708090);
265 if (ptr [1] != 0x60 || ptr [2] != 0x70 || ptr [3] != 0x80 || ptr [4] != 0x90)
268 Intrinsics.UnalignedStobj <long> (ptr + 1, 0x2030405060708090);
269 if (ptr [1] != 0x20 || ptr [2] != 0x30 || ptr [3] != 0x40 ||
270 ptr [4] != 0x50 || ptr [5] != 0x60 || ptr [6] != 0x70)
277 public static unsafe int test_0_ldobj_stobj ()
279 byte *dest = stackalloc byte [20];
280 byte *src = stackalloc byte [20];
282 for (int i = 0; i < 20; ++i)
285 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 0, src + 0);
286 if (dest [0] != src [0] || dest [1] != src [1])
289 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 1, src + 0);
290 if (dest [1] != src [0] || dest [2] != src [1])
293 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 0, src + 1);
294 if (dest [0] != src [1] || dest [1] != src [2])
297 Intrinsics.UnalignedLdobjStObjPair<short> (dest + 1, src + 1);
298 if (dest [1] != src [1] || dest [2] != src [2])
301 Intrinsics.UnalignedLdobjStObjPair<int> (dest + 1, src + 1);
302 if (dest [1] != src [1] || dest [2] != src [2])
305 Intrinsics.UnalignedLdobjStObjPair<long> (dest + 1, src + 1);
306 if (dest [1] != src [1] || dest [2] != src [2])
314 public static unsafe int test_0_cpblk ()
316 byte *dest = stackalloc byte [20];
317 byte *src = stackalloc byte [20];
318 for (int i = 0; i < 20; ++i)
322 Intrinsics.UnalignedCpblk (dest + 0, src + 0, 2);
323 if (dest [0] != src [0] || dest [1] != src [1])
326 Intrinsics.UnalignedCpblk (dest + 1, src + 0, 2);
327 if (dest [1] != src [0] || dest [2] != src [1])
330 Intrinsics.UnalignedCpblk (dest + 0, src + 1, 2);
331 if (dest [0] != src [1] || dest [1] != src [2])
334 Intrinsics.UnalignedCpblk (dest + 1, src + 1, 2);
335 if (dest [1] != src [1] || dest [2] != src [2])
338 Intrinsics.UnalignedCpblk (dest + 1, src + 1, 4);
339 for (int i = 0; i < 4; ++i) {
340 if (dest [i + 1] != src [i + 1])
344 Intrinsics.UnalignedCpblk (dest + 1, src + 1, 8);
345 for (int i = 0; i < 8; ++i) {
346 if (dest [i + 1] != src [i + 1])
354 public static unsafe int test_0_initblk ()
356 byte *ptr = stackalloc byte [20];
358 for (int i = 0; i < 20; ++i)
361 Intrinsics.UnalignedInit (ptr, 30, 2);
362 if (ptr [0] != 30 || ptr [1] != 30)
365 Intrinsics.UnalignedInit (ptr + 1, 31, 2);
366 if (ptr[0] != 30 || ptr [1] != 31 || ptr [2] != 31)