Merge pull request #260 from pcc/topmost
[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                         return CheckSub((short*)(-1), int.MaxValue);
15                 } catch (System.OverflowException) {}
16                 
17                 CheckSub2((short*)(-1), int.MaxValue);
18                         
19                 if ((long)Conversions (long.MaxValue) != (IntPtr.Size <= 4 ? uint.MaxValue : long.MaxValue))
20                         return 5;
21                 
22                 Console.WriteLine ("OK");
23                 return 0;
24         }
25         
26         unsafe static int* Conversions (long b)
27         {
28                 return (int*)b;
29         }
30         
31         unsafe static int CheckAdd(byte* ptr, int offset)
32         {
33                 if (checked(ptr + offset < ptr))
34                         return 1;
35                 
36                 return 101;
37         }
38         
39         unsafe static int CheckSub(short* ptr, int offset)
40         {
41                 if (checked(ptr - offset < ptr))
42                         return 2;
43
44                 return 102;
45         }
46
47         unsafe static int CheckSub2(short* ptr, int offset)
48         {
49                 short* b = ptr + offset;
50                 if (checked(ptr - b < 0))
51                         return 3;
52
53                 return 0;
54         }
55 }