[libc++] Refactor the tests for [iterator.range] (#67496)

The tests were a bit of a mess -- the testing coverage wasn't bad but it
was extremely difficult to see what was being tested and where. I split
up the tests to make them easier to audit for completeness and did such
an audit, adding a few missing tests (e.g. the conditional noexcept-ness
of std::cbegin and std::cend). I also audited the synopsis and adjusted
it where it needed to be adjusted.

This patch is in preparation of fixing #67471.
This commit is contained in:
Louis Dionne 2023-09-28 09:04:07 -04:00 committed by GitHub
parent 05b86a8fea
commit 000940e296
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 414 additions and 338 deletions

View File

@ -634,25 +634,25 @@ public:
bool failed() const noexcept;
};
template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
template <class C> constexpr auto end(C& c) -> decltype(c.end());
template <class C> constexpr auto end(const C& c) -> decltype(c.end());
template <class T, size_t N> constexpr T* begin(T (&array)[N]);
template <class T, size_t N> constexpr T* end(T (&array)[N]);
template <class C> constexpr auto begin(C& c) -> decltype(c.begin()); // constexpr since C++17
template <class C> constexpr auto begin(const C& c) -> decltype(c.begin()); // constexpr since C++17
template <class C> constexpr auto end(C& c) -> decltype(c.end()); // constexpr since C++17
template <class C> constexpr auto end(const C& c) -> decltype(c.end()); // constexpr since C++17
template <class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept; // constexpr since C++14
template <class T, size_t N> constexpr T* end(T (&array)[N]) noexcept; // constexpr since C++14
template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
template <class C> constexpr auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14
template <class C> constexpr auto cend(const C& c) -> decltype(std::end(c)); // C++14
template <class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin()); // C++14, constexpr since C++17
template <class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin()); // C++14, constexpr since C++17
template <class C> constexpr auto rend(C& c) -> decltype(c.rend()); // C++14, constexpr since C++17
template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14, constexpr since C++17
template <class E> constexpr reverse_iterator<const E*> rbegin(initializer_list<E> il); // C++14, constexpr since C++17
template <class E> constexpr reverse_iterator<const E*> rend(initializer_list<E> il); // C++14, constexpr since C++17
template <class T, size_t N> constexpr reverse_iterator<T*> rbegin(T (&array)[N]); // C++14, constexpr since C++17
template <class T, size_t N> constexpr reverse_iterator<T*> rend(T (&array)[N]); // C++14, constexpr since C++17
template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14, constexpr since C++17
template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14, constexpr since C++17
// 24.8, container access:
template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17

View File

@ -0,0 +1,93 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// Make sure that std::cbegin(x) effectively calls std::as_const(x).begin(), not x.cbegin().
//
// Also make sure that we don't get hijacked by ADL, see https://llvm.org/PR28927.
#include <cassert>
#include <iterator>
#include "test_macros.h"
struct ArrayHijacker {
friend constexpr int begin(ArrayHijacker (&)[3]) { return 42; }
friend constexpr int end(ArrayHijacker (&)[3]) { return 42; }
friend constexpr int begin(const ArrayHijacker (&)[3]) { return 42; }
friend constexpr int end(const ArrayHijacker (&)[3]) { return 42; }
};
struct ContainerHijacker {
int* a_;
constexpr int* begin() const { return a_; }
constexpr int* end() const { return a_ + 3; }
constexpr int* rbegin() const { return a_; }
constexpr int* rend() const { return a_ + 3; }
friend constexpr int begin(ContainerHijacker&) { return 42; }
friend constexpr int end(ContainerHijacker&) { return 42; }
friend constexpr int begin(const ContainerHijacker&) { return 42; }
friend constexpr int end(const ContainerHijacker&) { return 42; }
friend constexpr int cbegin(ContainerHijacker&) { return 42; }
friend constexpr int cend(ContainerHijacker&) { return 42; }
friend constexpr int cbegin(const ContainerHijacker&) { return 42; }
friend constexpr int cend(const ContainerHijacker&) { return 42; }
friend constexpr int rbegin(ContainerHijacker&) { return 42; }
friend constexpr int rend(ContainerHijacker&) { return 42; }
friend constexpr int rbegin(const ContainerHijacker&) { return 42; }
friend constexpr int rend(const ContainerHijacker&) { return 42; }
friend constexpr int crbegin(ContainerHijacker&) { return 42; }
friend constexpr int crend(ContainerHijacker&) { return 42; }
friend constexpr int crbegin(const ContainerHijacker&) { return 42; }
friend constexpr int crend(const ContainerHijacker&) { return 42; }
};
TEST_CONSTEXPR_CXX17 bool test() {
{
ArrayHijacker a[3] = {};
assert(begin(a) == 42);
assert(end(a) == 42);
assert(std::begin(a) == a);
assert(std::end(a) == a + 3);
#if TEST_STD_VER > 11
assert(std::cbegin(a) == a);
assert(std::cend(a) == a + 3);
assert(std::rbegin(a).base() == a + 3);
assert(std::rend(a).base() == a);
assert(std::crbegin(a).base() == a + 3);
assert(std::crend(a).base() == a);
#endif
}
{
int a[3] = {};
ContainerHijacker c{a};
assert(begin(c) == 42);
assert(end(c) == 42);
assert(std::begin(c) == a);
assert(std::end(c) == a + 3);
#if TEST_STD_VER > 11
assert(std::cbegin(c) == a);
assert(std::cend(c) == a + 3);
assert(std::rbegin(c) == a);
assert(std::rend(c) == a + 3);
assert(std::crbegin(c) == a);
assert(std::crend(c) == a + 3);
#endif
}
return true;
}
int main(int, char**) {
test();
#if TEST_STD_VER >= 17
static_assert(test());
#endif
return 0;
}

View File

@ -0,0 +1,83 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <iterator>
//
// template <class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept; // constexpr since C++14
// template <class T, size_t N> constexpr T* end(T (&array)[N]) noexcept; // constexpr since C++14
//
// template <class T, size_t N> constexpr reverse_iterator<T*> rbegin(T (&array)[N]); // C++14, constexpr since C++17
// template <class T, size_t N> constexpr reverse_iterator<T*> rend(T (&array)[N]); // C++14, constexpr since C++17
#include <cassert>
#include <iterator>
#include "test_macros.h"
TEST_CONSTEXPR_CXX14 bool test() {
int a[] = {1, 2, 3};
const auto& ca = a;
// std::begin(T (&)[N]) / std::end(T (&)[N])
{
ASSERT_SAME_TYPE(decltype(std::begin(a)), int*);
assert(std::begin(a) == a);
ASSERT_SAME_TYPE(decltype(std::end(a)), int*);
assert(std::end(a) == a + 3);
// kind of overkill since it follows from the definition, but worth testing
ASSERT_SAME_TYPE(decltype(std::begin(ca)), const int*);
assert(std::begin(ca) == ca);
ASSERT_SAME_TYPE(decltype(std::end(ca)), const int*);
assert(std::end(ca) == ca + 3);
}
return true;
}
TEST_CONSTEXPR_CXX17 bool test_r() {
#if TEST_STD_VER >= 14
int a[] = {1, 2, 3};
const auto& ca = a;
// std::rbegin(T (&)[N]) / std::rend(T (&)[N])
{
ASSERT_SAME_TYPE(decltype(std::rbegin(a)), std::reverse_iterator<int*>);
assert(std::rbegin(a).base() == a + 3);
ASSERT_SAME_TYPE(decltype(std::rend(a)), std::reverse_iterator<int*>);
assert(std::rend(a).base() == a);
// kind of overkill since it follows from the definition, but worth testing
ASSERT_SAME_TYPE(decltype(std::rbegin(ca)), std::reverse_iterator<const int*>);
assert(std::rbegin(ca).base() == ca + 3);
ASSERT_SAME_TYPE(decltype(std::rend(ca)), std::reverse_iterator<const int*>);
assert(std::rend(ca).base() == ca);
}
#endif
return true;
}
int main(int, char**) {
test();
test_r();
#if TEST_STD_VER >= 14
static_assert(test(), "");
#endif
#if TEST_STD_VER >= 17
static_assert(test_r(), "");
#endif
return 0;
}

View File

@ -0,0 +1,131 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <iterator>
//
// template <class C> constexpr auto begin(C& c) -> decltype(c.begin()); // constexpr since C++17
// template <class C> constexpr auto begin(const C& c) -> decltype(c.begin()); // constexpr since C++17
// template <class C> constexpr auto end(C& c) -> decltype(c.end()); // constexpr since C++17
// template <class C> constexpr auto end(const C& c) -> decltype(c.end()); // constexpr since C++17
//
// template <class C> constexpr auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14
// template <class C> constexpr auto cend(const C& c) -> decltype(std::end(c)); // C++14
// template <class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin()); // C++14, constexpr since C++17
// template <class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin()); // C++14, constexpr since C++17
// template <class C> constexpr auto rend(C& c) -> decltype(c.rend()); // C++14, constexpr since C++17
// template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14, constexpr since C++17
// template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14, constexpr since C++17
// template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14, constexpr since C++17
#include <array>
#include <cassert>
#include <iterator>
#include <list>
#include <vector>
#include "test_macros.h"
template <typename C,
typename Iterator = typename C::iterator,
typename ConstIterator = typename C::const_iterator,
typename ReverseIterator = typename C::reverse_iterator,
typename ConstReverseIterator = typename C::const_reverse_iterator>
TEST_CONSTEXPR_CXX17 bool test() {
C c = {1, 2, 3};
const C& cc = c;
// std::begin(C& c) / std::end(C& c)
{
ASSERT_SAME_TYPE(decltype(std::begin(c)), Iterator);
assert(std::begin(c) == c.begin());
ASSERT_SAME_TYPE(decltype(std::end(c)), Iterator);
assert(std::end(c) == c.end());
}
// std::begin(C const& c) / std::end(C const& c)
{
ASSERT_SAME_TYPE(decltype(std::begin(cc)), ConstIterator);
assert(std::begin(cc) == cc.begin());
ASSERT_SAME_TYPE(decltype(std::end(cc)), ConstIterator);
assert(std::end(cc) == cc.end());
}
#if TEST_STD_VER >= 14
// std::cbegin(C const&) / std::cend(C const&)
{
ASSERT_SAME_TYPE(decltype(std::cbegin(cc)), ConstIterator);
static_assert(noexcept(std::cbegin(cc)) == noexcept(std::begin(cc)), "");
assert(std::cbegin(cc) == std::begin(cc));
ASSERT_SAME_TYPE(decltype(std::cend(cc)), ConstIterator);
static_assert(noexcept(std::cend(cc)) == noexcept(std::end(cc)), "");
assert(std::cend(cc) == std::end(cc));
// kind of overkill, but whatever
ASSERT_SAME_TYPE(decltype(std::cbegin(c)), ConstIterator);
static_assert(noexcept(std::cbegin(c)) == noexcept(std::begin(cc)), "");
assert(std::cbegin(c) == std::begin(cc));
ASSERT_SAME_TYPE(decltype(std::cend(c)), ConstIterator);
static_assert(noexcept(std::cend(c)) == noexcept(std::end(cc)), "");
assert(std::cend(c) == std::end(cc));
}
// std::rbegin(C& c) / std::rend(C& c)
{
ASSERT_SAME_TYPE(decltype(std::rbegin(c)), ReverseIterator);
assert(std::rbegin(c) == c.rbegin());
ASSERT_SAME_TYPE(decltype(std::rend(c)), ReverseIterator);
assert(std::rend(c) == c.rend());
}
// std::rbegin(C const&) / std::rend(C const&)
{
ASSERT_SAME_TYPE(decltype(std::rbegin(cc)), ConstReverseIterator);
assert(std::rbegin(cc) == cc.rbegin());
ASSERT_SAME_TYPE(decltype(std::rend(cc)), ConstReverseIterator);
assert(std::rend(cc) == cc.rend());
}
// std::crbegin(C const&) / std::crend(C const&)
{
ASSERT_SAME_TYPE(decltype(std::crbegin(cc)), ConstReverseIterator);
assert(std::crbegin(cc) == std::rbegin(cc));
ASSERT_SAME_TYPE(decltype(std::crend(cc)), ConstReverseIterator);
assert(std::crend(cc) == std::rend(cc));
// kind of overkill, but whatever
ASSERT_SAME_TYPE(decltype(std::crbegin(c)), ConstReverseIterator);
assert(std::crbegin(c) == std::rbegin(cc));
ASSERT_SAME_TYPE(decltype(std::crend(c)), ConstReverseIterator);
assert(std::crend(c) == std::rend(cc));
}
#endif // TEST_STD_VER >= 14
return true;
}
int main(int, char**) {
test<std::array<int, 3>>();
test<std::list<int>>();
test<std::vector<int>>();
#if TEST_STD_VER >= 17
static_assert(test<std::array<int, 3>>());
#endif
return 0;
}

View File

@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11
// <iterator>
//
// Note that begin and end are tested in libcxx/test/std/language.support/support.initlist/support.initlist.range/begin_end.pass.cpp
//
// template <class E> constexpr reverse_iterator<const E*> rbegin(initializer_list<E> il); // C++14, constexpr since C++17
// template <class E> constexpr reverse_iterator<const E*> rend(initializer_list<E> il); // C++14, constexpr since C++17
#include <cassert>
#include <initializer_list>
#include <iterator>
#include "test_macros.h"
TEST_CONSTEXPR_CXX17 bool test() {
std::initializer_list<int> il = {1, 2, 3};
ASSERT_SAME_TYPE(decltype(std::rbegin(il)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::rend(il)), std::reverse_iterator<const int*>);
assert(std::rbegin(il).base() == il.end());
assert(std::rend(il).base() == il.begin());
const auto& cil = il;
ASSERT_SAME_TYPE(decltype(std::rbegin(cil)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::rend(cil)), std::reverse_iterator<const int*>);
assert(std::rbegin(cil).base() == il.end());
assert(std::rend(cil).base() == il.begin());
return true;
}
int main(int, char**) {
test();
#if TEST_STD_VER >= 17
static_assert(test());
#endif
return 0;
}

View File

@ -1,282 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <iterator>
// template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
// template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
// template <class C> constexpr auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14
// template <class C> constexpr auto cend(const C& c) -> decltype(std::end(c)); // C++14
// template <class C> constexpr auto end (C& c) -> decltype(c.end());
// template <class C> constexpr auto end (const C& c) -> decltype(c.end());
// template <class E> constexpr reverse_iterator<const E*> rbegin(initializer_list<E> il);
// template <class E> constexpr reverse_iterator<const E*> rend (initializer_list<E> il);
//
// template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
// template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
// template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
// template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
// template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
// template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
// template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
// template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
//
// All of these are constexpr in C++17
#include <array>
#include <cassert>
#include <initializer_list>
#include <iterator>
#include <list>
#include <vector>
#include "test_macros.h"
// Throughout this test, we consistently compare against c.begin() instead of c.cbegin()
// because some STL types (e.g. initializer_list) don't syntactically support il.cbegin().
// Note that std::cbegin(x) effectively calls std::as_const(x).begin(), not x.cbegin();
// see the ContainerHijacker test case below.
TEST_CONSTEXPR_CXX14 bool test_arrays_and_initializer_lists_forward()
{
{
int a[] = {1, 2, 3};
ASSERT_SAME_TYPE(decltype(std::begin(a)), int*);
ASSERT_SAME_TYPE(decltype(std::end(a)), int*);
assert(std::begin(a) == a);
assert(std::end(a) == a + 3);
#if TEST_STD_VER > 11
ASSERT_SAME_TYPE(decltype(std::cbegin(a)), const int*);
ASSERT_SAME_TYPE(decltype(std::cend(a)), const int*);
assert(std::cbegin(a) == a);
assert(std::cend(a) == a + 3);
#endif
const auto& ca = a;
ASSERT_SAME_TYPE(decltype(std::begin(ca)), const int*);
ASSERT_SAME_TYPE(decltype(std::end(ca)), const int*);
assert(std::begin(ca) == a);
assert(std::end(ca) == a + 3);
#if TEST_STD_VER > 11
ASSERT_SAME_TYPE(decltype(std::cbegin(ca)), const int*);
ASSERT_SAME_TYPE(decltype(std::cend(ca)), const int*);
assert(std::cbegin(ca) == a);
assert(std::cend(ca) == a + 3);
#endif
}
{
std::initializer_list<int> il = {1, 2, 3};
ASSERT_SAME_TYPE(decltype(std::begin(il)), const int*);
ASSERT_SAME_TYPE(decltype(std::end(il)), const int*);
assert(std::begin(il) == il.begin());
assert(std::end(il) == il.end());
#if TEST_STD_VER > 11
ASSERT_SAME_TYPE(decltype(std::cbegin(il)), const int*);
ASSERT_SAME_TYPE(decltype(std::cend(il)), const int*);
assert(std::cbegin(il) == il.begin());
assert(std::cend(il) == il.end());
#endif
const auto& cil = il;
ASSERT_SAME_TYPE(decltype(std::begin(cil)), const int*);
ASSERT_SAME_TYPE(decltype(std::end(cil)), const int*);
assert(std::begin(cil) == il.begin());
assert(std::end(cil) == il.end());
#if TEST_STD_VER > 11
ASSERT_SAME_TYPE(decltype(std::cbegin(cil)), const int*);
ASSERT_SAME_TYPE(decltype(std::cend(cil)), const int*);
assert(std::cbegin(cil) == il.begin());
assert(std::cend(cil) == il.end());
#endif
}
return true;
}
#if TEST_STD_VER > 11
TEST_CONSTEXPR_CXX17 bool test_arrays_and_initializer_lists_backward()
{
{
int a[] = {1, 2, 3};
ASSERT_SAME_TYPE(decltype(std::rbegin(a)), std::reverse_iterator<int*>);
ASSERT_SAME_TYPE(decltype(std::rend(a)), std::reverse_iterator<int*>);
assert(std::rbegin(a).base() == a + 3);
assert(std::rend(a).base() == a);
ASSERT_SAME_TYPE(decltype(std::crbegin(a)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::crend(a)), std::reverse_iterator<const int*>);
assert(std::crbegin(a).base() == a + 3);
assert(std::crend(a).base() == a);
const auto& ca = a;
ASSERT_SAME_TYPE(decltype(std::rbegin(ca)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::rend(ca)), std::reverse_iterator<const int*>);
assert(std::rbegin(ca).base() == a + 3);
assert(std::rend(ca).base() == a);
ASSERT_SAME_TYPE(decltype(std::crbegin(ca)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::crend(ca)), std::reverse_iterator<const int*>);
assert(std::crbegin(ca).base() == a + 3);
assert(std::crend(ca).base() == a);
}
{
std::initializer_list<int> il = {1, 2, 3};
ASSERT_SAME_TYPE(decltype(std::rbegin(il)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::rend(il)), std::reverse_iterator<const int*>);
assert(std::rbegin(il).base() == il.end());
assert(std::rend(il).base() == il.begin());
ASSERT_SAME_TYPE(decltype(std::crbegin(il)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::crend(il)), std::reverse_iterator<const int*>);
assert(std::crbegin(il).base() == il.end());
assert(std::crend(il).base() == il.begin());
const auto& cil = il;
ASSERT_SAME_TYPE(decltype(std::rbegin(cil)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::rend(cil)), std::reverse_iterator<const int*>);
assert(std::rbegin(cil).base() == il.end());
assert(std::rend(cil).base() == il.begin());
ASSERT_SAME_TYPE(decltype(std::crbegin(cil)), std::reverse_iterator<const int*>);
ASSERT_SAME_TYPE(decltype(std::crend(cil)), std::reverse_iterator<const int*>);
assert(std::crbegin(cil).base() == il.end());
assert(std::crend(cil).base() == il.begin());
}
return true;
}
#endif
template<typename C>
TEST_CONSTEXPR_CXX14 bool test_container() {
C c = {1, 2, 3};
ASSERT_SAME_TYPE(decltype(std::begin(c)), typename C::iterator);
ASSERT_SAME_TYPE(decltype(std::end(c)), typename C::iterator);
assert(std::begin(c) == c.begin());
assert(std::end(c) == c.end());
#if TEST_STD_VER > 11
ASSERT_SAME_TYPE(decltype(std::cbegin(c)), typename C::const_iterator);
ASSERT_SAME_TYPE(decltype(std::cend(c)), typename C::const_iterator);
assert(std::cbegin(c) == c.begin());
assert(std::cend(c) == c.end());
ASSERT_SAME_TYPE(decltype(std::rbegin(c)), typename C::reverse_iterator);
ASSERT_SAME_TYPE(decltype(std::rend(c)), typename C::reverse_iterator);
assert(std::rbegin(c).base() == c.end());
assert(std::rend(c).base() == c.begin());
ASSERT_SAME_TYPE(decltype(std::crbegin(c)), typename C::const_reverse_iterator);
ASSERT_SAME_TYPE(decltype(std::crend(c)), typename C::const_reverse_iterator);
assert(std::crbegin(c).base() == c.end());
assert(std::crend(c).base() == c.begin());
#endif
const C& cc = c;
ASSERT_SAME_TYPE(decltype(std::begin(cc)), typename C::const_iterator);
ASSERT_SAME_TYPE(decltype(std::end(cc)), typename C::const_iterator);
assert(std::begin(cc) == c.begin());
assert(std::end(cc) == c.end());
#if TEST_STD_VER > 11
ASSERT_SAME_TYPE(decltype(std::cbegin(cc)), typename C::const_iterator);
ASSERT_SAME_TYPE(decltype(std::cend(cc)), typename C::const_iterator);
assert(std::cbegin(cc) == c.begin());
assert(std::cend(cc) == c.end());
ASSERT_SAME_TYPE(decltype(std::rbegin(cc)), typename C::const_reverse_iterator);
ASSERT_SAME_TYPE(decltype(std::rend(cc)), typename C::const_reverse_iterator);
assert(std::rbegin(cc).base() == c.end());
assert(std::rend(cc).base() == c.begin());
ASSERT_SAME_TYPE(decltype(std::crbegin(cc)), typename C::const_reverse_iterator);
ASSERT_SAME_TYPE(decltype(std::crend(cc)), typename C::const_reverse_iterator);
assert(std::crbegin(cc).base() == c.end());
assert(std::crend(cc).base() == c.begin());
#endif
return true;
}
struct ArrayHijacker {
friend constexpr int begin(ArrayHijacker(&)[3]) { return 42; }
friend constexpr int end(ArrayHijacker(&)[3]) { return 42; }
friend constexpr int begin(const ArrayHijacker(&)[3]) { return 42; }
friend constexpr int end(const ArrayHijacker(&)[3]) { return 42; }
};
struct ContainerHijacker {
int *a_;
constexpr int *begin() const { return a_; }
constexpr int *end() const { return a_ + 3; }
constexpr int *rbegin() const { return a_; }
constexpr int *rend() const { return a_ + 3; }
friend constexpr int begin(ContainerHijacker&) { return 42; }
friend constexpr int end(ContainerHijacker&) { return 42; }
friend constexpr int begin(const ContainerHijacker&) { return 42; }
friend constexpr int end(const ContainerHijacker&) { return 42; }
friend constexpr int cbegin(ContainerHijacker&) { return 42; }
friend constexpr int cend(ContainerHijacker&) { return 42; }
friend constexpr int cbegin(const ContainerHijacker&) { return 42; }
friend constexpr int cend(const ContainerHijacker&) { return 42; }
friend constexpr int rbegin(ContainerHijacker&) { return 42; }
friend constexpr int rend(ContainerHijacker&) { return 42; }
friend constexpr int rbegin(const ContainerHijacker&) { return 42; }
friend constexpr int rend(const ContainerHijacker&) { return 42; }
friend constexpr int crbegin(ContainerHijacker&) { return 42; }
friend constexpr int crend(ContainerHijacker&) { return 42; }
friend constexpr int crbegin(const ContainerHijacker&) { return 42; }
friend constexpr int crend(const ContainerHijacker&) { return 42; }
};
TEST_CONSTEXPR_CXX17 bool test_adl_proofing() {
// https://llvm.org/PR28927
{
ArrayHijacker a[3] = {};
assert(begin(a) == 42);
assert(end(a) == 42);
assert(std::begin(a) == a);
assert(std::end(a) == a + 3);
#if TEST_STD_VER > 11
assert(std::cbegin(a) == a);
assert(std::cend(a) == a + 3);
assert(std::rbegin(a).base() == a + 3);
assert(std::rend(a).base() == a);
assert(std::crbegin(a).base() == a + 3);
assert(std::crend(a).base() == a);
#endif
}
{
int a[3] = {};
ContainerHijacker c{a};
assert(begin(c) == 42);
assert(end(c) == 42);
assert(std::begin(c) == a);
assert(std::end(c) == a + 3);
#if TEST_STD_VER > 11
assert(std::cbegin(c) == a);
assert(std::cend(c) == a + 3);
assert(std::rbegin(c) == a);
assert(std::rend(c) == a + 3);
assert(std::crbegin(c) == a);
assert(std::crend(c) == a + 3);
#endif
}
return true;
}
int main(int, char**) {
test_arrays_and_initializer_lists_forward();
#if TEST_STD_VER > 11
test_arrays_and_initializer_lists_backward();
#endif
test_container<std::array<int, 3>>();
test_container<std::list<int>>();
test_container<std::vector<int>>();
test_adl_proofing();
#if TEST_STD_VER > 11
static_assert(test_arrays_and_initializer_lists_forward(), "");
#endif
#if TEST_STD_VER > 14
static_assert(test_arrays_and_initializer_lists_backward());
static_assert(test_container<std::array<int, 3>>());
static_assert(test_adl_proofing());
#endif
return 0;
}

View File

@ -10,7 +10,8 @@
// <initializer_list>
// template<class E> const E* begin(initializer_list<E> il);
// template<class E> constexpr const E* begin(initializer_list<E> il) noexcept; // constexpr since C++14
// template<class E> constexpr const E* end(initializer_list<E> il) noexcept; // constexpr since C++14
#include <initializer_list>
#include <cassert>
@ -18,44 +19,50 @@
#include "test_macros.h"
struct A
{
A(std::initializer_list<int> il)
{
const int* b = begin(il);
const int* e = end(il);
assert(il.size() == 3);
assert(static_cast<std::size_t>(e - b) == il.size());
assert(*b++ == 3);
assert(*b++ == 2);
assert(*b++ == 1);
}
};
TEST_CONSTEXPR_CXX14 bool test() {
// unqualified begin/end
{
std::initializer_list<int> il = {3, 2, 1};
ASSERT_NOEXCEPT(begin(il));
ASSERT_NOEXCEPT(end(il));
ASSERT_SAME_TYPE(decltype(begin(il)), const int*);
ASSERT_SAME_TYPE(decltype(end(il)), const int*);
const int* b = begin(il);
const int* e = end(il);
assert(il.size() == 3);
assert(static_cast<std::size_t>(e - b) == il.size());
assert(*b++ == 3);
assert(*b++ == 2);
assert(*b++ == 1);
}
#if TEST_STD_VER > 11
struct B
{
constexpr B(std::initializer_list<int> il)
{
const int* b = begin(il);
const int* e = end(il);
assert(il.size() == 3);
assert(static_cast<std::size_t>(e - b) == il.size());
assert(*b++ == 3);
assert(*b++ == 2);
assert(*b++ == 1);
}
};
// qualified begin/end
{
std::initializer_list<int> il = {1, 2, 3};
ASSERT_NOEXCEPT(std::begin(il));
ASSERT_NOEXCEPT(std::end(il));
ASSERT_SAME_TYPE(decltype(std::begin(il)), const int*);
ASSERT_SAME_TYPE(decltype(std::end(il)), const int*);
assert(std::begin(il) == il.begin());
assert(std::end(il) == il.end());
#endif // TEST_STD_VER > 11
const auto& cil = il;
ASSERT_NOEXCEPT(std::begin(cil));
ASSERT_NOEXCEPT(std::end(cil));
ASSERT_SAME_TYPE(decltype(std::begin(cil)), const int*);
ASSERT_SAME_TYPE(decltype(std::end(cil)), const int*);
assert(std::begin(cil) == il.begin());
assert(std::end(cil) == il.end());
}
int main(int, char**)
{
A test1 = {3, 2, 1}; (void)test1;
#if TEST_STD_VER > 11
constexpr B test2 = {3, 2, 1};
(void)test2;
#endif // TEST_STD_VER > 11
return true;
}
int main(int, char**) {
test();
#if TEST_STD_VER >= 14
static_assert(test(), "");
#endif
return 0;
}

View File

@ -3550,7 +3550,6 @@ libcxx/test/std/iterators/iterator.primitives/std.iterator.tags/forward_iterator
libcxx/test/std/iterators/iterator.primitives/std.iterator.tags/input_iterator_tag.pass.cpp
libcxx/test/std/iterators/iterator.primitives/std.iterator.tags/output_iterator_tag.pass.cpp
libcxx/test/std/iterators/iterator.primitives/std.iterator.tags/random_access_iterator_tag.pass.cpp
libcxx/test/std/iterators/iterator.range/begin-end.pass.cpp
libcxx/test/std/iterators/iterator.requirements/alg.req.ind.copy/indirectly_copyable.compile.pass.cpp
libcxx/test/std/iterators/iterator.requirements/alg.req.ind.copy/indirectly_copyable_storable.compile.pass.cpp
libcxx/test/std/iterators/iterator.requirements/alg.req.ind.copy/indirectly_copyable_storable.subsumption.compile.pass.cpp
@ -3901,7 +3900,6 @@ libcxx/test/std/language.support/support.general/nothing_to_do.pass.cpp
libcxx/test/std/language.support/support.initlist/include_cxx03.pass.cpp
libcxx/test/std/language.support/support.initlist/support.initlist.access/access.pass.cpp
libcxx/test/std/language.support/support.initlist/support.initlist.cons/default.pass.cpp
libcxx/test/std/language.support/support.initlist/support.initlist.range/begin_end.pass.cpp
libcxx/test/std/language.support/support.initlist/types.pass.cpp
libcxx/test/std/language.support/support.limits/c.limits/cfloat.pass.cpp
libcxx/test/std/language.support/support.limits/c.limits/climits.pass.cpp