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