122 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <vector>10 11// const_reference at(size_type n) const; // constexpr since C++2012 13#include <cassert>14#include <memory>15#include <vector>16 17#include "min_allocator.h"18#include "test_allocator.h"19#include "test_macros.h"20 21#ifndef TEST_HAS_NO_EXCEPTIONS22# include <stdexcept>23#endif24 25template <typename Allocator>26TEST_CONSTEXPR_CXX20 void test() {27 using C = const std::vector<bool, Allocator>;28 using const_reference = typename C::const_reference;29 bool a[] = {1, 0, 1, 0, 1};30 C v(a, a + sizeof(a) / sizeof(a[0]));31 ASSERT_SAME_TYPE(const_reference, decltype(v.at(0)));32 assert(v.at(0) == true);33 assert(v.at(1) == false);34 assert(v.at(2) == true);35 assert(v.at(3) == false);36 assert(v.at(4) == true);37}38 39template <typename Allocator>40void test_exception() {41#ifndef TEST_HAS_NO_EXCEPTIONS42 {43 bool a[] = {1, 0, 1, 1};44 using C = const std::vector<bool, Allocator>;45 C v(a, a + sizeof(a) / sizeof(a[0]));46 47 try {48 TEST_IGNORE_NODISCARD v.at(4);49 assert(false);50 } catch (std::out_of_range const&) {51 // pass52 } catch (...) {53 assert(false);54 }55 56 try {57 TEST_IGNORE_NODISCARD v.at(5);58 assert(false);59 } catch (std::out_of_range const&) {60 // pass61 } catch (...) {62 assert(false);63 }64 65 try {66 TEST_IGNORE_NODISCARD v.at(6);67 assert(false);68 } catch (std::out_of_range const&) {69 // pass70 } catch (...) {71 assert(false);72 }73 74 try {75 using size_type = typename C::size_type;76 TEST_IGNORE_NODISCARD v.at(static_cast<size_type>(-1));77 assert(false);78 } catch (std::out_of_range const&) {79 // pass80 } catch (...) {81 assert(false);82 }83 }84 85 {86 std::vector<bool, Allocator> v;87 try {88 TEST_IGNORE_NODISCARD v.at(0);89 assert(false);90 } catch (std::out_of_range const&) {91 // pass92 } catch (...) {93 assert(false);94 }95 }96#endif97}98 99TEST_CONSTEXPR_CXX20 bool tests() {100 test<std::allocator<bool> >();101 test<min_allocator<bool> >();102 test<test_allocator<bool> >();103 return true;104}105 106void test_exceptions() {107 test_exception<std::allocator<bool> >();108 test_exception<min_allocator<bool> >();109 test_exception<test_allocator<bool> >();110}111 112int main(int, char**) {113 tests();114 test_exceptions();115 116#if TEST_STD_VER >= 20117 static_assert(tests());118#endif119 120 return 0;121}122