Debian Codegraph @

libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/copy.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);
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() {
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;
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
47 // Check that orig is still what is expected
48 LIBCPP_ASSERT(orig.bucket_count() == 5);
49 assert(orig.size() == 3);
50 assert(orig.at(1) == 2);
51 assert(orig.at(2) == 4);
52 assert(orig.at(3) == 1);
53 }
54 { // single element copy
55 using V = std::pair<const int, int>;
56 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
57
58 V arr[] = {V(1, 2)};
59 const Map orig(std::begin(arr), std::end(arr));
60 Map copy = orig;
61 LIBCPP_ASSERT(copy.bucket_count() == 2);
62 assert(copy.size() == 1);
63 assert(copy.at(1) == 2);
64 assert(static_cast<std::size_t>(std::distance(copy.begin(), copy.end())) == copy.size());
65 assert(std::fabs(copy.load_factor() - static_cast<float>(copy.size()) / copy.bucket_count()) < FLT_EPSILON);
66 assert(copy.max_load_factor() == 1.f);
67
68 // Check that orig is still what is expected
69 LIBCPP_ASSERT(orig.bucket_count() == 2);
70 assert(orig.size() == 1);
71 assert(orig.at(1) == 2);
72 }
73 { // Copy empty map
74 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, Alloc>;
75
76 const Map orig;
77 Map copy = orig;
78 LIBCPP_ASSERT(copy.bucket_count() == 0);
79 assert(copy.size() == 0);
80 assert(static_cast<std::size_t>(std::distance(copy.begin(), copy.end())) == copy.size());
81 assert(copy.max_load_factor() == 1.f);
82
83 // Check that orig is still what is expected
84 LIBCPP_ASSERT(orig.bucket_count() == 0);
85 assert(orig.size() == 0);
86 }
87 { // Ensure that the hash function is copied
88 using Map = std::unordered_map<int, int, test_hash<int>, std::equal_to<int>, Alloc>;
89 const Map orig(0, test_hash<int>(23));
90 Map copy = orig;
91 assert(copy.hash_function() == test_hash<int>(23));
92
93 // Check that orig is still what is expected
94 assert(orig.hash_function() == test_hash<int>(23));
95 }
96 { // Ensure that the quality comparator is copied
97 using Map = std::unordered_map<int, int, std::hash<int>, test_equal_to<int>, Alloc>;
98 const Map orig(0, std::hash<int>(), test_equal_to<int>(56));
99 Map copy = orig;
100 assert(copy.key_eq() == test_equal_to<int>(56));
101
102 // Check that orig is still what is expected
103 assert(orig.key_eq() == test_equal_to<int>(56));
104 }
105 }
106
107 void test() {
108 test_alloc<std::allocator<std::pair<const int, int> > >();
109 test_alloc<min_allocator<std::pair<const int, int> > >();
110
111 { // Ensure that the allocator is copied
112 using V = std::pair<const int, int>;
113 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, test_allocator<V> >;
114
115 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};
116 const Map orig(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), test_allocator<V>(10));
117 Map copy = orig;
118 assert(copy.size() == 3);
119 assert(copy.get_allocator() == test_allocator<V>(10));
120
121 // Check that orig is still what is expected
122 assert(orig.size() == 3);
123 assert(orig.get_allocator() == test_allocator<V>(10));
124 assert(orig.get_allocator().get_id() != test_alloc_base::moved_value);
125 }
126
127 { // Ensure that soccc is handled properly
128 using V = std::pair<const int, int>;
129 using Map = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, other_allocator<V> >;
130
131 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};
132 const Map orig(begin(arr), end(arr), 0, std::hash<int>(), std::equal_to<int>(), other_allocator<V>(10));
133 Map copy = orig;
134 assert(copy.size() == 3);
135 assert(copy.get_allocator() == other_allocator<V>(-2));
136
137 // Check that orig is still what is expected
138 assert(orig.size() == 3);
139 assert(orig.get_allocator() == other_allocator<V>(10));
140 }
141 }
142
143 int main(int, char**) {
144 test();
145
146 return 0;
147 }