• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 reserve(size_type n);
17 
18 #include <unordered_set>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "min_allocator.h"
23 
24 template <class C>
test(const C & c)25 void test(const C& c)
26 {
27     assert(c.size() == 4);
28     assert(c.count(1) == 1);
29     assert(c.count(2) == 1);
30     assert(c.count(3) == 1);
31     assert(c.count(4) == 1);
32 }
33 
reserve_invariant(size_t n)34 void reserve_invariant(size_t n) // LWG #2156
35 {
36     for (size_t i = 0; i < n; ++i)
37     {
38         std::unordered_set<size_t> c;
39         c.reserve(n);
40         size_t buckets = c.bucket_count();
41         for (size_t j = 0; j < i; ++j)
42         {
43             c.insert(i);
44             assert(buckets == c.bucket_count());
45         }
46     }
47 }
48 
main()49 int main()
50 {
51     {
52         typedef std::unordered_set<int> C;
53         typedef int P;
54         P a[] =
55         {
56             P(1),
57             P(2),
58             P(3),
59             P(4),
60             P(1),
61             P(2)
62         };
63         C c(a, a + sizeof(a)/sizeof(a[0]));
64         test(c);
65         assert(c.bucket_count() >= 5);
66         c.reserve(3);
67         LIBCPP_ASSERT(c.bucket_count() == 5);
68         test(c);
69         c.max_load_factor(2);
70         c.reserve(3);
71         assert(c.bucket_count() >= 2);
72         test(c);
73         c.reserve(31);
74         assert(c.bucket_count() >= 16);
75         test(c);
76     }
77 #if TEST_STD_VER >= 11
78     {
79         typedef std::unordered_set<int, std::hash<int>,
80                                       std::equal_to<int>, min_allocator<int>> C;
81         typedef int P;
82         P a[] =
83         {
84             P(1),
85             P(2),
86             P(3),
87             P(4),
88             P(1),
89             P(2)
90         };
91         C c(a, a + sizeof(a)/sizeof(a[0]));
92         test(c);
93         assert(c.bucket_count() >= 5);
94         c.reserve(3);
95         LIBCPP_ASSERT(c.bucket_count() == 5);
96         test(c);
97         c.max_load_factor(2);
98         c.reserve(3);
99         assert(c.bucket_count() >= 2);
100         test(c);
101         c.reserve(31);
102         assert(c.bucket_count() >= 16);
103         test(c);
104     }
105 #endif
106     reserve_invariant(20);
107 }
108