2014-07-16 54 views
0

我有相同功能合同的多個實現。有些是幼稚和直接的,有些則更加複雜和優化。我想通過使用PropSpec從輸入域隨機選取的點運行它們。冗餘和投票屬性檢查

的問題是如何運行的所有冗餘的實現和比較輸出成對。如果計算值因實現而異,則應將測試標記爲失敗。如果有兩個以上的實現,應該可以根據投票決定哪一個失敗,如TMR系統

回答

0

您的測試方法必須編排調用所有單個實現,然後進行投票。這是確保每個實現都以相同的輸入進行測試並確保輸出與其他所有輸出進行比較的唯一方法。

0

做一個發電機爲您的功能實現,並讓ScalaCheck隨機既實現和輸入。事情是這樣的概念代碼:

type Input = ... 
type Output = ... 

trait Algorithm { 
    def apply(input: Input): Output 
} 

val funA: Algorithm = ... 
val funB: Algorithm = ... 
val funC: Algorithm = ... 
val funD: Algorithm = ... 

import org.scalacheck._ 
import Prop.BooleanOperators // for the ==> operator 

genInput: Gen[Input] = ... 

genAlgorithm: Gen[Algorithm] = Gen.oneOf(funA, funB, funC, funD) 

propAlgorithm = Prop.forAll(genAlgorithm, genAlgorithm, genInput) { 
    case (f, g, x) => (f != g) ==> f(x) == g(x) 
} 

爲了使錯誤報告的幫助,你應該也有一個合理的toString方法上Algorithm

+0

建議的方法存在一些不便之處。它沒有針對每個實現的單獨報告。當測試失敗時,我只知道其中一個與另一個不同步,並且我不知道哪一個知道。這種方法存在更大的問題:如果測試用例失敗並出現異常,或者用無限循環超時,所有測試用例都會中止。 – ayvango