1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13 // class Alloc = allocator<pair<const Key, T>>>
14 // class unordered_multimap
16 // void rehash(size_type n);
18 #include <unordered_map>
25 #include "test_macros.h"
26 #include "min_allocator.h"
29 void rehash_postcondition(const C& c, 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;
42 assert(i->first == 1);
43 assert(i->second == "one");
45 assert(i->first == 1);
46 assert(i->second == "four");
47 eq = c.equal_range(2);
48 assert(std::distance(eq.first, eq.second) == 2);
50 assert(i->first == 2);
51 assert(i->second == "two");
53 assert(i->first == 2);
54 assert(i->second == "four");
56 eq = c.equal_range(3);
57 assert(std::distance(eq.first, eq.second) == 1);
59 assert(i->first == 3);
60 assert(i->second == "three");
61 eq = c.equal_range(4);
62 assert(std::distance(eq.first, eq.second) == 1);
64 assert(i->first == 4);
65 assert(i->second == "four");
66 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
67 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
68 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
74 typedef std::unordered_multimap<int, std::string> C;
75 typedef std::pair<int, std::string> P;
85 C c(a, a + sizeof(a)/sizeof(a[0]));
87 assert(c.bucket_count() >= 7);
89 rehash_postcondition(c, 3);
90 LIBCPP_ASSERT(c.bucket_count() == 7);
94 rehash_postcondition(c, 3);
95 LIBCPP_ASSERT(c.bucket_count() == 3);
98 rehash_postcondition(c, 31);
99 LIBCPP_ASSERT(c.bucket_count() == 31);
102 #if TEST_STD_VER >= 11
104 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
105 min_allocator<std::pair<const int, std::string>>> C;
106 typedef std::pair<int, std::string> P;
116 C c(a, a + sizeof(a)/sizeof(a[0]));
118 assert(c.bucket_count() >= 7);
120 rehash_postcondition(c, 3);
121 LIBCPP_ASSERT(c.bucket_count() == 7);
123 c.max_load_factor(2);
125 rehash_postcondition(c, 3);
126 LIBCPP_ASSERT(c.bucket_count() == 3);
129 rehash_postcondition(c, 31);
130 LIBCPP_ASSERT(c.bucket_count() == 31);