| 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 | // <unordered_set> |
| 10 | |
| 11 | // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>, |
| 12 | // class Alloc = allocator<Value>> |
| 13 | // class unordered_multiset |
| 14 | |
| 15 | // unordered_multiset& operator=(const unordered_multiset& u); |
| 16 | |
| 17 | #include <unordered_set> |
| 18 | #include <algorithm> |
| 19 | #include <cassert> |
| 20 | #include <cfloat> |
| 21 | #include <cmath> |
| 22 | #include <cstddef> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | #include "../../../check_consecutive.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(int, char**) { |
| 32 | { |
| 33 | typedef test_allocator<int> A; |
| 34 | typedef std::unordered_multiset<int, test_hash<int>, test_equal_to<int>, A > C; |
| 35 | typedef int P; |
| 36 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 37 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 38 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 39 | c = c0; |
| 40 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 41 | assert(c.size() == 6); |
| 42 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 1), end: c.end(), value: 1, count: 2); |
| 43 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 2), end: c.end(), value: 2, count: 2); |
| 44 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 3), end: c.end(), value: 3, count: 1); |
| 45 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 4), end: c.end(), value: 4, count: 1); |
| 46 | assert(c.hash_function() == test_hash<int>(8)); |
| 47 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 48 | assert(c.get_allocator() == A(4)); |
| 49 | assert(!c.empty()); |
| 50 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 51 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 52 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 53 | assert(c.max_load_factor() == 1); |
| 54 | } |
| 55 | { |
| 56 | typedef std::unordered_multiset<int> C; |
| 57 | typedef int P; |
| 58 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 59 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 60 | C* p = &c; |
| 61 | c = *p; |
| 62 | |
| 63 | assert(c.size() == 6); |
| 64 | assert(std::is_permutation(c.begin(), c.end(), a)); |
| 65 | } |
| 66 | { |
| 67 | typedef other_allocator<int> A; |
| 68 | typedef std::unordered_multiset<int, test_hash<int>, test_equal_to<int>, A > C; |
| 69 | typedef int P; |
| 70 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 71 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 72 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 73 | c = c0; |
| 74 | assert(c.bucket_count() >= 7); |
| 75 | assert(c.size() == 6); |
| 76 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 1), end: c.end(), value: 1, count: 2); |
| 77 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 2), end: c.end(), value: 2, count: 2); |
| 78 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 3), end: c.end(), value: 3, count: 1); |
| 79 | CheckConsecutiveValues<C::const_iterator>(pos: c.find(x: 4), end: c.end(), value: 4, count: 1); |
| 80 | assert(c.hash_function() == test_hash<int>(8)); |
| 81 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 82 | assert(c.get_allocator() == A(10)); |
| 83 | assert(!c.empty()); |
| 84 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 85 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 86 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 87 | assert(c.max_load_factor() == 1); |
| 88 | } |
| 89 | #if TEST_STD_VER >= 11 |
| 90 | { |
| 91 | typedef min_allocator<int> A; |
| 92 | typedef std::unordered_multiset<int, test_hash<int>, test_equal_to<int>, A > C; |
| 93 | typedef int P; |
| 94 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 95 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A()); |
| 96 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A()); |
| 97 | c = c0; |
| 98 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 99 | assert(c.size() == 6); |
| 100 | CheckConsecutiveValues<C::const_iterator>(c.find(1), c.end(), 1, 2); |
| 101 | CheckConsecutiveValues<C::const_iterator>(c.find(2), c.end(), 2, 2); |
| 102 | CheckConsecutiveValues<C::const_iterator>(c.find(3), c.end(), 3, 1); |
| 103 | CheckConsecutiveValues<C::const_iterator>(c.find(4), c.end(), 4, 1); |
| 104 | assert(c.hash_function() == test_hash<int>(8)); |
| 105 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 106 | assert(c.get_allocator() == A()); |
| 107 | assert(!c.empty()); |
| 108 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 109 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 110 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 111 | assert(c.max_load_factor() == 1); |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |