pfor/tests/mp/pack.cpp

151 lines
5.0 KiB
C++
Raw Normal View History

2021-05-10 16:11:23 +00:00
#include <catch.hpp>
#include "../common.h"
#include <pfor/mp/meta.h>
#include <pfor/mp/pack.h>
template<typename, typename> struct Comparator;
template<std::size_t A, std::size_t B>
struct Comparator<pfor::UIntToType<A>, pfor::UIntToType<B>> {
static constexpr bool value = A < B;
};
template<typename T, std::size_t I>
using TSBefore = pfor::impl::PackSortBefore<Comparator, T, pfor::UIntToType<I>>;
template<typename T, std::size_t I>
using TSAfter = pfor::impl::PackSortAfter<Comparator, T, pfor::UIntToType<I>>;
template<typename T, std::size_t I>
using TSInsert = pfor::PackSortInsert<Comparator, T, pfor::UIntToType<I>>;
template<typename T>
using TSort = pfor::PackSort<Comparator, T>;
TEST_CASE("Pack") {
using T1 = pfor::Pack<>;
using T2 = pfor::Pack<char>;
using T3 = pfor::Pack<char, char>;
using T4 = pfor::Pack<char, char, int>;
using T5 = pfor::Pack<char, int, char, float, int, char>;
using U3 = pfor::Pack<int, char>;
using U4 = pfor::Pack<char, int, char>;
using U5 = pfor::Pack<char, char, int, char>;
using M0 = pfor::Pack<>;
using M1 = pfor::Pack<char>;
using M2 = pfor::Pack<char, char, char>;
using M3 = pfor::Pack<char, int, char>;
using M4 = pfor::Pack<char, int, char, char, char, char, int, char>;
SECTION("PackGet") {
TEST(REQUIRE, is_same_v<pfor::PackGet<T2, 0>, char>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T3, 0>, char>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T3, 1>, char>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T4, 0>, char>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T4, 1>, char>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T4, 2>, int>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T5, 0>, char>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T5, 1>, int>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T5, 2>, char>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T5, 3>, float>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T5, 4>, int>);
TEST(REQUIRE, is_same_v<pfor::PackGet<T5, 5>, char>);
}
SECTION("PackAppend") {
TEST(REQUIRE, is_same_v<pfor::PackAppend<T1, char>, T2>);
TEST(REQUIRE, is_same_v<pfor::PackAppend<T2, char>, T3>);
TEST(REQUIRE, is_same_v<pfor::PackAppend<T3, int>, T4>);
}
SECTION("PackPrepend") {
TEST(REQUIRE, is_same_v<pfor::PackPrepend<T2, int>, U3>);
TEST(REQUIRE, is_same_v<pfor::PackPrepend<U3, char>, U4>);
TEST(REQUIRE, is_same_v<pfor::PackPrepend<U4, char>, U5>);
}
SECTION("PackRemove") {
TEST(REQUIRE, is_same_v<pfor::PackRemove<T4, int>, T3>);
TEST(REQUIRE, is_same_v<pfor::PackRemove<M4, char>, pfor::Pack<int, int>>);
TEST(REQUIRE, is_same_v<pfor::PackRemove<T5, double>, T5>);
}
SECTION("PackContains") {
TEST(REQUIRE_FALSE, pfor::packContains<T1, char>);
TEST(REQUIRE, pfor::packContains<T2, char>);
TEST(REQUIRE_FALSE, pfor::packContains<T2, int>);
TEST(REQUIRE, pfor::packContains<T3, char>);
TEST(REQUIRE_FALSE, pfor::packContains<T3, int>);
TEST(REQUIRE, pfor::packContains<T4, int>);
}
SECTION("PackUniq") {
using U1 = pfor::Pack<>;
using U2 = pfor::Pack<char>;
using U3 = pfor::Pack<char>;
using U4 = pfor::Pack<char, int>;
using U5 = pfor::Pack<char, int, float>;
TEST(REQUIRE, is_same_v<pfor::PackUniq<T1>, U1>);
TEST(REQUIRE, is_same_v<pfor::PackUniq<T2>, U2>);
TEST(REQUIRE, is_same_v<pfor::PackUniq<T3>, U3>);
TEST(REQUIRE, is_same_v<pfor::PackUniq<T4>, U4>);
TEST(REQUIRE, is_same_v<pfor::PackUniq<T5>, U5>);
}
SECTION("PackSize") {
TEST_IF(REQUIRE, ==, 0, pfor::packSize<T1>);
TEST_IF(REQUIRE, ==, 1, pfor::packSize<T2>);
TEST_IF(REQUIRE, ==, 2, pfor::packSize<T3>);
TEST_IF(REQUIRE, ==, 3, pfor::packSize<T4>);
TEST_IF(REQUIRE, ==, 6, pfor::packSize<T5>);
}
SECTION("PackMerge") {
TEST(REQUIRE, is_same_v<pfor::PackMerge<>, M0>);
TEST(REQUIRE, is_same_v<pfor::PackMerge<T1>, M0>);
TEST(REQUIRE, is_same_v<pfor::PackMerge<T2>, M1>);
TEST(REQUIRE, is_same_v<pfor::PackMerge<T2, T3>, M2>);
TEST(REQUIRE, is_same_v<pfor::PackMerge<T2, U3>, M3>);
TEST(REQUIRE, is_same_v<pfor::PackMerge<T2, U3, T1, T2, U5>, M4>);
}
SECTION("Pack sorting") {
using IT0 = UIntsToPack<>;
using IT1 = UIntsToPack<6, 4, 3, 8, 9, 2, 5, 3>;
using ST0 = IT0;
using ST1 = UIntsToPack<2, 3, 3, 4, 5, 6, 8, 9>;
using ST2 = UIntsToPack<2, 3, 3, 4, 5, 6, 7, 8, 9>;
using ST3 = UIntsToPack<2, 3, 3, 4, 5, 6, 7, 7, 8, 9>;
using TEmpty = pfor::Pack<>;
using BT1 = UIntsToPack<2, 3, 3, 4>;
using AT1 = UIntsToPack<5, 6, 8, 9>;
SECTION("PackSortBefore") {
TEST(REQUIRE, is_same_v<TSBefore<ST0, 5>, TEmpty>);
TEST(REQUIRE, is_same_v<TSBefore<ST1, 2>, TEmpty>);
TEST(REQUIRE, is_same_v<TSBefore<ST1, 5>, BT1>);
}
SECTION("PackSortAfter") {
TEST(REQUIRE, is_same_v<TSAfter<ST0, 5>, TEmpty>);
TEST(REQUIRE, is_same_v<TSAfter<ST1, 10>, TEmpty>);
TEST(REQUIRE, is_same_v<TSAfter<ST1, 5>, AT1>);
}
SECTION("PackSortInsert") {
TEST(REQUIRE, is_same_v<TSInsert<ST0, 7>, UIntsToPack<7>>);
TEST(REQUIRE, is_same_v<TSInsert<ST1, 7>, ST2>);
TEST(REQUIRE, is_same_v<TSInsert<ST2, 7>, ST3>);
}
SECTION("PackSort") {
TEST(REQUIRE, is_same_v<TSort<IT0>, ST0>);
TEST(REQUIRE, is_same_v<TSort<IT1>, ST1>);
}
}
}