| 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 | // const_reference at(size_type n) const; // constexpr since C++20 |
| 12 | |
| 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_EXCEPTIONS |
| 22 | # include <stdexcept> |
| 23 | #endif |
| 24 | |
| 25 | template <typename Allocator> |
| 26 | TEST_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 | |
| 39 | template <typename Allocator> |
| 40 | void test_exception() { |
| 41 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 42 | { |
| 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 | // pass |
| 52 | } 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 | // pass |
| 61 | } 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 | // pass |
| 70 | } 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 | // pass |
| 80 | } 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 | // pass |
| 92 | } catch (...) { |
| 93 | assert(false); |
| 94 | } |
| 95 | } |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 100 | test<std::allocator<bool> >(); |
| 101 | test<min_allocator<bool> >(); |
| 102 | test<test_allocator<bool> >(); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | void test_exceptions() { |
| 107 | test_exception<std::allocator<bool> >(); |
| 108 | test_exception<min_allocator<bool> >(); |
| 109 | test_exception<test_allocator<bool> >(); |
| 110 | } |
| 111 | |
| 112 | int main(int, char**) { |
| 113 | tests(); |
| 114 | test_exceptions(); |
| 115 | |
| 116 | #if TEST_STD_VER >= 20 |
| 117 | static_assert(tests()); |
| 118 | #endif |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |