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 // C++2a[container.requirements.general]p8 12 // Move constructors obtain an allocator by move construction from the allocator 13 // belonging to the container being moved. Such move construction of the 14 // allocator shall not exit via an exception. 15 16 #include <vector> 17 #include <deque> 18 #include <list> 19 #include <forward_list> 20 #include <set> 21 #include <map> 22 #include <unordered_map> 23 #include <unordered_set> 24 25 #include "test_macros.h" 26 #include "test_allocator.h" 27 28 template <class C> 29 void test(int expected_num_allocs = 1) { 30 { 31 test_alloc_base::clear(); 32 using AllocT = typename C::allocator_type; 33 C v(AllocT(42, 101)); 34 35 assert(test_alloc_base::count == expected_num_allocs); 36 37 const int num_stored_allocs = test_alloc_base::count; 38 { 39 const AllocT& a = v.get_allocator(); 40 assert(test_alloc_base::count == 1 + num_stored_allocs); 41 assert(a.get_data() == 42); 42 assert(a.get_id() == 101); 43 } 44 assert(test_alloc_base::count == num_stored_allocs); 45 test_alloc_base::clear_ctor_counters(); 46 47 C v2 = std::move(v); 48 assert(test_alloc_base::count == num_stored_allocs * 2); 49 assert(test_alloc_base::copied == 0); 50 assert(test_alloc_base::moved == num_stored_allocs); 51 { 52 const AllocT& a = v.get_allocator(); 53 assert(a.get_id() == test_alloc_base::moved_value); 54 assert(a.get_data() == test_alloc_base::moved_value); 55 } 56 { 57 const AllocT& a = v2.get_allocator(); 58 assert(a.get_id() == 101); 59 assert(a.get_data() == 42); 60 } 61 } 62 } 63 64 int main(int, char**) { 65 { // test sequence containers 66 test<std::vector<int, test_allocator<int> > >(); 67 test<std::vector<bool, test_allocator<bool> > >(); 68 test<std::list<int, test_allocator<int> > >(); 69 test<std::forward_list<int, test_allocator<int> > >(); 70 71 // libc++ stores two allocators in deque 72 #ifdef _LIBCPP_VERSION 73 int stored_allocators = 2; 74 #else 75 int stored_allocators = 1; 76 #endif 77 test<std::deque<int, test_allocator<int> > >(stored_allocators); 78 } 79 { // test associative containers 80 test<std::set<int, std::less<int>, test_allocator<int> > >(); 81 test<std::multiset<int, std::less<int>, test_allocator<int> > >(); 82 83 using KV = std::pair<const int, int>; 84 test<std::map<int, int, std::less<int>, test_allocator<KV> > >(); 85 test<std::multimap<int, int, std::less<int>, test_allocator<KV> > >(); 86 } 87 { // test unordered containers 88 // libc++ stores two allocators in the unordered containers. 89 #ifdef _LIBCPP_VERSION 90 int stored_allocators = 2; 91 #else 92 int stored_allocators = 1; 93 #endif 94 test<std::unordered_set<int, std::hash<int>, std::equal_to<int>, 95 test_allocator<int> > >(stored_allocators); 96 test<std::unordered_multiset<int, std::hash<int>, std::equal_to<int>, 97 test_allocator<int> > >(stored_allocators); 98 99 using KV = std::pair<const int, int>; 100 test<std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, 101 test_allocator<KV> > >(stored_allocators); 102 test<std::unordered_multimap<int, int, std::hash<int>, std::equal_to<int>, 103 test_allocator<KV> > >(stored_allocators); 104 } 105 106 return 0; 107 } 108