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 13 // vector(vector&& c); 14 15 #include <vector> 16 #include <cassert> 17 #include "test_macros.h" 18 #include "test_allocator.h" 19 #include "min_allocator.h" 20 21 TEST_CONSTEXPR_CXX20 bool tests() 22 { 23 test_allocator_statistics alloc_stats; 24 { 25 std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5, &alloc_stats)); 26 std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5, &alloc_stats)); 27 for (int i = 1; i <= 3; ++i) 28 { 29 l.push_back(true); 30 lo.push_back(true); 31 } 32 std::vector<bool, test_allocator<bool> > l2 = std::move(l); 33 assert(l2 == lo); 34 assert(l.empty()); 35 assert(l2.get_allocator() == lo.get_allocator()); 36 } 37 { 38 std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5)); 39 std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5)); 40 for (int i = 1; i <= 3; ++i) 41 { 42 l.push_back(true); 43 lo.push_back(true); 44 } 45 std::vector<bool, other_allocator<bool> > l2 = std::move(l); 46 assert(l2 == lo); 47 assert(l.empty()); 48 assert(l2.get_allocator() == lo.get_allocator()); 49 } 50 { 51 std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{}); 52 std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{}); 53 for (int i = 1; i <= 3; ++i) 54 { 55 l.push_back(true); 56 lo.push_back(true); 57 } 58 std::vector<bool, min_allocator<bool> > l2 = std::move(l); 59 assert(l2 == lo); 60 assert(l.empty()); 61 assert(l2.get_allocator() == lo.get_allocator()); 62 } 63 { 64 alloc_stats.clear(); 65 using Vect = std::vector<bool, test_allocator<bool> >; 66 using AllocT = Vect::allocator_type; 67 Vect v(test_allocator<bool>(42, 101, &alloc_stats)); 68 assert(alloc_stats.count == 1); 69 { 70 const AllocT& a = v.get_allocator(); 71 assert(alloc_stats.count == 2); 72 assert(a.get_data() == 42); 73 assert(a.get_id() == 101); 74 } 75 assert(alloc_stats.count == 1); 76 alloc_stats.clear_ctor_counters(); 77 78 Vect v2 = std::move(v); 79 assert(alloc_stats.count == 2); 80 assert(alloc_stats.copied == 0); 81 assert(alloc_stats.moved == 1); 82 { 83 const AllocT& a1 = v.get_allocator(); 84 assert(a1.get_id() == test_alloc_base::moved_value); 85 assert(a1.get_data() == 42); 86 87 const AllocT& a2 = v2.get_allocator(); 88 assert(a2.get_id() == 101); 89 assert(a2.get_data() == 42); 90 91 assert(a1 == a2); 92 } 93 } 94 95 return true; 96 } 97 98 int main(int, char**) 99 { 100 tests(); 101 #if TEST_STD_VER > 17 102 static_assert(tests()); 103 #endif 104 return 0; 105 } 106