Browse Source

Move correctness tests to single namespace

pull/728/merge
Siegfried Pammer 9 years ago
parent
commit
f24f1e007e
  1. 6
      ICSharpCode.Decompiler/Tests/CorrectnessTestRunner.cs
  2. 1
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  3. 41
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/Capturing.cs
  4. 2
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/Comparisons.cs
  5. 133
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/CompoundAssignment.cs
  6. 1
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/ConditionalAttr.cs
  7. 137
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/ControlFlow.cs
  8. 2
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/Conversions.cs
  9. 2
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/DecimalFields.cs
  10. 2
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/Generics.cs
  11. 2
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/HelloWorld.cs
  12. 933
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/InitializerTests.cs
  13. 2
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/MemberLookup.cs
  14. 4
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/PropertiesAndEvents.cs
  15. 224
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/Switch.cs
  16. 81
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/UndocumentedExpressions.cs
  17. 331
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/UnsafeCode.cs
  18. 6
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/ValueTypeCall.cs

6
ICSharpCode.Decompiler/Tests/CorrectnessTestRunner.cs

@ -156,6 +156,12 @@ namespace ICSharpCode.Decompiler.Tests
RunCS(options: options); RunCS(options: options);
} }
[Test]
public void Capturing([ValueSource("defaultOptions")] CompilerOptions options)
{
RunCS(options: options);
}
void RunCS([CallerMemberName] string testName = null, CompilerOptions options = CompilerOptions.UseDebug) void RunCS([CallerMemberName] string testName = null, CompilerOptions options = CompilerOptions.UseDebug)
{ {
string testFileName = testName + ".cs"; string testFileName = testName + ".cs";

1
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -126,6 +126,7 @@
<Compile Include="Helpers\TypeSystemHelper.cs" /> <Compile Include="Helpers\TypeSystemHelper.cs" />
<Compile Include="PrettyTestRunner.cs" /> <Compile Include="PrettyTestRunner.cs" />
<Compile Include="RoundtripAssembly.cs" /> <Compile Include="RoundtripAssembly.cs" />
<Compile Include="TestCases\Correctness\Capturing.cs" />
<Compile Include="TestCases\Correctness\OverloadResolution.cs" /> <Compile Include="TestCases\Correctness\OverloadResolution.cs" />
<Compile Include="TestCases\Pretty\AnonymousTypes.cs" /> <Compile Include="TestCases\Pretty\AnonymousTypes.cs" />
<Compile Include="TestCases\Pretty\Async.cs" /> <Compile Include="TestCases\Pretty\Async.cs" />

41
ICSharpCode.Decompiler/Tests/TestCases/Correctness/Capturing.cs

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{
class Capturing
{
static void Main(string[] args)
{
TestCase1();
}
static void TestCase1()
{
Console.WriteLine("TestCase1");
for (int i = 0; i < 10; i++)
Console.WriteLine(i);
// i no longer declared
List<Action> actions = new List<Action>();
int max = 5;
string line;
while (ReadLine(out line, ref max)) {
actions.Add(() => Console.WriteLine(line));
}
// line still declared
line = null;
Console.WriteLine("----");
foreach (var action in actions)
action();
}
private static bool ReadLine(out string line, ref int v)
{
line = v + " line";
return --v > 0;
}
}
}

2
ICSharpCode.Decompiler/Tests/TestCases/Correctness/Comparisons.cs

@ -20,7 +20,7 @@ using System;
#pragma warning disable 652 #pragma warning disable 652
namespace ICSharpCode.Decompiler.Tests.TestCases namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
public class Comparisons public class Comparisons
{ {

133
ICSharpCode.Decompiler/Tests/TestCases/Correctness/CompoundAssignment.cs

@ -19,74 +19,79 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
class CompoundAssignment namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
static void Main() class CompoundAssignment
{ {
PreIncrementProperty(); static void Main()
} {
PreIncrementProperty();
static void Test(int a, int b)
{
Console.WriteLine("{0} {1}", a, b);
}
static int x;
static int X()
{
Console.Write("X ");
return ++x;
}
int instanceField;
public int InstanceProperty {
get {
Console.WriteLine("In get_InstanceProperty");
return instanceField;
} }
set {
Console.WriteLine("In set_InstanceProperty, value=" + value); static void Test(int a, int b)
instanceField = value; {
Console.WriteLine("{0} {1}", a, b);
} }
}
static int x;
static int staticField;
static int X()
public static int StaticProperty { {
get { Console.Write("X ");
Console.WriteLine("In get_StaticProperty"); return ++x;
return staticField;
} }
set {
Console.WriteLine("In set_StaticProperty, value=" + value); int instanceField;
staticField = value;
public int InstanceProperty
{
get {
Console.WriteLine("In get_InstanceProperty");
return instanceField;
}
set {
Console.WriteLine("In set_InstanceProperty, value=" + value);
instanceField = value;
}
}
static int staticField;
public static int StaticProperty
{
get {
Console.WriteLine("In get_StaticProperty");
return staticField;
}
set {
Console.WriteLine("In set_StaticProperty, value=" + value);
staticField = value;
}
}
public static Dictionary<string, int> GetDict()
{
Console.WriteLine("In GetDict()");
return new Dictionary<string, int>();
}
static string GetString()
{
Console.WriteLine("In GetString()");
return "the string";
}
static void PreIncrementProperty()
{
Console.WriteLine("PreIncrementProperty:");
Test(X(), ++new CompoundAssignment().InstanceProperty);
Test(X(), ++StaticProperty);
}
static void PreIncrementIndexer()
{
Console.WriteLine("PreIncrementIndexer:");
Test(X(), ++GetDict()[GetString()]);
} }
} }
}
public static Dictionary<string, int> GetDict()
{
Console.WriteLine("In GetDict()");
return new Dictionary<string, int>();
}
static string GetString()
{
Console.WriteLine("In GetString()");
return "the string";
}
static void PreIncrementProperty()
{
Console.WriteLine("PreIncrementProperty:");
Test(X(), ++new CompoundAssignment().InstanceProperty);
Test(X(), ++StaticProperty);
}
static void PreIncrementIndexer()
{
Console.WriteLine("PreIncrementIndexer:");
Test(X(), ++GetDict()[GetString()]);
}
}

1
ICSharpCode.Decompiler/Tests/TestCases/Correctness/ConditionalAttr.cs

@ -5,7 +5,6 @@ using System.Diagnostics;
namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
class ConditionalAttr class ConditionalAttr
{ {
[Conditional("PRINT")] [Conditional("PRINT")]

137
ICSharpCode.Decompiler/Tests/TestCases/Correctness/ControlFlow.cs

@ -18,88 +18,91 @@
using System; using System;
class ControlFlow namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
public static int Main() class ControlFlow
{ {
int result = 0; public static int Main()
EmptyIf("Empty", ref result); {
EmptyIf("test", ref result); int result = 0;
NormalIf("none", ref result); EmptyIf("Empty", ref result);
NormalIf("test", ref result); EmptyIf("test", ref result);
NormalIf2("none", ref result); NormalIf("none", ref result);
NormalIf2("test", ref result); NormalIf("test", ref result);
NormalIf3("none", ref result); NormalIf2("none", ref result);
NormalIf3("test", ref result); NormalIf2("test", ref result);
Test("none", ref result); NormalIf3("none", ref result);
Test("test", ref result); NormalIf3("test", ref result);
Console.WriteLine(result); Test("none", ref result);
return 0; Test("test", ref result);
} Console.WriteLine(result);
return 0;
static void EmptyIf(string input, ref int result)
{
if (input.Contains("test")) {
} }
result = result + 1;
Console.WriteLine("EmptyIf");
}
static void NormalIf(string input, ref int result) static void EmptyIf(string input, ref int result)
{ {
if (input.Contains("test")) { if (input.Contains("test")) {
Console.WriteLine("result"); }
} else { result = result + 1;
Console.WriteLine("else"); Console.WriteLine("EmptyIf");
} }
result = result + 1;
Console.WriteLine("end");
}
static void NormalIf2(string input, ref int result) static void NormalIf(string input, ref int result)
{ {
if (input.Contains("test")) { if (input.Contains("test")) {
Console.WriteLine("result"); Console.WriteLine("result");
} else {
Console.WriteLine("else");
}
result = result + 1;
Console.WriteLine("end");
} }
result = result + 1;
Console.WriteLine("end");
}
static void NormalIf3(string input, ref int result) static void NormalIf2(string input, ref int result)
{ {
if (input.Contains("test")) { if (input.Contains("test")) {
Console.WriteLine("result"); Console.WriteLine("result");
} else { }
Console.WriteLine("else"); result = result + 1;
Console.WriteLine("end");
} }
result = result + 1;
}
static void Test(string input, ref int result) static void NormalIf3(string input, ref int result)
{ {
foreach (char c in input) { if (input.Contains("test")) {
Console.Write(c); Console.WriteLine("result");
} else {
Console.WriteLine("else");
}
result = result + 1; result = result + 1;
} }
if (input.Contains("test")) {
Console.WriteLine("result"); static void Test(string input, ref int result)
} else { {
Console.WriteLine("else"); foreach (char c in input) {
Console.Write(c);
result = result + 1;
}
if (input.Contains("test")) {
Console.WriteLine("result");
} else {
Console.WriteLine("else");
}
} }
}
int Dim2Search(int arg) int Dim2Search(int arg)
{ {
var tens = new[] { 10, 20, 30 }; var tens = new[] { 10, 20, 30 };
var ones = new[] { 1, 2, 3 }; var ones = new[] { 1, 2, 3 };
for (int i = 0; i < tens.Length; i++) { for (int i = 0; i < tens.Length; i++) {
for (int j = 0; j < ones.Length; j++) { for (int j = 0; j < ones.Length; j++) {
if (tens[i] + ones[j] == arg) if (tens[i] + ones[j] == arg)
return i; return i;
}
} }
}
return -1; return -1;
}
} }
} }

2
ICSharpCode.Decompiler/Tests/TestCases/Correctness/Conversions.cs

@ -21,7 +21,7 @@
using System; using System;
using ICSharpCode.Decompiler.Util; using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.Tests.TestCases namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
public class Conversions public class Conversions
{ {

2
ICSharpCode.Decompiler/Tests/TestCases/Correctness/DecimalFields.cs

@ -18,7 +18,7 @@
using System; using System;
namespace ICSharpCode.Decompiler.Tests.TestCases namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
/// <summary> /// <summary>
/// Description of DecimalFields. /// Description of DecimalFields.

2
ICSharpCode.Decompiler/Tests/TestCases/Correctness/Generics.cs

@ -18,7 +18,7 @@
using System; using System;
namespace Generics namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
/// <summary> /// <summary>
/// Description of Generics. /// Description of Generics.

2
ICSharpCode.Decompiler/Tests/TestCases/Correctness/HelloWorld.cs

@ -18,7 +18,7 @@
using System; using System;
namespace HelloWorld namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
class HelloWorld class HelloWorld
{ {

933
ICSharpCode.Decompiler/Tests/TestCases/Correctness/InitializerTests.cs

File diff suppressed because it is too large Load Diff

2
ICSharpCode.Decompiler/Tests/TestCases/Correctness/MemberLookup.cs

@ -18,7 +18,7 @@
using System; using System;
namespace ICSharpCode.Decompiler.Tests.TestCases namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
public class MemberLookup public class MemberLookup
{ {

4
ICSharpCode.Decompiler/Tests/TestCases/Correctness/PropertiesAndEvents.cs

@ -1,8 +1,8 @@
using System; using System;
namespace PropertiesAndEvents namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
class Program class PropertiesAndEvents
{ {
public static int Main(string[] args) public static int Main(string[] args)
{ {

224
ICSharpCode.Decompiler/Tests/TestCases/Correctness/Switch.cs

@ -18,131 +18,133 @@
using System; using System;
public static class Switch namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
public static void Main() public static class Switch
{ {
TestCase(SparseIntegerSwitch, -100, 1, 2, 3, 4); public static void Main()
TestCase(ShortSwitchOverString, "First case", "Else"); {
TestCase(SwitchOverString1, "First case", "Second case", "2nd case", "Third case", "Fourth case", "Fifth case", "Sixth case", null, "default", "else"); TestCase(SparseIntegerSwitch, -100, 1, 2, 3, 4);
Console.WriteLine(SwitchOverString2()); TestCase(ShortSwitchOverString, "First case", "Else");
Console.WriteLine(SwitchOverBool(true)); TestCase(SwitchOverString1, "First case", "Second case", "2nd case", "Third case", "Fourth case", "Fifth case", "Sixth case", null, "default", "else");
Console.WriteLine(SwitchOverBool(false)); Console.WriteLine(SwitchOverString2());
} Console.WriteLine(SwitchOverBool(true));
Console.WriteLine(SwitchOverBool(false));
static void TestCase<T>(Func<T, string> target, params T[] args)
{
foreach (var arg in args) {
Console.WriteLine(target(arg));
} }
}
public static string SparseIntegerSwitch(int i) static void TestCase<T>(Func<T, string> target, params T[] args)
{ {
switch (i) { foreach (var arg in args) {
case -10000000: return "-10 mln"; Console.WriteLine(target(arg));
case -100: return "-hundred"; }
case -1: return "-1";
case 0: return "0";
case 1: return "1";
case 2: return "2";
case 4: return "4";
case 100: return "hundred";
case 10000: return "ten thousand";
case 10001: return "ten thousand and one";
case int.MaxValue: return "int.MaxValue";
default: return "something else";
} }
}
public static string SparseIntegerSwitch(int i)
public static string ShortSwitchOverString(string text) {
{ switch (i) {
switch (text) { case -10000000: return "-10 mln";
case "First case": case -100: return "-hundred";
return "Text"; case -1: return "-1";
default: case 0: return "0";
return "Default"; case 1: return "1";
case 2: return "2";
case 4: return "4";
case 100: return "hundred";
case 10000: return "ten thousand";
case 10001: return "ten thousand and one";
case int.MaxValue: return "int.MaxValue";
default: return "something else";
}
} }
}
public static string SwitchOverString1(string text) public static string ShortSwitchOverString(string text)
{ {
switch (text) { switch (text) {
case "First case": case "First case":
return "Text1"; return "Text";
case "Second case": default:
case "2nd case": return "Default";
return "Text2"; }
case "Third case":
return "Text3";
case "Fourth case":
return "Text4";
case "Fifth case":
return "Text5";
case "Sixth case":
return "Text6";
case null:
return null;
default:
return "Default";
} }
}
public static string SwitchOverString2() public static string SwitchOverString1(string text)
{ {
switch (Environment.UserName) { switch (text) {
case "First case": case "First case":
return "Text1"; return "Text1";
case "Second case": case "Second case":
return "Text2"; case "2nd case":
case "Third case": return "Text2";
return "Text3"; case "Third case":
case "Fourth case": return "Text3";
return "Text4"; case "Fourth case":
case "Fifth case": return "Text4";
return "Text5"; case "Fifth case":
case "Sixth case": return "Text5";
return "Text6"; case "Sixth case":
default: return "Text6";
return "Default"; case null:
return null;
default:
return "Default";
}
} }
}
public static string SwitchOverBool(bool b) public static string SwitchOverString2()
{ {
switch (b) { switch (Environment.UserName) {
case true: case "First case":
return bool.TrueString; return "Text1";
case false: case "Second case":
return bool.FalseString; return "Text2";
default: case "Third case":
return null; return "Text3";
case "Fourth case":
return "Text4";
case "Fifth case":
return "Text5";
case "Sixth case":
return "Text6";
default:
return "Default";
}
} }
}
public static void SwitchInLoop(int i) public static string SwitchOverBool(bool b)
{ {
while (true) { switch (b) {
switch (i) { case true:
case 1: return bool.TrueString;
Console.WriteLine("one"); case false:
break; return bool.FalseString;
case 2:
Console.WriteLine("two");
break;
case 3:
Console.WriteLine("three");
continue;
case 4:
Console.WriteLine("four");
return;
default: default:
Console.WriteLine("default"); return null;
Console.WriteLine("more code");
throw new ArgumentException();
} }
i++;
} }
}
}
public static void SwitchInLoop(int i)
{
while (true) {
switch (i) {
case 1:
Console.WriteLine("one");
break;
case 2:
Console.WriteLine("two");
break;
case 3:
Console.WriteLine("three");
continue;
case 4:
Console.WriteLine("four");
return;
default:
Console.WriteLine("default");
Console.WriteLine("more code");
throw new ArgumentException();
}
i++;
}
}
}
}

81
ICSharpCode.Decompiler/Tests/TestCases/Correctness/UndocumentedExpressions.cs

@ -18,48 +18,51 @@
using System; using System;
public class UndocumentedExpressions namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
static void Main(string[] args) public class UndocumentedExpressions
{ {
MakeTypedRef("abc"); static void Main(string[] args)
VarArgs(1, __arglist()); {
VarArgs(__arglist(1)); MakeTypedRef("abc");
VarArgs(1, __arglist("abc", 2, true)); VarArgs(1, __arglist());
} VarArgs(__arglist(1));
VarArgs(1, __arglist("abc", 2, true));
public static void VarArgs(int normalArg, __arglist) }
{
ArgIterator argIterator = new ArgIterator(__arglist); public static void VarArgs(int normalArg, __arglist)
Console.WriteLine("Called with {0} arguments", argIterator.GetRemainingCount()); {
int pos = 0; ArgIterator argIterator = new ArgIterator(__arglist);
while (argIterator.GetRemainingCount() > 0) { Console.WriteLine("Called with {0} arguments", argIterator.GetRemainingCount());
TypedReference tr = argIterator.GetNextArg(); int pos = 0;
object val; while (argIterator.GetRemainingCount() > 0) {
try { TypedReference tr = argIterator.GetNextArg();
val = __refvalue(tr, object); object val;
} catch (Exception ex) { try {
val = ex.GetType().Name; val = __refvalue(tr, object);
} catch (Exception ex) {
val = ex.GetType().Name;
}
Console.WriteLine("{0} : {1} = {2}", pos++, __reftype(tr).Name, val);
} }
Console.WriteLine("{0} : {1} = {2}", pos++, __reftype(tr).Name, val);
} }
}
public static void VarArgs(__arglist) public static void VarArgs(__arglist)
{ {
Console.WriteLine("The other varargs overload"); Console.WriteLine("The other varargs overload");
} }
public static void MakeTypedRef(object o) public static void MakeTypedRef(object o)
{ {
TypedReference tr = __makeref(o); TypedReference tr = __makeref(o);
UndocumentedExpressions.AcceptTypedRef(tr); UndocumentedExpressions.AcceptTypedRef(tr);
} }
private static void AcceptTypedRef(TypedReference tr) private static void AcceptTypedRef(TypedReference tr)
{ {
Console.WriteLine("Value is: " + __refvalue(tr, object).ToString()); Console.WriteLine("Value is: " + __refvalue(tr, object).ToString());
Console.WriteLine("Type is: " + __reftype(tr).Name); Console.WriteLine("Type is: " + __reftype(tr).Name);
__refvalue(tr, object) = 1; __refvalue(tr, object) = 1;
}
} }
} }

331
ICSharpCode.Decompiler/Tests/TestCases/Correctness/UnsafeCode.cs

@ -18,201 +18,194 @@
using System; using System;
public class UnsafeCode namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
struct SimpleStruct public class UnsafeCode
{ {
public int X; struct SimpleStruct
public double Y;
}
static void Main()
{
// TODO: test behavior, or convert this into a pretty-test
// (but for now, it's already valuable knowing whether the decompiled code can be re-compiled)
}
public unsafe int* NullPointer
{
get
{ {
return null; public int X;
public double Y;
} }
}
static void Main()
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); // TODO: test behavior, or convert this into a pretty-test
// (but for now, it's already valuable knowing whether the decompiled code can be re-compiled)
} }
}
public unsafe int* NullPointer
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); get {
this.PointerReferenceExpression(ptr); return null;
}
} }
}
public unsafe int* PointerCast(long* p)
public unsafe int MultipleExitsOutOfFixedBlock(int[] arr)
{
fixed (int* ptr = &arr[0])
{
if (*ptr < 0)
return *ptr;
if (*ptr == 21)
return 42;
if (*ptr == 42)
goto outside;
}
return 1;
outside:
Console.WriteLine("outside");
return 2;
}
public unsafe void FixedStringAccess(string text)
{
fixed (char* ptr = text)
{ {
char* ptr2 = ptr; return (int*)p;
while (*ptr2 != '\0') }
{
*ptr2 = 'A'; public unsafe long ConvertDoubleToLong(double d)
ptr2++; {
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)
public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val)
{
fixed (long* ptr = array)
{ {
((double*)ptr)[index] = val; this.PassRefParameterAsPointer(ref *p);
} }
}
public unsafe void AddressInMultiDimensionalArray(double[,] matrix)
public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val)
{
fixed (long* ptr = &array[index])
{ {
*(double*)ptr = val; fixed (double* ptr = &matrix[1, 2]) {
this.PointerReferenceExpression(ptr);
this.PointerReferenceExpression(ptr);
}
} }
}
public unsafe int MultipleExitsOutOfFixedBlock(int[] arr)
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)
{ {
*ptr = 'c'; fixed (int* ptr = &arr[0]) {
*userName = 'd'; if (*ptr < 0)
*ptr2 = 'e'; return *ptr;
if (*ptr == 21)
return 42;
if (*ptr == 42)
goto outside;
}
return 1;
outside:
Console.WriteLine("outside");
return 2;
} }
}
public unsafe void FixedStringAccess(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; fixed (char* ptr = text) {
char* ptr2 = ptr;
while (*ptr2 != '\0') {
*ptr2 = 'A';
ptr2++;
}
}
} }
return this.PointerReferenceExpression((double*)ptr);
}
public unsafe string StackAllocStruct(int count) public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val)
{ {
SimpleStruct* s = stackalloc SimpleStruct[checked(count * 2)]; fixed (long* ptr = array) {
SimpleStruct* p = stackalloc SimpleStruct[10]; ((double*)ptr)[index] = val;
return this.PointerReferenceExpression(&s->Y); }
} }
public unsafe int* PointerArithmetic(int* p)
{
return p + 2;
}
public unsafe byte* PointerArithmetic2(long* p, int y, int x) public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val)
{ {
return (byte*)((short*)p + (y * x)); fixed (long* ptr = &array[index]) {
} *(double*)ptr = val;
}
}
public unsafe long* PointerArithmetic3(long* p) public unsafe string PointerReferenceExpression(double* d)
{ {
return (long*)((byte*)p + 3); return d->ToString();
} }
public unsafe long* PointerArithmetic4(void* p) public unsafe string PointerReferenceExpression2(long addr)
{ {
return (long*)((byte*)p + 3); return ((int*)addr)->ToString();
} }
public unsafe int PointerArithmetic5(void* p, byte* q, int i) public unsafe void FixMultipleStrings(string text)
{ {
return (int)(q[i] + *(byte*)p); fixed (char* ptr = text, userName = Environment.UserName, ptr2 = text) {
} *ptr = 'c';
*userName = 'd';
*ptr2 = 'e';
}
}
public unsafe int PointerSubtraction(long* p, long* q) public unsafe string StackAlloc(int count)
{ {
return (int)((long)(p - q)); 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 int PointerSubtraction2(long* p, short* q) public unsafe string StackAllocStruct(int count)
{ {
return (int)((long)((byte*)p - (byte*)q)); SimpleStruct* s = stackalloc SimpleStruct[checked(count * 2)];
} SimpleStruct* p = stackalloc SimpleStruct[10];
return this.PointerReferenceExpression(&s->Y);
}
public unsafe int PointerSubtraction3(void* p, void* q) public unsafe int* PointerArithmetic(int* p)
{ {
return (int)((long)((byte*)p - (byte*)q)); return p + 2;
} }
unsafe ~UnsafeCode() public unsafe byte* PointerArithmetic2(long* p, int y, int x)
{ {
this.PassPointerAsRefParameter(this.NullPointer); 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);
}
} }
} }

6
ICSharpCode.Decompiler/Tests/TestCases/Correctness/ValueTypeCall.cs

@ -1,6 +1,6 @@
using System; using System;
namespace ValueTypeCall namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
{ {
public struct MutValueType public struct MutValueType
{ {
@ -41,7 +41,7 @@ namespace ValueTypeCall
} }
} }
public class Program public class ValueTypeCall
{ {
public static void Main() public static void Main()
{ {
@ -52,7 +52,7 @@ namespace ValueTypeCall
Box(); Box();
var gvt = new GenericValueType<string>("Test"); var gvt = new GenericValueType<string>("Test");
gvt.Call(ref gvt); gvt.Call(ref gvt);
new Program().InstanceFieldTests(); new ValueTypeCall().InstanceFieldTests();
} }
static void RefParameter(ref MutValueType m) static void RefParameter(ref MutValueType m)

Loading…
Cancel
Save