| 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 | // vector<bool> |
| 11 | |
| 12 | // XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 13 | |
| 14 | // This test ensures that std::vector<bool> handles allocator types with small size types |
| 15 | // properly. Related issue: https://github.com/llvm/llvm-project/issues/121713. |
| 16 | |
| 17 | #include <cassert> |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| 20 | #include <limits> |
| 21 | #include <memory> |
| 22 | #include <new> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "sized_allocator.h" |
| 26 | #include "test_macros.h" |
| 27 | |
| 28 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 29 | { |
| 30 | using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 31 | std::vector<bool, Alloc> c(Alloc(1)); |
| 32 | c.resize(10); |
| 33 | assert(c.size() == 10); |
| 34 | assert(c.capacity() >= 10); |
| 35 | } |
| 36 | { |
| 37 | using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 38 | std::vector<bool, Alloc> c(Alloc(1)); |
| 39 | c.assign(10, true); |
| 40 | assert(c.size() == 10); |
| 41 | assert(c.capacity() >= 10); |
| 42 | } |
| 43 | { |
| 44 | using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 45 | std::vector<bool, Alloc> c(Alloc(1)); |
| 46 | c.insert(c.end(), true); |
| 47 | assert(c.size() == 1); |
| 48 | assert(c.capacity() >= 1); |
| 49 | } |
| 50 | { |
| 51 | using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 52 | std::vector<bool, Alloc> c(Alloc(1)); |
| 53 | c.insert(c.end(), 10, true); |
| 54 | assert(c.size() == 10); |
| 55 | assert(c.capacity() >= 10); |
| 56 | } |
| 57 | { |
| 58 | using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 59 | std::vector<bool, Alloc> c(Alloc(1)); |
| 60 | c.push_back(true); |
| 61 | assert(c.size() == 1); |
| 62 | assert(c.capacity() >= 1); |
| 63 | } |
| 64 | { |
| 65 | using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 66 | std::vector<bool, Alloc> c(Alloc(1)); |
| 67 | c.resize(10, true); |
| 68 | assert(c.size() == 10); |
| 69 | assert(c.capacity() >= 10); |
| 70 | } |
| 71 | { |
| 72 | using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>; |
| 73 | std::vector<bool, Alloc> c(Alloc(1)); |
| 74 | c.resize(10); |
| 75 | assert(c.size() == 10); |
| 76 | assert(c.capacity() >= 10); |
| 77 | } |
| 78 | { |
| 79 | using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>; |
| 80 | std::vector<bool, Alloc> c(Alloc(1)); |
| 81 | c.resize(10); |
| 82 | assert(c.size() == 10); |
| 83 | assert(c.capacity() >= 10); |
| 84 | } |
| 85 | { |
| 86 | using Alloc = sized_allocator<bool, std::size_t, std::ptrdiff_t>; |
| 87 | std::vector<bool, Alloc> c(Alloc(1)); |
| 88 | c.resize(10); |
| 89 | assert(c.size() == 10); |
| 90 | assert(c.capacity() >= 10); |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | int main(int, char**) { |
| 97 | tests(); |
| 98 | #if TEST_STD_VER >= 20 |
| 99 | static_assert(tests()); |
| 100 | #endif |
| 101 | return 0; |
| 102 | } |
| 103 | |