Skip to content

Commit

Permalink
Use the new mettle::in_interval
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Feb 25, 2024
1 parent f499e70 commit 55ad566
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions test/test_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,19 @@ struct at_eof : matcher_tag {

template<typename T,
typename std::enable_if_t<bencode::detail::is_iterable_v<T>, int> = 0>
auto in_range(const T &t) {
return all(
greater_equal(&*std::begin(t)),
less_equal(&*std::end(t))
);
auto reuses_memory(const T &t) {
return in_interval(&*std::begin(t), &*std::end(t), interval::closed);
}

template<typename T,
typename std::enable_if_t<!bencode::detail::is_iterable_v<T>, int> = 0>
auto in_range(const T &) {
auto reuses_memory(const T &) {
return is_not(anything());
}

template<typename T>
auto in_range(const T *t) {
return all(
greater_equal(t),
less_equal(t + std::strlen(t))
);
auto reuses_memory(const T *t) {
return in_interval(t, t + std::strlen(t), interval::closed);
}

template<typename Nested, typename Matcher>
Expand Down Expand Up @@ -97,14 +91,14 @@ auto decode_tests(Builder &_, Callable &&do_decode) {

_.test("string", [do_decode]() {
auto data = make_data<InType>("4:spam");
auto in_data_range = in_range(data);
auto reuses_data_memory = reuses_memory(data);

auto value = do_decode(data);
auto str = get<typename OutType::string>(value);
expect(str, equal_to("spam"));
if constexpr(bencode::detail::is_view_v<typename OutType::string>) {
expect(&*str.begin(), in_data_range);
expect(&*str.end(), in_data_range);
expect(&*str.begin(), reuses_data_memory);
expect(&*str.end(), reuses_data_memory);
}
});

Expand All @@ -117,7 +111,7 @@ auto decode_tests(Builder &_, Callable &&do_decode) {

_.test("dict", [do_decode]() {
auto data = make_data<InType>("d4:spami42ee");
auto in_data_range = in_range(data);
auto reuses_data_memory = reuses_memory(data);

auto value = do_decode(data);
auto dict = get<typename OutType::dict>(value);
Expand All @@ -126,8 +120,8 @@ auto decode_tests(Builder &_, Callable &&do_decode) {
auto str = dict.find("spam")->first;
expect(str, equal_to("spam"));
if constexpr (bencode::detail::is_view_v<typename OutType::string>) {
expect(&*str.begin(), in_data_range);
expect(&*str.end(), in_data_range);
expect(&*str.begin(), reuses_data_memory);
expect(&*str.end(), reuses_data_memory);
}
});

Expand Down

0 comments on commit 55ad566

Please sign in to comment.