QuickCheck++ 自明な入力データ

入力データによっては、テストするまでもなく結果が自明である場合があります。


たとえば、以下の「reverseを2回適用したら元に戻るかどうか」のテストの場合、
1要素しかないvectorの場合は、テストするまでもなく成功することがわかります。

#include <vector>
#include <algorithm>
#include <quickcheck/quickcheck.hh>

typedef std::vector<int> Vector;

struct prop_reverse : public quickcheck::Property<Vector> {
    bool holdsFor(const Vector& xs)
    {
        Vector ys = xs;
        std::reverse(ys.begin(), ys.end());
        std::reverse(ys.begin(), ys.end());
        return xs == ys;
    }
};

int main()
{
    prop_sorted_minimum().check();
}

こういった場合、quickcheck::Propertyを継承したクラスで
isTrivialFor()関数を定義することにより、入力データが自明なデータかどうかをチェックできます。

#include <vector>
#include <algorithm>
#include <quickcheck/quickcheck.hh>

typedef std::vector<int> Vector;

struct prop_reverse : public quickcheck::Property<Vector> {
    bool holdsFor(const Vector& xs)
    {
        Vector ys = xs;
        std::reverse(ys.begin(), ys.end());
        std::reverse(ys.begin(), ys.end());
        return xs == ys;
    }

    bool isTrivialFor(const Vector& xs)
    {
        return xs.size() < 2;
    }
};

int main()
{
    prop_reverse().check();
}

テストを実行すると以下のように出力され、自動生成されたテストデータに
自明なデータがどれくらい含まれていたかがわかります。
(テスト自体は行われます)

OK, passed 100 tests (4% trivial).