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// <unordered_map>
10
11// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12// class Alloc = allocator<pair<const Key, T>>>
13// class unordered_map
14
15// mapped_type& operator[](const key_type& k);
16// mapped_type& operator[](key_type&& k);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
21
22#include "test_macros.h"
23#include "MoveOnly.h"
24#include "min_allocator.h"
25#include "count_new.h"
26
27#if TEST_STD_VER >= 11
28#include "container_test_types.h"
29#endif
30
31int main(int, char**)
32{
33 {
34 typedef std::unordered_map<int, std::string> C;
35 typedef std::pair<int, std::string> P;
36 P a[] =
37 {
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 C c(a, a + sizeof(a)/sizeof(a[0]));
46 assert(c.size() == 4);
47 c[1] = "ONE";
48 assert(c.at(1) == "ONE");
49 c[11] = "eleven";
50 assert(c.size() == 5);
51 assert(c.at(11) == "eleven");
52 }
53#if TEST_STD_VER >= 11
54 {
55 typedef std::unordered_map<MoveOnly, std::string> C;
56 typedef std::pair<int, std::string> P;
57 P a[] =
58 {
59 P(1, "one"),
60 P(2, "two"),
61 P(3, "three"),
62 P(4, "four"),
63 P(1, "four"),
64 P(2, "four"),
65 };
66 C c(a, a + sizeof(a)/sizeof(a[0]));
67 assert(c.size() == 4);
68 c[1] = "ONE";
69 assert(c.at(1) == "ONE");
70 c[11] = "eleven";
71 assert(c.size() == 5);
72 assert(c.at(11) == "eleven");
73 }
74 {
75 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
76 min_allocator<std::pair<const int, std::string>>> C;
77 typedef std::pair<int, std::string> P;
78 P a[] =
79 {
80 P(1, "one"),
81 P(2, "two"),
82 P(3, "three"),
83 P(4, "four"),
84 P(1, "four"),
85 P(2, "four"),
86 };
87 C c(a, a + sizeof(a)/sizeof(a[0]));
88 assert(c.size() == 4);
89 c[1] = "ONE";
90 assert(c.at(1) == "ONE");
91 c[11] = "eleven";
92 assert(c.size() == 5);
93 assert(c.at(11) == "eleven");
94 }
95
96 {
97 typedef std::unordered_map<MoveOnly, std::string, std::hash<MoveOnly>, std::equal_to<MoveOnly>,
98 min_allocator<std::pair<const MoveOnly, std::string>>> C;
99 typedef std::pair<int, std::string> P;
100 P a[] =
101 {
102 P(1, "one"),
103 P(2, "two"),
104 P(3, "three"),
105 P(4, "four"),
106 P(1, "four"),
107 P(2, "four"),
108 };
109 C c(a, a + sizeof(a)/sizeof(a[0]));
110 assert(c.size() == 4);
111 c[1] = "ONE";
112 assert(c.at(1) == "ONE");
113 c[11] = "eleven";
114 assert(c.size() == 5);
115 assert(c.at(11) == "eleven");
116 }
117 {
118 using Container = TCT::unordered_map<>;
119 using Key = Container::key_type;
120 using MappedType = Container::mapped_type;
121 ConstructController* cc = getConstructController();
122 cc->reset();
123 {
124 Container c;
125 const Key k(1);
126 cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>();
127 MappedType& mref = c[k];
128 assert(!cc->unchecked());
129 {
130 DisableAllocationGuard g;
131 MappedType& mref2 = c[k];
132 assert(&mref == &mref2);
133 }
134 }
135 {
136 Container c;
137 Key k(1);
138 cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>();
139 MappedType& mref = c[k];
140 assert(!cc->unchecked());
141 {
142 DisableAllocationGuard g;
143 MappedType& mref2 = c[k];
144 assert(&mref == &mref2);
145 }
146 }
147 {
148 Container c;
149 Key k(1);
150 cc->expect<std::piecewise_construct_t const&, std::tuple<Key &&>&&, std::tuple<>&&>();
151 MappedType& mref = c[std::move(k)];
152 assert(!cc->unchecked());
153 {
154 Key k2(1);
155 DisableAllocationGuard g;
156 MappedType& mref2 = c[std::move(k2)];
157 assert(&mref == &mref2);
158 }
159 }
160 }
161#endif
162
163 return 0;
164}
165

source code of libcxx/test/std/containers/unord/unord.map/unord.map.elem/index.pass.cpp