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>
26 #include "test_macros.h"
27 #include "min_allocator.h"
30 void rehash_postcondition(const C& c, size_t n)
32 assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n);
38 assert(c.size() == 6);
39 typedef std::pair<typename C::const_iterator, typename C::const_iterator> Eq;
40 Eq eq = c.equal_range(1);
41 assert(std::distance(eq.first, eq.second) == 2);
42 typename C::const_iterator i = eq.first;
44 std::set<std::string> s;
47 for ( int n = 0; n < 2; ++n )
49 assert(i->first == 1);
50 assert(s.find(i->second) != s.end());
51 s.erase(s.find(i->second));
55 eq = c.equal_range(2);
56 assert(std::distance(eq.first, eq.second) == 2);
59 std::set<std::string> s;
62 for ( int n = 0; n < 2; ++n )
64 assert(i->first == 2);
65 assert(s.find(i->second) != s.end());
66 s.erase(s.find(i->second));
70 eq = c.equal_range(3);
71 assert(std::distance(eq.first, eq.second) == 1);
73 assert(i->first == 3);
74 assert(i->second == "three");
75 eq = c.equal_range(4);
76 assert(std::distance(eq.first, eq.second) == 1);
78 assert(i->first == 4);
79 assert(i->second == "four");
80 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
81 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
82 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
88 typedef std::unordered_multimap<int, std::string> C;
89 typedef std::pair<int, std::string> P;
99 C c(a, a + sizeof(a)/sizeof(a[0]));
101 assert(c.bucket_count() >= 7);
103 rehash_postcondition(c, 3);
104 LIBCPP_ASSERT(c.bucket_count() == 7);
106 c.max_load_factor(2);
108 rehash_postcondition(c, 3);
109 LIBCPP_ASSERT(c.bucket_count() == 3);
112 rehash_postcondition(c, 31);
113 LIBCPP_ASSERT(c.bucket_count() == 31);
116 #if TEST_STD_VER >= 11
118 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
119 min_allocator<std::pair<const int, std::string>>> C;
120 typedef std::pair<int, std::string> P;
130 C c(a, a + sizeof(a)/sizeof(a[0]));
132 assert(c.bucket_count() >= 7);
134 rehash_postcondition(c, 3);
135 LIBCPP_ASSERT(c.bucket_count() == 7);
137 c.max_load_factor(2);
139 rehash_postcondition(c, 3);
140 LIBCPP_ASSERT(c.bucket_count() == 3);
143 rehash_postcondition(c, 31);
144 LIBCPP_ASSERT(c.bucket_count() == 31);