我有几个功能:
func x1(_ data: [Double], _ par1: Int, _ par2: Int) -> Double {
…
}
func x2(_ data: [Double], _ par1: Int, _ par2: Int, _ par3: Int) -> Double {
…
}
我希望将[-1 … 100]
作为par1, par2, …
进行测试,因此我必须致电:
x1(data, -1, -1)
x1(data, -1, 0)
...
x1(data, -1, 100)
x1(data, 0, -1)
…
x1(data, 100, 100)
与
相同func x2(data, -1, -1, -1) … x2(data, 100, 100, 100)
然后比较结果以找到x1
或x2
中的最大值。我如何编写一个func测试来调用另一个具有未知参数量的函数(x1有3,x2有4),我该如何调用这个测试函数?
func test(testfunc: (_ data: [Double], _ par: Int…) -> Double, _ data: [Double], params: [Int]…) -> [Int] {
//?
return *optimal parameters , for example: [10, 57]
}
答案 0 :(得分:1)
正如@Rob Napier所说,在测试中实现类型安全的唯一方法是为每个select b.ID,
a.senderid_final,
a.receiverid_final,
b.Text
from
(
select a.receiverid as a_receiverid,
a.senderid as a_senderid ,
b.receiverid as b_receiverid,
b.senderid as b_senderid,
case when max(a.id) > max (b.id) then a.receiverid else b.receiverid end as receiverid_final,
case when max(a.id) > max (b.id) then a.senderid else b.senderid end as senderid_final
from my_tables as a
inner join my_table as b
on a.receiverid = b.senderid
and b.receiverid = a.senderid
group by a.receiverid, a.senderid, b.receiverid, b.senderid
) as a
inner join my_tables as b
on a.receiverid_final = b.receiverid
and b.senderid = a.senderid_final
Order by b.id desc
函数设置一个测试函数。但是,如果您可以更改x*
函数,则可以使用 更简单的方法:声明一个单 x*
函数,该函数采用变量参数数量:
x()
现在你可以用任何个参数来调用它,所以:
func x(_ data: [Double], _ pars: Int...) -> Double {
// Loop through the variable number of pars like this:
for index in pars { ... }
}
您的测试功能同样简单:
let maxOf2 = x(data, -1, -1)
let maxOf7 = x(data, 100, 100, 100, 100, 100, 100, 100)
使用func test(functionToTest: ([Double], Int...) -> (Double), data: [Double], pars: Int...) -> [Double] {
return functionToTest(data, pars)
}
:
typealias
答案 1 :(得分:-1)
我还没有完全理解你的问题,但也许这暗示你的代码太复杂了。尝试将其分成几个任务并单独测试每个任务。
基于协议的面向对象方法可能如下所示:
protocol OODouble {
var value: Double { get }
}
final class ThreeParamDouble: OODouble {
private let data: [Double]
private let par1: Int
private let par2: Int
init(_ data: [Double], _ par1: Int, _ par2: Int) {
self.data = data
self.par1 = par1
self.par2 = par2
}
var value: Double {
get { /* calculate result (aka x1) here */ }
}
}
final class FourParamDouble: OODouble {
private let data: [Double]
private let par1: Int
private let par2: Int
private let par3: Int
init(_ data: [Double], _ par1: Int, _ par2: Int, _ par3: Int) {
self.data = data
self.par1 = par1
self.par2 = par2
self.par3 = par3
}
var value: Double {
get { /* calculate result (aka x2) here */ }
}
}
final class LargestDouble: OODouble {
private let doubles: [OODouble]
init(_ doubles: [OODouble]) {
self.doubles = doubles
}
var value: Double {
get {
var largestDouble: Double = 0
(doubles.map { $0.value }).forEach {
largestDouble = largestDouble < $0 ? $0 : largestDouble
}
return largestDouble
}
}
}
let largestValue = LargestDouble([
FourParamDouble(data, -1, -1, -1),
...
FourParamDouble(data, 100, 100, 100)
])
let result = largestValue.value
现在,您可以独立于计算x2测试x1的计算,甚至可以独立测试“找到最大的一个”。最后一个可以用伪协议实现来测试:
final class OODoubleFake: OODouble {
init(_ value: Double) {
self.value = value
}
let value: Double
}