blob: 48239db1fa2216fedf00f1fc856a529b48e31f08 [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// unordered_multimap(initializer_list<value_type> il, size_type n);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
Howard Hinnantf836d532011-12-02 21:23:14 +000021#include <cfloat>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022
23#include "../../../test_compare.h"
24#include "../../../test_hash.h"
Marshall Clow1b921882013-12-03 00:18:10 +000025#include "test_allocator.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000026#include "min_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027
28int main()
29{
Howard Hinnante3e32912011-08-12 21:56:02 +000030#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031 {
32 typedef std::unordered_multimap<int, std::string,
33 test_hash<std::hash<int> >,
34 test_compare<std::equal_to<int> >,
35 test_allocator<std::pair<const int, std::string> >
36 > C;
37 typedef std::pair<int, std::string> P;
38 C c({
39 P(1, "one"),
40 P(2, "two"),
41 P(3, "three"),
42 P(4, "four"),
43 P(1, "four"),
44 P(2, "four"),
45 },
46 7
47 );
48 assert(c.bucket_count() == 7);
49 assert(c.size() == 6);
50 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
51 Eq eq = c.equal_range(1);
52 assert(std::distance(eq.first, eq.second) == 2);
53 C::const_iterator i = eq.first;
54 assert(i->first == 1);
55 assert(i->second == "one");
56 ++i;
57 assert(i->first == 1);
58 assert(i->second == "four");
59 eq = c.equal_range(2);
60 assert(std::distance(eq.first, eq.second) == 2);
61 i = eq.first;
62 assert(i->first == 2);
63 assert(i->second == "two");
64 ++i;
65 assert(i->first == 2);
66 assert(i->second == "four");
67
68 eq = c.equal_range(3);
69 assert(std::distance(eq.first, eq.second) == 1);
70 i = eq.first;
71 assert(i->first == 3);
72 assert(i->second == "three");
73 eq = c.equal_range(4);
74 assert(std::distance(eq.first, eq.second) == 1);
75 i = eq.first;
76 assert(i->first == 4);
77 assert(i->second == "four");
78 assert(std::distance(c.begin(), c.end()) == c.size());
79 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -070080 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000081 assert(c.max_load_factor() == 1);
82 assert(c.hash_function() == test_hash<std::hash<int> >());
83 assert(c.key_eq() == test_compare<std::equal_to<int> >());
84 assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
85 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000086#if __cplusplus >= 201103L
87 {
88 typedef std::unordered_multimap<int, std::string,
89 test_hash<std::hash<int> >,
90 test_compare<std::equal_to<int> >,
91 min_allocator<std::pair<const int, std::string> >
92 > C;
93 typedef std::pair<int, std::string> P;
94 C c({
95 P(1, "one"),
96 P(2, "two"),
97 P(3, "three"),
98 P(4, "four"),
99 P(1, "four"),
100 P(2, "four"),
101 },
102 7
103 );
104 assert(c.bucket_count() == 7);
105 assert(c.size() == 6);
106 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
107 Eq eq = c.equal_range(1);
108 assert(std::distance(eq.first, eq.second) == 2);
109 C::const_iterator i = eq.first;
110 assert(i->first == 1);
111 assert(i->second == "one");
112 ++i;
113 assert(i->first == 1);
114 assert(i->second == "four");
115 eq = c.equal_range(2);
116 assert(std::distance(eq.first, eq.second) == 2);
117 i = eq.first;
118 assert(i->first == 2);
119 assert(i->second == "two");
120 ++i;
121 assert(i->first == 2);
122 assert(i->second == "four");
123
124 eq = c.equal_range(3);
125 assert(std::distance(eq.first, eq.second) == 1);
126 i = eq.first;
127 assert(i->first == 3);
128 assert(i->second == "three");
129 eq = c.equal_range(4);
130 assert(std::distance(eq.first, eq.second) == 1);
131 i = eq.first;
132 assert(i->first == 4);
133 assert(i->second == "four");
134 assert(std::distance(c.begin(), c.end()) == c.size());
135 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700136 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000137 assert(c.max_load_factor() == 1);
138 assert(c.hash_function() == test_hash<std::hash<int> >());
139 assert(c.key_eq() == test_compare<std::equal_to<int> >());
140 assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
141 }
142#endif
Howard Hinnante3e32912011-08-12 21:56:02 +0000143#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144}