You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
193 lines
9.5 KiB
193 lines
9.5 KiB
// Copyright (c) 2010 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; |
|
using ICSharpCode.NRefactory.TypeSystem; |
|
using NUnit.Framework; |
|
|
|
namespace ICSharpCode.NRefactory.CSharp.Resolver |
|
{ |
|
// assign short name to the fake reflection type |
|
using dynamic = ICSharpCode.NRefactory.TypeSystem.ReflectionHelper.Dynamic; |
|
|
|
[TestFixture] |
|
public unsafe class UnaryOperatorTests : ResolverTestBase |
|
{ |
|
[Test] |
|
public void TestAddressOf() |
|
{ |
|
TestOperator(UnaryOperatorType.AddressOf, MakeResult(typeof(int)), |
|
Conversion.IdentityConversion, typeof(int*)); |
|
|
|
TestOperator(UnaryOperatorType.AddressOf, MakeResult(typeof(byte*)), |
|
Conversion.IdentityConversion, typeof(byte**)); |
|
|
|
TestOperator(UnaryOperatorType.AddressOf, MakeResult(typeof(dynamic)), |
|
Conversion.IdentityConversion, typeof(dynamic)); |
|
} |
|
|
|
[Test] |
|
public void TestDereference() |
|
{ |
|
TestOperator(UnaryOperatorType.Dereference, MakeResult(typeof(int*)), |
|
Conversion.IdentityConversion, typeof(int)); |
|
|
|
TestOperator(UnaryOperatorType.Dereference, MakeResult(typeof(long**)), |
|
Conversion.IdentityConversion, typeof(long*)); |
|
|
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Dereference, MakeResult(typeof(int))).IsError); |
|
|
|
TestOperator(UnaryOperatorType.Dereference, MakeResult(typeof(dynamic)), |
|
Conversion.IdentityConversion, typeof(dynamic)); |
|
} |
|
|
|
[Test] |
|
public void TestIncrementDecrement() |
|
{ |
|
TestOperator(UnaryOperatorType.Increment, MakeResult(typeof(byte)), |
|
Conversion.IdentityConversion, typeof(byte)); |
|
|
|
TestOperator(UnaryOperatorType.Decrement, MakeResult(typeof(ulong)), |
|
Conversion.IdentityConversion, typeof(ulong)); |
|
|
|
TestOperator(UnaryOperatorType.PostDecrement, MakeResult(typeof(short?)), |
|
Conversion.IdentityConversion, typeof(short?)); |
|
|
|
TestOperator(UnaryOperatorType.PostIncrement, MakeResult(typeof(TypeCode)), |
|
Conversion.IdentityConversion, typeof(TypeCode)); |
|
|
|
TestOperator(UnaryOperatorType.PostIncrement, MakeResult(typeof(TypeCode?)), |
|
Conversion.IdentityConversion, typeof(TypeCode?)); |
|
|
|
TestOperator(UnaryOperatorType.PostIncrement, MakeResult(typeof(dynamic)), |
|
Conversion.IdentityConversion, typeof(dynamic)); |
|
|
|
AssertError(typeof(object), resolver.ResolveUnaryOperator(UnaryOperatorType.Increment, MakeResult(typeof(object)))); |
|
|
|
TestOperator(UnaryOperatorType.Increment, MakeResult(typeof(int*)), |
|
Conversion.IdentityConversion, typeof(int*)); |
|
|
|
TestOperator(UnaryOperatorType.PostDecrement, MakeResult(typeof(uint*)), |
|
Conversion.IdentityConversion, typeof(uint*)); |
|
} |
|
|
|
[Test] |
|
public void TestUnaryPlus() |
|
{ |
|
AssertConstant(1, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant((sbyte)1))); |
|
AssertConstant(1, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant((byte)1))); |
|
AssertConstant(1, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant((short)1))); |
|
AssertConstant(1, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant((ushort)1))); |
|
AssertConstant(65, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant('A'))); |
|
AssertConstant(1, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant(1))); |
|
AssertConstant((uint)1, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant((uint)1))); |
|
AssertConstant(1L, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant((long)1))); |
|
AssertConstant((ulong)1, resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant((ulong)1))); |
|
|
|
TestOperator(UnaryOperatorType.Plus, MakeResult(typeof(dynamic)), |
|
Conversion.IdentityConversion, typeof(dynamic)); |
|
|
|
TestOperator(UnaryOperatorType.Plus, MakeResult(typeof(ushort?)), |
|
Conversion.ImplicitNullableConversion, typeof(int?)); |
|
} |
|
|
|
[Test] |
|
public void TestUnaryMinus() |
|
{ |
|
AssertConstant(-1, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(1))); |
|
AssertConstant(-1L, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant((uint)1))); |
|
AssertConstant(-2147483648L, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(2147483648))); |
|
AssertConstant(-1.0f, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(1.0f))); |
|
AssertConstant(-1.0, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(1.0))); |
|
AssertConstant(1m, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(-1m))); |
|
AssertConstant(-65, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant('A'))); |
|
|
|
TestOperator(UnaryOperatorType.Minus, MakeResult(typeof(dynamic)), |
|
Conversion.IdentityConversion, typeof(dynamic)); |
|
|
|
TestOperator(UnaryOperatorType.Minus, MakeResult(typeof(uint?)), |
|
Conversion.ImplicitNullableConversion, typeof(long?)); |
|
} |
|
|
|
[Test] |
|
public void TestUnaryMinusUncheckedOverflow() |
|
{ |
|
AssertConstant(-2147483648, resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(-2147483648))); |
|
} |
|
|
|
[Test] |
|
public void TestUnaryMinusCheckedOverflow() |
|
{ |
|
resolver.CheckForOverflow = true; |
|
AssertError(typeof(int), resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(-2147483648))); |
|
} |
|
|
|
[Test] |
|
public void TestBitwiseNot() |
|
{ |
|
AssertConstant(1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant(-2))); |
|
AssertConstant(~'A', resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant('A'))); |
|
AssertConstant(~(sbyte)1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant((sbyte)1))); |
|
AssertConstant(~(byte)1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant((byte)1))); |
|
AssertConstant(~(short)1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant((short)1))); |
|
AssertConstant(~(ushort)1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant((ushort)1))); |
|
AssertConstant(~(uint)1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant((uint)1))); |
|
AssertConstant(~(long)1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant((long)1))); |
|
AssertConstant(~(ulong)1, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant((ulong)1))); |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant(1.0)).IsError); |
|
|
|
TestOperator(UnaryOperatorType.BitNot, MakeResult(typeof(dynamic)), |
|
Conversion.IdentityConversion, typeof(dynamic)); |
|
|
|
TestOperator(UnaryOperatorType.BitNot, MakeResult(typeof(uint)), |
|
Conversion.IdentityConversion, typeof(uint)); |
|
|
|
TestOperator(UnaryOperatorType.BitNot, MakeResult(typeof(sbyte)), |
|
Conversion.ImplicitNumericConversion, typeof(int)); |
|
|
|
TestOperator(UnaryOperatorType.BitNot, MakeResult(typeof(ushort?)), |
|
Conversion.ImplicitNullableConversion, typeof(int?)); |
|
} |
|
|
|
[Test] |
|
public void TestLogicalNot() |
|
{ |
|
AssertConstant(true, resolver.ResolveUnaryOperator(UnaryOperatorType.Not, MakeConstant(false))); |
|
AssertConstant(false, resolver.ResolveUnaryOperator(UnaryOperatorType.Not, MakeConstant(true))); |
|
|
|
TestOperator(UnaryOperatorType.Not, MakeResult(typeof(dynamic)), |
|
Conversion.IdentityConversion, typeof(dynamic)); |
|
|
|
TestOperator(UnaryOperatorType.Not, MakeResult(typeof(bool)), |
|
Conversion.IdentityConversion, typeof(bool)); |
|
|
|
TestOperator(UnaryOperatorType.Not, MakeResult(typeof(bool?)), |
|
Conversion.IdentityConversion, typeof(bool?)); |
|
} |
|
|
|
[Test] |
|
public void TestInvalidUnaryOperatorsOnEnum() |
|
{ |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Not, MakeConstant(StringComparison.Ordinal)).IsError); |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeConstant(StringComparison.Ordinal)).IsError); |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeConstant(StringComparison.Ordinal)).IsError); |
|
|
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Not, MakeResult(typeof(StringComparison))).IsError); |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeResult(typeof(StringComparison))).IsError); |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeResult(typeof(StringComparison))).IsError); |
|
|
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Not, MakeResult(typeof(StringComparison?))).IsError); |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Plus, MakeResult(typeof(StringComparison?))).IsError); |
|
Assert.IsTrue(resolver.ResolveUnaryOperator(UnaryOperatorType.Minus, MakeResult(typeof(StringComparison?))).IsError); |
|
} |
|
|
|
[Test] |
|
public void TestBitwiseNotOnEnum() |
|
{ |
|
AssertConstant(~StringComparison.Ordinal, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant(StringComparison.Ordinal))); |
|
AssertConstant(~StringComparison.CurrentCultureIgnoreCase, resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeConstant(StringComparison.CurrentCultureIgnoreCase))); |
|
AssertType(typeof(StringComparison), resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeResult(typeof(StringComparison)))); |
|
AssertType(typeof(StringComparison?), resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeResult(typeof(StringComparison?)))); |
|
} |
|
} |
|
}
|
|
|