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