| 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_set |
| 14 | |
| 15 | // unordered_set& operator=(const unordered_set& 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 "../../../test_compare.h" |
| 26 | #include "../../../test_hash.h" |
| 27 | #include "test_allocator.h" |
| 28 | #include "min_allocator.h" |
| 29 | |
| 30 | int main(int, char**) { |
| 31 | { |
| 32 | typedef test_allocator<int> A; |
| 33 | typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, A > C; |
| 34 | typedef int P; |
| 35 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 36 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 37 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 38 | c = c0; |
| 39 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 40 | assert(c.size() == 4); |
| 41 | assert(c.count(1) == 1); |
| 42 | assert(c.count(2) == 1); |
| 43 | assert(c.count(3) == 1); |
| 44 | assert(c.count(4) == 1); |
| 45 | assert(c.hash_function() == test_hash<int>(8)); |
| 46 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 47 | assert(c.get_allocator() == A(4)); |
| 48 | assert(!c.empty()); |
| 49 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 50 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 51 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 52 | assert(c.max_load_factor() == 1); |
| 53 | } |
| 54 | { |
| 55 | typedef std::unordered_set<int> C; |
| 56 | typedef int P; |
| 57 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 58 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 59 | C* p = &c; |
| 60 | c = *p; |
| 61 | assert(c.size() == 4); |
| 62 | assert(std::is_permutation(c.begin(), c.end(), a)); |
| 63 | } |
| 64 | { |
| 65 | typedef other_allocator<int> A; |
| 66 | typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, A > C; |
| 67 | typedef int P; |
| 68 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 69 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 70 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 71 | c = c0; |
| 72 | assert(c.bucket_count() >= 5); |
| 73 | assert(c.size() == 4); |
| 74 | assert(c.count(1) == 1); |
| 75 | assert(c.count(2) == 1); |
| 76 | assert(c.count(3) == 1); |
| 77 | assert(c.count(4) == 1); |
| 78 | assert(c.hash_function() == test_hash<int>(8)); |
| 79 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 80 | assert(c.get_allocator() == A(10)); |
| 81 | assert(!c.empty()); |
| 82 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 83 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 84 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 85 | assert(c.max_load_factor() == 1); |
| 86 | } |
| 87 | #if TEST_STD_VER >= 11 |
| 88 | { |
| 89 | typedef min_allocator<int> A; |
| 90 | typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, A > C; |
| 91 | typedef int P; |
| 92 | P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)}; |
| 93 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A()); |
| 94 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A()); |
| 95 | c = c0; |
| 96 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 97 | assert(c.size() == 4); |
| 98 | assert(c.count(1) == 1); |
| 99 | assert(c.count(2) == 1); |
| 100 | assert(c.count(3) == 1); |
| 101 | assert(c.count(4) == 1); |
| 102 | assert(c.hash_function() == test_hash<int>(8)); |
| 103 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 104 | assert(c.get_allocator() == A()); |
| 105 | assert(!c.empty()); |
| 106 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 107 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 108 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 109 | assert(c.max_load_factor() == 1); |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |