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// <vector>
10
11// reference operator[](size_type __i);
12// const_reference operator[](size_type __i) const;
13//
14// reference at(size_type __i);
15// const_reference at(size_type __i) const;
16//
17// reference front();
18// const_reference front() const;
19//
20// reference back();
21// const_reference back() const;
22// libc++ marks these as 'noexcept' (except 'at')
23
24#include <vector>
25#include <cassert>
26#include <stdexcept>
27
28#include "min_allocator.h"
29#include "test_macros.h"
30
31template <class C>
32TEST_CONSTEXPR_CXX20 C make(int size, int start) {
33 C c;
34 for (int i = 0; i < size; ++i)
35 c.push_back(start + i);
36 return c;
37}
38
39template <class Vector>
40TEST_CONSTEXPR_CXX20 void test_get_basic(Vector& c, int start_value) {
41 const int n = static_cast<int>(c.size());
42 for (int i = 0; i < n; ++i)
43 assert(c[i] == start_value + i);
44 for (int i = 0; i < n; ++i)
45 assert(c.at(i) == start_value + i);
46
47#ifndef TEST_HAS_NO_EXCEPTIONS
48 if (!TEST_IS_CONSTANT_EVALUATED) {
49 try {
50 TEST_IGNORE_NODISCARD c.at(n);
51 assert(false);
52 } catch (const std::out_of_range&) {}
53 }
54#endif
55
56 assert(c.front() == start_value);
57 assert(c.back() == start_value + n - 1);
58}
59
60template <class Vector>
61TEST_CONSTEXPR_CXX20 void test_get() {
62 int start_value = 35;
63 Vector c = make<Vector>(10, start_value);
64 const Vector& cc = c;
65 test_get_basic(c, start_value);
66 test_get_basic(cc, start_value);
67}
68
69template <class Vector>
70TEST_CONSTEXPR_CXX20 void test_set() {
71 int start_value = 35;
72 const int n = 10;
73 Vector c = make<Vector>(n, start_value);
74
75 for (int i = 0; i < n; ++i) {
76 assert(c[i] == start_value + i);
77 c[i] = start_value + i + 1;
78 assert(c[i] == start_value + i + 1);
79 }
80 for (int i = 0; i < n; ++i) {
81 assert(c.at(i) == start_value + i + 1);
82 c.at(i) = start_value + i + 2;
83 assert(c.at(i) == start_value + i + 2);
84 }
85
86 assert(c.front() == start_value + 2);
87 c.front() = start_value + 3;
88 assert(c.front() == start_value + 3);
89
90 assert(c.back() == start_value + n + 1);
91 c.back() = start_value + n + 2;
92 assert(c.back() == start_value + n + 2);
93}
94
95template <class Vector>
96TEST_CONSTEXPR_CXX20 void test() {
97 test_get<Vector>();
98 test_set<Vector>();
99
100 Vector c;
101 const Vector& cc = c;
102 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c[0]));
103 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc[0]));
104
105 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.at(0)));
106 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.at(0)));
107
108 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.front()));
109 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.front()));
110
111 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.back()));
112 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.back()));
113}
114
115TEST_CONSTEXPR_CXX20 bool tests() {
116 test<std::vector<int> >();
117#if TEST_STD_VER >= 11
118 test<std::vector<int, min_allocator<int> > >();
119 test<std::vector<int, safe_allocator<int> > >();
120#endif
121 return true;
122}
123
124int main(int, char**) {
125 tests();
126#if TEST_STD_VER > 17
127 static_assert(tests());
128#endif
129 return 0;
130}
131

source code of libcxx/test/std/containers/sequences/vector/access.pass.cpp