Exemple #1
0
namespace boost { namespace hana { namespace test {
    template <typename G>
    auto laws<Group, G> = [] {
        static_assert(models<Group(G)>{}, "");

        laws<Monoid, G>();

        // Instance laws
        {
            for_each(objects<G>, [](auto x) {
                BOOST_HANA_CHECK(equal(
                    plus(x, negate(x)),
                    zero<G>()
                ));

                BOOST_HANA_CHECK(equal(
                    plus(negate(x), x),
                    zero<G>()
                ));
            });
        }

        // minus
        {
            for_each(objects<G>, [](auto x) {
                for_each(objects<G>, [=](auto y) {
                    BOOST_HANA_CHECK(equal(
                        minus(x, y),
                        plus(x, negate(y))
                    ));

                    BOOST_HANA_CHECK(equal(
                        minus(y, x),
                        plus(y, negate(x))
                    ));
                });
            });
        }

        // negate
        {
            for_each(objects<G>, [](auto x) {
                BOOST_HANA_CHECK(equal(
                    negate(negate(x)),
                    x
                ));
            });
        }
    };
}}} // end namespace boost::hana::test
Exemple #2
0
namespace boost { namespace hana { namespace test {
    template <typename E>
    auto laws<Enumerable, E> = [] {
        static_assert(models<Enumerable(E)>{}, "");

        for_each(objects<E>, [](auto x) {
            BOOST_HANA_CHECK(equal(
                succ(pred(x)),
                x
            ));

            BOOST_HANA_CHECK(equal(
                pred(succ(x)),
                x
            ));
        });
    };
}}} // end namespace boost::hana::test
Exemple #3
0
namespace boost { namespace hana { namespace test {
    template <typename F>
    auto laws<Functor, F> = [] {
        static_assert(models<Functor(F)>{}, "");

        auto f = injection([]{});
        auto g = injection([]{});
        auto v = injection([]{})();
        auto pred = always(true_);

        for_each(objects<F>, [=](auto xs) {
            BOOST_HANA_CHECK(
                equal(transform(xs, id), xs)
            );

            BOOST_HANA_CHECK(equal(
                transform(xs, compose(f, g)),
                transform(transform(xs, g), f)
            ));

            BOOST_HANA_CHECK(equal(
                adjust(xs, pred, f),
                transform(xs, [=](auto x) {
                    return eval_if(pred(x),
                        [=](auto _) { return _(f)(x); },
                        [=](auto) { return x; }
                    );
                })
            ));

            BOOST_HANA_CHECK(equal(
                replace(xs, pred, v),
                adjust(xs, pred, always(v))
            ));

            BOOST_HANA_CHECK(equal(
                fill(xs, v),
                replace(xs, always(true_), v)
            ));
        });
    };
}}} // end namespace boost::hana::test
Exemple #4
0
namespace boost { namespace hana { namespace test {
    template <typename T>
    auto laws<Foldable, T> = [] {
        static_assert(models<Foldable(T)>{}, "");

        auto f = injection([]{});
        auto s = injection([]{})();

        for_each(objects<T>, [=](auto xs) {
            BOOST_HANA_CHECK(equal(
                foldl(xs, s, f),
                foldl(to<Tuple>(xs), s, f)
            ));

            BOOST_HANA_CHECK(equal(
                foldr(xs, s, f),
                foldr(to<Tuple>(xs), s, f)
            ));
        });
    };
}}} // end namespace boost::hana::test