blob: 879e6811f2d99e2894ce0188ae5b4fff66ba97b2 [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_multimap
15
16// template <class InputIterator>
17// unordered_multimap(InputIterator first, InputIterator last, size_type n,
18// const hasher& hf, const key_equal& eql);
19
20#include <unordered_map>
21#include <string>
22#include <cassert>
Howard Hinnantf836d532011-12-02 21:23:14 +000023#include <cfloat>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024
Marshall Clow83e2c4d2013-01-05 03:21:01 +000025#include "test_iterators.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026#include "../../../NotConstructible.h"
27#include "../../../test_compare.h"
28#include "../../../test_hash.h"
Marshall Clow1b921882013-12-03 00:18:10 +000029#include "test_allocator.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000030#include "min_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031
32int main()
33{
34 {
35 typedef std::unordered_multimap<int, std::string,
36 test_hash<std::hash<int> >,
37 test_compare<std::equal_to<int> >,
38 test_allocator<std::pair<const int, std::string> >
39 > C;
40 typedef std::pair<int, std::string> P;
41 P a[] =
42 {
43 P(1, "one"),
44 P(2, "two"),
45 P(3, "three"),
46 P(4, "four"),
47 P(1, "four"),
48 P(2, "four"),
49 };
50 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
51 7,
52 test_hash<std::hash<int> >(8),
53 test_compare<std::equal_to<int> >(9)
54 );
55 assert(c.bucket_count() == 7);
56 assert(c.size() == 6);
57 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
58 Eq eq = c.equal_range(1);
59 assert(std::distance(eq.first, eq.second) == 2);
60 C::const_iterator i = eq.first;
61 assert(i->first == 1);
62 assert(i->second == "one");
63 ++i;
64 assert(i->first == 1);
65 assert(i->second == "four");
66 eq = c.equal_range(2);
67 assert(std::distance(eq.first, eq.second) == 2);
68 i = eq.first;
69 assert(i->first == 2);
70 assert(i->second == "two");
71 ++i;
72 assert(i->first == 2);
73 assert(i->second == "four");
74
75 eq = c.equal_range(3);
76 assert(std::distance(eq.first, eq.second) == 1);
77 i = eq.first;
78 assert(i->first == 3);
79 assert(i->second == "three");
80 eq = c.equal_range(4);
81 assert(std::distance(eq.first, eq.second) == 1);
82 i = eq.first;
83 assert(i->first == 4);
84 assert(i->second == "four");
85 assert(std::distance(c.begin(), c.end()) == c.size());
86 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -070087 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088 assert(c.max_load_factor() == 1);
89 assert(c.hash_function() == test_hash<std::hash<int> >(8));
90 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
91 assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
92 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000093#if __cplusplus >= 201103L
94 {
95 typedef std::unordered_multimap<int, std::string,
96 test_hash<std::hash<int> >,
97 test_compare<std::equal_to<int> >,
98 min_allocator<std::pair<const int, std::string> >
99 > C;
100 typedef std::pair<int, std::string> P;
101 P a[] =
102 {
103 P(1, "one"),
104 P(2, "two"),
105 P(3, "three"),
106 P(4, "four"),
107 P(1, "four"),
108 P(2, "four"),
109 };
110 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
111 7,
112 test_hash<std::hash<int> >(8),
113 test_compare<std::equal_to<int> >(9)
114 );
115 assert(c.bucket_count() == 7);
116 assert(c.size() == 6);
117 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
118 Eq eq = c.equal_range(1);
119 assert(std::distance(eq.first, eq.second) == 2);
120 C::const_iterator i = eq.first;
121 assert(i->first == 1);
122 assert(i->second == "one");
123 ++i;
124 assert(i->first == 1);
125 assert(i->second == "four");
126 eq = c.equal_range(2);
127 assert(std::distance(eq.first, eq.second) == 2);
128 i = eq.first;
129 assert(i->first == 2);
130 assert(i->second == "two");
131 ++i;
132 assert(i->first == 2);
133 assert(i->second == "four");
134
135 eq = c.equal_range(3);
136 assert(std::distance(eq.first, eq.second) == 1);
137 i = eq.first;
138 assert(i->first == 3);
139 assert(i->second == "three");
140 eq = c.equal_range(4);
141 assert(std::distance(eq.first, eq.second) == 1);
142 i = eq.first;
143 assert(i->first == 4);
144 assert(i->second == "four");
145 assert(std::distance(c.begin(), c.end()) == c.size());
146 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700147 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000148 assert(c.max_load_factor() == 1);
149 assert(c.hash_function() == test_hash<std::hash<int> >(8));
150 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
151 assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
152 }
153#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154}