Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / test-643.cs
1 // Compiler options: -unsafe
2
3 using System;
4
5 class PointerArithmeticTest
6 {
7         unsafe public static int Main()
8         {
9                 try {
10                         return CheckAdd((byte*)(-1), -1);
11                 } catch (System.OverflowException) {}
12                 
13                 try {
14                         if (IntPtr.Size <= 4)
15                                 return CheckSub((short*)(-1), int.MaxValue);
16                         else
17                                 return CheckSub((short*)(-1), long.MaxValue);
18                 } catch (System.OverflowException) {}
19                 
20                 CheckSub2((short*)(-1), int.MaxValue);
21                         
22                 if ((long)Conversions (long.MaxValue) != (IntPtr.Size <= 4 ? uint.MaxValue : long.MaxValue))
23                         return 5;
24                 
25                 Console.WriteLine ("OK");
26                 return 0;
27         }
28         
29         unsafe static int* Conversions (long b)
30         {
31                 return (int*)b;
32         }
33         
34         unsafe static int CheckAdd(byte* ptr, int offset)
35         {
36                 if (checked(ptr + offset < ptr))
37                         return 1;
38                 
39                 return 101;
40         }
41         
42         unsafe static int CheckSub(short* ptr, int offset)
43         {
44                 if (checked(ptr - offset < ptr))
45                         return 2;
46
47                 return 102;
48         }
49
50         unsafe static int CheckSub(short* ptr, long offset)
51         {
52                 if (checked(ptr - offset < ptr))
53                         return 2;
54
55                 return 102;
56         }
57
58         unsafe static int CheckSub2(short* ptr, int offset)
59         {
60                 short* b = ptr + offset;
61                 if (checked(ptr - b < 0))
62                         return 3;
63
64                 return 0;
65         }
66 }