move_alloc.pass.cpp revision f836d531b4146465342939fe32ae47d157ee07b2
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(unordered_map&& u, const allocator_type& a); 17 18#include <unordered_map> 19#include <string> 20#include <cassert> 21#include <cfloat> 22 23#include "../../../test_compare.h" 24#include "../../../test_hash.h" 25#include "../../../test_allocator.h" 26 27int main() 28{ 29#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 30 { 31 typedef std::pair<int, std::string> P; 32 typedef test_allocator<std::pair<const int, std::string>> A; 33 typedef std::unordered_map<int, std::string, 34 test_hash<std::hash<int> >, 35 test_compare<std::equal_to<int> >, 36 A 37 > C; 38 P a[] = 39 { 40 P(1, "one"), 41 P(2, "two"), 42 P(3, "three"), 43 P(4, "four"), 44 P(1, "four"), 45 P(2, "four"), 46 }; 47 C c0(a, a + sizeof(a)/sizeof(a[0]), 48 7, 49 test_hash<std::hash<int> >(8), 50 test_compare<std::equal_to<int> >(9), 51 A(10) 52 ); 53 C c(std::move(c0), A(12)); 54 assert(c.bucket_count() >= 5); 55 assert(c.size() == 4); 56 assert(c.at(1) == "one"); 57 assert(c.at(2) == "two"); 58 assert(c.at(3) == "three"); 59 assert(c.at(4) == "four"); 60 assert(c.hash_function() == test_hash<std::hash<int> >(8)); 61 assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); 62 assert(c.get_allocator() == A(12)); 63 assert(!c.empty()); 64 assert(std::distance(c.begin(), c.end()) == c.size()); 65 assert(std::distance(c.cbegin(), c.cend()) == c.size()); 66 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 67 assert(c.max_load_factor() == 1); 68 69 assert(c0.empty()); 70 } 71 { 72 typedef std::pair<int, std::string> P; 73 typedef test_allocator<std::pair<const int, std::string>> A; 74 typedef std::unordered_map<int, std::string, 75 test_hash<std::hash<int> >, 76 test_compare<std::equal_to<int> >, 77 A 78 > C; 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 A(10) 93 ); 94 C c(std::move(c0), A(10)); 95 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() == A(10)); 104 assert(!c.empty()); 105 assert(std::distance(c.begin(), c.end()) == c.size()); 106 assert(std::distance(c.cbegin(), c.cend()) == c.size()); 107 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); 108 assert(c.max_load_factor() == 1); 109 110 assert(c0.empty()); 111 } 112#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 113} 114