1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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
15 // mapped_type& operator[](const key_type& k);
16 // mapped_type& operator[](key_type&& k);
18 #include <unordered_map>
22 #include "test_macros.h"
24 #include "min_allocator.h"
25 #include "count_new.h"
27 #if TEST_STD_VER >= 11
28 #include "container_test_types.h"
34 typedef std::unordered_map
<int, std::string
> C
;
35 typedef std::pair
<int, std::string
> P
;
45 C
c(a
, a
+ sizeof(a
)/sizeof(a
[0]));
46 assert(c
.size() == 4);
48 assert(c
.at(1) == "ONE");
50 assert(c
.size() == 5);
51 assert(c
.at(11) == "eleven");
53 #if TEST_STD_VER >= 11
55 typedef std::unordered_map
<MoveOnly
, std::string
> C
;
56 typedef std::pair
<int, std::string
> P
;
66 C
c(a
, a
+ sizeof(a
)/sizeof(a
[0]));
67 assert(c
.size() == 4);
69 assert(c
.at(1) == "ONE");
71 assert(c
.size() == 5);
72 assert(c
.at(11) == "eleven");
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
;
87 C
c(a
, a
+ sizeof(a
)/sizeof(a
[0]));
88 assert(c
.size() == 4);
90 assert(c
.at(1) == "ONE");
92 assert(c
.size() == 5);
93 assert(c
.at(11) == "eleven");
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
;
109 C
c(a
, a
+ sizeof(a
)/sizeof(a
[0]));
110 assert(c
.size() == 4);
112 assert(c
.at(1) == "ONE");
114 assert(c
.size() == 5);
115 assert(c
.at(11) == "eleven");
118 using Container
= TCT::unordered_map
<>;
119 using Key
= Container::key_type
;
120 using MappedType
= Container::mapped_type
;
121 ConstructController
* cc
= getConstructController();
126 cc
->expect
<std::piecewise_construct_t
const&, std::tuple
<Key
const&>&&, std::tuple
<>&&>();
127 MappedType
& mref
= c
[k
];
128 assert(!cc
->unchecked());
130 DisableAllocationGuard g
;
131 MappedType
& mref2
= c
[k
];
132 assert(&mref
== &mref2
);
138 cc
->expect
<std::piecewise_construct_t
const&, std::tuple
<Key
const&>&&, std::tuple
<>&&>();
139 MappedType
& mref
= c
[k
];
140 assert(!cc
->unchecked());
142 DisableAllocationGuard g
;
143 MappedType
& mref2
= c
[k
];
144 assert(&mref
== &mref2
);
150 cc
->expect
<std::piecewise_construct_t
const&, std::tuple
<Key
&&>&&, std::tuple
<>&&>();
151 MappedType
& mref
= c
[std::move(k
)];
152 assert(!cc
->unchecked());
155 DisableAllocationGuard g
;
156 MappedType
& mref2
= c
[std::move(k2
)];
157 assert(&mref
== &mref2
);