copy_alloc.pass.cpp revision d4badbbc5a4c63fa9c644d4ae78ec0e38564c8dc
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, const allocator_type& a);
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, test_allocator<std::pair<const int, std::string> >(5));
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> >(5)));
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#if TEST_STD_VER >= 11
72    {
73        typedef std::unordered_map<int, std::string,
74                                   test_hash<std::hash<int> >,
75                                   test_compare<std::equal_to<int> >,
76                                   min_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            min_allocator<std::pair<const int, std::string> >()
93           );
94        C c(c0, min_allocator<std::pair<const int, std::string> >());
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               (min_allocator<std::pair<const int, std::string> >()));
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    {
112        typedef explicit_allocator<std::pair<const int, std::string>> A;
113        typedef std::unordered_map<int, std::string,
114                                   test_hash<std::hash<int> >,
115                                   test_compare<std::equal_to<int> >,
116                                   A
117                                   > C;
118        typedef std::pair<int, std::string> P;
119        P a[] =
120        {
121            P(1, "one"),
122            P(2, "two"),
123            P(3, "three"),
124            P(4, "four"),
125            P(1, "four"),
126            P(2, "four"),
127        };
128        C c0(a, a + sizeof(a)/sizeof(a[0]),
129            7,
130            test_hash<std::hash<int> >(8),
131            test_compare<std::equal_to<int> >(9),
132            A{}
133           );
134        C c(c0, A{});
135        LIBCPP_ASSERT(c.bucket_count() == 7);
136        assert(c.size() == 4);
137        assert(c.at(1) == "one");
138        assert(c.at(2) == "two");
139        assert(c.at(3) == "three");
140        assert(c.at(4) == "four");
141        assert(c.hash_function() == test_hash<std::hash<int> >(8));
142        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
143        assert(c.get_allocator() == A{});
144        assert(!c.empty());
145        assert(std::distance(c.begin(), c.end()) == c.size());
146        assert(std::distance(c.cbegin(), c.cend()) == c.size());
147        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
148        assert(c.max_load_factor() == 1);
149    }
150#endif
151}
152