Browse Source

Adjust unit tests.

pull/100/head
Daniel Grunwald 15 years ago
parent
commit
b5e29f0e05
  1. 39
      ICSharpCode.Decompiler/Tests/CheckedUnchecked.cs
  2. 17
      ICSharpCode.Decompiler/Tests/DelegateConstruction.cs
  3. 40
      ICSharpCode.Decompiler/Tests/ExceptionHandling.cs
  4. 4
      ICSharpCode.Decompiler/Tests/Generics.cs
  5. 6
      ICSharpCode.Decompiler/Tests/Helpers/RemoveCompilerAttribute.cs
  6. 2
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  7. 16
      ICSharpCode.Decompiler/Tests/Loops.cs
  8. 20
      ICSharpCode.Decompiler/Tests/MultidimensionalArray.cs
  9. 63
      ICSharpCode.Decompiler/Tests/PropertiesAndEvents.cs
  10. 88
      ICSharpCode.Decompiler/Tests/TestRunner.cs
  11. 62
      ICSharpCode.Decompiler/Tests/UnsafeCode.cs
  12. 57
      ICSharpCode.Decompiler/Tests/ValueTypes.cs

39
ICSharpCode.Decompiler/Tests/CheckedUnchecked.cs

@ -1,39 +1,36 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt) // This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
public class CheckedUnchecked public class CheckedUnchecked
{ {
public void Operators(int a, int b) public int Operators(int a, int b)
{ {
int c1 = checked(a + b); int num = checked(a + b);
int u1 = unchecked(a + b); int num2 = a + b;
int c2 = checked(a - b); int num3 = checked(a - b);
int u2 = unchecked(a - b); int num4 = a - b;
int c3 = checked(a * b); int num5 = checked(a * b);
int u3 = unchecked(a * b); int num6 = a * b;
int c4 = checked(a / b); int num7 = a / b;
int u4 = unchecked(a / b); int num8 = a % b;
int c5 = checked(a % b); // The division operators / and % only exist in one form (checked vs. unchecked doesn't matter for them)
int u5 = unchecked(a % b); return num * num2 * num3 * num4 * num5 * num6 * num7 * num8;
} }
public void Cast(int a) public int Cast(int a)
{ {
short c1 = checked((short)a); short num = checked((short)a);
short u1 = unchecked((short)a); short num2 = (short)a;
byte c2 = checked((byte)a); byte b = checked((byte)a);
byte u2 = unchecked((byte)a); byte b2 = (byte)a;
return num * num2 * b * b2;
} }
public void ForWithCheckedIteratorAndUncheckedBody(int n) public void ForWithCheckedIteratorAndUncheckedBody(int n)
{ {
checked { checked {
for (int i = n + 1; i < n + 1; i++) { for (int i = n + 1; i < n + 1; i++) {
unchecked { n = unchecked(i * i);
n = i * i;
}
} }
} }
} }

17
ICSharpCode.Decompiler/Tests/DelegateConstruction.cs

@ -46,7 +46,11 @@ public static class DelegateConstruction
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
int counter; int counter;
list.Add(x => counter = x); list.Add(delegate(int x)
{
counter = x;
}
);
} }
return list; return list;
} }
@ -57,13 +61,20 @@ public static class DelegateConstruction
int counter; int counter;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
list.Add(x => counter = x); list.Add(delegate(int x)
{
counter = x;
}
);
} }
return list; return list;
} }
public static Action StaticAnonymousMethodNoClosure() public static Action StaticAnonymousMethodNoClosure()
{ {
return delegate { Console.WriteLine(); }; return delegate
{
Console.WriteLine();
};
} }
} }

40
ICSharpCode.Decompiler/Tests/ExceptionHandling.cs

@ -7,42 +7,60 @@ public class ExceptionHandling
{ {
public void MethodEndingWithEndFinally() public void MethodEndingWithEndFinally()
{ {
try { try
{
throw null; throw null;
} finally { }
finally
{
Console.WriteLine(); Console.WriteLine();
} }
} }
public void MethodEndingWithRethrow() public void MethodEndingWithRethrow()
{ {
try { try
{
throw null; throw null;
} catch { }
catch
{
throw; throw;
} }
} }
public void TryCatchFinally() public void TryCatchFinally()
{ {
try { try
{
Console.WriteLine("Try"); Console.WriteLine("Try");
} catch (Exception ex) { }
catch (Exception ex)
{
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
} finally { }
finally
{
Console.WriteLine("Finally"); Console.WriteLine("Finally");
} }
} }
public void TryCatchMultipleHandlers() public void TryCatchMultipleHandlers()
{ {
try { try
{
Console.WriteLine("Try"); Console.WriteLine("Try");
} catch (InvalidOperationException ex) { }
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
} catch (Exception ex) { }
catch (Exception ex)
{
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
} catch { }
catch
{
Console.WriteLine("other"); Console.WriteLine("other");
} }
} }

4
ICSharpCode.Decompiler/Tests/Generics.cs

@ -23,7 +23,7 @@ public static class Generics
public void Size(int capacity) public void Size(int capacity)
{ {
Array.Resize(ref this.arr, capacity); Array.Resize<T>(ref this.arr, capacity);
} }
public void Grow(int capacity) public void Grow(int capacity)
@ -43,7 +43,7 @@ public static class Generics
{ {
} }
public static Dictionary<string, string>.KeyCollection.Enumerator GetEnumerator(Dictionary<string, string> d, MyArray<string>.NestedClass<int> nc) public static Dictionary<string, string>.KeyCollection.Enumerator GetEnumerator(Dictionary<string, string> d, Generics.MyArray<string>.NestedClass<int> nc)
{ {
// Tests references to inner classes in generic classes // Tests references to inner classes in generic classes
return d.Keys.GetEnumerator(); return d.Keys.GetEnumerator();

6
ICSharpCode.Decompiler/Tests/Helpers/RemoveCompilerAttribute.cs

@ -21,6 +21,12 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
if (section.Attributes.Count == 0) if (section.Attributes.Count == 0)
section.Remove(); section.Remove();
} }
if (section.AttributeTarget == AttributeTarget.Module && type.Identifier == "UnverifiableCode")
{
attribute.Remove();
if (section.Attributes.Count == 0)
section.Remove();
}
return null; return null;
} }

2
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -4,7 +4,7 @@
<ProjectGuid>{FEC0DA52-C4A6-4710-BE36-B484A20C5E22}</ProjectGuid> <ProjectGuid>{FEC0DA52-C4A6-4710-BE36-B484A20C5E22}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform> <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<OutputType>Exe</OutputType> <OutputType>Library</OutputType>
<RootNamespace>ICSharpCode.Decompiler.Tests</RootNamespace> <RootNamespace>ICSharpCode.Decompiler.Tests</RootNamespace>
<AssemblyName>ICSharpCode.Decompiler.Tests</AssemblyName> <AssemblyName>ICSharpCode.Decompiler.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

16
ICSharpCode.Decompiler/Tests/Loops.cs

@ -8,29 +8,33 @@ public class Loops
{ {
public void ForEach(IEnumerable<string> enumerable) public void ForEach(IEnumerable<string> enumerable)
{ {
foreach (string text in enumerable) { foreach (string current in enumerable)
text.ToLower(); {
current.ToLower();
} }
} }
public void ForEachOverList(List<string> list) public void ForEachOverList(List<string> list)
{ {
// List has a struct as enumerator, so produces quite different IL than foreach over the IEnumerable interface // List has a struct as enumerator, so produces quite different IL than foreach over the IEnumerable interface
foreach (string text in list) { foreach (string current in list)
text.ToLower(); {
current.ToLower();
} }
} }
public void ForEachOverArray(string[] array) public void ForEachOverArray(string[] array)
{ {
foreach (string text in array) { foreach (string text in array)
{
text.ToLower(); text.ToLower();
} }
} }
public void ForOverArray(string[] array) public void ForOverArray(string[] array)
{ {
for (int i = 0; i < array.Length; i++) { for (int i = 0; i < array.Length; i++)
{
array[i].ToLower(); array[i].ToLower();
} }
} }

20
ICSharpCode.Decompiler/Tests/MultidimensionalArray.cs

@ -1,8 +1,6 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt) // This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
public static class MultidimensionalArray public static class MultidimensionalArray
{ {
internal class Generic<T, S> where T : new() internal class Generic<T, S> where T : new()
@ -12,16 +10,22 @@ public static class MultidimensionalArray
public T this[int i, int j] public T this[int i, int j]
{ {
get { return a[i, j]; } get
set { a[i, j] = value; } {
return this.a[i, j];
}
set
{
this.a[i, j] = value;
}
} }
public void TestB(S x, ref S y) public void TestB(S x, ref S y)
{ {
b[5, 3] = new S[10]; this.b[5, 3] = new S[10];
b[5, 3][0] = default(S); this.b[5, 3][0] = default(S);
b[5, 3][1] = x; this.b[5, 3][1] = x;
b[5, 3][2] = y; this.b[5, 3][2] = y;
} }
} }

63
ICSharpCode.Decompiler/Tests/PropertiesAndEvents.cs

@ -6,48 +6,61 @@ using System.Text;
public class PropertiesAndEvents public class PropertiesAndEvents
{ {
public int Getter(StringBuilder b) public event EventHandler AutomaticEvent;
{
return b.Length;
}
public void Setter(StringBuilder b) [field: NonSerialized]
public event EventHandler AutomaticEventWithInitializer = delegate
{ {
b.Capacity = 100;
} }
;
public char IndexerGetter(StringBuilder b) public event EventHandler CustomEvent
{ {
return b[50]; add
{
this.AutomaticEvent += value;
}
remove
{
this.AutomaticEvent -= value;
}
} }
public void IndexerSetter(StringBuilder b) public int AutomaticProperty
{ {
b[42] = 'b'; get;
set;
} }
public int AutomaticProperty { get; set; } public int CustomProperty
{
public int CustomProperty { get
get { {
return this.AutomaticProperty; return this.AutomaticProperty;
} }
set { set
{
this.AutomaticProperty = value; this.AutomaticProperty = value;
} }
} }
public event EventHandler AutomaticEvent; public int Getter(StringBuilder b)
{
return b.Length;
}
[field: NonSerialized] public void Setter(StringBuilder b)
public event EventHandler AutomaticEventWithInitializer = delegate {}; {
b.Capacity = 100;
}
public event EventHandler CustomEvent { public char IndexerGetter(StringBuilder b)
add { {
this.AutomaticEvent += value; return b[50];
} }
remove {
this.AutomaticEvent -= value; public void IndexerSetter(StringBuilder b)
} {
b[42] = 'b';
} }
} }

88
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -6,25 +6,88 @@ using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using ICSharpCode.Decompiler.Ast; using ICSharpCode.Decompiler.Ast;
using Microsoft.CSharp; using Microsoft.CSharp;
using Mono.Cecil; using Mono.Cecil;
using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests namespace ICSharpCode.Decompiler.Tests
{ {
[TestFixture]
public class TestRunner public class TestRunner
{ {
public static void Main(string[] args) [Test, Ignore("unncessary primitive casts")]
public void CheckedUnchecked()
{ {
if (args.Length == 1) TestFile(@"..\..\Tests\CheckedUnchecked.cs");
TestFile(args[0]);
else
TestFile(@"..\..\Tests\DelegateConstruction.cs");
Console.ReadKey();
} }
[Test, Ignore("Missing cast on null")]
public void DelegateConstruction()
{
TestFile(@"..\..\Tests\DelegateConstruction.cs");
}
[Test, Ignore("bug with variable-less catch")]
public void ExceptionHandling()
{
TestFile(@"..\..\Tests\ExceptionHandling.cs");
}
[Test]
public void Generics()
{
TestFile(@"..\..\Tests\Generics.cs");
}
[Test, Ignore("Formatting issues (array initializers not on single line)")]
public void InitializerTests()
{
TestFile(@"..\..\Tests\InitializerTests.cs");
}
[Test, Ignore("ForEachOverArray not supported")]
public void Loops()
{
TestFile(@"..\..\Tests\Loops.cs");
}
[Test]
public void MultidimensionalArray()
{
TestFile(@"..\..\Tests\MultidimensionalArray.cs");
}
[Test]
public void PropertiesAndEvents()
{
TestFile(@"..\..\Tests\PropertiesAndEvents.cs");
}
[Test, Ignore]
public void Switch()
{
TestFile(@"..\..\Tests\Switch.cs");
}
[Test, Ignore("has incorrect casts to IntPtr")]
public void UnsafeCode()
{
TestFile(@"..\..\Tests\UnsafeCode.cs");
}
[Test, Ignore("IncrementArrayLocation not yet supported")]
public void ValueTypes()
{
TestFile(@"..\..\Tests\ValueTypes.cs");
}
[Test, Ignore("Redundant yield break; not removed")]
public void YieldReturn()
{
TestFile(@"..\..\Tests\YieldReturn.cs");
}
static void TestFile(string fileName) static void TestFile(string fileName)
{ {
string code = File.ReadAllText(fileName); string code = File.ReadAllText(fileName);
@ -49,19 +112,19 @@ namespace ICSharpCode.Decompiler.Tests
string line1, line2; string line1, line2;
while ((line1 = r1.ReadLine()) != null) { while ((line1 = r1.ReadLine()) != null) {
string trimmed = line1.Trim(); string trimmed = line1.Trim();
if (trimmed.Length == 0 || trimmed.StartsWith("//", StringComparison.Ordinal) || line1.StartsWith("using ", StringComparison.Ordinal)) { if (trimmed.Length == 0 || trimmed.StartsWith("//", StringComparison.Ordinal) | trimmed.StartsWith("#", StringComparison.Ordinal)) {
diff.WriteLine(" " + line1); diff.WriteLine(" " + line1);
continue; continue;
} }
line2 = r2.ReadLine(); line2 = r2.ReadLine();
while (line2 != null && (line2.StartsWith("using ", StringComparison.Ordinal) || line2.Trim().Length == 0)) while (line2 != null && string.IsNullOrWhiteSpace(line2))
line2 = r2.ReadLine(); line2 = r2.ReadLine();
if (line2 == null) { if (line2 == null) {
ok = false; ok = false;
diff.WriteLine("-" + line1); diff.WriteLine("-" + line1);
continue; continue;
} }
if (line1 != line2) { if (line1.Trim() != line2.Trim()) {
ok = false; ok = false;
if (numberOfContinuousMistakes++ > 5) if (numberOfContinuousMistakes++ > 5)
return false; return false;
@ -84,6 +147,7 @@ namespace ICSharpCode.Decompiler.Tests
{ {
CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }); CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
CompilerParameters options = new CompilerParameters(); CompilerParameters options = new CompilerParameters();
options.CompilerOptions = "/unsafe";
options.ReferencedAssemblies.Add("System.Core.dll"); options.ReferencedAssemblies.Add("System.Core.dll");
CompilerResults results = provider.CompileAssemblyFromSource(options, code); CompilerResults results = provider.CompileAssemblyFromSource(options, code);
try { try {

62
ICSharpCode.Decompiler/Tests/UnsafeCode.cs

@ -5,50 +5,58 @@ using System;
public class UnsafeCode public class UnsafeCode
{ {
public unsafe long ConvertDoubleToLong(double d) public unsafe int* NullPointer
{ {
return *(long*)&d; get
} {
public unsafe int* NullPointer {
get {
return null; return null;
} }
} }
public unsafe long ConvertDoubleToLong(double d)
{
return *(long*)(&d);
}
public unsafe void PassRefParameterAsPointer(ref int p) public unsafe void PassRefParameterAsPointer(ref int p)
{ {
fixed (int* ptr = &p) fixed (int* ptr = &p)
PassPointerAsRefParameter(ptr); {
this.PassPointerAsRefParameter(ptr);
}
} }
public unsafe void PassPointerAsRefParameter(int* p) public unsafe void PassPointerAsRefParameter(int* p)
{ {
PassRefParameterAsPointer(ref *p); this.PassRefParameterAsPointer(ref *p);
} }
public unsafe void FixedStringAccess(string text) public unsafe void FixedStringAccess(string text)
{ {
fixed (char* c = text) { fixed (char* ptr = text)
char* tmp = c; {
while (*tmp != 0) { char* ptr2 = ptr;
*tmp = 'A'; while (*ptr2 != 0)
tmp++; {
*ptr2 = 'A';
ptr2++;
} }
} }
} }
public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val) public unsafe void PutDoubleIntoLongArray1(long[] array, int index, double val)
{ {
fixed (long* l = array) { fixed (long* ptr = array)
((double*)l)[index] = val; {
((double*)ptr)[index] = val;
} }
} }
public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val) public unsafe void PutDoubleIntoLongArray2(long[] array, int index, double val)
{ {
fixed (long* l = &array[index]) { fixed (long* ptr = &array[index])
*(double*)l = val; {
*(double*)ptr = val;
} }
} }
@ -59,24 +67,26 @@ public class UnsafeCode
public unsafe void FixMultipleStrings(string text) public unsafe void FixMultipleStrings(string text)
{ {
fixed (char* c = text, d = Environment.UserName, e = text) { fixed (char* ptr = text, userName = Environment.UserName, ptr2 = text)
*c = 'c'; {
*d = 'd'; *ptr = 'c';
*e = 'e'; *userName = 'd';
*ptr2 = 'e';
} }
} }
public unsafe string StackAlloc(int count) public unsafe string StackAlloc(int count)
{ {
char* a = stackalloc char[count]; char* ptr = stackalloc char[count];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++)
a[i] = (char)i; {
ptr[i] = (char)i;
} }
return PointerReferenceExpression((double*)a); return this.PointerReferenceExpression((double*)ptr);
} }
unsafe ~UnsafeCode() unsafe ~UnsafeCode()
{ {
PassPointerAsRefParameter(NullPointer); this.PassPointerAsRefParameter(this.NullPointer);
} }
} }

57
ICSharpCode.Decompiler/Tests/ValueTypes.cs

@ -22,70 +22,71 @@ public static class ValueTypes
public void MethodCalls() public void MethodCalls()
{ {
this.SetField(); this.SetField();
Test(this); ValueTypes.S.Test(this);
Test(ref this); ValueTypes.S.Test(ref this);
} }
static void Test(S byVal) private static void Test(ValueTypes.S byVal)
{ {
} }
static void Test(ref S byRef) private static void Test(ref ValueTypes.S byRef)
{ {
} }
} }
public static S InitObj1() public static ValueTypes.S InitObj1()
{ {
S s = default(S); ValueTypes.S result = default(ValueTypes.S);
return s; ValueTypes.MakeArray();
return result;
} }
public static S InitObj2() public static ValueTypes.S InitObj2()
{ {
return default(S); return default(ValueTypes.S);
} }
public static void InitObj3(out S p) public static void InitObj3(out ValueTypes.S p)
{ {
p = default(S); p = default(ValueTypes.S);
} }
public static S CallValueTypeCtor1() public static ValueTypes.S CallValueTypeCtor1()
{ {
return new S(10); return new ValueTypes.S(10);
} }
public static S CallValueTypeCtor2() public static ValueTypes.S CallValueTypeCtor2()
{ {
S s = new S(10); ValueTypes.S result = new ValueTypes.S(10);
return s; return result;
} }
public static S Copy1(S p) public static ValueTypes.S Copy1(ValueTypes.S p)
{ {
return p; return p;
} }
public static S Copy2(ref S p) public static ValueTypes.S Copy2(ref ValueTypes.S p)
{ {
return p; return p;
} }
public static void Copy3(S p, out S o) public static void Copy3(ValueTypes.S p, out ValueTypes.S o)
{ {
o = p; o = p;
} }
public static void Copy4(ref S p, out S o) public static void Copy4(ref ValueTypes.S p, out ValueTypes.S o)
{ {
o = p; o = p;
} }
public static void Copy4b(ref S p, out S o) public static void Copy4b(ref ValueTypes.S p, out ValueTypes.S o)
{ {
// test passing through by-ref arguments // test passing through by-ref arguments
Copy4(ref p, out o); ValueTypes.Copy4(ref p, out o);
} }
public static void Issue56(int i, out string str) public static void Issue56(int i, out string str)
@ -94,20 +95,20 @@ public static class ValueTypes
str += i.ToString(); str += i.ToString();
} }
public static void CopyAroundAndModifyField(S s) public static void CopyAroundAndModifyField(ValueTypes.S s)
{ {
S locS = s; ValueTypes.S s2 = s;
locS.Field += 10; s2.Field += 10;
s = locS; s = s2;
} }
static int[] MakeArray() private static int[] MakeArray()
{ {
return null; return null;
} }
public static void IncrementArrayLocation() public static void IncrementArrayLocation()
{ {
MakeArray()[Environment.TickCount]++; ValueTypes.MakeArray()[Environment.TickCount]++;
} }
} }

Loading…
Cancel
Save