| 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 | // UNSUPPORTED: c++03 |
| 10 | |
| 11 | // <vector> |
| 12 | // vector<bool> |
| 13 | |
| 14 | // vector& operator=(vector&& c); |
| 15 | |
| 16 | #include <cassert> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "min_allocator.h" |
| 20 | #include "test_allocator.h" |
| 21 | #include "test_macros.h" |
| 22 | |
| 23 | TEST_CONSTEXPR_CXX20 void test_move_assignment(unsigned N) { |
| 24 | // |
| 25 | // Testing for container move where either POCMA = true_type or the allocators compare equal |
| 26 | // |
| 27 | { // Test with POCMA = true_type |
| 28 | std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5)); |
| 29 | std::vector<bool, other_allocator<bool> > lo(N, true, other_allocator<bool>(5)); |
| 30 | std::vector<bool, other_allocator<bool> > l2(N + 10, false, other_allocator<bool>(42)); |
| 31 | l2 = std::move(l); |
| 32 | assert(l2 == lo); |
| 33 | LIBCPP_ASSERT(l.empty()); // After move, source vector is in a vliad but unspecified state. libc++ leaves it empty. |
| 34 | assert(l2.get_allocator() == lo.get_allocator()); |
| 35 | } |
| 36 | { // Test with POCMA = false_type and allocators compare equal |
| 37 | std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 38 | std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 39 | std::vector<bool, test_allocator<bool> > l2(N + 10, false, test_allocator<bool>(5)); |
| 40 | l2 = std::move(l); |
| 41 | assert(l2 == lo); |
| 42 | LIBCPP_ASSERT(l.empty()); |
| 43 | assert(l2.get_allocator() == lo.get_allocator()); |
| 44 | } |
| 45 | { // Test with POCMA = false_type and allocators compare equal |
| 46 | std::vector<bool, min_allocator<bool> > l(N, true, min_allocator<bool>{}); |
| 47 | std::vector<bool, min_allocator<bool> > lo(N, true, min_allocator<bool>{}); |
| 48 | std::vector<bool, min_allocator<bool> > l2(N + 10, false, min_allocator<bool>{}); |
| 49 | l2 = std::move(l); |
| 50 | assert(l2 == lo); |
| 51 | LIBCPP_ASSERT(l.empty()); |
| 52 | assert(l2.get_allocator() == lo.get_allocator()); |
| 53 | } |
| 54 | |
| 55 | // |
| 56 | // Testing for element-wise move where POCMA = false_type and allocators compare unequal |
| 57 | // |
| 58 | { // Test with reallocation during the element-wise move due to empty destination vector. |
| 59 | std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 60 | std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 61 | std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(42)); |
| 62 | l2 = std::move(l); |
| 63 | assert(l2 == lo); |
| 64 | LIBCPP_ASSERT(!l.empty()); |
| 65 | assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 66 | } |
| 67 | { // Test with reallocation occurs during the element-wise move due to insufficient destination space. |
| 68 | std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5)); |
| 69 | std::vector<bool, test_allocator<bool> > lo(N + 64, true, test_allocator<bool>(5)); |
| 70 | std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(42)); |
| 71 | l2 = std::move(l); |
| 72 | assert(l2 == lo); |
| 73 | LIBCPP_ASSERT(!l.empty()); |
| 74 | assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 75 | } |
| 76 | { // Test without reallocation where source vector elements fit within destination size. |
| 77 | std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5)); |
| 78 | std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5)); |
| 79 | std::vector<bool, test_allocator<bool> > l2(N * 2, false, test_allocator<bool>(42)); |
| 80 | l2 = std::move(l); |
| 81 | assert(l2 == lo); |
| 82 | LIBCPP_ASSERT(!l.empty()); |
| 83 | assert(l2.get_allocator() == test_allocator<bool>(42)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 88 | test_move_assignment(N: 3); |
| 89 | test_move_assignment(N: 18); |
| 90 | test_move_assignment(N: 33); |
| 91 | test_move_assignment(N: 65); |
| 92 | test_move_assignment(N: 299); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | int main(int, char**) { |
| 98 | tests(); |
| 99 | #if TEST_STD_VER > 17 |
| 100 | static_assert(tests()); |
| 101 | #endif |
| 102 | return 0; |
| 103 | } |
| 104 | |