Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="TestCases\Correctness\Using.cs" />
<Compile Include="TestCases\ILPretty\Issue379.cs" />
<Compile Include="TestCases\Pretty\FixProxyCalls.cs" />
<Compile Include="TestCases\Pretty\UnsafeCode.cs" />
<Compile Include="TestCases\Pretty\InitializerTests.cs" />
<None Include="TestCases\ILPretty\Issue646.cs" />
<Compile Include="TestCases\Pretty\Async.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public void CheckedUnchecked([ValueSource("defaultOptions")] CompilerOptions csc
Run(cscOptions: cscOptions);
}

[Test]
public void UnsafeCode([ValueSource("defaultOptions")] CompilerOptions cscOptions)
{
Run(cscOptions: cscOptions);
}

[Test]
public void PInvoke([ValueSource("defaultOptions")] CompilerOptions cscOptions)
{
Expand Down
46 changes: 46 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Correctness/TrickyTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TrickyTypes
static void Main()
{
InterestingConstants();
TruncatedComp();
}

static void Print<T>(T val)
Expand All @@ -46,5 +47,50 @@ static void InterestingConstants()
Print(val2);
Print(2147483648u);
}

static void TruncatedComp()
{
Console.WriteLine("TruncatedComp1(1):");
TruncatedComp1(1);

Console.WriteLine("TruncatedComp1(-1):");
TruncatedComp1(-1);

Console.WriteLine("TruncatedComp1(0x100000001):");
TruncatedComp1(0x100000001);

Console.WriteLine("TruncatedComp1(long.MinValue):");
TruncatedComp1(long.MinValue);

Console.WriteLine("TruncatedComp2(1):");
TruncatedComp2(1, 1);

Console.WriteLine("TruncatedComp2(-1):");
TruncatedComp2(-1, -1);

Console.WriteLine("TruncatedComp2(0x100000001):");
TruncatedComp2(0x100000001, 1);

Console.WriteLine("TruncatedComp2(long.MinValue):");
TruncatedComp2(long.MinValue, int.MinValue);
}

static void TruncatedComp1(long val)
{
Print((int)val == val);
Print(val == (int)val);
Print(val < (int)val);
Print((int)val >= val);
}

static void TruncatedComp2(long val1, int val2)
{
Print(val1 == val2);
Print((int)val1 == val2);
Print(val1 < val2);
Print((int)val1 < val2);
Print(val1 <= val2);
Print((int)val1 <= val2);
}
}
}
148 changes: 2 additions & 146 deletions ICSharpCode.Decompiler.Tests/TestCases/Correctness/UnsafeCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{
public class UnsafeCode
{
struct SimpleStruct
private struct SimpleStruct
{
public int X;
public double Y;
Expand All @@ -34,58 +34,6 @@ static void Main()
// (but for now, it's already valuable knowing whether the decompiled code can be re-compiled)
}

public unsafe int* NullPointer
{
get {
return null;
}
}

public unsafe int* PointerCast(long* p)
{
return (int*)p;
}

public unsafe long ConvertDoubleToLong(double d)
{
return *(long*)(&d);
}

public unsafe double ConvertLongToDouble(long d)
{
return *(double*)(&d);
}

public unsafe int ConvertFloatToInt(float d)
{
return *(int*)(&d);
}

public unsafe float ConvertIntToFloat(int d)
{
return *(float*)(&d);
}

public unsafe void PassRefParameterAsPointer(ref int p)
{
fixed (int* ptr = &p) {
this.PassPointerAsRefParameter(ptr);
}
}

public unsafe void PassPointerAsRefParameter(int* p)
{
this.PassRefParameterAsPointer(ref *p);
}

public unsafe void AddressInMultiDimensionalArray(double[,] matrix)
{
fixed (double* ptr = &matrix[1, 2]) {
this.PointerReferenceExpression(ptr);
this.PointerReferenceExpression(ptr);
}
}

public unsafe int MultipleExitsOutOfFixedBlock(int[] arr)
{
fixed (int* ptr = &arr[0]) {
Expand All @@ -101,42 +49,7 @@ public unsafe int MultipleExitsOutOfFixedBlock(int[] arr)
Console.WriteLine("outside");
return 2;
}

public unsafe void FixedStringAccess(string text)
{
fixed (char* ptr = text) {
char* ptr2 = ptr;
while (*ptr2 != '\0') {
*ptr2 = 'A';
ptr2++;
}
}
}

public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val)
{
fixed (long* ptr = array) {
((double*)ptr)[index] = val;
}
}

public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val)
{
fixed (long* ptr = &array[index]) {
*(double*)ptr = val;
}
}

public unsafe string PointerReferenceExpression(double* d)
{
return d->ToString();
}

public unsafe string PointerReferenceExpression2(long addr)
{
return ((int*)addr)->ToString();
}


public unsafe void FixMultipleStrings(string text)
{
fixed (char* ptr = text, userName = Environment.UserName, ptr2 = text) {
Expand All @@ -146,66 +59,9 @@ public unsafe void FixMultipleStrings(string text)
}
}

public unsafe string StackAlloc(int count)
{
char* ptr = stackalloc char[count];
char* ptr2 = stackalloc char[100];
for (int i = 0; i < count; i++) {
ptr[i] = (char)i;
}
return this.PointerReferenceExpression((double*)ptr);
}

public unsafe string StackAllocStruct(int count)
{
SimpleStruct* s = stackalloc SimpleStruct[checked(count * 2)];
SimpleStruct* p = stackalloc SimpleStruct[10];
return this.PointerReferenceExpression(&s->Y);
}

public unsafe int* PointerArithmetic(int* p)
{
return p + 2;
}

public unsafe byte* PointerArithmetic2(long* p, int y, int x)
{
return (byte*)((short*)p + (y * x));
}

public unsafe long* PointerArithmetic3(long* p)
{
return (long*)((byte*)p + 3);
}

public unsafe long* PointerArithmetic4(void* p)
{
return (long*)((byte*)p + 3);
}

public unsafe int PointerArithmetic5(void* p, byte* q, int i)
{
return (int)(q[i] + *(byte*)p);
}

public unsafe int PointerSubtraction(long* p, long* q)
{
return (int)((long)(p - q));
}

public unsafe int PointerSubtraction2(long* p, short* q)
{
return (int)((long)((byte*)p - (byte*)q));
}

public unsafe int PointerSubtraction3(void* p, void* q)
{
return (int)((long)((byte*)p - (byte*)q));
}

unsafe ~UnsafeCode()
{
this.PassPointerAsRefParameter(this.NullPointer);
}
}
}
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/ILPretty/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*.dll
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*.res
/*.dll
/*.exe
/*.pdb
Loading