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 // <map>
10
11 // class map
12
13 // mapped_type& at(const key_type& k);
14 // const mapped_type& at(const key_type& k) const;
15
16 #include <cassert>
17 #include <map>
18 #include <stdexcept>
19
20 #include "min_allocator.h"
21 #include "test_macros.h"
22
main(int,char **)23 int main(int, char**)
24 {
25 {
26 typedef std::pair<const int, double> V;
27 V ar[] =
28 {
29 V(1, 1.5),
30 V(2, 2.5),
31 V(3, 3.5),
32 V(4, 4.5),
33 V(5, 5.5),
34 V(7, 7.5),
35 V(8, 8.5),
36 };
37 std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
38 assert(m.size() == 7);
39 assert(m.at(1) == 1.5);
40 m.at(1) = -1.5;
41 assert(m.at(1) == -1.5);
42 assert(m.at(2) == 2.5);
43 assert(m.at(3) == 3.5);
44 assert(m.at(4) == 4.5);
45 assert(m.at(5) == 5.5);
46 #ifndef TEST_HAS_NO_EXCEPTIONS
47 try
48 {
49 TEST_IGNORE_NODISCARD m.at(6);
50 assert(false);
51 }
52 catch (std::out_of_range&)
53 {
54 }
55 #endif
56 assert(m.at(7) == 7.5);
57 assert(m.at(8) == 8.5);
58 assert(m.size() == 7);
59 }
60 {
61 typedef std::pair<const int, double> V;
62 V ar[] =
63 {
64 V(1, 1.5),
65 V(2, 2.5),
66 V(3, 3.5),
67 V(4, 4.5),
68 V(5, 5.5),
69 V(7, 7.5),
70 V(8, 8.5),
71 };
72 const std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
73 assert(m.size() == 7);
74 assert(m.at(1) == 1.5);
75 assert(m.at(2) == 2.5);
76 assert(m.at(3) == 3.5);
77 assert(m.at(4) == 4.5);
78 assert(m.at(5) == 5.5);
79 #ifndef TEST_HAS_NO_EXCEPTIONS
80 try
81 {
82 TEST_IGNORE_NODISCARD m.at(6);
83 assert(false);
84 }
85 catch (std::out_of_range&)
86 {
87 }
88 #endif
89 assert(m.at(7) == 7.5);
90 assert(m.at(8) == 8.5);
91 assert(m.size() == 7);
92 }
93 #if TEST_STD_VER >= 11
94 {
95 typedef std::pair<const int, double> V;
96 V ar[] =
97 {
98 V(1, 1.5),
99 V(2, 2.5),
100 V(3, 3.5),
101 V(4, 4.5),
102 V(5, 5.5),
103 V(7, 7.5),
104 V(8, 8.5),
105 };
106 std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
107 assert(m.size() == 7);
108 assert(m.at(1) == 1.5);
109 m.at(1) = -1.5;
110 assert(m.at(1) == -1.5);
111 assert(m.at(2) == 2.5);
112 assert(m.at(3) == 3.5);
113 assert(m.at(4) == 4.5);
114 assert(m.at(5) == 5.5);
115 #ifndef TEST_HAS_NO_EXCEPTIONS
116 try
117 {
118 TEST_IGNORE_NODISCARD m.at(6);
119 assert(false);
120 }
121 catch (std::out_of_range&)
122 {
123 }
124 #endif
125 assert(m.at(7) == 7.5);
126 assert(m.at(8) == 8.5);
127 assert(m.size() == 7);
128 }
129 {
130 typedef std::pair<const int, double> V;
131 V ar[] =
132 {
133 V(1, 1.5),
134 V(2, 2.5),
135 V(3, 3.5),
136 V(4, 4.5),
137 V(5, 5.5),
138 V(7, 7.5),
139 V(8, 8.5),
140 };
141 const std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
142 assert(m.size() == 7);
143 assert(m.at(1) == 1.5);
144 assert(m.at(2) == 2.5);
145 assert(m.at(3) == 3.5);
146 assert(m.at(4) == 4.5);
147 assert(m.at(5) == 5.5);
148 #ifndef TEST_HAS_NO_EXCEPTIONS
149 try
150 {
151 TEST_IGNORE_NODISCARD m.at(6);
152 assert(false);
153 }
154 catch (std::out_of_range&)
155 {
156 }
157 #endif
158 assert(m.at(7) == 7.5);
159 assert(m.at(8) == 8.5);
160 assert(m.size() == 7);
161 }
162 #endif
163
164 return 0;
165 }
166