copy_alloc.pass.cpp (5786B)
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 #include <cstddef> 24 25 #include "test_macros.h" 26 #include "../../../test_compare.h" 27 #include "../../../test_hash.h" 28 #include "test_allocator.h" 29 #include "min_allocator.h" 30 31 int main() 32 { 33 { 34 typedef std::unordered_map<int, std::string, 35 test_hash<std::hash<int> >, 36 test_compare<std::equal_to<int> >, 37 test_allocator<std::pair<const int, std::string> > 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 test_allocator<std::pair<const int, std::string> >(10) 54 ); 55 C c(c0, test_allocator<std::pair<const int, std::string> >(5)); 56 LIBCPP_ASSERT(c.bucket_count() == 7); 57 assert(c.size() == 4); 58 assert(c.at(1) == "one"); 59 assert(c.at(2) == "two"); 60 assert(c.at(3) == "three"); 61 assert(c.at(4) == "four"); 62 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 63 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 64 assert(c.get_allocator() == 65 (test_allocator<std::pair<const int, std::string> >(5))); 66 assert(!c.empty()); 67 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 68 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 69 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 70 assert(c.max_load_factor() == 1); 71 } 72 #if TEST_STD_VER >= 11 73 { 74 typedef std::unordered_map<int, std::string, 75 test_hash<std::hash<int> >, 76 test_compare<std::equal_to<int> >, 77 min_allocator<std::pair<const int, std::string> > 78 > C; 79 typedef std::pair<int, std::string> P; 80 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 c0(a, a + sizeof(a)/sizeof(a[0]), 90 7, 91 test_hash<std::hash<int> >(8), 92 test_compare<std::equal_to<int> >(9), 93 min_allocator<std::pair<const int, std::string> >() 94 ); 95 C c(c0, min_allocator<std::pair<const int, std::string> >()); 96 LIBCPP_ASSERT(c.bucket_count() == 7); 97 assert(c.size() == 4); 98 assert(c.at(1) == "one"); 99 assert(c.at(2) == "two"); 100 assert(c.at(3) == "three"); 101 assert(c.at(4) == "four"); 102 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 103 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 104 assert(c.get_allocator() == 105 (min_allocator<std::pair<const int, std::string> >())); 106 assert(!c.empty()); 107 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 108 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 109 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 110 assert(c.max_load_factor() == 1); 111 } 112 { 113 typedef explicit_allocator<std::pair<const int, std::string>> A; 114 typedef std::unordered_map<int, std::string, 115 test_hash<std::hash<int> >, 116 test_compare<std::equal_to<int> >, 117 A 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 A{} 134 ); 135 C c(c0, A{}); 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() == A{}); 145 assert(!c.empty()); 146 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 147 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 148 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 149 assert(c.max_load_factor() == 1); 150 } 151 #endif 152 }