| 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 | |
| 31 | int main(int, char**) { |
| 32 | { |
| 33 | typedef std::unordered_map<int, std::string> C; |
| 34 | typedef std::pair<int, std::string> P; |
| 35 | P a[] = { |
| 36 | P(1, "one" ), |
| 37 | P(2, "two" ), |
| 38 | P(3, "three" ), |
| 39 | P(4, "four" ), |
| 40 | P(1, "four" ), |
| 41 | P(2, "four" ), |
| 42 | }; |
| 43 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 44 | assert(c.size() == 4); |
| 45 | c[1] = "ONE" ; |
| 46 | assert(c.at(1) == "ONE" ); |
| 47 | c[11] = "eleven" ; |
| 48 | assert(c.size() == 5); |
| 49 | assert(c.at(11) == "eleven" ); |
| 50 | } |
| 51 | #if TEST_STD_VER >= 11 |
| 52 | { |
| 53 | typedef std::unordered_map<MoveOnly, std::string> C; |
| 54 | typedef std::pair<int, std::string> P; |
| 55 | P a[] = { |
| 56 | P(1, "one" ), |
| 57 | P(2, "two" ), |
| 58 | P(3, "three" ), |
| 59 | P(4, "four" ), |
| 60 | P(1, "four" ), |
| 61 | P(2, "four" ), |
| 62 | }; |
| 63 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 64 | assert(c.size() == 4); |
| 65 | c[1] = "ONE" ; |
| 66 | assert(c.at(1) == "ONE" ); |
| 67 | c[11] = "eleven" ; |
| 68 | assert(c.size() == 5); |
| 69 | assert(c.at(11) == "eleven" ); |
| 70 | } |
| 71 | { |
| 72 | typedef std::unordered_map<int, |
| 73 | std::string, |
| 74 | std::hash<int>, |
| 75 | std::equal_to<int>, |
| 76 | min_allocator<std::pair<const int, std::string>>> |
| 77 | C; |
| 78 | typedef std::pair<int, std::string> P; |
| 79 | P a[] = { |
| 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, |
| 98 | std::string, |
| 99 | std::hash<MoveOnly>, |
| 100 | std::equal_to<MoveOnly>, |
| 101 | min_allocator<std::pair<const MoveOnly, std::string>>> |
| 102 | C; |
| 103 | typedef std::pair<int, std::string> P; |
| 104 | P a[] = { |
| 105 | P(1, "one" ), |
| 106 | P(2, "two" ), |
| 107 | P(3, "three" ), |
| 108 | P(4, "four" ), |
| 109 | P(1, "four" ), |
| 110 | P(2, "four" ), |
| 111 | }; |
| 112 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 113 | assert(c.size() == 4); |
| 114 | c[1] = "ONE" ; |
| 115 | assert(c.at(1) == "ONE" ); |
| 116 | c[11] = "eleven" ; |
| 117 | assert(c.size() == 5); |
| 118 | assert(c.at(11) == "eleven" ); |
| 119 | } |
| 120 | { |
| 121 | using Container = TCT::unordered_map<>; |
| 122 | using Key = Container::key_type; |
| 123 | using MappedType = Container::mapped_type; |
| 124 | ConstructController* cc = getConstructController(); |
| 125 | cc->reset(); |
| 126 | { |
| 127 | Container c; |
| 128 | const Key k(1); |
| 129 | cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>(); |
| 130 | MappedType& mref = c[k]; |
| 131 | assert(!cc->unchecked()); |
| 132 | { |
| 133 | DisableAllocationGuard g; |
| 134 | MappedType& mref2 = c[k]; |
| 135 | assert(&mref == &mref2); |
| 136 | } |
| 137 | } |
| 138 | { |
| 139 | Container c; |
| 140 | Key k(1); |
| 141 | cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>(); |
| 142 | MappedType& mref = c[k]; |
| 143 | assert(!cc->unchecked()); |
| 144 | { |
| 145 | DisableAllocationGuard g; |
| 146 | MappedType& mref2 = c[k]; |
| 147 | assert(&mref == &mref2); |
| 148 | } |
| 149 | } |
| 150 | { |
| 151 | Container c; |
| 152 | Key k(1); |
| 153 | cc->expect<std::piecewise_construct_t const&, std::tuple<Key&&>&&, std::tuple<>&&>(); |
| 154 | MappedType& mref = c[std::move(k)]; |
| 155 | assert(!cc->unchecked()); |
| 156 | { |
| 157 | Key k2(1); |
| 158 | DisableAllocationGuard g; |
| 159 | MappedType& mref2 = c[std::move(k2)]; |
| 160 | assert(&mref == &mref2); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |