xref: /llvm-project/libcxx/test/std/iterators/iterator.range/begin-end.array.pass.cpp (revision cb950c9dc9bff37367068310bdfad75c7274915c)
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 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <iterator>
12 //
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;
15 //
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
18 
19 #include <cassert>
20 #include <iterator>
21 
22 #include "test_macros.h"
23 
test()24 TEST_CONSTEXPR_CXX14 bool test() {
25   int a[]        = {1, 2, 3};
26   const auto& ca = a;
27 
28   // std::begin(T (&)[N]) / std::end(T (&)[N])
29   {
30     ASSERT_NOEXCEPT(std::begin(a));
31     ASSERT_SAME_TYPE(decltype(std::begin(a)), int*);
32     assert(std::begin(a) == a);
33 
34     ASSERT_NOEXCEPT(std::end(a));
35     ASSERT_SAME_TYPE(decltype(std::end(a)), int*);
36     assert(std::end(a) == a + 3);
37 
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);
42 
43     ASSERT_NOEXCEPT(std::end(ca));
44     ASSERT_SAME_TYPE(decltype(std::end(ca)), const int*);
45     assert(std::end(ca) == ca + 3);
46   }
47 
48   return true;
49 }
50 
test_r()51 TEST_CONSTEXPR_CXX17 bool test_r() {
52 #if TEST_STD_VER >= 14
53   int a[]        = {1, 2, 3};
54   const auto& ca = a;
55 
56   // std::rbegin(T (&)[N]) / std::rend(T (&)[N])
57   {
58     ASSERT_SAME_TYPE(decltype(std::rbegin(a)), std::reverse_iterator<int*>);
59     assert(std::rbegin(a).base() == a + 3);
60 
61     ASSERT_SAME_TYPE(decltype(std::rend(a)), std::reverse_iterator<int*>);
62     assert(std::rend(a).base() == a);
63 
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);
67 
68     ASSERT_SAME_TYPE(decltype(std::rend(ca)), std::reverse_iterator<const int*>);
69     assert(std::rend(ca).base() == ca);
70   }
71 #endif
72 
73   return true;
74 }
75 
main(int,char **)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
85 
86   // Make sure std::begin(T (&)[N]) and std::end(T (&)[N]) are constexpr in C++11 too (see LWG2280).
87   {
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);
93   }
94 
95   return 0;
96 }
97