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

source code of libcxx/test/std/containers/unord/unord.map/unord.map.elem/at.pass.cpp