Skip to content

Commit

Permalink
[libcxx][test] array and basic_string_view iterators are not portably…
Browse files Browse the repository at this point in the history
… pointers

Fixup tests that believe them to be so. Most notably including some heavy refactoring in `std/iterators/iterator.primitives/iterator.traits/cxx20_iterator_traits.compile.pass.cpp`, which now detects pointers and validates that `iterator_concept` is present only for pointers.

Differential Revision: https://reviews.llvm.org/D117368
  • Loading branch information
CaseyCarter committed Feb 21, 2022
1 parent c31ef42 commit 2d653b7
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 179 deletions.

Large diffs are not rendered by default.

Expand Up @@ -51,8 +51,8 @@ class iterator_wrapper {
I base_ = I{};
};

template <typename I>
constexpr void unqualified_lookup_move(I first_, I last_, I result_first_, I result_last_) {
template <typename It, typename Out>
constexpr void unqualified_lookup_move(It first_, It last_, Out result_first_, Out result_last_) {
auto first = ::check_unqualified_lookup::unqualified_lookup_wrapper{std::move(first_)};
auto last = ::check_unqualified_lookup::unqualified_lookup_wrapper{std::move(last_)};
auto result_first = ::check_unqualified_lookup::unqualified_lookup_wrapper{std::move(result_first_)};
Expand All @@ -65,8 +65,8 @@ constexpr void unqualified_lookup_move(I first_, I last_, I result_first_, I res
}
}

template <typename I>
constexpr void lvalue_move(I first_, I last_, I result_first_, I result_last_) {
template <typename It, typename Out>
constexpr void lvalue_move(It first_, It last_, Out result_first_, Out result_last_) {
auto first = iterator_wrapper{std::move(first_)};
auto last = ::iterator_wrapper{std::move(last_)};
auto result_first = iterator_wrapper{std::move(result_first_)};
Expand All @@ -80,8 +80,8 @@ constexpr void lvalue_move(I first_, I last_, I result_first_, I result_last_) {
}
}

template <typename I>
constexpr void rvalue_move(I first_, I last_, I result_first_, I result_last_) {
template <typename It, typename Out>
constexpr void rvalue_move(It first_, It last_, Out result_first_, Out result_last_) {
auto first = iterator_wrapper{std::move(first_)};
auto last = iterator_wrapper{std::move(last_)};
auto result_first = iterator_wrapper{std::move(result_first_)};
Expand Down
Expand Up @@ -20,25 +20,33 @@
#include "make_string.h"
#include "test_iterators.h"

template<class CharT, class Sentinel>
constexpr void test() {
auto val = MAKE_STRING_VIEW(CharT, "test");
auto sv = std::basic_string_view<CharT>(val.begin(), Sentinel(val.end()));
ASSERT_SAME_TYPE(decltype(sv), std::basic_string_view<CharT>);
assert(sv.size() == val.size());
template<class It, class Sentinel, class CharT>
constexpr void test_construction(std::basic_string_view<CharT> val) {
auto sv = std::basic_string_view<CharT>(It(val.data()), Sentinel(It(val.data() + val.size())));
assert(sv.data() == val.data());
assert(sv.size() == val.size());
}

template<class CharT>
constexpr void test_with_char() {
const auto val = MAKE_STRING_VIEW(CharT, "test");
test_construction<CharT*, CharT*>(val);
test_construction<CharT*, const CharT*>(val);
test_construction<const CharT*, CharT*>(val);
test_construction<const CharT*, sized_sentinel<const CharT*>>(val);
test_construction<contiguous_iterator<const CharT*>, contiguous_iterator<const CharT*>>(val);
test_construction<contiguous_iterator<const CharT*>, sized_sentinel<contiguous_iterator<const CharT*>>>(val);
}

constexpr bool test() {
test<char, char*>();
test_with_char<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t, wchar_t*>();
test_with_char<wchar_t>();
#endif
test<char8_t, char8_t*>();
test<char16_t, char16_t*>();
test<char32_t, char32_t*>();
test<char, const char*>();
test<char, sized_sentinel<const char*>>();
test_with_char<char8_t>();
test_with_char<char16_t>();
test_with_char<char32_t>();

return true;
}

Expand All @@ -54,7 +62,7 @@ template <class CharT>
void test_throwing() {
auto val = MAKE_STRING_VIEW(CharT, "test");
try {
(void)std::basic_string_view<CharT>(val.begin(), ThrowingSentinel<CharT>());
(void)std::basic_string_view<CharT>(val.data(), ThrowingSentinel<CharT>());
assert(false);
} catch (int i) {
assert(i == 42);
Expand Down Expand Up @@ -89,4 +97,3 @@ int main(int, char**) {

return 0;
}

Expand Up @@ -20,30 +20,37 @@
#include "test_macros.h"
#include "test_iterators.h"

template<class CharT, class Sentinel>
constexpr void test() {
auto val = MAKE_STRING_VIEW(CharT, "test");
auto sv = std::basic_string_view(val.begin(), Sentinel(val.end()));
template<class It, class Sentinel, class CharT>
constexpr void test_ctad(std::basic_string_view<CharT> val) {
auto sv = std::basic_string_view(It(val.data()), Sentinel(It(val.data() + val.size())));
ASSERT_SAME_TYPE(decltype(sv), std::basic_string_view<CharT>);
assert(sv.size() == val.size());
assert(sv.data() == val.data());
assert(sv.size() == val.size());
}

template<class CharT>
constexpr void test_with_char() {
const auto val = MAKE_STRING_VIEW(CharT, "test");
test_ctad<CharT*, CharT*>(val);
test_ctad<CharT*, const CharT*>(val);
test_ctad<const CharT*, CharT*>(val);
test_ctad<const CharT*, sized_sentinel<const CharT*>>(val);
test_ctad<contiguous_iterator<const CharT*>, contiguous_iterator<const CharT*>>(val);
test_ctad<contiguous_iterator<const CharT*>, sized_sentinel<contiguous_iterator<const CharT*>>>(val);
}

constexpr void test() {
test<char, char*>();
test_with_char<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t, wchar_t*>();
test_with_char<wchar_t>();
#endif
test<char8_t, char8_t*>();
test<char16_t, char16_t*>();
test<char32_t, char32_t*>();
test<char, const char*>();
test<char, sized_sentinel<const char*>>();
test_with_char<char8_t>();
test_with_char<char16_t>();
test_with_char<char32_t>();
}

int main(int, char**) {
test();

return 0;
}

Expand Up @@ -26,10 +26,10 @@ constexpr void test(const CharT* fmt) {
std::basic_format_parse_context<CharT> context(fmt);

context.advance_to(context.begin() + 1);
assert(context.begin() == &fmt[1]);
assert(std::to_address(context.begin()) == fmt + 1);

context.advance_to(context.begin() + 1);
assert(context.begin() == &fmt[2]);
assert(std::to_address(context.begin()) == fmt + 2);

context.advance_to(context.begin() + 1);
assert(context.begin() == context.end());
Expand All @@ -39,10 +39,10 @@ constexpr void test(const CharT* fmt) {
std::basic_format_parse_context context(view);

context.advance_to(context.begin() + 1);
assert(std::to_address(context.begin()) == std::to_address(view.begin()) + 1);
assert(std::to_address(context.begin()) == fmt + 1);

context.advance_to(context.begin() + 1);
assert(std::to_address(context.begin()) == std::to_address(view.begin()) + 2);
assert(std::to_address(context.begin()) == fmt + 2);

context.advance_to(context.begin() + 1);
assert(context.begin() == context.end());
Expand Down
Expand Up @@ -24,7 +24,7 @@ template <class CharT>
constexpr void test(const CharT* fmt) {
{
std::basic_format_parse_context<CharT> context(fmt);
assert(context.begin() == &fmt[0]);
assert(std::to_address(context.begin()) == &fmt[0]);
ASSERT_NOEXCEPT(context.begin());
}
{
Expand Down
Expand Up @@ -47,8 +47,8 @@ constexpr void test(const CharT* fmt) {

{
std::basic_format_parse_context<CharT> context(fmt);
assert(context.begin() == &fmt[0]);
assert(context.end() == &fmt[3]);
assert(std::to_address(context.begin()) == &fmt[0]);
assert(std::to_address(context.end()) == &fmt[3]);
}
{
std::basic_string_view view{fmt};
Expand Down
Expand Up @@ -24,7 +24,7 @@ template <class CharT>
constexpr void test(const CharT* fmt) {
{
std::basic_format_parse_context<CharT> context(fmt);
assert(context.end() == &fmt[3]);
assert(std::to_address(context.end()) == &fmt[3]);
ASSERT_NOEXCEPT(context.end());
}
{
Expand Down

0 comments on commit 2d653b7

Please sign in to comment.