| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// UNSUPPORTED: c++03 |
| 10 |
|
| 11 |
// <unordered_map> |
| 12 |
|
| 13 |
// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 14 |
// class Alloc = allocator<pair<const Key, T>>> |
| 15 |
// class unordered_map |
| 16 |
|
| 17 |
// unordered_map& operator=(initializer_list<value_type> il); |
| 18 |
|
| 19 |
#include <unordered_map> |
| 20 |
#include <string> |
| 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 "min_allocator.h" |
| 30 |
|
| 31 |
int main(int, char**) |
| 32 |
{ |
| 33 |
{ |
| 34 |
typedef std::allocator<std::pair<const int, std::string> > A; |
| 35 |
typedef std::unordered_map<int, std::string, |
| 36 |
test_hash<int>, |
| 37 |
test_equal_to<int>, |
| 38 |
A |
| 39 |
> C; |
| 40 |
typedef std::pair<int, std::string> P; |
| 41 |
C c = { |
| 42 |
P(4, "four"), |
| 43 |
P(1, "four"), |
| 44 |
P(2, "four"), |
| 45 |
}; |
| 46 |
c = { |
| 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 |
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(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 61 |
assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 62 |
assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
| 63 |
assert(c.max_load_factor() == 1); |
| 64 |
} |
| 65 |
{ |
| 66 |
typedef min_allocator<std::pair<const int, std::string> > A; |
| 67 |
typedef std::unordered_map<int, std::string, |
| 68 |
test_hash<int>, |
| 69 |
test_equal_to<int>, |
| 70 |
A |
| 71 |
> C; |
| 72 |
typedef std::pair<int, std::string> P; |
| 73 |
C c = { |
| 74 |
P(4, "four"), |
| 75 |
P(1, "four"), |
| 76 |
P(2, "four"), |
| 77 |
}; |
| 78 |
c = { |
| 79 |
P(1, "one"), |
| 80 |
P(2, "two"), |
| 81 |
P(3, "three"), |
| 82 |
P(4, "four"), |
| 83 |
P(1, "four"), |
| 84 |
P(2, "four"), |
| 85 |
}; |
| 86 |
assert(c.bucket_count() >= 5); |
| 87 |
assert(c.size() == 4); |
| 88 |
assert(c.at(1) == "one"); |
| 89 |
assert(c.at(2) == "two"); |
| 90 |
assert(c.at(3) == "three"); |
| 91 |
assert(c.at(4) == "four"); |
| 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(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
| 95 |
assert(c.max_load_factor() == 1); |
| 96 |
} |
| 97 |
|
| 98 |
return 0; |
| 99 |
} |