1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // reference operator[](size_type __i);
12 // const_reference operator[](size_type __i) const;
14 // reference at(size_type __i);
15 // const_reference at(size_type __i) const;
18 // const_reference front() const;
21 // const_reference back() const;
22 // libc++ marks these as 'noexcept' (except 'at')
27 #include "min_allocator.h"
28 #include "test_macros.h"
32 make(int size
, int start
= 0)
35 for (int i
= 0; i
< size
; ++i
)
36 c
.push_back(start
+ i
);
43 typedef std::vector
<int> C
;
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
)
55 for (int i
= 0; i
< 10; ++i
)
57 assert(c
.front() == 0);
58 assert(c
.back() == 9);
61 typedef std::vector
<int> C
;
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);
79 #if TEST_STD_VER >= 11
81 typedef std::vector
<int, min_allocator
<int>> C
;
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);
100 typedef std::vector
<int, min_allocator
<int>> C
;
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);