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 = 0)
33 {
34     C c;
35     for (int i = 0; i < size; ++i)
36         c.push_back(start + i);
37     return c;
38 }
39 
40 int main(int, char**)
41 {
42     {
43         typedef std::vector<int> C;
44         C c = make<C>(10);
45         LIBCPP_ASSERT_NOEXCEPT(c[0]);
46         LIBCPP_ASSERT_NOEXCEPT(c.front());
47         LIBCPP_ASSERT_NOEXCEPT(c.back());
48         // at() is NOT noexcept
49         ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
50         ASSERT_SAME_TYPE(C::reference, decltype(c.at(0)));
51         ASSERT_SAME_TYPE(C::reference, decltype(c.front()));
52         ASSERT_SAME_TYPE(C::reference, decltype(c.back()));
53         for (int i = 0; i < 10; ++i)
54             assert(c[i] == i);
55         for (int i = 0; i < 10; ++i)
56             assert(c.at(i) == i);
57         assert(c.front() == 0);
58         assert(c.back() == 9);
59     }
60     {
61         typedef std::vector<int> C;
62         const int N = 5;
63         const C c = make<C>(10, N);
64         LIBCPP_ASSERT_NOEXCEPT(c[0]);
65         LIBCPP_ASSERT_NOEXCEPT(c.front());
66         LIBCPP_ASSERT_NOEXCEPT(c.back());
67         // at() is NOT noexcept
68         ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
69         ASSERT_SAME_TYPE(C::const_reference, decltype(c.at(0)));
70         ASSERT_SAME_TYPE(C::const_reference, decltype(c.front()));
71         ASSERT_SAME_TYPE(C::const_reference, decltype(c.back()));
72         for (int i = 0; i < 10; ++i)
73             assert(c[i] == N + i);
74         for (int i = 0; i < 10; ++i)
75             assert(c.at(i) == N + i);
76         assert(c.front() == N);
77         assert(c.back() == N + 9);
78     }
79 #if TEST_STD_VER >= 11
80     {
81         typedef std::vector<int, min_allocator<int>> C;
82         const int N = 34;
83         C c = make<C>(10, N);
84         LIBCPP_ASSERT_NOEXCEPT(c[0]);
85         LIBCPP_ASSERT_NOEXCEPT(c.front());
86         LIBCPP_ASSERT_NOEXCEPT(c.back());
87         // at() is NOT noexcept
88         ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
89         ASSERT_SAME_TYPE(C::reference, decltype(c.at(0)));
90         ASSERT_SAME_TYPE(C::reference, decltype(c.front()));
91         ASSERT_SAME_TYPE(C::reference, decltype(c.back()));
92         for (int i = 0; i < 10; ++i)
93             assert(c[i] == N + i);
94         for (int i = 0; i < 10; ++i)
95             assert(c.at(i) == N + i);
96         assert(c.front() == N);
97         assert(c.back() == N + 9);
98     }
99     {
100         typedef std::vector<int, min_allocator<int>> C;
101         const int N = 23;
102         const C c = make<C>(10, N);
103         LIBCPP_ASSERT_NOEXCEPT(c[0]);
104         LIBCPP_ASSERT_NOEXCEPT(c.front());
105         LIBCPP_ASSERT_NOEXCEPT(c.back());
106         // at() is NOT noexcept
107         ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
108         ASSERT_SAME_TYPE(C::const_reference, decltype(c.at(0)));
109         ASSERT_SAME_TYPE(C::const_reference, decltype(c.front()));
110         ASSERT_SAME_TYPE(C::const_reference, decltype(c.back()));
111         for (int i = 0; i < 10; ++i)
112             assert(c[i] == N + i);
113         for (int i = 0; i < 10; ++i)
114             assert(c.at(i) == N + i);
115         assert(c.front() == N);
116         assert(c.back() == N + 9);
117     }
118 #endif
119 
120   return 0;
121 }
122