move.pass.cpp revision 1b92188a82b01e76ac6e8ad5f997293c2a078adc
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// unordered_map(unordered_map&& u);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
21#include <cfloat>
22
23#include "../../../test_compare.h"
24#include "../../../test_hash.h"
25#include "test_allocator.h"
26#include "min_allocator.h"
27
28int main()
29{
30#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
31    {
32        typedef std::unordered_map<int, std::string,
33                                   test_hash<std::hash<int> >,
34                                   test_compare<std::equal_to<int> >,
35                                   test_allocator<std::pair<const int, std::string> >
36                                   > C;
37        typedef std::pair<int, std::string> P;
38        P a[] =
39        {
40            P(1, "one"),
41            P(2, "two"),
42            P(3, "three"),
43            P(4, "four"),
44            P(1, "four"),
45            P(2, "four"),
46        };
47        C c0(7,
48            test_hash<std::hash<int> >(8),
49            test_compare<std::equal_to<int> >(9),
50            test_allocator<std::pair<const int, std::string> >(10)
51           );
52        C c = std::move(c0);
53        assert(c.bucket_count() == 7);
54        assert(c.size() == 0);
55        assert(c.hash_function() == test_hash<std::hash<int> >(8));
56        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
57        assert(c.get_allocator() ==
58               (test_allocator<std::pair<const int, std::string> >(10)));
59        assert(c.empty());
60        assert(std::distance(c.begin(), c.end()) == c.size());
61        assert(std::distance(c.cbegin(), c.cend()) == c.size());
62        assert(c.load_factor() == 0);
63        assert(c.max_load_factor() == 1);
64
65        assert(c0.empty());
66    }
67    {
68        typedef std::unordered_map<int, std::string,
69                                   test_hash<std::hash<int> >,
70                                   test_compare<std::equal_to<int> >,
71                                   test_allocator<std::pair<const int, std::string> >
72                                   > C;
73        typedef std::pair<int, std::string> P;
74        P a[] =
75        {
76            P(1, "one"),
77            P(2, "two"),
78            P(3, "three"),
79            P(4, "four"),
80            P(1, "four"),
81            P(2, "four"),
82        };
83        C c0(a, a + sizeof(a)/sizeof(a[0]),
84            7,
85            test_hash<std::hash<int> >(8),
86            test_compare<std::equal_to<int> >(9),
87            test_allocator<std::pair<const int, std::string> >(10)
88           );
89        C c = std::move(c0);
90        assert(c.bucket_count() == 7);
91        assert(c.size() == 4);
92        assert(c.at(1) == "one");
93        assert(c.at(2) == "two");
94        assert(c.at(3) == "three");
95        assert(c.at(4) == "four");
96        assert(c.hash_function() == test_hash<std::hash<int> >(8));
97        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
98        assert(c.get_allocator() ==
99               (test_allocator<std::pair<const int, std::string> >(10)));
100        assert(!c.empty());
101        assert(std::distance(c.begin(), c.end()) == c.size());
102        assert(std::distance(c.cbegin(), c.cend()) == c.size());
103        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
104        assert(c.max_load_factor() == 1);
105
106        assert(c0.empty());
107    }
108#if __cplusplus >= 201103L
109    {
110        typedef std::unordered_map<int, std::string,
111                                   test_hash<std::hash<int> >,
112                                   test_compare<std::equal_to<int> >,
113                                   min_allocator<std::pair<const int, std::string> >
114                                   > C;
115        typedef std::pair<int, std::string> P;
116        P a[] =
117        {
118            P(1, "one"),
119            P(2, "two"),
120            P(3, "three"),
121            P(4, "four"),
122            P(1, "four"),
123            P(2, "four"),
124        };
125        C c0(7,
126            test_hash<std::hash<int> >(8),
127            test_compare<std::equal_to<int> >(9),
128            min_allocator<std::pair<const int, std::string> >()
129           );
130        C c = std::move(c0);
131        assert(c.bucket_count() == 7);
132        assert(c.size() == 0);
133        assert(c.hash_function() == test_hash<std::hash<int> >(8));
134        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
135        assert(c.get_allocator() ==
136               (min_allocator<std::pair<const int, std::string> >()));
137        assert(c.empty());
138        assert(std::distance(c.begin(), c.end()) == c.size());
139        assert(std::distance(c.cbegin(), c.cend()) == c.size());
140        assert(c.load_factor() == 0);
141        assert(c.max_load_factor() == 1);
142
143        assert(c0.empty());
144    }
145    {
146        typedef std::unordered_map<int, std::string,
147                                   test_hash<std::hash<int> >,
148                                   test_compare<std::equal_to<int> >,
149                                   min_allocator<std::pair<const int, std::string> >
150                                   > C;
151        typedef std::pair<int, std::string> P;
152        P a[] =
153        {
154            P(1, "one"),
155            P(2, "two"),
156            P(3, "three"),
157            P(4, "four"),
158            P(1, "four"),
159            P(2, "four"),
160        };
161        C c0(a, a + sizeof(a)/sizeof(a[0]),
162            7,
163            test_hash<std::hash<int> >(8),
164            test_compare<std::equal_to<int> >(9),
165            min_allocator<std::pair<const int, std::string> >()
166           );
167        C c = std::move(c0);
168        assert(c.bucket_count() == 7);
169        assert(c.size() == 4);
170        assert(c.at(1) == "one");
171        assert(c.at(2) == "two");
172        assert(c.at(3) == "three");
173        assert(c.at(4) == "four");
174        assert(c.hash_function() == test_hash<std::hash<int> >(8));
175        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
176        assert(c.get_allocator() ==
177               (min_allocator<std::pair<const int, std::string> >()));
178        assert(!c.empty());
179        assert(std::distance(c.begin(), c.end()) == c.size());
180        assert(std::distance(c.cbegin(), c.cend()) == c.size());
181        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
182        assert(c.max_load_factor() == 1);
183
184        assert(c0.empty());
185    }
186#endif
187#if _LIBCPP_DEBUG >= 1
188    {
189        std::unordered_map<int, int> s1 = {{1, 1}, {2, 2}, {3, 3}};
190        std::unordered_map<int, int>::iterator i = s1.begin();
191        std::pair<const int, int> k = *i;
192        std::unordered_map<int, int> s2 = std::move(s1);
193        assert(*i == k);
194        s2.erase(i);
195        assert(s2.size() == 2);
196    }
197#endif
198#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
199}
200