blob: 48667cdc7f9288a5c35147ab826525e13da0c3bd [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_map
15
16// void reserve(size_type n);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
21
Marshall Clow061d0cc2013-11-26 20:58:02 +000022#include "min_allocator.h"
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000023
24template <class C>
25void test(const C& c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026{
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
34int main()
35{
36 {
37 typedef std::unordered_map<int, std::string> C;
38 typedef std::pair<int, std::string> P;
39 P a[] =
40 {
41 P(1, "one"),
42 P(2, "two"),
43 P(3, "three"),
44 P(4, "four"),
45 P(1, "four"),
46 P(2, "four"),
47 };
48 C c(a, a + sizeof(a)/sizeof(a[0]));
49 test(c);
50 assert(c.bucket_count() >= 5);
51 c.reserve(3);
52 assert(c.bucket_count() == 5);
53 test(c);
54 c.max_load_factor(2);
55 c.reserve(3);
56 assert(c.bucket_count() >= 2);
57 test(c);
58 c.reserve(31);
Howard Hinnant7a445152012-07-06 17:31:14 +000059 assert(c.bucket_count() >= 16);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 test(c);
61 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000062#if __cplusplus >= 201103L
63 {
64 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
65 min_allocator<std::pair<const 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() >= 5);
79 c.reserve(3);
80 assert(c.bucket_count() == 5);
81 test(c);
82 c.max_load_factor(2);
83 c.reserve(3);
84 assert(c.bucket_count() >= 2);
85 test(c);
86 c.reserve(31);
87 assert(c.bucket_count() >= 16);
88 test(c);
89 }
90#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091}