f3e3aab35a
Former-commit-id: 9c2cb47f45fa221e661ab616387c9cda183f283d
67 lines
1.1 KiB
C#
67 lines
1.1 KiB
C#
// Compiler options: -unsafe
|
|
|
|
using System;
|
|
|
|
class PointerArithmeticTest
|
|
{
|
|
unsafe public static int Main()
|
|
{
|
|
try {
|
|
return CheckAdd((byte*)(-1), -1);
|
|
} catch (System.OverflowException) {}
|
|
|
|
try {
|
|
if (IntPtr.Size <= 4)
|
|
return CheckSub((short*)(-1), int.MaxValue);
|
|
else
|
|
return CheckSub((short*)(-1), long.MaxValue);
|
|
} catch (System.OverflowException) {}
|
|
|
|
CheckSub2((short*)(-1), int.MaxValue);
|
|
|
|
if ((long)Conversions (long.MaxValue) != (IntPtr.Size <= 4 ? uint.MaxValue : long.MaxValue))
|
|
return 5;
|
|
|
|
Console.WriteLine ("OK");
|
|
return 0;
|
|
}
|
|
|
|
unsafe static int* Conversions (long b)
|
|
{
|
|
return (int*)b;
|
|
}
|
|
|
|
unsafe static int CheckAdd(byte* ptr, int offset)
|
|
{
|
|
if (checked(ptr + offset < ptr))
|
|
return 1;
|
|
|
|
return 101;
|
|
}
|
|
|
|
unsafe static int CheckSub(short* ptr, int offset)
|
|
{
|
|
if (checked(ptr - offset < ptr))
|
|
return 2;
|
|
|
|
return 102;
|
|
}
|
|
|
|
unsafe static int CheckSub(short* ptr, long offset)
|
|
{
|
|
if (checked(ptr - offset < ptr))
|
|
return 2;
|
|
|
|
return 102;
|
|
}
|
|
|
|
unsafe static int CheckSub2(short* ptr, int offset)
|
|
{
|
|
short* b = ptr + offset;
|
|
if (checked(ptr - b < 0))
|
|
return 3;
|
|
|
|
return 0;
|
|
}
|
|
}
|