1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03
10
11// <unordered_map>
12
13// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
14// class Alloc = allocator<pair<const Key, T>>>
15// class unordered_multimap
16
17// void insert(initializer_list<value_type> il);
18
19#include <unordered_map>
20#include <string>
21#include <set>
22#include <cassert>
23#include <cstddef>
24
25#include "test_macros.h"
26#include "../../../check_consecutive.h"
27#include "test_iterators.h"
28#include "min_allocator.h"
29
30int main(int, char**)
31{
32 {
33 typedef std::unordered_multimap<int, std::string> C;
34 typedef std::pair<int, std::string> P;
35 C c;
36 c.insert(
37 l: {
38 P(1, "one"),
39 P(2, "two"),
40 P(3, "three"),
41 P(4, "four"),
42 P(1, "four"),
43 P(2, "four"),
44 }
45 );
46 assert(c.size() == 6);
47 typedef std::pair<C::iterator, C::iterator> Eq;
48 Eq eq = c.equal_range(x: 1);
49 assert(std::distance(eq.first, eq.second) == 2);
50 std::multiset<std::string> s;
51 s.insert(x: "one");
52 s.insert(x: "four");
53 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
54 eq = c.equal_range(x: 2);
55 assert(std::distance(eq.first, eq.second) == 2);
56 s.insert(x: "two");
57 s.insert(x: "four");
58 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s);
59 eq = c.equal_range(x: 3);
60 assert(std::distance(eq.first, eq.second) == 1);
61 C::iterator k = eq.first;
62 assert(k->first == 3);
63 assert(k->second == "three");
64 eq = c.equal_range(x: 4);
65 assert(std::distance(eq.first, eq.second) == 1);
66 k = eq.first;
67 assert(k->first == 4);
68 assert(k->second == "four");
69 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
70 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
71 }
72 {
73 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
74 min_allocator<std::pair<const int, std::string>>> C;
75 typedef std::pair<int, std::string> P;
76 C c;
77 c.insert(
78 {
79 P(1, "one"),
80 P(2, "two"),
81 P(3, "three"),
82 P(4, "four"),
83 P(1, "four"),
84 P(2, "four"),
85 }
86 );
87 assert(c.size() == 6);
88 typedef std::pair<C::iterator, C::iterator> Eq;
89 Eq eq = c.equal_range(1);
90 assert(std::distance(eq.first, eq.second) == 2);
91 std::multiset<std::string> s;
92 s.insert(x: "one");
93 s.insert(x: "four");
94 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
95 eq = c.equal_range(2);
96 assert(std::distance(eq.first, eq.second) == 2);
97 s.insert(x: "two");
98 s.insert(x: "four");
99 CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
100 eq = c.equal_range(3);
101 assert(std::distance(eq.first, eq.second) == 1);
102 C::iterator k = eq.first;
103 assert(k->first == 3);
104 assert(k->second == "three");
105 eq = c.equal_range(4);
106 assert(std::distance(eq.first, eq.second) == 1);
107 k = eq.first;
108 assert(k->first == 4);
109 assert(k->second == "four");
110 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
111 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
112 }
113
114 return 0;
115}
116

source code of libcxx/test/std/containers/unord/unord.multimap/unord.multimap.modifiers/insert_init.pass.cpp