blob: 1d99208596c1dc9fc3ddc83eb578576540721954 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <unordered_map>
11
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
15
16// void rehash(size_type n);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
Howard Hinnantf836d532011-12-02 21:23:14 +000021#include <cfloat>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022
Marshall Clow061d0cc2013-11-26 20:58:02 +000023#include "min_allocator.h"
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000024
25template <class C>
26void test(const C& c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028 assert(c.size() == 6);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000029 typedef std::pair<typename C::const_iterator, typename C::const_iterator> Eq;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030 Eq eq = c.equal_range(1);
31 assert(std::distance(eq.first, eq.second) == 2);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000032 typename C::const_iterator i = eq.first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033 assert(i->first == 1);
34 assert(i->second == "one");
35 ++i;
36 assert(i->first == 1);
37 assert(i->second == "four");
38 eq = c.equal_range(2);
39 assert(std::distance(eq.first, eq.second) == 2);
40 i = eq.first;
41 assert(i->first == 2);
42 assert(i->second == "two");
43 ++i;
44 assert(i->first == 2);
45 assert(i->second == "four");
46
47 eq = c.equal_range(3);
48 assert(std::distance(eq.first, eq.second) == 1);
49 i = eq.first;
50 assert(i->first == 3);
51 assert(i->second == "three");
52 eq = c.equal_range(4);
53 assert(std::distance(eq.first, eq.second) == 1);
54 i = eq.first;
55 assert(i->first == 4);
56 assert(i->second == "four");
57 assert(std::distance(c.begin(), c.end()) == c.size());
58 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -070059 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060}
61
62int main()
63{
64 {
65 typedef std::unordered_multimap<int, std::string> C;
66 typedef std::pair<int, std::string> P;
67 P a[] =
68 {
69 P(1, "one"),
70 P(2, "two"),
71 P(3, "three"),
72 P(4, "four"),
73 P(1, "four"),
74 P(2, "four"),
75 };
76 C c(a, a + sizeof(a)/sizeof(a[0]));
77 test(c);
78 assert(c.bucket_count() >= 7);
79 c.rehash(3);
80 assert(c.bucket_count() == 7);
81 test(c);
82 c.max_load_factor(2);
83 c.rehash(3);
84 assert(c.bucket_count() == 3);
85 test(c);
86 c.rehash(31);
87 assert(c.bucket_count() == 31);
88 test(c);
89 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000090#if __cplusplus >= 201103L
91 {
92 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
93 min_allocator<std::pair<const int, std::string>>> C;
94 typedef std::pair<int, std::string> P;
95 P a[] =
96 {
97 P(1, "one"),
98 P(2, "two"),
99 P(3, "three"),
100 P(4, "four"),
101 P(1, "four"),
102 P(2, "four"),
103 };
104 C c(a, a + sizeof(a)/sizeof(a[0]));
105 test(c);
106 assert(c.bucket_count() >= 7);
107 c.rehash(3);
108 assert(c.bucket_count() == 7);
109 test(c);
110 c.max_load_factor(2);
111 c.rehash(3);
112 assert(c.bucket_count() == 3);
113 test(c);
114 c.rehash(31);
115 assert(c.bucket_count() == 31);
116 test(c);
117 }
118#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119}