]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/containers/unord/unord.set/rehash.pass.cpp
Vendor import of libc++ trunk r256633:
[FreeBSD/FreeBSD.git] / test / std / containers / unord / unord.set / 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_set>
11
12 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
13 //           class Alloc = allocator<Value>>
14 // class unordered_set
15
16 // void rehash(size_type n);
17
18 #include <unordered_set>
19 #include <cassert>
20
21 #include "min_allocator.h"
22
23 template <class C>
24 void rehash_postcondition(const C& c, size_t n)
25 {
26         assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n);
27 }
28
29 template <class C>
30 void test(const C& c)
31 {
32     assert(c.size() == 4);
33     assert(c.count(1) == 1);
34     assert(c.count(2) == 1);
35     assert(c.count(3) == 1);
36     assert(c.count(4) == 1);
37 }
38
39 int main()
40 {
41     {
42         typedef std::unordered_set<int> C;
43         typedef int P;
44         P a[] =
45         {
46             P(1),
47             P(2),
48             P(3),
49             P(4),
50             P(1),
51             P(2)
52         };
53         C c(a, a + sizeof(a)/sizeof(a[0]));
54         test(c);
55         assert(c.bucket_count() >= 5);
56         c.rehash(3);
57         rehash_postcondition(c, 3);
58         assert(c.bucket_count() == 5);
59         test(c);
60         c.max_load_factor(2);
61         c.rehash(3);
62         rehash_postcondition(c, 3);
63         assert(c.bucket_count() == 3);
64         test(c);
65         c.rehash(31);
66         rehash_postcondition(c, 31);
67         assert(c.bucket_count() == 31);
68         test(c);
69     }
70 #if __cplusplus >= 201103L
71     {
72         typedef std::unordered_set<int, std::hash<int>,
73                                       std::equal_to<int>, min_allocator<int>> C;
74         typedef int P;
75         P a[] =
76         {
77             P(1),
78             P(2),
79             P(3),
80             P(4),
81             P(1),
82             P(2)
83         };
84         C c(a, a + sizeof(a)/sizeof(a[0]));
85         test(c);
86         assert(c.bucket_count() >= 5);
87         c.rehash(3);
88         rehash_postcondition(c, 3);
89         assert(c.bucket_count() == 5);
90         test(c);
91         c.max_load_factor(2);
92         c.rehash(3);
93         rehash_postcondition(c, 3);
94         assert(c.bucket_count() == 3);
95         test(c);
96         c.rehash(31);
97         rehash_postcondition(c, 31);
98         assert(c.bucket_count() == 31);
99         test(c);
100     }
101 #endif
102 }