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 
27 #include "min_allocator.h"
28 #include "test_macros.h"
29 
30 template <class C>
31 C
32 make(int size, int start)
33 {
34     C c;
35     for (int i = 0; i < size; ++i)
36         c.push_back(start + i);
37     return c;
38 }
39 
40 template <class Vector>
41 void test_get_basic(Vector& c, int start_value) {
42     const int n = c.size();
43     for (int i = 0; i < n; ++i)
44         assert(c[i] == start_value + i);
45     for (int i = 0; i < n; ++i)
46         assert(c.at(i) == start_value + i);
47 
48 #ifndef TEST_HAS_NO_EXCEPTIONS
49     try {
50         c.at(n);
51         assert(false);
52     } catch (const std::out_of_range&) {}
53 #endif
54 
55     assert(c.front() == start_value);
56     assert(c.back() == start_value + n - 1);
57 }
58 
59 template <class Vector>
60 void test_get() {
61     int start_value = 35;
62     Vector c = make<Vector>(10, start_value);
63     const Vector& cc = c;
64     test_get_basic(c, start_value);
65     test_get_basic(cc, start_value);
66 }
67 
68 template <class Vector>
69 void test_set() {
70     int start_value = 35;
71     const int n = 10;
72     Vector c = make<Vector>(n, start_value);
73 
74     for (int i = 0; i < n; ++i) {
75         assert(c[i] == start_value + i);
76         c[i] = start_value + i + 1;
77         assert(c[i] == start_value + i + 1);
78     }
79     for (int i = 0; i < n; ++i) {
80         assert(c.at(i) == start_value + i + 1);
81         c.at(i) = start_value + i + 2;
82         assert(c.at(i) == start_value + i + 2);
83     }
84 
85     assert(c.front() == start_value + 2);
86     c.front() = start_value + 3;
87     assert(c.front() == start_value + 3);
88 
89     assert(c.back() == start_value + n + 1);
90     c.back() = start_value + n + 2;
91     assert(c.back() == start_value + n + 2);
92 }
93 
94 template <class Vector>
95 void test() {
96     test_get<Vector>();
97     test_set<Vector>();
98 
99     Vector c;
100     const Vector& cc = c;
101     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c[0]));
102     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc[0]));
103 
104     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.at(0)));
105     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.at(0)));
106 
107     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.front()));
108     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.front()));
109 
110     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.back()));
111     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.back()));
112 }
113 
114 int main(int, char**)
115 {
116     test<std::vector<int> >();
117 #if TEST_STD_VER >= 11
118     test<std::vector<int, min_allocator<int> > >();
119 #endif
120 
121   return 0;
122 }
123