copy.pass.cpp revision 0809a7f62b7e43e2e5f3a31f66dd8cf097cfeb71
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(const unordered_map& u);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
21#include <cfloat>
22#include <cmath>
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 std::unordered_map<int, std::string,
34                                   test_hash<std::hash<int> >,
35                                   test_compare<std::equal_to<int> >,
36                                   test_allocator<std::pair<const int, std::string> >
37                                   > C;
38        typedef std::pair<int, std::string> P;
39        P a[] =
40        {
41            P(1, "one"),
42            P(2, "two"),
43            P(3, "three"),
44            P(4, "four"),
45            P(1, "four"),
46            P(2, "four"),
47        };
48        C c0(a, a + sizeof(a)/sizeof(a[0]),
49            7,
50            test_hash<std::hash<int> >(8),
51            test_compare<std::equal_to<int> >(9),
52            test_allocator<std::pair<const int, std::string> >(10)
53           );
54        C c = c0;
55        LIBCPP_ASSERT(c.bucket_count() == 7);
56        assert(c.size() == 4);
57        assert(c.at(1) == "one");
58        assert(c.at(2) == "two");
59        assert(c.at(3) == "three");
60        assert(c.at(4) == "four");
61        assert(c.hash_function() == test_hash<std::hash<int> >(8));
62        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
63        assert(c.get_allocator() ==
64               (test_allocator<std::pair<const int, std::string> >(10)));
65        assert(!c.empty());
66        assert(std::distance(c.begin(), c.end()) == c.size());
67        assert(std::distance(c.cbegin(), c.cend()) == c.size());
68        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
69        assert(c.max_load_factor() == 1);
70    }
71#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
72    {
73        typedef std::unordered_map<int, std::string,
74                                   test_hash<std::hash<int> >,
75                                   test_compare<std::equal_to<int> >,
76                                   other_allocator<std::pair<const int, std::string> >
77                                   > C;
78        typedef std::pair<int, std::string> P;
79        P a[] =
80        {
81            P(1, "one"),
82            P(2, "two"),
83            P(3, "three"),
84            P(4, "four"),
85            P(1, "four"),
86            P(2, "four"),
87        };
88        C c0(a, a + sizeof(a)/sizeof(a[0]),
89            7,
90            test_hash<std::hash<int> >(8),
91            test_compare<std::equal_to<int> >(9),
92            other_allocator<std::pair<const int, std::string> >(10)
93           );
94        C c = c0;
95        LIBCPP_ASSERT(c.bucket_count() == 7);
96        assert(c.size() == 4);
97        assert(c.at(1) == "one");
98        assert(c.at(2) == "two");
99        assert(c.at(3) == "three");
100        assert(c.at(4) == "four");
101        assert(c.hash_function() == test_hash<std::hash<int> >(8));
102        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
103        assert(c.get_allocator() ==
104               (other_allocator<std::pair<const int, std::string> >(-2)));
105        assert(!c.empty());
106        assert(std::distance(c.begin(), c.end()) == c.size());
107        assert(std::distance(c.cbegin(), c.cend()) == c.size());
108        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
109        assert(c.max_load_factor() == 1);
110    }
111#endif  // _LIBCPP_HAS_NO_ADVANCED_SFINAE
112#if TEST_STD_VER >= 11
113    {
114        typedef std::unordered_map<int, std::string,
115                                   test_hash<std::hash<int> >,
116                                   test_compare<std::equal_to<int> >,
117                                   min_allocator<std::pair<const int, std::string> >
118                                   > C;
119        typedef std::pair<int, std::string> P;
120        P a[] =
121        {
122            P(1, "one"),
123            P(2, "two"),
124            P(3, "three"),
125            P(4, "four"),
126            P(1, "four"),
127            P(2, "four"),
128        };
129        C c0(a, a + sizeof(a)/sizeof(a[0]),
130            7,
131            test_hash<std::hash<int> >(8),
132            test_compare<std::equal_to<int> >(9),
133            min_allocator<std::pair<const int, std::string> >()
134           );
135        C c = c0;
136        LIBCPP_ASSERT(c.bucket_count() == 7);
137        assert(c.size() == 4);
138        assert(c.at(1) == "one");
139        assert(c.at(2) == "two");
140        assert(c.at(3) == "three");
141        assert(c.at(4) == "four");
142        assert(c.hash_function() == test_hash<std::hash<int> >(8));
143        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
144        assert(c.get_allocator() ==
145               (min_allocator<std::pair<const int, std::string> >()));
146        assert(!c.empty());
147        assert(std::distance(c.begin(), c.end()) == c.size());
148        assert(std::distance(c.cbegin(), c.cend()) == c.size());
149        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
150        assert(c.max_load_factor() == 1);
151    }
152#endif
153}
154