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_multimap
15
16// unordered_multimap(initializer_list<value_type> il, size_type n,
17//               const hasher& hf, const key_equal& eql);
18
19#include <unordered_map>
20#include <string>
21#include <cassert>
22#include <cfloat>
23#include <cmath>
24#include <cstddef>
25
26#include "test_macros.h"
27#include "../../../test_compare.h"
28#include "../../../test_hash.h"
29#include "test_allocator.h"
30#include "min_allocator.h"
31
32int main()
33{
34#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
35    {
36        typedef std::unordered_multimap<int, std::string,
37                                   test_hash<std::hash<int> >,
38                                   test_compare<std::equal_to<int> >,
39                                   test_allocator<std::pair<const int, std::string> >
40                                   > C;
41        typedef std::pair<int, std::string> P;
42        C c({
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            7,
51            test_hash<std::hash<int> >(8),
52            test_compare<std::equal_to<int> >(9)
53           );
54        LIBCPP_ASSERT(c.bucket_count() == 7);
55        assert(c.size() == 6);
56        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
57        Eq eq = c.equal_range(1);
58        assert(std::distance(eq.first, eq.second) == 2);
59        C::const_iterator i = eq.first;
60        assert(i->first == 1);
61        assert(i->second == "one");
62        ++i;
63        assert(i->first == 1);
64        assert(i->second == "four");
65        eq = c.equal_range(2);
66        assert(std::distance(eq.first, eq.second) == 2);
67        i = eq.first;
68        assert(i->first == 2);
69        assert(i->second == "two");
70        ++i;
71        assert(i->first == 2);
72        assert(i->second == "four");
73
74        eq = c.equal_range(3);
75        assert(std::distance(eq.first, eq.second) == 1);
76        i = eq.first;
77        assert(i->first == 3);
78        assert(i->second == "three");
79        eq = c.equal_range(4);
80        assert(std::distance(eq.first, eq.second) == 1);
81        i = eq.first;
82        assert(i->first == 4);
83        assert(i->second == "four");
84        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
85        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
86        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
87        assert(c.max_load_factor() == 1);
88        assert(c.hash_function() == test_hash<std::hash<int> >(8));
89        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
90        assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
91    }
92#if TEST_STD_VER >= 11
93    {
94        typedef std::unordered_multimap<int, std::string,
95                                   test_hash<std::hash<int> >,
96                                   test_compare<std::equal_to<int> >,
97                                   min_allocator<std::pair<const int, std::string> >
98                                   > C;
99        typedef std::pair<int, std::string> P;
100        C c({
101                P(1, "one"),
102                P(2, "two"),
103                P(3, "three"),
104                P(4, "four"),
105                P(1, "four"),
106                P(2, "four"),
107            },
108            7,
109            test_hash<std::hash<int> >(8),
110            test_compare<std::equal_to<int> >(9)
111           );
112        LIBCPP_ASSERT(c.bucket_count() == 7);
113        assert(c.size() == 6);
114        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
115        Eq eq = c.equal_range(1);
116        assert(std::distance(eq.first, eq.second) == 2);
117        C::const_iterator i = eq.first;
118        assert(i->first == 1);
119        assert(i->second == "one");
120        ++i;
121        assert(i->first == 1);
122        assert(i->second == "four");
123        eq = c.equal_range(2);
124        assert(std::distance(eq.first, eq.second) == 2);
125        i = eq.first;
126        assert(i->first == 2);
127        assert(i->second == "two");
128        ++i;
129        assert(i->first == 2);
130        assert(i->second == "four");
131
132        eq = c.equal_range(3);
133        assert(std::distance(eq.first, eq.second) == 1);
134        i = eq.first;
135        assert(i->first == 3);
136        assert(i->second == "three");
137        eq = c.equal_range(4);
138        assert(std::distance(eq.first, eq.second) == 1);
139        i = eq.first;
140        assert(i->first == 4);
141        assert(i->second == "four");
142        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
143        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
144        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
145        assert(c.max_load_factor() == 1);
146        assert(c.hash_function() == test_hash<std::hash<int> >(8));
147        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
148        assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
149    }
150#endif
151#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
152}
153