reserve.pass.cpp revision d24c465beaec2fe9a0e365e6379cd5d3acaeb2ca
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_map
15
16// void reserve(size_type n);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
21
22#include "min_allocator.h"
23
24template <class C>
25void test(const C& c)
26{
27    assert(c.size() == 4);
28    assert(c.at(1) == "one");
29    assert(c.at(2) == "two");
30    assert(c.at(3) == "three");
31    assert(c.at(4) == "four");
32}
33
34void reserve_invariant(size_t n) // LWG #2156
35{
36    for (size_t i = 0; i < n; ++i)
37    {
38        std::unordered_map<size_t, 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[i] = i;
44            assert(buckets == c.bucket_count());
45        }
46    }
47}
48
49int main()
50{
51    {
52        typedef std::unordered_map<int, std::string> C;
53        typedef std::pair<int, std::string> P;
54        P a[] =
55        {
56            P(1, "one"),
57            P(2, "two"),
58            P(3, "three"),
59            P(4, "four"),
60            P(1, "four"),
61            P(2, "four"),
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        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_map<int, std::string, std::hash<int>, std::equal_to<int>,
80                            min_allocator<std::pair<const int, std::string>>> C;
81        typedef std::pair<int, std::string> P;
82        P a[] =
83        {
84            P(1, "one"),
85            P(2, "two"),
86            P(3, "three"),
87            P(4, "four"),
88            P(1, "four"),
89            P(2, "four"),
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        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