Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 <unordered_map> |
| 20 | #include <string> |
| 21 | #include <cassert> |
| 22 | |
Marshall Clow | df00d5e | 2015-01-28 21:22:53 +0000 | [diff] [blame] | 23 | #include "MoveOnly.h" |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 24 | #include "min_allocator.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 25 | |
| 26 | int main() |
| 27 | { |
| 28 | { |
| 29 | typedef std::unordered_map<int, std::string> C; |
| 30 | typedef std::pair<int, std::string> P; |
| 31 | P a[] = |
| 32 | { |
| 33 | P(1, "one"), |
| 34 | P(2, "two"), |
| 35 | P(3, "three"), |
| 36 | P(4, "four"), |
| 37 | P(1, "four"), |
| 38 | P(2, "four"), |
| 39 | }; |
| 40 | C c(a, a + sizeof(a)/sizeof(a[0])); |
| 41 | assert(c.size() == 4); |
| 42 | c.at(1) = "ONE"; |
| 43 | assert(c.at(1) == "ONE"); |
| 44 | try |
| 45 | { |
| 46 | c.at(11) = "eleven"; |
| 47 | assert(false); |
| 48 | } |
| 49 | catch (std::out_of_range&) |
| 50 | { |
| 51 | } |
| 52 | assert(c.size() == 4); |
| 53 | } |
| 54 | { |
| 55 | typedef std::unordered_map<int, 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 | const C c(a, a + sizeof(a)/sizeof(a[0])); |
| 67 | assert(c.size() == 4); |
| 68 | assert(c.at(1) == "one"); |
| 69 | try |
| 70 | { |
| 71 | c.at(11); |
| 72 | assert(false); |
| 73 | } |
| 74 | catch (std::out_of_range&) |
| 75 | { |
| 76 | } |
| 77 | assert(c.size() == 4); |
| 78 | } |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 79 | #if __cplusplus >= 201103L |
| 80 | { |
| 81 | typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>, |
| 82 | min_allocator<std::pair<const int, std::string>>> C; |
| 83 | typedef std::pair<int, std::string> P; |
| 84 | P a[] = |
| 85 | { |
| 86 | P(1, "one"), |
| 87 | P(2, "two"), |
| 88 | P(3, "three"), |
| 89 | P(4, "four"), |
| 90 | P(1, "four"), |
| 91 | P(2, "four"), |
| 92 | }; |
| 93 | C c(a, a + sizeof(a)/sizeof(a[0])); |
| 94 | assert(c.size() == 4); |
| 95 | c.at(1) = "ONE"; |
| 96 | assert(c.at(1) == "ONE"); |
| 97 | try |
| 98 | { |
| 99 | c.at(11) = "eleven"; |
| 100 | assert(false); |
| 101 | } |
| 102 | catch (std::out_of_range&) |
| 103 | { |
| 104 | } |
| 105 | assert(c.size() == 4); |
| 106 | } |
| 107 | { |
| 108 | typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>, |
| 109 | min_allocator<std::pair<const int, std::string>>> C; |
| 110 | typedef std::pair<int, std::string> P; |
| 111 | P a[] = |
| 112 | { |
| 113 | P(1, "one"), |
| 114 | P(2, "two"), |
| 115 | P(3, "three"), |
| 116 | P(4, "four"), |
| 117 | P(1, "four"), |
| 118 | P(2, "four"), |
| 119 | }; |
| 120 | const C c(a, a + sizeof(a)/sizeof(a[0])); |
| 121 | assert(c.size() == 4); |
| 122 | assert(c.at(1) == "one"); |
| 123 | try |
| 124 | { |
| 125 | c.at(11); |
| 126 | assert(false); |
| 127 | } |
| 128 | catch (std::out_of_range&) |
| 129 | { |
| 130 | } |
| 131 | assert(c.size() == 4); |
| 132 | } |
| 133 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | } |