|
| 1 | +#include <stdx/bitset.hpp> |
| 2 | +#include <stdx/ct_conversions.hpp> |
| 3 | + |
| 4 | +#include <catch2/catch_template_test_macros.hpp> |
| 5 | +#include <catch2/catch_test_macros.hpp> |
| 6 | + |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdint> |
| 9 | +#include <string> |
| 10 | +#include <type_traits> |
| 11 | + |
| 12 | +TEST_CASE("bitset size", "[type_bitset]") { |
| 13 | + STATIC_CHECK(stdx::type_bitset<int>{}.size() == 1u); |
| 14 | + STATIC_CHECK(stdx::type_bitset<int, float>{}.size() == 2u); |
| 15 | +} |
| 16 | + |
| 17 | +TEST_CASE("index operation", "[type_bitset]") { |
| 18 | + STATIC_CHECK(not stdx::type_bitset<int>{}[stdx::type_identity_v<int>]); |
| 19 | +} |
| 20 | + |
| 21 | +TEST_CASE("set single bit", "[type_bitset]") { |
| 22 | + auto bs = stdx::type_bitset<int, float, bool>{}; |
| 23 | + CHECK(not bs[stdx::type_identity_v<int>]); |
| 24 | + bs.set<int>(); |
| 25 | + CHECK(bs[stdx::type_identity_v<int>]); |
| 26 | + bs.set<int>(false); |
| 27 | + CHECK(not bs[stdx::type_identity_v<int>]); |
| 28 | +} |
| 29 | + |
| 30 | +TEST_CASE("reset single bit", "[type_bitset]") { |
| 31 | + auto bs = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 32 | + CHECK(bs[stdx::type_identity_v<int>]); |
| 33 | + bs.reset<int>(); |
| 34 | + CHECK(not bs[stdx::type_identity_v<int>]); |
| 35 | +} |
| 36 | + |
| 37 | +TEST_CASE("flip single bit", "[type_bitset]") { |
| 38 | + auto bs = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 39 | + CHECK(bs[stdx::type_identity_v<int>]); |
| 40 | + bs.flip<int>(); |
| 41 | + CHECK(not bs[stdx::type_identity_v<int>]); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_CASE("construct with a value", "[type bitset]") { |
| 45 | + constexpr auto bs1 = stdx::type_bitset<int, float>{1ul}; |
| 46 | + STATIC_CHECK(bs1[stdx::type_identity_v<int>]); |
| 47 | + |
| 48 | + constexpr auto bs2 = stdx::type_bitset<int, float>{255ul}; |
| 49 | + STATIC_CHECK(bs2[stdx::type_identity_v<int>]); |
| 50 | + STATIC_CHECK(bs2[stdx::type_identity_v<float>]); |
| 51 | +} |
| 52 | + |
| 53 | +TEST_CASE("construct with values for bits", "[type_bitset]") { |
| 54 | + constexpr auto bs = |
| 55 | + stdx::type_bitset<int, float, bool>{stdx::type_list<int, bool>{}}; |
| 56 | + STATIC_CHECK(bs[stdx::type_identity_v<int>]); |
| 57 | + STATIC_CHECK(not bs[stdx::type_identity_v<float>]); |
| 58 | + STATIC_CHECK(bs[stdx::type_identity_v<bool>]); |
| 59 | +} |
| 60 | + |
| 61 | +TEMPLATE_TEST_CASE("convert to unsigned integral type", "[type_bitset]", |
| 62 | + std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t) { |
| 63 | + constexpr auto bs = stdx::type_bitset<int, float, bool>{255ul}; |
| 64 | + constexpr auto val = bs.template to<TestType>(); |
| 65 | + STATIC_REQUIRE(std::is_same_v<decltype(val), TestType const>); |
| 66 | + STATIC_REQUIRE(val == 7u); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_CASE("convert to type that fits", "[type_bitset]") { |
| 70 | + constexpr auto bs = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 71 | + constexpr auto val = bs.to_natural(); |
| 72 | + STATIC_REQUIRE(std::is_same_v<decltype(val), std::uint8_t const>); |
| 73 | + STATIC_REQUIRE(val == 7u); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_CASE("all", "[type bitset]") { |
| 77 | + constexpr auto bs1 = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 78 | + STATIC_CHECK(bs1.all()); |
| 79 | + |
| 80 | + constexpr auto bs2 = stdx::type_bitset<int, float, bool>{}; |
| 81 | + STATIC_CHECK(not bs2.all()); |
| 82 | +} |
| 83 | + |
| 84 | +TEST_CASE("any", "[type bitset]") { |
| 85 | + constexpr auto bs1 = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 86 | + STATIC_CHECK(bs1.any()); |
| 87 | + |
| 88 | + constexpr auto bs2 = stdx::type_bitset<int, float, bool>{}; |
| 89 | + STATIC_CHECK(not bs2.any()); |
| 90 | +} |
| 91 | + |
| 92 | +TEST_CASE("none", "[type bitset]") { |
| 93 | + constexpr auto bs1 = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 94 | + STATIC_CHECK(not bs1.none()); |
| 95 | + |
| 96 | + constexpr auto bs2 = stdx::type_bitset<int, float, bool>{}; |
| 97 | + STATIC_CHECK(bs2.none()); |
| 98 | +} |
| 99 | + |
| 100 | +TEST_CASE("count", "[type_bitset]") { |
| 101 | + constexpr auto bs1 = stdx::type_bitset<int, float, bool>{}; |
| 102 | + STATIC_CHECK(bs1.count() == 0u); |
| 103 | + |
| 104 | + constexpr auto bs2 = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 105 | + STATIC_CHECK(bs2.count() == 3u); |
| 106 | +} |
| 107 | + |
| 108 | +TEST_CASE("set all bits", "[type_bitset]") { |
| 109 | + auto bs = stdx::type_bitset<int, float, bool>{}; |
| 110 | + bs.set(); |
| 111 | + CHECK(bs == stdx::type_bitset<int, float, bool>{stdx::all_bits}); |
| 112 | + bs.set(); |
| 113 | + CHECK(bs == stdx::type_bitset<int, float, bool>{stdx::all_bits}); |
| 114 | +} |
| 115 | + |
| 116 | +TEST_CASE("reset all bits", "[type_bitset]") { |
| 117 | + auto bs = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 118 | + bs.reset(); |
| 119 | + CHECK(bs == stdx::type_bitset<int, float, bool>{}); |
| 120 | + bs.reset(); |
| 121 | + CHECK(bs == stdx::type_bitset<int, float, bool>{}); |
| 122 | +} |
| 123 | + |
| 124 | +TEST_CASE("flip all bits", "[type_bitset]") { |
| 125 | + auto bs = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 126 | + bs.flip(); |
| 127 | + CHECK(bs == stdx::type_bitset<int, float, bool>{}); |
| 128 | + bs.flip(); |
| 129 | + CHECK(bs == stdx::type_bitset<int, float, bool>{stdx::all_bits}); |
| 130 | +} |
| 131 | + |
| 132 | +TEST_CASE("or", "[type_bitset]") { |
| 133 | + constexpr auto bs1 = stdx::type_bitset<int, float, bool>{0b101ul}; |
| 134 | + constexpr auto bs2 = stdx::type_bitset<int, float, bool>{0b010ul}; |
| 135 | + STATIC_REQUIRE((bs1 | bs2) == |
| 136 | + stdx::type_bitset<int, float, bool>{stdx::all_bits}); |
| 137 | +} |
| 138 | + |
| 139 | +TEST_CASE("and", "[type_bitset]") { |
| 140 | + constexpr auto bs1 = stdx::type_bitset<int, float, bool>{0b101ul}; |
| 141 | + constexpr auto bs2 = stdx::type_bitset<int, float, bool>{0b100ul}; |
| 142 | + STATIC_REQUIRE((bs1 & bs2) == stdx::type_bitset<int, float, bool>{0b100ul}); |
| 143 | +} |
| 144 | + |
| 145 | +TEST_CASE("xor", "[type_bitset]") { |
| 146 | + constexpr auto bs1 = stdx::type_bitset<int, float, bool>{0b101ul}; |
| 147 | + constexpr auto bs2 = stdx::type_bitset<int, float, bool>{0b010ul}; |
| 148 | + STATIC_REQUIRE((bs1 ^ bs2) == |
| 149 | + stdx::type_bitset<int, float, bool>{stdx::all_bits}); |
| 150 | +} |
| 151 | + |
| 152 | +TEST_CASE("not", "[type_bitset]") { |
| 153 | + constexpr auto bs = stdx::type_bitset<int, float, bool>{0b101ul}; |
| 154 | + STATIC_REQUIRE(~bs == stdx::type_bitset<int, float, bool>{0b10ul}); |
| 155 | +} |
| 156 | + |
| 157 | +#if __cplusplus >= 202002L |
| 158 | +TEST_CASE("for_each", "[type_bitset]") { |
| 159 | + constexpr auto bs = stdx::type_bitset<int, float, bool>{stdx::all_bits}; |
| 160 | + auto result = std::string{}; |
| 161 | + bs.for_each([&]<typename T, std::size_t I>() -> void { |
| 162 | + result += std::string{stdx::type_as_string<T>()} + std::to_string(I); |
| 163 | + }); |
| 164 | + CHECK(result == "int0float1bool2"); |
| 165 | +} |
| 166 | +#endif |
0 commit comments