它不起作用。这里有 3 个失败的测试用例。事实上,函数接口并没有任何失败的返回结果。
我写了一个更正的版本,GetNextValidSize2。由于无法返回失败消息,因此我会针对这些情况抛出异常。以下是运行结果:
test1 : GetNextValidSize 失败
test1 : GetNextValidSize2 通过
test2 : GetNextValidSize 对象引用未设置为对象的实例。
test2 : GetNextValidSize2 validSizes 什么都不是
test3 : GetNextValidSize 已通过
test3 : GetNextValidSize2 validSizes 中没有项目
顺便说一句,LINQ 可能更简单或更容易,但它几乎不可能更有效。如果查询优化器/CLR 优化器运行良好,它可能同样有效。
这是代码 - 它在 VB 中,因为这是我目前正在使用的,不想切换思维方式:
模块模块1
''' <summary>
''' Error - does not work if validSizes is Nothing, or has 0 elements, or if
''' the list contains a validSize that is not the closest one before a closer one,
''' or there are no valid sizes.
''' </summary>
Public Function GetNextValidSize(ByVal size As Integer, ByVal validSizes As List(Of Integer)) As Integer
Dim returnValue As Integer = size
For i As Integer = 0 To validSizes.Count - 1 Step 1
If validSizes.Item(i) >= size Then
returnValue = validSizes.Item(i)
Exit For
End If
Next
Return returnValue
End Function
''' <summary>
''' Returns the closest item in validSizes that is >= size. Throws an exception if one cannot
''' be found.
''' </summary>
Public Function GetNextValidSize2(ByVal size As Integer, ByVal validSizes As List(Of Integer)) As Integer
Dim closestValue As Integer = Integer.MaxValue
Dim found As Boolean = False
If validSizes Is Nothing Then
Throw New Exception("validSizes is nothing")
End If
If validSizes.Count = 0 Then
Throw New Exception("No items in validSizes")
End If
For Each x In validSizes
If x >= size Then
found = True
If x < closestValue Then
closestValue = x
End If
End If
Next
If Not found Then
Throw New Exception("No items found")
End If
Return closestValue
End Function
''' <summary>
''' Output the result of a test.
''' </summary>
Public Sub outputResult(ByVal testName As String, ByVal result As Boolean, ByVal funcName As String)
Dim passFail As String
If result Then
passFail = " passed"
Else
passFail = " failed"
End If
Console.WriteLine(testName & " : " & funcName & passFail)
End Sub
''' <summary>
''' Output the result of a test where an exception occurred.
''' </summary>
Public Sub outputResult(ByVal testName As String, ByVal ex As Exception, ByVal funcName As String)
Console.WriteLine(testName & " : " & funcName & " " & ex.Message())
End Sub
''' <summary>
''' Test with a list of 3 integers
''' </summary>
Public Sub test1()
Dim aList As New List(Of Integer)
aList.Add(5)
aList.Add(4)
aList.Add(3)
Dim result = GetNextValidSize(3, aList)
outputResult("test1", 3 = GetNextValidSize(3, aList), "GetNextValidSize")
outputResult("test1", 3 = GetNextValidSize2(3, aList), "GetNextValidSize2")
End Sub
''' <summary>
''' Test with a null reference
''' </summary>
Public Sub test2()
Dim aList = Nothing
Try
outputResult("test2", GetNextValidSize(3, aList), "GetNextValidSize")
Catch ex As Exception
outputResult("test2", ex, "GetNextValidSize")
End Try
Try
outputResult("test2", GetNextValidSize2(3, aList), "GetNextValidSize2")
Catch ex As Exception
outputResult("test2", ex, "GetNextValidSize2")
End Try
End Sub
''' <summary>
''' Test with an empty array.
''' </summary>
Public Sub test3()
Dim aList As New List(Of Integer)
Try
outputResult("test3", GetNextValidSize(3, aList), "GetNextValidSize")
Catch ex As Exception
outputResult("test3", ex, "GetNextValidSize")
End Try
Try
outputResult("test3", GetNextValidSize2(3, aList), "GetNextValidSize2")
Catch ex As Exception
outputResult("test3", ex, "GetNextValidSize2")
End Try
End Sub
''' <summary>
''' Run all tests.
''' </summary>
Public Sub testAll()
test1()
test2()
test3()
End Sub
Sub Main()
testAll()
Console.ReadLine()
End Sub
结束模块