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 // reference at(size_type n); // 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 = std::vector<bool, Allocator>; 28*71e9a482SPeng Liu using reference = typename C::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(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 v.at(1) = 1; 38*71e9a482SPeng Liu assert(v.at(1) == true); 39*71e9a482SPeng Liu v.at(3) = 1; 40*71e9a482SPeng Liu assert(v.at(3) == true); 41*71e9a482SPeng Liu } 42*71e9a482SPeng Liu 43*71e9a482SPeng Liu template <typename Allocator> 44*71e9a482SPeng Liu void test_exception() { 45*71e9a482SPeng Liu #ifndef TEST_HAS_NO_EXCEPTIONS 46*71e9a482SPeng Liu { 47*71e9a482SPeng Liu bool a[] = {1, 0, 1, 1}; 48*71e9a482SPeng Liu using C = std::vector<bool, Allocator>; 49*71e9a482SPeng Liu C v(a, a + sizeof(a) / sizeof(a[0])); 50*71e9a482SPeng Liu 51*71e9a482SPeng Liu try { 52*71e9a482SPeng Liu TEST_IGNORE_NODISCARD v.at(4); 53*71e9a482SPeng Liu assert(false); 54*71e9a482SPeng Liu } catch (std::out_of_range const&) { 55*71e9a482SPeng Liu // pass 56*71e9a482SPeng Liu } catch (...) { 57*71e9a482SPeng Liu assert(false); 58*71e9a482SPeng Liu } 59*71e9a482SPeng Liu 60*71e9a482SPeng Liu try { 61*71e9a482SPeng Liu TEST_IGNORE_NODISCARD v.at(5); 62*71e9a482SPeng Liu assert(false); 63*71e9a482SPeng Liu } catch (std::out_of_range const&) { 64*71e9a482SPeng Liu // pass 65*71e9a482SPeng Liu } catch (...) { 66*71e9a482SPeng Liu assert(false); 67*71e9a482SPeng Liu } 68*71e9a482SPeng Liu 69*71e9a482SPeng Liu try { 70*71e9a482SPeng Liu TEST_IGNORE_NODISCARD v.at(6); 71*71e9a482SPeng Liu assert(false); 72*71e9a482SPeng Liu } catch (std::out_of_range const&) { 73*71e9a482SPeng Liu // pass 74*71e9a482SPeng Liu } catch (...) { 75*71e9a482SPeng Liu assert(false); 76*71e9a482SPeng Liu } 77*71e9a482SPeng Liu 78*71e9a482SPeng Liu try { 79*71e9a482SPeng Liu using size_type = typename C::size_type; 80*71e9a482SPeng Liu TEST_IGNORE_NODISCARD v.at(static_cast<size_type>(-1)); 81*71e9a482SPeng Liu assert(false); 82*71e9a482SPeng Liu } catch (std::out_of_range const&) { 83*71e9a482SPeng Liu // pass 84*71e9a482SPeng Liu } catch (...) { 85*71e9a482SPeng Liu assert(false); 86*71e9a482SPeng Liu } 87*71e9a482SPeng Liu } 88*71e9a482SPeng Liu 89*71e9a482SPeng Liu { 90*71e9a482SPeng Liu std::vector<bool, Allocator> v; 91*71e9a482SPeng Liu try { 92*71e9a482SPeng Liu TEST_IGNORE_NODISCARD v.at(0); 93*71e9a482SPeng Liu assert(false); 94*71e9a482SPeng Liu } catch (std::out_of_range const&) { 95*71e9a482SPeng Liu // pass 96*71e9a482SPeng Liu } catch (...) { 97*71e9a482SPeng Liu assert(false); 98*71e9a482SPeng Liu } 99*71e9a482SPeng Liu } 100*71e9a482SPeng Liu #endif 101*71e9a482SPeng Liu } 102*71e9a482SPeng Liu 103*71e9a482SPeng Liu TEST_CONSTEXPR_CXX20 bool tests() { 104*71e9a482SPeng Liu test<std::allocator<bool> >(); 105*71e9a482SPeng Liu test<min_allocator<bool> >(); 106*71e9a482SPeng Liu test<test_allocator<bool> >(); 107*71e9a482SPeng Liu return true; 108*71e9a482SPeng Liu } 109*71e9a482SPeng Liu 110*71e9a482SPeng Liu void test_exceptions() { 111*71e9a482SPeng Liu test_exception<std::allocator<bool> >(); 112*71e9a482SPeng Liu test_exception<min_allocator<bool> >(); 113*71e9a482SPeng Liu test_exception<test_allocator<bool> >(); 114*71e9a482SPeng Liu } 115*71e9a482SPeng Liu 116*71e9a482SPeng Liu int main(int, char**) { 117*71e9a482SPeng Liu tests(); 118*71e9a482SPeng Liu test_exceptions(); 119*71e9a482SPeng Liu 120*71e9a482SPeng Liu #if TEST_STD_VER >= 20 121*71e9a482SPeng Liu static_assert(tests()); 122*71e9a482SPeng Liu #endif 123*71e9a482SPeng Liu 124*71e9a482SPeng Liu return 0; 125*71e9a482SPeng Liu } 126