Browse Source

Adjust unit tests.

pull/100/head
Daniel Grunwald 14 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 @@ @@ -1,39 +1,36 @@
// 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)
using System;
public class CheckedUnchecked
{
public void Operators(int a, int b)
public int Operators(int a, int b)
{
int c1 = checked(a + b);
int u1 = unchecked(a + b);
int c2 = checked(a - b);
int u2 = unchecked(a - b);
int c3 = checked(a * b);
int u3 = unchecked(a * b);
int c4 = checked(a / b);
int u4 = unchecked(a / b);
int c5 = checked(a % b);
int u5 = unchecked(a % b);
int num = checked(a + b);
int num2 = a + b;
int num3 = checked(a - b);
int num4 = a - b;
int num5 = checked(a * b);
int num6 = a * b;
int num7 = a / b;
int num8 = a % b;
// The division operators / and % only exist in one form (checked vs. unchecked doesn't matter for them)
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 u1 = unchecked((short)a);
byte c2 = checked((byte)a);
byte u2 = unchecked((byte)a);
short num = checked((short)a);
short num2 = (short)a;
byte b = checked((byte)a);
byte b2 = (byte)a;
return num * num2 * b * b2;
}
public void ForWithCheckedIteratorAndUncheckedBody(int n)
{
checked {
for (int i = n + 1; i < n + 1; i++) {
unchecked {
n = i * i;
}
n = unchecked(i * i);
}
}
}

17
ICSharpCode.Decompiler/Tests/DelegateConstruction.cs

@ -46,7 +46,11 @@ public static class DelegateConstruction @@ -46,7 +46,11 @@ public static class DelegateConstruction
for (int i = 0; i < 10; i++)
{
int counter;
list.Add(x => counter = x);
list.Add(delegate(int x)
{
counter = x;
}
);
}
return list;
}
@ -57,13 +61,20 @@ public static class DelegateConstruction @@ -57,13 +61,20 @@ public static class DelegateConstruction
int counter;
for (int i = 0; i < 10; i++)
{
list.Add(x => counter = x);
list.Add(delegate(int x)
{
counter = x;
}
);
}
return list;
}
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 @@ -7,42 +7,60 @@ public class ExceptionHandling
{
public void MethodEndingWithEndFinally()
{
try {
try
{
throw null;
} finally {
}
finally
{
Console.WriteLine();
}
}
public void MethodEndingWithRethrow()
{
try {
try
{
throw null;
} catch {
}
catch
{
throw;
}
}
public void TryCatchFinally()
{
try {
try
{
Console.WriteLine("Try");
} catch (Exception ex) {
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} finally {
}
finally
{
Console.WriteLine("Finally");
}
}
public void TryCatchMultipleHandlers()
{
try {
try
{
Console.WriteLine("Try");
} catch (InvalidOperationException ex) {
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
} catch (Exception ex) {
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} catch {
}
catch
{
Console.WriteLine("other");
}
}

4
ICSharpCode.Decompiler/Tests/Generics.cs

@ -23,7 +23,7 @@ public static class Generics @@ -23,7 +23,7 @@ public static class Generics
public void Size(int capacity)
{
Array.Resize(ref this.arr, capacity);
Array.Resize<T>(ref this.arr, capacity);
}
public void Grow(int capacity)
@ -43,7 +43,7 @@ public static class Generics @@ -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
return d.Keys.GetEnumerator();

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

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

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

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

16
ICSharpCode.Decompiler/Tests/Loops.cs

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

20
ICSharpCode.Decompiler/Tests/MultidimensionalArray.cs

@ -1,8 +1,6 @@ @@ -1,8 +1,6 @@
// 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)
using System;
public static class MultidimensionalArray
{
internal class Generic<T, S> where T : new()
@ -12,16 +10,22 @@ public static class MultidimensionalArray @@ -12,16 +10,22 @@ public static class MultidimensionalArray
public T this[int i, int j]
{
get { return a[i, j]; }
set { a[i, j] = value; }
get
{
return this.a[i, j];
}
set
{
this.a[i, j] = value;
}
}
public void TestB(S x, ref S y)
{
b[5, 3] = new S[10];
b[5, 3][0] = default(S);
b[5, 3][1] = x;
b[5, 3][2] = y;
this.b[5, 3] = new S[10];
this.b[5, 3][0] = default(S);
this.b[5, 3][1] = x;
this.b[5, 3][2] = y;
}
}

63
ICSharpCode.Decompiler/Tests/PropertiesAndEvents.cs

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

88
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -6,25 +6,88 @@ using System.CodeDom.Compiler; @@ -6,25 +6,88 @@ using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ICSharpCode.Decompiler.Ast;
using Microsoft.CSharp;
using Mono.Cecil;
using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests
{
[TestFixture]
public class TestRunner
{
public static void Main(string[] args)
[Test, Ignore("unncessary primitive casts")]
public void CheckedUnchecked()
{
if (args.Length == 1)
TestFile(args[0]);
else
TestFile(@"..\..\Tests\DelegateConstruction.cs");
Console.ReadKey();
TestFile(@"..\..\Tests\CheckedUnchecked.cs");
}
[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)
{
string code = File.ReadAllText(fileName);
@ -49,19 +112,19 @@ namespace ICSharpCode.Decompiler.Tests @@ -49,19 +112,19 @@ namespace ICSharpCode.Decompiler.Tests
string line1, line2;
while ((line1 = r1.ReadLine()) != null) {
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);
continue;
}
line2 = r2.ReadLine();
while (line2 != null && (line2.StartsWith("using ", StringComparison.Ordinal) || line2.Trim().Length == 0))
while (line2 != null && string.IsNullOrWhiteSpace(line2))
line2 = r2.ReadLine();
if (line2 == null) {
ok = false;
diff.WriteLine("-" + line1);
continue;
}
if (line1 != line2) {
if (line1.Trim() != line2.Trim()) {
ok = false;
if (numberOfContinuousMistakes++ > 5)
return false;
@ -84,6 +147,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -84,6 +147,7 @@ namespace ICSharpCode.Decompiler.Tests
{
CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
CompilerParameters options = new CompilerParameters();
options.CompilerOptions = "/unsafe";
options.ReferencedAssemblies.Add("System.Core.dll");
CompilerResults results = provider.CompileAssemblyFromSource(options, code);
try {

62
ICSharpCode.Decompiler/Tests/UnsafeCode.cs

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

57
ICSharpCode.Decompiler/Tests/ValueTypes.cs

@ -22,70 +22,71 @@ public static class ValueTypes @@ -22,70 +22,71 @@ public static class ValueTypes
public void MethodCalls()
{
this.SetField();
Test(this);
Test(ref this);
ValueTypes.S.Test(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);
return s;
ValueTypes.S result = default(ValueTypes.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);
return s;
ValueTypes.S result = new ValueTypes.S(10);
return result;
}
public static S Copy1(S p)
public static ValueTypes.S Copy1(ValueTypes.S p)
{
return p;
}
public static S Copy2(ref S p)
public static ValueTypes.S Copy2(ref ValueTypes.S 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;
}
public static void Copy4(ref S p, out S o)
public static void Copy4(ref ValueTypes.S p, out ValueTypes.S o)
{
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
Copy4(ref p, out o);
ValueTypes.Copy4(ref p, out o);
}
public static void Issue56(int i, out string str)
@ -94,20 +95,20 @@ public static class ValueTypes @@ -94,20 +95,20 @@ public static class ValueTypes
str += i.ToString();
}
public static void CopyAroundAndModifyField(S s)
public static void CopyAroundAndModifyField(ValueTypes.S s)
{
S locS = s;
locS.Field += 10;
s = locS;
ValueTypes.S s2 = s;
s2.Field += 10;
s = s2;
}
static int[] MakeArray()
private static int[] MakeArray()
{
return null;
}
public static void IncrementArrayLocation()
{
MakeArray()[Environment.TickCount]++;
ValueTypes.MakeArray()[Environment.TickCount]++;
}
}

Loading…
Cancel
Save