-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathut.hpp
152 lines (123 loc) · 3.42 KB
/
ut.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#include <include/boost/ut.hpp>
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <ostream>
#include <ranges>
#include <type_traits>
namespace starflate::test::detail {
template <class Printer>
auto& print(Printer& p, std::byte b)
{
using U = std::uint8_t;
const auto value = static_cast<std::uint8_t>(b);
for (auto mask = U{0x80U}; mask != U{}; mask >>= 1U) {
p << (static_cast<bool>(value & mask) ? '1' : '0');
}
return p;
}
} // namespace starflate::test::detail
template <
class Ostream,
class = std::enable_if_t<
std::derived_from<std::remove_cvref_t<Ostream>, std::ostream>>>
auto operator<<(Ostream&& os, std::byte b)
-> decltype(os << char{}, std::forward<Ostream>(os))
{
::starflate::test::detail::print(os, b);
return std::forward<Ostream>(os);
}
namespace boost::ut {
auto& operator<<(printer& p, std::byte b)
{
return ::starflate::test::detail::print(p, b);
}
namespace detail {
template <class TLhs, class TRhs>
struct range_eq_ : op
{
constexpr range_eq_(const TLhs& lhs = {}, const TRhs& rhs = {})
: lhs_{lhs},
rhs_{rhs},
error_{[&] -> std::remove_const_t<decltype(error_)> {
if constexpr (
type_traits::is_range_v<TLhs> and type_traits::is_range_v<TRhs>) {
using std::ranges::cend;
auto count = unsigned{};
const auto result = std::ranges::mismatch(
lhs_, rhs_, [&count](const auto& x, const auto& y) {
using std::operator==;
++count;
return x == y;
});
if (result.in1 == cend(lhs_) and result.in2 == cend(rhs_)) {
return {};
}
if (result.in1 != cend(lhs_) and result.in2 != cend(rhs_)) {
return {{count - 1U, result.in1, result.in2}};
}
return {{count, result.in1, result.in2}};
}
}()}
{}
[[nodiscard]]
constexpr
operator bool() const
{
return not error_;
}
[[nodiscard]]
constexpr auto& lhs() const
{
return lhs_;
}
[[nodiscard]]
constexpr auto& rhs() const
{
return rhs_;
}
[[nodiscard]]
constexpr auto error() const
{
return *error_;
}
const TLhs lhs_{};
const TRhs rhs_{};
const std::optional<std::tuple<
unsigned,
std::ranges::iterator_t<const TLhs>,
std::ranges::iterator_t<const TRhs>>>
error_{};
friend auto& operator<<(printer& p, const range_eq_& op)
{
static constexpr auto colors_ = colors{};
static constexpr auto color = [](bool cond) {
return cond ? colors_.pass : colors_.fail;
};
p << color(op) << "ranges_eq(" << reflection::decay_type_name<TLhs>()
<< ", " << reflection::decay_type_name<TRhs>() << ")";
if (not op) {
const auto [index, in1, in2] = op.error();
p << "\n at element " << index << ": ";
if (in1 != op.lhs().end() and in2 != op.rhs().end()) {
p << "[ " << *in1 << " == " << *in2 << " ]";
} else if (in1 == op.lhs().end()) {
p << "lhs has fewer elements than rhs";
} else {
p << "lhs has more elements than rhs";
}
}
return (p << colors_.none);
}
};
} // namespace detail
template <class TLhs, class TRhs>
[[nodiscard]]
constexpr auto range_eq(const TLhs& lhs, const TRhs& rhs)
{
return detail::range_eq_{lhs, rhs};
}
} // namespace boost::ut