QuickCheck のforAll関数を使用する必要があります。次のタイプがあります。
forAll :: (Show a, Testable prop)
=> Gen a -- ^ The generator to use for generating values
-> (a -> prop) -- ^ A function which returns a testable property
-> Property
forAll
次の 2 つの引数を取ります。
- ジェネレーターは、値を生成する方法を記述します。ジェネレーターの例は、choose、任意、oneofなどです。
- この関数は、指定された入力のプロパティをテストします。のインスタンスである値を返す必要があります。
Testable
たとえば、別Property
のBool
や関数です。
choose および elements ジェネレーターを使用したネストされた forAll の例:
-- This generates a Property p for all x's in the closed interval [1,3]
-- The property p in turn generates a property q for all y ∈ [4,5]
-- The property q is True if x < y.
prop_choose = forAll (choose (1,3)) $ \x ->
forAll (elements [4,5]) $ \y -> x < y
テスト プロパティでは、2 番目と 3 番目の引数に choose を指定して forAll を使用できます。最初の引数には、Positive a
タイプ a の任意の正の値を生成するために使用できる QuickCheck のタイプがあります (a が Num の場合、Arbitrary インスタンスがあります)。
prop_alwayLessThanMaxIdx :: Positive Integer -> Property
prop_alwaysLessThanMaxIdx (Positive idx) =
forAll (choose (0,1)) $ \r1 ->
forAll (choose (0,1)) $ \r2 ->
(rndListIndex idx r1 r2) < idx