diff --git a/test_ut/test_ut_example.cpp b/test_ut/test_ut_example.cpp index b6de9fc..586180f 100644 --- a/test_ut/test_ut_example.cpp +++ b/test_ut/test_ut_example.cpp @@ -2,13 +2,18 @@ #include #include +#include auto main() -> int { + using ::boost::ut::eq; using ::boost::ut::expect; using ::boost::ut::range_eq; using ::boost::ut::test; + // all of these tests are intended to fail in order to demonstrate information + // printed on test failure + test("test range eq, lhs smaller") = [] { constexpr auto data1 = std::array{1, 2, 3}; constexpr auto data2 = std::views::iota(1, 5); @@ -29,4 +34,12 @@ auto main() -> int expect(range_eq(data1, data2)); }; + + test("print bytes") = [] { + constexpr auto expected = "01100000"; + + const auto actual = (std::stringstream{} << std::byte{0b1110'0000}).str(); + + expect(eq(expected, actual)); + }; } diff --git a/ut.hpp b/ut.hpp index 2ee77a9..3fdec5e 100644 --- a/ut.hpp +++ b/ut.hpp @@ -3,10 +3,37 @@ #include #include +#include +#include +#include #include +#include #include #include +auto operator<<(std::ostream& os, std::byte b) -> std::ostream& +{ + using U = std::uint8_t; + + const auto value = static_cast(b); + + for (auto mask = U{0x80U}; mask != U{}; mask >>= 1U) { + os << static_cast(value & mask); + } + + return os; +} +template < + class Ostream, + class = std::enable_if_t< + not std::is_reference_v and + std::derived_from>> +auto operator<<(Ostream&& os, std::byte bp) -> Ostream&& +{ + os << bp; + return std::move(os); +} + namespace boost::ut { namespace detail {