xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/at_const.pass.cpp (revision 71e9a48227a0599130b2f9ed090366bb973c57e5)
1*71e9a482SPeng Liu //===----------------------------------------------------------------------===//
2*71e9a482SPeng Liu //
3*71e9a482SPeng Liu // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71e9a482SPeng Liu // See https://llvm.org/LICENSE.txt for license information.
5*71e9a482SPeng Liu // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71e9a482SPeng Liu //
7*71e9a482SPeng Liu //===----------------------------------------------------------------------===//
8*71e9a482SPeng Liu 
9*71e9a482SPeng Liu // <vector>
10*71e9a482SPeng Liu 
11*71e9a482SPeng Liu // const_reference at(size_type n) const; // constexpr since C++20
12*71e9a482SPeng Liu 
13*71e9a482SPeng Liu #include <cassert>
14*71e9a482SPeng Liu #include <memory>
15*71e9a482SPeng Liu #include <vector>
16*71e9a482SPeng Liu 
17*71e9a482SPeng Liu #include "min_allocator.h"
18*71e9a482SPeng Liu #include "test_allocator.h"
19*71e9a482SPeng Liu #include "test_macros.h"
20*71e9a482SPeng Liu 
21*71e9a482SPeng Liu #ifndef TEST_HAS_NO_EXCEPTIONS
22*71e9a482SPeng Liu #  include <stdexcept>
23*71e9a482SPeng Liu #endif
24*71e9a482SPeng Liu 
25*71e9a482SPeng Liu template <typename Allocator>
26*71e9a482SPeng Liu TEST_CONSTEXPR_CXX20 void test() {
27*71e9a482SPeng Liu   using C               = const std::vector<bool, Allocator>;
28*71e9a482SPeng Liu   using const_reference = typename C::const_reference;
29*71e9a482SPeng Liu   bool a[]              = {1, 0, 1, 0, 1};
30*71e9a482SPeng Liu   C v(a, a + sizeof(a) / sizeof(a[0]));
31*71e9a482SPeng Liu   ASSERT_SAME_TYPE(const_reference, decltype(v.at(0)));
32*71e9a482SPeng Liu   assert(v.at(0) == true);
33*71e9a482SPeng Liu   assert(v.at(1) == false);
34*71e9a482SPeng Liu   assert(v.at(2) == true);
35*71e9a482SPeng Liu   assert(v.at(3) == false);
36*71e9a482SPeng Liu   assert(v.at(4) == true);
37*71e9a482SPeng Liu }
38*71e9a482SPeng Liu 
39*71e9a482SPeng Liu template <typename Allocator>
40*71e9a482SPeng Liu void test_exception() {
41*71e9a482SPeng Liu #ifndef TEST_HAS_NO_EXCEPTIONS
42*71e9a482SPeng Liu   {
43*71e9a482SPeng Liu     bool a[] = {1, 0, 1, 1};
44*71e9a482SPeng Liu     using C  = const std::vector<bool, Allocator>;
45*71e9a482SPeng Liu     C v(a, a + sizeof(a) / sizeof(a[0]));
46*71e9a482SPeng Liu 
47*71e9a482SPeng Liu     try {
48*71e9a482SPeng Liu       TEST_IGNORE_NODISCARD v.at(4);
49*71e9a482SPeng Liu       assert(false);
50*71e9a482SPeng Liu     } catch (std::out_of_range const&) {
51*71e9a482SPeng Liu       // pass
52*71e9a482SPeng Liu     } catch (...) {
53*71e9a482SPeng Liu       assert(false);
54*71e9a482SPeng Liu     }
55*71e9a482SPeng Liu 
56*71e9a482SPeng Liu     try {
57*71e9a482SPeng Liu       TEST_IGNORE_NODISCARD v.at(5);
58*71e9a482SPeng Liu       assert(false);
59*71e9a482SPeng Liu     } catch (std::out_of_range const&) {
60*71e9a482SPeng Liu       // pass
61*71e9a482SPeng Liu     } catch (...) {
62*71e9a482SPeng Liu       assert(false);
63*71e9a482SPeng Liu     }
64*71e9a482SPeng Liu 
65*71e9a482SPeng Liu     try {
66*71e9a482SPeng Liu       TEST_IGNORE_NODISCARD v.at(6);
67*71e9a482SPeng Liu       assert(false);
68*71e9a482SPeng Liu     } catch (std::out_of_range const&) {
69*71e9a482SPeng Liu       // pass
70*71e9a482SPeng Liu     } catch (...) {
71*71e9a482SPeng Liu       assert(false);
72*71e9a482SPeng Liu     }
73*71e9a482SPeng Liu 
74*71e9a482SPeng Liu     try {
75*71e9a482SPeng Liu       using size_type = typename C::size_type;
76*71e9a482SPeng Liu       TEST_IGNORE_NODISCARD v.at(static_cast<size_type>(-1));
77*71e9a482SPeng Liu       assert(false);
78*71e9a482SPeng Liu     } catch (std::out_of_range const&) {
79*71e9a482SPeng Liu       // pass
80*71e9a482SPeng Liu     } catch (...) {
81*71e9a482SPeng Liu       assert(false);
82*71e9a482SPeng Liu     }
83*71e9a482SPeng Liu   }
84*71e9a482SPeng Liu 
85*71e9a482SPeng Liu   {
86*71e9a482SPeng Liu     std::vector<bool, Allocator> v;
87*71e9a482SPeng Liu     try {
88*71e9a482SPeng Liu       TEST_IGNORE_NODISCARD v.at(0);
89*71e9a482SPeng Liu       assert(false);
90*71e9a482SPeng Liu     } catch (std::out_of_range const&) {
91*71e9a482SPeng Liu       // pass
92*71e9a482SPeng Liu     } catch (...) {
93*71e9a482SPeng Liu       assert(false);
94*71e9a482SPeng Liu     }
95*71e9a482SPeng Liu   }
96*71e9a482SPeng Liu #endif
97*71e9a482SPeng Liu }
98*71e9a482SPeng Liu 
99*71e9a482SPeng Liu TEST_CONSTEXPR_CXX20 bool tests() {
100*71e9a482SPeng Liu   test<std::allocator<bool> >();
101*71e9a482SPeng Liu   test<min_allocator<bool> >();
102*71e9a482SPeng Liu   test<test_allocator<bool> >();
103*71e9a482SPeng Liu   return true;
104*71e9a482SPeng Liu }
105*71e9a482SPeng Liu 
106*71e9a482SPeng Liu void test_exceptions() {
107*71e9a482SPeng Liu   test_exception<std::allocator<bool> >();
108*71e9a482SPeng Liu   test_exception<min_allocator<bool> >();
109*71e9a482SPeng Liu   test_exception<test_allocator<bool> >();
110*71e9a482SPeng Liu }
111*71e9a482SPeng Liu 
112*71e9a482SPeng Liu int main(int, char**) {
113*71e9a482SPeng Liu   tests();
114*71e9a482SPeng Liu   test_exceptions();
115*71e9a482SPeng Liu 
116*71e9a482SPeng Liu #if TEST_STD_VER >= 20
117*71e9a482SPeng Liu   static_assert(tests());
118*71e9a482SPeng Liu #endif
119*71e9a482SPeng Liu 
120*71e9a482SPeng Liu   return 0;
121*71e9a482SPeng Liu }
122