1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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_multimap
15 // void rehash(size_type n);
17 #include <unordered_map>
25 #include "test_macros.h"
26 #include "min_allocator.h"
29 void rehash_postcondition(const C
& c
, std::size_t n
)
31 assert(c
.bucket_count() >= c
.size() / c
.max_load_factor() && c
.bucket_count() >= n
);
37 assert(c
.size() == 6);
38 typedef std::pair
<typename
C::const_iterator
, typename
C::const_iterator
> Eq
;
39 Eq eq
= c
.equal_range(1);
40 assert(std::distance(eq
.first
, eq
.second
) == 2);
41 typename
C::const_iterator i
= eq
.first
;
43 std::set
<std::string
> s
;
46 for ( int n
= 0; n
< 2; ++n
)
48 assert(i
->first
== 1);
49 assert(s
.find(i
->second
) != s
.end());
50 s
.erase(s
.find(i
->second
));
54 eq
= c
.equal_range(2);
55 assert(std::distance(eq
.first
, eq
.second
) == 2);
58 std::set
<std::string
> s
;
61 for ( int n
= 0; n
< 2; ++n
)
63 assert(i
->first
== 2);
64 assert(s
.find(i
->second
) != s
.end());
65 s
.erase(s
.find(i
->second
));
69 eq
= c
.equal_range(3);
70 assert(std::distance(eq
.first
, eq
.second
) == 1);
72 assert(i
->first
== 3);
73 assert(i
->second
== "three");
74 eq
= c
.equal_range(4);
75 assert(std::distance(eq
.first
, eq
.second
) == 1);
77 assert(i
->first
== 4);
78 assert(i
->second
== "four");
79 assert(static_cast<std::size_t>(std::distance(c
.begin(), c
.end())) == c
.size());
80 assert(static_cast<std::size_t>(std::distance(c
.cbegin(), c
.cend())) == c
.size());
81 assert(std::fabs(c
.load_factor() - (float)c
.size()/c
.bucket_count()) < FLT_EPSILON
);
87 typedef std::unordered_multimap
<int, std::string
> C
;
88 typedef std::pair
<int, std::string
> P
;
98 C
c(a
, a
+ sizeof(a
)/sizeof(a
[0]));
100 assert(c
.bucket_count() >= 7);
102 rehash_postcondition(c
, 3);
103 LIBCPP_ASSERT(c
.bucket_count() == 7);
105 c
.max_load_factor(2);
107 rehash_postcondition(c
, 3);
108 LIBCPP_ASSERT(c
.bucket_count() == 3);
111 rehash_postcondition(c
, 31);
112 LIBCPP_ASSERT(c
.bucket_count() == 31);
115 #if TEST_STD_VER >= 11
117 typedef std::unordered_multimap
<int, std::string
, std::hash
<int>, std::equal_to
<int>,
118 min_allocator
<std::pair
<const int, std::string
>>> C
;
119 typedef std::pair
<int, std::string
> P
;
129 C
c(a
, a
+ sizeof(a
)/sizeof(a
[0]));
131 assert(c
.bucket_count() >= 7);
133 rehash_postcondition(c
, 3);
134 LIBCPP_ASSERT(c
.bucket_count() == 7);
136 c
.max_load_factor(2);
138 rehash_postcondition(c
, 3);
139 LIBCPP_ASSERT(c
.bucket_count() == 3);
142 rehash_postcondition(c
, 31);
143 LIBCPP_ASSERT(c
.bucket_count() == 31);