Skip to content

Commit

Permalink
Add more VB operator tests (dotnet/corefx#31320)
Browse files Browse the repository at this point in the history
* Add VB operator tests

* Delete dead code from VB operators

* Add comparison operator tests

* Add more tests

* Fix VB handling of DBNull.Value

* Do all number parsing in the invariant culture

* Workaround https://github.com/dotnet/coreclr/issues/8648


Commit migrated from dotnet/corefx@7c745fb
  • Loading branch information
hughbe authored and danmoseley committed Sep 24, 2018
1 parent d4f0f1e commit 9a4895c
Show file tree
Hide file tree
Showing 4 changed files with 7,158 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Globalization
Imports System.Reflection
Imports System.Runtime.CompilerServices

Imports Microsoft.VisualBasic.CompilerServices.ExceptionUtils
Imports Microsoft.VisualBasic.CompilerServices.Symbols
Expand Down Expand Up @@ -887,13 +888,7 @@ Namespace Microsoft.VisualBasic.CompilerServices

Public Shared Function PlusObject(ByVal Operand As Object) As Object

If Operand Is Nothing Then
Return Boxed_ZeroInteger
End If

Dim typ As TypeCode = GetTypeCode(Operand)


Select Case typ

Case TypeCode.Empty
Expand Down Expand Up @@ -955,13 +950,6 @@ Namespace Microsoft.VisualBasic.CompilerServices
Public Shared Function NegateObject(ByVal Operand As Object) As Object

Dim tc As TypeCode = GetTypeCode(Operand)

If Operand Is Nothing Then
tc = TypeCode.Empty
Else
tc = Operand.GetType.GetTypeCode
End If

Select Case tc

Case TypeCode.Empty
Expand Down Expand Up @@ -1123,13 +1111,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
End Function

Private Shared Function NegateDecimal(ByVal operand As Decimal) As Object
'Using try/catch instead of check with MinValue since the overflow case should be very rare
'and a compare would be a big cost for the normal case.
Try
Return -operand
Catch ex As OverflowException
Return -CDbl(operand)
End Try
Return -operand
End Function

Private Shared Function NegateSingle(ByVal operand As Single) As Object
Expand Down Expand Up @@ -2498,7 +2480,8 @@ Namespace Microsoft.VisualBasic.CompilerServices
Case TypeCode.Empty * s_TCMAX + TypeCode.Decimal,
TypeCode.Empty * s_TCMAX + TypeCode.Single,
TypeCode.Empty * s_TCMAX + TypeCode.Double,
TypeCode.Empty * s_TCMAX + TypeCode.String
TypeCode.Empty * s_TCMAX + TypeCode.String,
TypeCode.DBNull * s_TCMAX + TypeCode.String

Return Right

Expand Down Expand Up @@ -2760,7 +2743,8 @@ Namespace Microsoft.VisualBasic.CompilerServices
Case TypeCode.Decimal * s_TCMAX + TypeCode.Empty,
TypeCode.Single * s_TCMAX + TypeCode.Empty,
TypeCode.Double * s_TCMAX + TypeCode.Empty,
TypeCode.String * s_TCMAX + TypeCode.Empty
TypeCode.String * s_TCMAX + TypeCode.Empty,
TypeCode.String * s_TCMAX + TypeCode.DBNull

Return Left

Expand Down Expand Up @@ -4601,6 +4585,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
Throw GetNoValidOperatorException(UserDefinedOperator.Modulus, Left, Right)
End Function

<MethodImpl(MethodImplOptions.NoInlining)> ' To work around https://github.com/dotnet/coreclr/issues/8648
Private Shared Function ModSByte(ByVal left As SByte, ByVal right As SByte) As Object
Return left Mod right
End Function
Expand All @@ -4610,27 +4595,18 @@ Namespace Microsoft.VisualBasic.CompilerServices
End Function

Private Shared Function ModInt16(ByVal left As Int16, ByVal right As Int16) As Object
Dim result As Integer = CInt(left) Mod CInt(right)

If result < Int16.MinValue OrElse result > Int16.MaxValue Then
Return result
Else
Return CShort(result)
End If
Return left Mod right
End Function

Private Shared Function ModUInt16(ByVal left As UInt16, ByVal right As UInt16) As Object
Return left Mod right
End Function

Private Shared Function ModInt32(ByVal left As Integer, ByVal right As Integer) As Object
'Do operation with Int64 to avoid OverflowException with Int32.MinValue and -1
Dim result As Long = CLng(left) Mod CLng(right)

If result < Int32.MinValue OrElse result > Int32.MaxValue Then
Return result
If left = Integer.MinValue AndAlso right = -1 Then
Return 0
Else
Return CInt(result)
Return left Mod right
End If
End Function

Expand Down Expand Up @@ -5131,9 +5107,30 @@ Namespace Microsoft.VisualBasic.CompilerServices
#Region " Operator Concatenate & "

Public Shared Function ConcatenateObject(ByVal Left As Object, ByVal Right As Object) As Object
Dim conv1, conv2 As IConvertible
Dim tc1, tc2 As TypeCode

Dim tc1 As TypeCode = GetTypeCode(Left)
Dim tc2 As TypeCode = GetTypeCode(Right)
conv1 = TryCast(Left, IConvertible)
If conv1 Is Nothing Then
If Left Is Nothing Then
tc1 = TypeCode.Empty
Else
tc1 = TypeCode.Object
End If
Else
tc1 = conv1.GetTypeCode()
End If

conv2 = TryCast(Right, IConvertible)
If conv2 Is Nothing Then
If Right Is Nothing Then
tc2 = TypeCode.Empty
Else
tc2 = TypeCode.Object
End If
Else
tc2 = conv2.GetTypeCode()
End If

'Special cases for Char()
If (tc1 = TypeCode.Object) AndAlso (TypeOf Left Is Char()) Then
Expand All @@ -5148,6 +5145,17 @@ Namespace Microsoft.VisualBasic.CompilerServices
Return InvokeUserDefinedOperator(UserDefinedOperator.Concatenate, Left, Right)
End If

Dim LeftIsNull As Boolean = (tc1 = TypeCode.DBNull)
Dim RightIsNull As Boolean = (tc2 = TypeCode.DBNull)

If LeftIsNull And RightIsNull Then
Return Left
ElseIf LeftIsNull And Not RightIsNull Then
Left = ""
ElseIf RightIsNull And Not LeftIsNull Then
Right = ""
End If

Return CStr(Left) & CStr(Right)
End Function

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
<Compile Include="Microsoft/VisualBasic/CompilerServices/StandardModuleAttributeTests.cs" />
<Compile Include="Microsoft/VisualBasic/Devices/NetworkAvailableEventArgsTests.cs" />
<Compile Include="ConversionsTests.cs" />
<Compile Include="OperatorsTests.cs" />
<Compile Include="DateAndTimeTests.cs" />
<Compile Include="OperatorsTests.cs" />
<Compile Include="OperatorsTests.Comparison.cs" />
<Compile Include="InformationTests.cs" />
<Compile Include="UtilsTests.cs" />
<Compile Include="VBMathTests.cs" />
Expand Down
Loading

0 comments on commit 9a4895c

Please sign in to comment.