[libc][test] Adjust header paths in tests (#119623)
[llvm-project.git] / libcxx / test / std / iterators / iterator.range / begin-end.array.pass.cpp
blob5161069ec8667838877ebcdf0bb0129e40dfca1c
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 // <iterator>
13 // template <class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
14 // template <class T, size_t N> constexpr T* end(T (&array)[N]) noexcept;
16 // template <class T, size_t N> constexpr reverse_iterator<T*> rbegin(T (&array)[N]); // C++14, constexpr since C++17
17 // template <class T, size_t N> constexpr reverse_iterator<T*> rend(T (&array)[N]); // C++14, constexpr since C++17
19 #include <cassert>
20 #include <iterator>
22 #include "test_macros.h"
24 TEST_CONSTEXPR_CXX14 bool test() {
25 int a[] = {1, 2, 3};
26 const auto& ca = a;
28 // std::begin(T (&)[N]) / std::end(T (&)[N])
30 ASSERT_NOEXCEPT(std::begin(a));
31 ASSERT_SAME_TYPE(decltype(std::begin(a)), int*);
32 assert(std::begin(a) == a);
34 ASSERT_NOEXCEPT(std::end(a));
35 ASSERT_SAME_TYPE(decltype(std::end(a)), int*);
36 assert(std::end(a) == a + 3);
38 // kind of overkill since it follows from the definition, but worth testing
39 ASSERT_NOEXCEPT(std::begin(ca));
40 ASSERT_SAME_TYPE(decltype(std::begin(ca)), const int*);
41 assert(std::begin(ca) == ca);
43 ASSERT_NOEXCEPT(std::end(ca));
44 ASSERT_SAME_TYPE(decltype(std::end(ca)), const int*);
45 assert(std::end(ca) == ca + 3);
48 return true;
51 TEST_CONSTEXPR_CXX17 bool test_r() {
52 #if TEST_STD_VER >= 14
53 int a[] = {1, 2, 3};
54 const auto& ca = a;
56 // std::rbegin(T (&)[N]) / std::rend(T (&)[N])
58 ASSERT_SAME_TYPE(decltype(std::rbegin(a)), std::reverse_iterator<int*>);
59 assert(std::rbegin(a).base() == a + 3);
61 ASSERT_SAME_TYPE(decltype(std::rend(a)), std::reverse_iterator<int*>);
62 assert(std::rend(a).base() == a);
64 // kind of overkill since it follows from the definition, but worth testing
65 ASSERT_SAME_TYPE(decltype(std::rbegin(ca)), std::reverse_iterator<const int*>);
66 assert(std::rbegin(ca).base() == ca + 3);
68 ASSERT_SAME_TYPE(decltype(std::rend(ca)), std::reverse_iterator<const int*>);
69 assert(std::rend(ca).base() == ca);
71 #endif
73 return true;
76 int main(int, char**) {
77 test();
78 test_r();
79 #if TEST_STD_VER >= 14
80 static_assert(test(), "");
81 #endif
82 #if TEST_STD_VER >= 17
83 static_assert(test_r(), "");
84 #endif
86 // Make sure std::begin(T (&)[N]) and std::end(T (&)[N]) are constexpr in C++11 too (see LWG2280).
88 static constexpr int a[] = {1, 2, 3};
89 constexpr auto b = std::begin(a);
90 assert(b == a);
91 constexpr auto e = std::end(a);
92 assert(e == a + 3);
95 return 0;