at.pass.cpp (3685B)
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& at(const key_type& k); 17 // const mapped_type& at(const key_type& k) const; 18 19 #include <cassert> 20 #include <stdexcept> 21 #include <string> 22 #include <unordered_map> 23 24 #include "MoveOnly.h" 25 #include "min_allocator.h" 26 #include "test_macros.h" 27 28 int main() 29 { 30 { 31 typedef std::unordered_map<int, std::string> C; 32 typedef std::pair<int, std::string> P; 33 P a[] = 34 { 35 P(1, "one"), 36 P(2, "two"), 37 P(3, "three"), 38 P(4, "four"), 39 P(1, "four"), 40 P(2, "four"), 41 }; 42 C c(a, a + sizeof(a)/sizeof(a[0])); 43 assert(c.size() == 4); 44 c.at(1) = "ONE"; 45 assert(c.at(1) == "ONE"); 46 #ifndef TEST_HAS_NO_EXCEPTIONS 47 try 48 { 49 c.at(11) = "eleven"; 50 assert(false); 51 } 52 catch (std::out_of_range&) 53 { 54 } 55 assert(c.size() == 4); 56 #endif 57 } 58 { 59 typedef std::unordered_map<int, std::string> C; 60 typedef std::pair<int, std::string> P; 61 P a[] = 62 { 63 P(1, "one"), 64 P(2, "two"), 65 P(3, "three"), 66 P(4, "four"), 67 P(1, "four"), 68 P(2, "four"), 69 }; 70 const C c(a, a + sizeof(a)/sizeof(a[0])); 71 assert(c.size() == 4); 72 assert(c.at(1) == "one"); 73 #ifndef TEST_HAS_NO_EXCEPTIONS 74 try 75 { 76 TEST_IGNORE_NODISCARD c.at(11); 77 assert(false); 78 } 79 catch (std::out_of_range&) 80 { 81 } 82 assert(c.size() == 4); 83 #endif 84 } 85 #if TEST_STD_VER >= 11 86 { 87 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>, 88 min_allocator<std::pair<const int, std::string>>> C; 89 typedef std::pair<int, std::string> P; 90 P a[] = 91 { 92 P(1, "one"), 93 P(2, "two"), 94 P(3, "three"), 95 P(4, "four"), 96 P(1, "four"), 97 P(2, "four"), 98 }; 99 C c(a, a + sizeof(a)/sizeof(a[0])); 100 assert(c.size() == 4); 101 c.at(1) = "ONE"; 102 assert(c.at(1) == "ONE"); 103 #ifndef TEST_HAS_NO_EXCEPTIONS 104 try 105 { 106 c.at(11) = "eleven"; 107 assert(false); 108 } 109 catch (std::out_of_range&) 110 { 111 } 112 assert(c.size() == 4); 113 #endif 114 } 115 { 116 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>, 117 min_allocator<std::pair<const int, std::string>>> C; 118 typedef std::pair<int, std::string> P; 119 P a[] = 120 { 121 P(1, "one"), 122 P(2, "two"), 123 P(3, "three"), 124 P(4, "four"), 125 P(1, "four"), 126 P(2, "four"), 127 }; 128 const C c(a, a + sizeof(a)/sizeof(a[0])); 129 assert(c.size() == 4); 130 assert(c.at(1) == "one"); 131 #ifndef TEST_HAS_NO_EXCEPTIONS 132 try 133 { 134 TEST_IGNORE_NODISCARD c.at(11); 135 assert(false); 136 } 137 catch (std::out_of_range&) 138 { 139 } 140 assert(c.size() == 4); 141 #endif 142 } 143 #endif 144 }