Debian Codegraph @

libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/copy_alloc.pass.cpp

raw
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 // <unordered_map>
10
11 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12 // class Alloc = allocator<pair<const Key, T>>>
13 // class unordered_map
14
15 // unordered_map(const unordered_map& u, const allocator_type& a);
16
17 #include <cassert>
18 #include <cfloat>
19 #include <cmath>
20 #include <cstddef>
21 #include <unordered_map>
22
23 #include "../../../test_compare.h"
24 #include "../../../test_hash.h"
25 #include "min_allocator.h"
26 #include "test_allocator.h"
27 #include "test_macros.h"
28
29 template <class Alloc>
30 void test_alloc(const Alloc& new_alloc) {
31 { // Simple check
32 using V = std::pair<const int, int>;
33 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
34
35 V arr[] = {V(1, 2), V(2, 4), V(3, 1)};
36 const Map orig(std::begin(arr), std::end(arr));
37 Map copy(orig, new_alloc);
38 LIBCPP_ASSERT(copy.bucket_count() == 5);
39 assert(copy.size() == 3);
40 assert(copy.at(1) == 2);
41 assert(copy.at(2) == 4);
42 assert(copy.at(3) == 1);
43 assert(static_cast<std::size_t>(std::distance(copy.begin(), copy.end())) == copy.size());
44 assert(std::fabs(copy.load_factor() - static_cast<float>(copy.size()) / copy.bucket_count()) < FLT_EPSILON);
45 assert(copy.max_load_factor() == 1.f);
46 assert(copy.get_allocator() == new_alloc);
47
48 // Check that orig is still what is expected
49 LIBCPP_ASSERT(orig.bucket_count() == 5);
50 assert(orig.size() == 3);
51 assert(orig.at(1) == 2);
52 assert(orig.at(2) == 4);
53 assert(orig.at(3) == 1);
54 }
55 { // single element check
56 using V = std::pair<const int, int>;
57 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
58
59 V arr[] = {V(1, 2)};
60 const Map orig(std::begin(arr), std::end(arr));
61 Map copy(orig, new_alloc);
62 LIBCPP_ASSERT(copy.bucket_count() == 2);
63 assert(copy.size() == 1);
64 assert(copy.at(1) == 2);
65 assert(static_cast<std::size_t>(std::distance(copy.begin(), copy.end())) == copy.size());
66 assert(std::fabs(copy.load_factor() - static_cast<float>(copy.size()) / copy.bucket_count()) < FLT_EPSILON);
67 assert(copy.max_load_factor() == 1.f);
68 assert(copy.get_allocator() == new_alloc);
69
70 // Check that orig is still what is expected
71 LIBCPP_ASSERT(orig.bucket_count() == 2);
72 assert(orig.size() == 1);
73 assert(orig.at(1) == 2);
74 }
75 { // Copy empty map
76 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
77
78 const Map orig;
79 Map copy(orig, new_alloc);
80 LIBCPP_ASSERT(copy.bucket_count() == 0);
81 assert(copy.size() == 0);
82 assert(static_cast<std::size_t>(std::distance(copy.begin(), copy.end())) == copy.size());
83 assert(copy.max_load_factor() == 1.f);
84 assert(copy.get_allocator() == new_alloc);
85
86 // Check that orig is still what is expected
87 LIBCPP_ASSERT(orig.bucket_count() == 0);
88 assert(orig.size() == 0);
89 }
90 { // Ensure that the hash function is copied
91 using Map = std::unordered_map<int, int, test_hash<int>, std::equal_to<int>, Alloc>;
92 const Map orig(0, test_hash<int>(23));
93 Map copy(orig, new_alloc);
94 assert(copy.hash_function() == test_hash<int>(23));
95 assert(copy.get_allocator() == new_alloc);
96
97 // Check that orig is still what is expected
98 assert(orig.hash_function() == test_hash<int>(23));
99 }
100 { // Ensure that the quality comparator is copied
101 using Map = std::unordered_map<int, int, std::hash<int>, test_equal_to<int>, Alloc>;
102 const Map orig(0, std::hash<int>(), test_equal_to<int>(56));
103 Map copy(orig, new_alloc);
104 assert(copy.key_eq() == test_equal_to<int>(56));
105 assert(copy.get_allocator() == new_alloc);
106
107 // Check that orig is still what is expected
108 assert(orig.key_eq() == test_equal_to<int>(56));
109 }
110 }
111
112 void test() {
113 test_alloc(std::allocator<std::pair<const int, int> >());
114 test_alloc(min_allocator<std::pair<const int, int> >());
115 test_alloc(test_allocator<std::pair<const int, int> >(25));
116 }
117
118 int main(int, char**) {
119 test();
120
121 return 0;
122 }