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