Debian Codegraph @

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

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