copy.pass.cpp (6905B)
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_multimap 15 16 // unordered_multimap(const unordered_multimap& u); 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_multimap<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; 56 LIBCPP_ASSERT(c.bucket_count() == 7); 57 assert(c.size() == 6); 58 C::const_iterator i = c.cbegin(); 59 assert(i->first == 1); 60 assert(i->second == "one"); 61 ++i; 62 assert(i->first == 1); 63 assert(i->second == "four"); 64 ++i; 65 assert(i->first == 2); 66 assert(i->second == "two"); 67 ++i; 68 assert(i->first == 2); 69 assert(i->second == "four"); 70 ++i; 71 assert(i->first == 3); 72 assert(i->second == "three"); 73 ++i; 74 assert(i->first == 4); 75 assert(i->second == "four"); 76 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 77 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 78 assert(c.get_allocator() == 79 (test_allocator<std::pair<const int, std::string> >(10))); 80 assert(!c.empty()); 81 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 82 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 83 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 84 assert(c.max_load_factor() == 1); 85 } 86 #if TEST_STD_VER >= 11 87 { 88 typedef std::unordered_multimap<int, std::string, 89 test_hash<std::hash<int> >, 90 test_compare<std::equal_to<int> >, 91 other_allocator<std::pair<const int, std::string> > 92 > C; 93 typedef std::pair<int, std::string> P; 94 P a[] = 95 { 96 P(1, "one"), 97 P(2, "two"), 98 P(3, "three"), 99 P(4, "four"), 100 P(1, "four"), 101 P(2, "four"), 102 }; 103 C c0(a, a + sizeof(a)/sizeof(a[0]), 104 7, 105 test_hash<std::hash<int> >(8), 106 test_compare<std::equal_to<int> >(9), 107 other_allocator<std::pair<const int, std::string> >(10) 108 ); 109 C c = c0; 110 LIBCPP_ASSERT(c.bucket_count() == 7); 111 assert(c.size() == 6); 112 C::const_iterator i = c.cbegin(); 113 assert(i->first == 1); 114 assert(i->second == "one"); 115 ++i; 116 assert(i->first == 1); 117 assert(i->second == "four"); 118 ++i; 119 assert(i->first == 2); 120 assert(i->second == "two"); 121 ++i; 122 assert(i->first == 2); 123 assert(i->second == "four"); 124 ++i; 125 assert(i->first == 3); 126 assert(i->second == "three"); 127 ++i; 128 assert(i->first == 4); 129 assert(i->second == "four"); 130 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 131 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 132 assert(c.get_allocator() == 133 (other_allocator<std::pair<const int, std::string> >(-2))); 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(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 138 assert(c.max_load_factor() == 1); 139 } 140 { 141 typedef std::unordered_multimap<int, std::string, 142 test_hash<std::hash<int> >, 143 test_compare<std::equal_to<int> >, 144 min_allocator<std::pair<const int, std::string> > 145 > C; 146 typedef std::pair<int, std::string> P; 147 P a[] = 148 { 149 P(1, "one"), 150 P(2, "two"), 151 P(3, "three"), 152 P(4, "four"), 153 P(1, "four"), 154 P(2, "four"), 155 }; 156 C c0(a, a + sizeof(a)/sizeof(a[0]), 157 7, 158 test_hash<std::hash<int> >(8), 159 test_compare<std::equal_to<int> >(9), 160 min_allocator<std::pair<const int, std::string> >() 161 ); 162 C c = c0; 163 LIBCPP_ASSERT(c.bucket_count() == 7); 164 assert(c.size() == 6); 165 C::const_iterator i = c.cbegin(); 166 assert(i->first == 1); 167 assert(i->second == "one"); 168 ++i; 169 assert(i->first == 1); 170 assert(i->second == "four"); 171 ++i; 172 assert(i->first == 2); 173 assert(i->second == "two"); 174 ++i; 175 assert(i->first == 2); 176 assert(i->second == "four"); 177 ++i; 178 assert(i->first == 3); 179 assert(i->second == "three"); 180 ++i; 181 assert(i->first == 4); 182 assert(i->second == "four"); 183 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 184 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 185 assert(c.get_allocator() == 186 (min_allocator<std::pair<const int, std::string> >())); 187 assert(!c.empty()); 188 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 189 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 190 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 191 assert(c.max_load_factor() == 1); 192 } 193 #endif 194 }