#include #include #include #include #include #include #include #include TEST_CASE("bitset size", "[type_bitset]") { STATIC_CHECK(stdx::type_bitset<>{}.size() == 0u); STATIC_CHECK(stdx::type_bitset{}.size() == 1u); STATIC_CHECK(stdx::type_bitset{}.size() == 2u); } TEST_CASE("index operation", "[type_bitset]") { STATIC_CHECK(not stdx::type_bitset{}[stdx::type_identity_v]); } TEST_CASE("set single bit", "[type_bitset]") { auto bs = stdx::type_bitset{}; CHECK(not bs[stdx::type_identity_v]); bs.set(); CHECK(bs[stdx::type_identity_v]); bs.set(false); CHECK(not bs[stdx::type_identity_v]); } TEST_CASE("reset single bit", "[type_bitset]") { auto bs = stdx::type_bitset{stdx::all_bits}; CHECK(bs[stdx::type_identity_v]); bs.reset(); CHECK(not bs[stdx::type_identity_v]); } TEST_CASE("flip single bit", "[type_bitset]") { auto bs = stdx::type_bitset{stdx::all_bits}; CHECK(bs[stdx::type_identity_v]); bs.flip(); CHECK(not bs[stdx::type_identity_v]); } TEST_CASE("construct with a value", "[type bitset]") { constexpr auto bs1 = stdx::type_bitset{1ul}; STATIC_CHECK(bs1[stdx::type_identity_v]); constexpr auto bs2 = stdx::type_bitset{255ul}; STATIC_CHECK(bs2[stdx::type_identity_v]); STATIC_CHECK(bs2[stdx::type_identity_v]); } TEST_CASE("construct with values for bits", "[type_bitset]") { constexpr auto bs = stdx::type_bitset{stdx::type_list{}}; STATIC_CHECK(bs[stdx::type_identity_v]); STATIC_CHECK(not bs[stdx::type_identity_v]); STATIC_CHECK(bs[stdx::type_identity_v]); } TEMPLATE_TEST_CASE("convert to unsigned integral type", "[type_bitset]", std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t) { constexpr auto bs = stdx::type_bitset{255ul}; constexpr auto val = bs.template to(); STATIC_CHECK(std::is_same_v); STATIC_CHECK(val == 7u); } TEST_CASE("convert to type that fits", "[type_bitset]") { constexpr auto bs = stdx::type_bitset{stdx::all_bits}; constexpr auto val = bs.to_natural(); STATIC_CHECK(std::is_same_v); STATIC_CHECK(val == 7u); } TEST_CASE("all", "[type bitset]") { constexpr auto bs1 = stdx::type_bitset{stdx::all_bits}; STATIC_CHECK(bs1.all()); constexpr auto bs2 = stdx::type_bitset{}; STATIC_CHECK(not bs2.all()); } TEST_CASE("any", "[type bitset]") { constexpr auto bs1 = stdx::type_bitset{stdx::all_bits}; STATIC_CHECK(bs1.any()); constexpr auto bs2 = stdx::type_bitset{}; STATIC_CHECK(not bs2.any()); } TEST_CASE("none", "[type bitset]") { constexpr auto bs1 = stdx::type_bitset{stdx::all_bits}; STATIC_CHECK(not bs1.none()); constexpr auto bs2 = stdx::type_bitset{}; STATIC_CHECK(bs2.none()); } TEST_CASE("count", "[type_bitset]") { constexpr auto bs1 = stdx::type_bitset{}; STATIC_CHECK(bs1.count() == 0u); constexpr auto bs2 = stdx::type_bitset{stdx::all_bits}; STATIC_CHECK(bs2.count() == 3u); } TEST_CASE("set all bits", "[type_bitset]") { auto bs = stdx::type_bitset{}; bs.set(); CHECK(bs == stdx::type_bitset{stdx::all_bits}); bs.set(); CHECK(bs == stdx::type_bitset{stdx::all_bits}); } TEST_CASE("reset all bits", "[type_bitset]") { auto bs = stdx::type_bitset{stdx::all_bits}; bs.reset(); CHECK(bs == stdx::type_bitset{}); bs.reset(); CHECK(bs == stdx::type_bitset{}); } TEST_CASE("flip all bits", "[type_bitset]") { auto bs = stdx::type_bitset{stdx::all_bits}; bs.flip(); CHECK(bs == stdx::type_bitset{}); bs.flip(); CHECK(bs == stdx::type_bitset{stdx::all_bits}); } TEST_CASE("or", "[type_bitset]") { constexpr auto bs1 = stdx::type_bitset{0b101ul}; constexpr auto bs2 = stdx::type_bitset{0b010ul}; STATIC_CHECK((bs1 | bs2) == stdx::type_bitset{stdx::all_bits}); } TEST_CASE("and", "[type_bitset]") { constexpr auto bs1 = stdx::type_bitset{0b101ul}; constexpr auto bs2 = stdx::type_bitset{0b100ul}; STATIC_CHECK((bs1 & bs2) == stdx::type_bitset{0b100ul}); } TEST_CASE("xor", "[type_bitset]") { constexpr auto bs1 = stdx::type_bitset{0b101ul}; constexpr auto bs2 = stdx::type_bitset{0b010ul}; STATIC_CHECK((bs1 ^ bs2) == stdx::type_bitset{stdx::all_bits}); } TEST_CASE("not", "[type_bitset]") { constexpr auto bs = stdx::type_bitset{0b101ul}; STATIC_CHECK(~bs == stdx::type_bitset{0b10ul}); } TEST_CASE("difference", "[type_bitset]") { constexpr auto bs1 = stdx::type_bitset{0b101ul}; constexpr auto bs2 = stdx::type_bitset{0b011ul}; STATIC_CHECK((bs1 - bs2) == stdx::type_bitset{0b100ul}); } #if __cplusplus >= 202002L TEST_CASE("for_each", "[type_bitset]") { constexpr auto bs = stdx::type_bitset{stdx::all_bits}; auto result = std::string{}; bs.for_each([&]() -> void { result += std::string{stdx::type_as_string()} + std::to_string(I); }); CHECK(result == "int0float1bool2"); } #endif