move.pass.cpp (6224B)
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 // UNSUPPORTED: c++98, c++03 11 12 // <unordered_set> 13 14 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>, 15 // class Alloc = allocator<Value>> 16 // class unordered_multiset 17 18 // unordered_multiset(unordered_multiset&& u); 19 20 #include <unordered_set> 21 #include <cassert> 22 #include <cfloat> 23 #include <cmath> 24 #include <cstddef> 25 26 #include "test_macros.h" 27 #include "../../../test_compare.h" 28 #include "../../../test_hash.h" 29 #include "test_allocator.h" 30 #include "min_allocator.h" 31 32 int main() 33 { 34 { 35 typedef std::unordered_multiset<int, 36 test_hash<std::hash<int> >, 37 test_compare<std::equal_to<int> >, 38 test_allocator<int> 39 > C; 40 C c0(7, 41 test_hash<std::hash<int> >(8), 42 test_compare<std::equal_to<int> >(9), 43 test_allocator<int>(10) 44 ); 45 C c = std::move(c0); 46 LIBCPP_ASSERT(c.bucket_count() == 7); 47 assert(c.size() == 0); 48 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 49 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 50 assert(c.get_allocator() == test_allocator<int>(10)); 51 assert(c.empty()); 52 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 53 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 54 assert(c.load_factor() == 0); 55 assert(c.max_load_factor() == 1); 56 57 assert(c0.empty()); 58 } 59 { 60 typedef std::unordered_multiset<int, 61 test_hash<std::hash<int> >, 62 test_compare<std::equal_to<int> >, 63 test_allocator<int> 64 > C; 65 typedef int P; 66 P a[] = 67 { 68 P(1), 69 P(2), 70 P(3), 71 P(4), 72 P(1), 73 P(2) 74 }; 75 C c0(a, a + sizeof(a)/sizeof(a[0]), 76 7, 77 test_hash<std::hash<int> >(8), 78 test_compare<std::equal_to<int> >(9), 79 test_allocator<int>(10) 80 ); 81 C c = std::move(c0); 82 LIBCPP_ASSERT(c.bucket_count() == 7); 83 assert(c.size() == 6); 84 assert(c.count(1) == 2); 85 assert(c.count(2) == 2); 86 assert(c.count(3) == 1); 87 assert(c.count(4) == 1); 88 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 89 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 90 assert(c.get_allocator() == test_allocator<int>(10)); 91 assert(!c.empty()); 92 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 93 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 94 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 95 assert(c.max_load_factor() == 1); 96 97 assert(c0.empty()); 98 } 99 { 100 typedef std::unordered_multiset<int, 101 test_hash<std::hash<int> >, 102 test_compare<std::equal_to<int> >, 103 min_allocator<int> 104 > C; 105 C c0(7, 106 test_hash<std::hash<int> >(8), 107 test_compare<std::equal_to<int> >(9), 108 min_allocator<int>() 109 ); 110 C c = std::move(c0); 111 LIBCPP_ASSERT(c.bucket_count() == 7); 112 assert(c.size() == 0); 113 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 114 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 115 assert(c.get_allocator() == min_allocator<int>()); 116 assert(c.empty()); 117 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 118 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 119 assert(c.load_factor() == 0); 120 assert(c.max_load_factor() == 1); 121 122 assert(c0.empty()); 123 } 124 { 125 typedef std::unordered_multiset<int, 126 test_hash<std::hash<int> >, 127 test_compare<std::equal_to<int> >, 128 min_allocator<int> 129 > C; 130 typedef int P; 131 P a[] = 132 { 133 P(1), 134 P(2), 135 P(3), 136 P(4), 137 P(1), 138 P(2) 139 }; 140 C c0(a, a + sizeof(a)/sizeof(a[0]), 141 7, 142 test_hash<std::hash<int> >(8), 143 test_compare<std::equal_to<int> >(9), 144 min_allocator<int>() 145 ); 146 C c = std::move(c0); 147 LIBCPP_ASSERT(c.bucket_count() == 7); 148 assert(c.size() == 6); 149 assert(c.count(1) == 2); 150 assert(c.count(2) == 2); 151 assert(c.count(3) == 1); 152 assert(c.count(4) == 1); 153 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 154 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 155 assert(c.get_allocator() == min_allocator<int>()); 156 assert(!c.empty()); 157 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); 158 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); 159 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 160 assert(c.max_load_factor() == 1); 161 162 assert(c0.empty()); 163 } 164 #if _LIBCPP_DEBUG >= 1 165 { 166 std::unordered_multiset<int> s1 = {1, 2, 3}; 167 std::unordered_multiset<int>::iterator i = s1.begin(); 168 int k = *i; 169 std::unordered_multiset<int> s2 = std::move(s1); 170 assert(*i == k); 171 s2.erase(i); 172 assert(s2.size() == 2); 173 } 174 #endif 175 }