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