| 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 | // vector& operator=(const vector& c); |
| 12 | |
| 13 | #include <vector> |
| 14 | #include <cassert> |
| 15 | #include "test_macros.h" |
| 16 | #include "test_allocator.h" |
| 17 | #include "min_allocator.h" |
| 18 | #include "allocators.h" |
| 19 | |
| 20 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 21 | { |
| 22 | std::vector<int, test_allocator<int> > l(3, 2, test_allocator<int>(5)); |
| 23 | std::vector<int, test_allocator<int> > l2(l, test_allocator<int>(3)); |
| 24 | l2 = l; |
| 25 | assert(l2 == l); |
| 26 | assert(l2.get_allocator() == test_allocator<int>(3)); |
| 27 | } |
| 28 | { |
| 29 | std::vector<int, other_allocator<int> > l(3, 2, other_allocator<int>(5)); |
| 30 | std::vector<int, other_allocator<int> > l2(l, other_allocator<int>(3)); |
| 31 | l2 = l; |
| 32 | assert(l2 == l); |
| 33 | assert(l2.get_allocator() == other_allocator<int>(5)); |
| 34 | } |
| 35 | #if TEST_STD_VER >= 11 |
| 36 | { |
| 37 | // Test with Allocator::propagate_on_container_copy_assignment == false_type |
| 38 | using Alloc = NonPOCCAAllocator<int>; |
| 39 | bool copy_assigned_into = false; |
| 40 | std::vector<int, Alloc> l(3, 2, Alloc(5, nullptr)); |
| 41 | std::vector<int, Alloc> l2(l, Alloc(3, ©_assigned_into)); |
| 42 | assert(!copy_assigned_into); |
| 43 | l2 = l; |
| 44 | assert(!copy_assigned_into); |
| 45 | assert(l2 == l); |
| 46 | assert(l2.get_allocator() == Alloc(3, nullptr)); |
| 47 | } |
| 48 | { |
| 49 | // Test with Allocator::propagate_on_container_copy_assignment == true_type |
| 50 | // and equal allocators |
| 51 | using Alloc = POCCAAllocator<int>; |
| 52 | bool copy_assigned_into = false; |
| 53 | std::vector<int, Alloc> l(3, 2, Alloc(5, nullptr)); |
| 54 | std::vector<int, Alloc> l2(l, Alloc(5, ©_assigned_into)); |
| 55 | assert(!copy_assigned_into); |
| 56 | l2 = l; |
| 57 | assert(copy_assigned_into); |
| 58 | assert(l2 == l); |
| 59 | assert(l2.get_allocator() == Alloc(5, nullptr)); |
| 60 | } |
| 61 | { |
| 62 | // Test with Allocator::propagate_on_container_copy_assignment == true_type |
| 63 | // and unequal allocators |
| 64 | using Alloc = POCCAAllocator<int>; |
| 65 | bool copy_assigned_into = false; |
| 66 | std::vector<int, Alloc> l(3, 2, Alloc(5, nullptr)); |
| 67 | std::vector<int, Alloc> l2(l, Alloc(3, ©_assigned_into)); |
| 68 | assert(!copy_assigned_into); |
| 69 | l2 = l; |
| 70 | assert(copy_assigned_into); |
| 71 | assert(l2 == l); |
| 72 | assert(l2.get_allocator() == Alloc(5, nullptr)); |
| 73 | } |
| 74 | { |
| 75 | std::vector<int, min_allocator<int> > l(3, 2, min_allocator<int>()); |
| 76 | std::vector<int, min_allocator<int> > l2(l, min_allocator<int>()); |
| 77 | l2 = l; |
| 78 | assert(l2 == l); |
| 79 | assert(l2.get_allocator() == min_allocator<int>()); |
| 80 | } |
| 81 | { |
| 82 | std::vector<int, safe_allocator<int> > l(3, 2, safe_allocator<int>()); |
| 83 | std::vector<int, safe_allocator<int> > l2(l, safe_allocator<int>()); |
| 84 | l2 = l; |
| 85 | assert(l2 == l); |
| 86 | assert(l2.get_allocator() == safe_allocator<int>()); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | int main(int, char**) { |
| 94 | tests(); |
| 95 | #if TEST_STD_VER > 17 |
| 96 | static_assert(tests()); |
| 97 | #endif |
| 98 | return 0; |
| 99 | } |
| 100 | |