blob: 5fd9f9d6114a878e949c7e7c37687c803e42c023 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
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 Clowdf00d5e2015-01-28 21:22:53 +000023#include "MoveOnly.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000024#include "min_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025
26int 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 Hinnant7a6b7ce2013-06-22 15:21:29 +000079#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 Hinnantbc8d3f92010-05-11 19:42:16 +0000134}