Skip to content

Commit

Permalink
define stream insertion for std::byte in tests
Browse files Browse the repository at this point in the history
Change-Id: I5dca691416482ae27ab31bc176fd5217b8e3fd8f
  • Loading branch information
oliverlee committed Nov 21, 2023
1 parent ff860df commit 5f2a504
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test_ut/test_ut_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

#include <array>
#include <ranges>
#include <sstream>

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);
Expand All @@ -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));
};
}
27 changes: 27 additions & 0 deletions ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@
#include <include/boost/ut.hpp>

#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <ostream>
#include <ranges>
#include <type_traits>

auto operator<<(std::ostream& os, std::byte b) -> std::ostream&

Check warning on line 14 in ut.hpp

View check run for this annotation

Codecov / codecov/patch

ut.hpp#L14

Added line #L14 was not covered by tests
{
using U = std::uint8_t;

const auto value = static_cast<std::uint8_t>(b);

Check warning on line 18 in ut.hpp

View check run for this annotation

Codecov / codecov/patch

ut.hpp#L18

Added line #L18 was not covered by tests

for (auto mask = U{0x80U}; mask != U{}; mask >>= 1U) {
os << static_cast<bool>(value & mask);

Check warning on line 21 in ut.hpp

View check run for this annotation

Codecov / codecov/patch

ut.hpp#L20-L21

Added lines #L20 - L21 were not covered by tests
}

return os;

Check warning on line 24 in ut.hpp

View check run for this annotation

Codecov / codecov/patch

ut.hpp#L24

Added line #L24 was not covered by tests
}
template <
class Ostream,
class = std::enable_if_t<
not std::is_reference_v<Ostream> and
std::derived_from<Ostream, std::ostream>>>
auto operator<<(Ostream&& os, std::byte bp) -> Ostream&&
{
os << bp;
return std::move(os);
}

namespace boost::ut {
namespace detail {

Expand Down

0 comments on commit 5f2a504

Please sign in to comment.