emplace.pass.cpp revision 061d0cc4db18d17bf01ed14c5db0be098205bd47
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// template <class... Args>
17//     pair<iterator, bool> emplace(Args&&... args);
18
19#include <unordered_map>
20#include <cassert>
21
22#include "../../../Emplaceable.h"
23#include "min_allocator.h"
24
25int main()
26{
27#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
28    {
29        typedef std::unordered_map<int, Emplaceable> C;
30        typedef std::pair<C::iterator, bool> R;
31        C c;
32        R r = c.emplace(std::piecewise_construct, std::forward_as_tuple(3),
33                                                  std::forward_as_tuple());
34        assert(r.second);
35        assert(c.size() == 1);
36        assert(r.first->first == 3);
37        assert(r.first->second == Emplaceable());
38
39        r = c.emplace(std::pair<const int, Emplaceable>(4, Emplaceable(5, 6)));
40        assert(r.second);
41        assert(c.size() == 2);
42        assert(r.first->first == 4);
43        assert(r.first->second == Emplaceable(5, 6));
44
45        r = c.emplace(std::piecewise_construct, std::forward_as_tuple(5),
46                                               std::forward_as_tuple(6, 7));
47        assert(r.second);
48        assert(c.size() == 3);
49        assert(r.first->first == 5);
50        assert(r.first->second == Emplaceable(6, 7));
51    }
52#if __cplusplus >= 201103L
53    {
54        typedef std::unordered_map<int, Emplaceable, std::hash<int>, std::equal_to<int>,
55                            min_allocator<std::pair<const int, Emplaceable>>> C;
56        typedef std::pair<C::iterator, bool> R;
57        C c;
58        R r = c.emplace(std::piecewise_construct, std::forward_as_tuple(3),
59                                                  std::forward_as_tuple());
60        assert(r.second);
61        assert(c.size() == 1);
62        assert(r.first->first == 3);
63        assert(r.first->second == Emplaceable());
64
65        r = c.emplace(std::pair<const int, Emplaceable>(4, Emplaceable(5, 6)));
66        assert(r.second);
67        assert(c.size() == 2);
68        assert(r.first->first == 4);
69        assert(r.first->second == Emplaceable(5, 6));
70
71        r = c.emplace(std::piecewise_construct, std::forward_as_tuple(5),
72                                               std::forward_as_tuple(6, 7));
73        assert(r.second);
74        assert(c.size() == 3);
75        assert(r.first->first == 5);
76        assert(r.first->second == Emplaceable(6, 7));
77    }
78#endif
79#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
80}
81