]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/containers/unord/unord.multimap/rehash.pass.cpp
Vendor import of libc++ release_39 branch r276489:
[FreeBSD/FreeBSD.git] / test / std / containers / unord / unord.multimap / rehash.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
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>
21 #include <cfloat>
22 #include <cmath>
23
24 #include "min_allocator.h"
25
26 template <class C>
27 void rehash_postcondition(const C& c, size_t n)
28 {
29         assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n);
30 }
31
32 template <class C>
33 void test(const C& c)
34 {
35     assert(c.size() == 6);
36     typedef std::pair<typename C::const_iterator, typename C::const_iterator> Eq;
37     Eq eq = c.equal_range(1);
38     assert(std::distance(eq.first, eq.second) == 2);
39     typename C::const_iterator i = eq.first;
40     assert(i->first == 1);
41     assert(i->second == "one");
42     ++i;
43     assert(i->first == 1);
44     assert(i->second == "four");
45     eq = c.equal_range(2);
46     assert(std::distance(eq.first, eq.second) == 2);
47     i = eq.first;
48     assert(i->first == 2);
49     assert(i->second == "two");
50     ++i;
51     assert(i->first == 2);
52     assert(i->second == "four");
53
54     eq = c.equal_range(3);
55     assert(std::distance(eq.first, eq.second) == 1);
56     i = eq.first;
57     assert(i->first == 3);
58     assert(i->second == "three");
59     eq = c.equal_range(4);
60     assert(std::distance(eq.first, eq.second) == 1);
61     i = eq.first;
62     assert(i->first == 4);
63     assert(i->second == "four");
64     assert(std::distance(c.begin(), c.end()) == c.size());
65     assert(std::distance(c.cbegin(), c.cend()) == c.size());
66     assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
67 }
68
69 int main()
70 {
71     {
72         typedef std::unordered_multimap<int, std::string> C;
73         typedef std::pair<int, std::string> P;
74         P a[] =
75         {
76             P(1, "one"),
77             P(2, "two"),
78             P(3, "three"),
79             P(4, "four"),
80             P(1, "four"),
81             P(2, "four"),
82         };
83         C c(a, a + sizeof(a)/sizeof(a[0]));
84         test(c);
85         assert(c.bucket_count() >= 7);
86         c.rehash(3);
87         rehash_postcondition(c, 3);
88         assert(c.bucket_count() == 7);
89         test(c);
90         c.max_load_factor(2);
91         c.rehash(3);
92         rehash_postcondition(c, 3);
93         assert(c.bucket_count() == 3);
94         test(c);
95         c.rehash(31);
96         rehash_postcondition(c, 31);
97         assert(c.bucket_count() == 31);
98         test(c);
99     }
100 #if TEST_STD_VER >= 11
101     {
102         typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
103                             min_allocator<std::pair<const int, std::string>>> C;
104         typedef std::pair<int, std::string> P;
105         P a[] =
106         {
107             P(1, "one"),
108             P(2, "two"),
109             P(3, "three"),
110             P(4, "four"),
111             P(1, "four"),
112             P(2, "four"),
113         };
114         C c(a, a + sizeof(a)/sizeof(a[0]));
115         test(c);
116         assert(c.bucket_count() >= 7);
117         c.rehash(3);
118         rehash_postcondition(c, 3);
119         assert(c.bucket_count() == 7);
120         test(c);
121         c.max_load_factor(2);
122         c.rehash(3);
123         rehash_postcondition(c, 3);
124         assert(c.bucket_count() == 3);
125         test(c);
126         c.rehash(31);
127         rehash_postcondition(c, 31);
128         assert(c.bucket_count() == 31);
129         test(c);
130     }
131 #endif
132 }