| 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 | // vector(const vector& v, const allocator_type& a); |
| 13 | |
| 14 | #include <array> |
| 15 | #include <cassert> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "min_allocator.h" |
| 19 | #include "test_allocator.h" |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | template <class C> |
| 23 | TEST_CONSTEXPR_CXX20 void test(const C& x, const typename C::allocator_type& a) { |
| 24 | typename C::size_type s = x.size(); |
| 25 | C c(x, a); |
| 26 | LIBCPP_ASSERT(c.__invariants()); |
| 27 | assert(c.size() == s); |
| 28 | assert(c == x); |
| 29 | assert(c.get_allocator() == a); |
| 30 | } |
| 31 | |
| 32 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 33 | std::array<int, 5> a1 = {1, 0, 1, 0, 1}; |
| 34 | std::array<int, 18> a2 = {0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0}; |
| 35 | std::array<int, 33> a3 = {0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0}; |
| 36 | std::array<int, 65> a4 = {0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0}; |
| 37 | std::array<int, 299> a5 = {}; |
| 38 | for (unsigned i = 0; i < a5.size(); i += 2) |
| 39 | a5[i] = 1; |
| 40 | |
| 41 | // Tests for allocator-extended copy constructor with word size up to 5 (i.e., bit size > 256 on a 64-bit system) |
| 42 | { // Test with the default std::allocator |
| 43 | test(std::vector<bool>(a1.begin(), a1.end()), std::allocator<bool>()); |
| 44 | test(std::vector<bool>(a2.begin(), a2.end()), std::allocator<bool>()); |
| 45 | test(std::vector<bool>(a3.begin(), a3.end()), std::allocator<bool>()); |
| 46 | test(std::vector<bool>(a4.begin(), a4.end()), std::allocator<bool>()); |
| 47 | test(std::vector<bool>(a5.begin(), a5.end()), std::allocator<bool>()); |
| 48 | } |
| 49 | { // Test with test_allocator |
| 50 | using A = test_allocator<bool>; |
| 51 | using C = std::vector<bool, A>; |
| 52 | test(C(a1.begin(), a1.end(), A(5)), A(3)); |
| 53 | test(C(a2.begin(), a2.end(), A(5)), A(3)); |
| 54 | test(C(a3.begin(), a3.end(), A(5)), A(3)); |
| 55 | test(C(a4.begin(), a4.end(), A(5)), A(3)); |
| 56 | test(C(a5.begin(), a5.end(), A(5)), A(3)); |
| 57 | } |
| 58 | { // Test with other_allocator |
| 59 | using A = other_allocator<bool>; |
| 60 | using C = std::vector<bool, A>; |
| 61 | test(C(a1.begin(), a1.end(), A(5)), A(3)); |
| 62 | test(C(a2.begin(), a2.end(), A(5)), A(3)); |
| 63 | test(C(a3.begin(), a3.end(), A(5)), A(3)); |
| 64 | test(C(a4.begin(), a4.end(), A(5)), A(3)); |
| 65 | test(C(a5.begin(), a5.end(), A(5)), A(3)); |
| 66 | } |
| 67 | { // Test with min_allocator |
| 68 | using A = min_allocator<bool>; |
| 69 | using C = std::vector<bool, A>; |
| 70 | test(C(a1.begin(), a1.end(), A()), A()); |
| 71 | test(C(a2.begin(), a2.end(), A()), A()); |
| 72 | test(C(a3.begin(), a3.end(), A()), A()); |
| 73 | test(C(a4.begin(), a4.end(), A()), A()); |
| 74 | test(C(a5.begin(), a5.end(), A()), A()); |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | int main(int, char**) { |
| 81 | tests(); |
| 82 | #if TEST_STD_VER > 17 |
| 83 | static_assert(tests()); |
| 84 | #endif |
| 85 | return 0; |
| 86 | } |
| 87 | |