]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/containers/unord/unord.map/unord.map.elem/at.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / containers / unord / unord.map / unord.map.elem / at.pass.cpp
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 <unordered_map>
20 #include <string>
21 #include <cassert>
22
23 #include "MoveOnly.h"
24 #include "min_allocator.h"
25 #include "test_macros.h"
26
27 int main()
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(1) = "ONE";
44         assert(c.at(1) == "ONE");
45 #ifndef TEST_HAS_NO_EXCEPTIONS
46         try
47         {
48             c.at(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             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             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 }