at.pass.cpp revision b64f8b07c104c6cc986570ac8ee0ed16a9f23976
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
25int main()
26{
27    {
28        typedef std::unordered_map<int, std::string> C;
29        typedef std::pair<int, std::string> P;
30        P a[] =
31        {
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        try
44        {
45            c.at(11) = "eleven";
46            assert(false);
47        }
48        catch (std::out_of_range&)
49        {
50        }
51        assert(c.size() == 4);
52    }
53    {
54        typedef std::unordered_map<int, std::string> C;
55        typedef std::pair<int, std::string> P;
56        P a[] =
57        {
58            P(1, "one"),
59            P(2, "two"),
60            P(3, "three"),
61            P(4, "four"),
62            P(1, "four"),
63            P(2, "four"),
64        };
65        const C c(a, a + sizeof(a)/sizeof(a[0]));
66        assert(c.size() == 4);
67        assert(c.at(1) == "one");
68        try
69        {
70            c.at(11);
71            assert(false);
72        }
73        catch (std::out_of_range&)
74        {
75        }
76        assert(c.size() == 4);
77    }
78}
79