• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // <array>
10 
11 // iterator begin() noexcept;                         // constexpr in C++17
12 // const_iterator begin() const noexcept;             // constexpr in C++17
13 // iterator end() noexcept;                           // constexpr in C++17
14 // const_iterator end() const noexcept;               // constexpr in C++17
15 //
16 // reverse_iterator rbegin() noexcept;                // constexpr in C++17
17 // const_reverse_iterator rbegin() const noexcept;    // constexpr in C++17
18 // reverse_iterator rend() noexcept;                  // constexpr in C++17
19 // const_reverse_iterator rend() const noexcept;      // constexpr in C++17
20 //
21 // const_iterator cbegin() const noexcept;            // constexpr in C++17
22 // const_iterator cend() const noexcept;              // constexpr in C++17
23 // const_reverse_iterator crbegin() const noexcept;   // constexpr in C++17
24 // const_reverse_iterator crend() const noexcept;     // constexpr in C++17
25 
26 #include <array>
27 #include <iterator>
28 #include <cassert>
29 
30 #include "test_macros.h"
31 
32 // std::array is explicitly allowed to be initialized with A a = { init-list };.
33 // Disable the missing braces warning for this reason.
34 #include "disable_missing_braces_warning.h"
35 
36 struct NoDefault {
NoDefaultNoDefault37     TEST_CONSTEXPR NoDefault(int) { }
38 };
39 
tests()40 TEST_CONSTEXPR_CXX17 bool tests()
41 {
42     {
43         typedef std::array<int, 5> C;
44         C array = {};
45         typename C::iterator i = array.begin();
46         typename C::const_iterator j = array.cbegin();
47         assert(i == j);
48     }
49     {
50         typedef std::array<int, 0> C;
51         C array = {};
52         typename C::iterator i = array.begin();
53         typename C::const_iterator j = array.cbegin();
54         assert(i == j);
55     }
56 
57     {
58         typedef std::array<int, 0> C;
59         C array = {};
60         typename C::iterator i = array.begin();
61         typename C::const_iterator j = array.cbegin();
62         assert(i == array.end());
63         assert(j == array.cend());
64     }
65     {
66         typedef std::array<int, 1> C;
67         C array = {1};
68         typename C::iterator i = array.begin();
69         assert(*i == 1);
70         assert(&*i == array.data());
71         *i = 99;
72         assert(array[0] == 99);
73     }
74     {
75         typedef std::array<int, 2> C;
76         C array = {1, 2};
77         typename C::iterator i = array.begin();
78         assert(*i == 1);
79         assert(&*i == array.data());
80         *i = 99;
81         assert(array[0] == 99);
82         assert(array[1] == 2);
83     }
84     {
85         typedef std::array<double, 3> C;
86         C array = {1, 2, 3.5};
87         typename C::iterator i = array.begin();
88         assert(*i == 1);
89         assert(&*i == array.data());
90         *i = 5.5;
91         assert(array[0] == 5.5);
92         assert(array[1] == 2.0);
93     }
94     {
95         typedef std::array<NoDefault, 0> C;
96         C array = {};
97         typename C::iterator ib = array.begin();
98         typename C::iterator ie = array.end();
99         assert(ib == ie);
100     }
101 
102 #if TEST_STD_VER >= 14
103     { // N3644 testing
104         {
105             typedef std::array<int, 5> C;
106             C::iterator ii1{}, ii2{};
107             C::iterator ii4 = ii1;
108             C::const_iterator cii{};
109             assert(ii1 == ii2);
110             assert(ii1 == ii4);
111             assert(ii1 == cii);
112 
113             assert(!(ii1 != ii2));
114             assert(!(ii1 != cii));
115 
116             C c = {};
117             assert(c.begin()   == std::begin(c));
118             assert(c.cbegin()  == std::cbegin(c));
119             assert(c.rbegin()  == std::rbegin(c));
120             assert(c.crbegin() == std::crbegin(c));
121             assert(c.end()     == std::end(c));
122             assert(c.cend()    == std::cend(c));
123             assert(c.rend()    == std::rend(c));
124             assert(c.crend()   == std::crend(c));
125 
126             assert(std::begin(c)   != std::end(c));
127             assert(std::rbegin(c)  != std::rend(c));
128             assert(std::cbegin(c)  != std::cend(c));
129             assert(std::crbegin(c) != std::crend(c));
130         }
131         {
132             typedef std::array<int, 0> C;
133             C::iterator ii1{}, ii2{};
134             C::iterator ii4 = ii1;
135             C::const_iterator cii{};
136             assert(ii1 == ii2);
137             assert(ii1 == ii4);
138 
139             assert(!(ii1 != ii2));
140 
141             assert( (ii1 == cii));
142             assert( (cii == ii1));
143             assert(!(ii1 != cii));
144             assert(!(cii != ii1));
145             assert(!(ii1 <  cii));
146             assert(!(cii <  ii1));
147             assert( (ii1 <= cii));
148             assert( (cii <= ii1));
149             assert(!(ii1 >  cii));
150             assert(!(cii >  ii1));
151             assert( (ii1 >= cii));
152             assert( (cii >= ii1));
153             assert(cii - ii1 == 0);
154             assert(ii1 - cii == 0);
155 
156             C c = {};
157             assert(c.begin()   == std::begin(c));
158             assert(c.cbegin()  == std::cbegin(c));
159             assert(c.rbegin()  == std::rbegin(c));
160             assert(c.crbegin() == std::crbegin(c));
161             assert(c.end()     == std::end(c));
162             assert(c.cend()    == std::cend(c));
163             assert(c.rend()    == std::rend(c));
164             assert(c.crend()   == std::crend(c));
165 
166             assert(std::begin(c)   == std::end(c));
167             assert(std::rbegin(c)  == std::rend(c));
168             assert(std::cbegin(c)  == std::cend(c));
169             assert(std::crbegin(c) == std::crend(c));
170         }
171     }
172 #endif
173     return true;
174 }
175 
main(int,char **)176 int main(int, char**)
177 {
178     tests();
179 #if TEST_STD_VER >= 17
180     static_assert(tests(), "");
181 #endif
182   return 0;
183 }
184