| 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, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <flat_set> |
| 12 | |
| 13 | // explicit flat_set(container_type key_cont, const key_compare& comp = key_compare()); |
| 14 | // template<class Allocator> |
| 15 | // flat_set(const container_type& key_cont, const Allocator& a); |
| 16 | // template<class Alloc> |
| 17 | // flat_set(const container_type& key_cont, const key_compare& comp, const Alloc& a); |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <deque> |
| 21 | #include <flat_set> |
| 22 | #include <functional> |
| 23 | #include <type_traits> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "min_allocator.h" |
| 27 | #include "MoveOnly.h" |
| 28 | #include "test_allocator.h" |
| 29 | #include "test_iterators.h" |
| 30 | #include "test_macros.h" |
| 31 | #include "../../../test_compare.h" |
| 32 | |
| 33 | template <class T> |
| 34 | void conversion_test(T); |
| 35 | |
| 36 | template <class T, class... Args> |
| 37 | concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test<T>({std::forward<Args>(args)...}); }; |
| 38 | |
| 39 | template <template <class...> class KeyContainer> |
| 40 | constexpr void test() { |
| 41 | { |
| 42 | // flat_set(container_type) |
| 43 | using M = std::flat_set<int, std::less<int>, KeyContainer<int>>; |
| 44 | KeyContainer<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3}; |
| 45 | auto m = M(ks); |
| 46 | assert(std::ranges::equal(m, std::vector<int>{1, 2, 3})); |
| 47 | |
| 48 | // explicit |
| 49 | static_assert(std::is_constructible_v<M, const KeyContainer<int>&>); |
| 50 | static_assert(!ImplicitlyConstructible<M, const KeyContainer<int>&>); |
| 51 | } |
| 52 | { |
| 53 | // flat_set(container_type) |
| 54 | // move-only |
| 55 | MoveOnly expected[] = {3, 2, 1}; |
| 56 | using Ks = KeyContainer<MoveOnly, min_allocator<MoveOnly>>; |
| 57 | using M = std::flat_set<MoveOnly, std::greater<MoveOnly>, Ks>; |
| 58 | Ks ks; |
| 59 | ks.push_back(1); |
| 60 | ks.push_back(3); |
| 61 | ks.push_back(2); |
| 62 | auto m = M(std::move(ks)); |
| 63 | assert(ks.empty()); // it was moved-from |
| 64 | assert(std::ranges::equal(m, expected)); |
| 65 | } |
| 66 | { |
| 67 | // flat_set(container_type) |
| 68 | // container's allocator is used |
| 69 | using A = test_allocator<int>; |
| 70 | using M = std::flat_set<int, std::less<int>, KeyContainer<int, A>>; |
| 71 | auto ks = KeyContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5)); |
| 72 | auto m = M(std::move(ks)); |
| 73 | assert(ks.empty()); // it was moved-from |
| 74 | assert((m == M{1, 2, 3})); |
| 75 | auto keys = std::move(m).extract(); |
| 76 | assert(keys.get_allocator() == A(5)); |
| 77 | } |
| 78 | { |
| 79 | // flat_set(container_type , key_compare) |
| 80 | using C = test_less<int>; |
| 81 | using M = std::flat_set<int, C, KeyContainer<int>>; |
| 82 | KeyContainer<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3}; |
| 83 | auto m = M(ks, C(4)); |
| 84 | assert(std::ranges::equal(m, std::vector<int>{1, 2, 3})); |
| 85 | assert(m.key_comp() == C(4)); |
| 86 | |
| 87 | // explicit |
| 88 | static_assert(std::is_constructible_v<M, const KeyContainer<int>&, const C&>); |
| 89 | static_assert(!ImplicitlyConstructible<M, const KeyContainer<int>&, const C&>); |
| 90 | } |
| 91 | { |
| 92 | // flat_set(container_type , const Allocator&) |
| 93 | using A = test_allocator<int>; |
| 94 | using M = std::flat_set<int, std::less<int>, KeyContainer<int, A>>; |
| 95 | auto ks = KeyContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5)); |
| 96 | auto m = M(ks, A(4)); // replaces the allocators |
| 97 | assert(!ks.empty()); // it was an lvalue above |
| 98 | assert((m == M{1, 2, 3})); |
| 99 | auto keys = M(m).extract(); |
| 100 | assert(keys.get_allocator() == A(4)); |
| 101 | |
| 102 | // explicit(false) |
| 103 | static_assert(ImplicitlyConstructible<M, const KeyContainer<int, A>&, const A&>); |
| 104 | M m2 = {ks, A(4)}; // implicit ctor |
| 105 | assert(!ks.empty()); // it was an lvalue above |
| 106 | assert(m2 == m); |
| 107 | auto keys2 = std::move(m).extract(); |
| 108 | assert(keys2.get_allocator() == A(4)); |
| 109 | } |
| 110 | { |
| 111 | // flat_set(container_type , key_compare, const Allocator&) |
| 112 | using C = test_less<int>; |
| 113 | using A = test_allocator<int>; |
| 114 | using M = std::flat_set<int, C, KeyContainer<int, A>>; |
| 115 | KeyContainer<int, A> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3}; |
| 116 | auto m = M(ks, C(4), A(5)); |
| 117 | assert(std::ranges::equal(m, std::vector<int, A>{1, 2, 3})); |
| 118 | assert(m.key_comp() == C(4)); |
| 119 | auto m_copy = m; |
| 120 | auto keys = std::move(m_copy).extract(); |
| 121 | assert(keys.get_allocator() == A(5)); |
| 122 | |
| 123 | // explicit(false) |
| 124 | static_assert(ImplicitlyConstructible<M, const KeyContainer<int, A>&, const A&>); |
| 125 | M m2 = {ks, C(4), A(5)}; |
| 126 | assert(m2 == m); |
| 127 | assert(m2.key_comp() == C(4)); |
| 128 | keys = std::move(m2).extract(); |
| 129 | assert(keys.get_allocator() == A(5)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | constexpr bool test() { |
| 134 | { |
| 135 | // The constructors in this subclause shall not participate in overload |
| 136 | // resolution unless uses_allocator_v<container_type, Alloc> is true. |
| 137 | |
| 138 | using C = test_less<int>; |
| 139 | using A1 = test_allocator<int>; |
| 140 | using A2 = other_allocator<int>; |
| 141 | using V1 = std::vector<int, A1>; |
| 142 | using V2 = std::vector<int, A2>; |
| 143 | using M1 = std::flat_set<int, C, V1>; |
| 144 | using M2 = std::flat_set<int, C, V2>; |
| 145 | static_assert(std::is_constructible_v<M1, const V1&, const A1&>); |
| 146 | static_assert(std::is_constructible_v<M2, const V2&, const A2&>); |
| 147 | static_assert(!std::is_constructible_v<M1, const V1&, const A2&>); |
| 148 | static_assert(!std::is_constructible_v<M2, const V2&, const A1&>); |
| 149 | |
| 150 | static_assert(std::is_constructible_v<M1, const V1&, const C&, const A1&>); |
| 151 | static_assert(std::is_constructible_v<M2, const V2&, const C&, const A2&>); |
| 152 | static_assert(!std::is_constructible_v<M1, const V1&, const C&, const A2&>); |
| 153 | static_assert(!std::is_constructible_v<M2, const V2&, const C&, const A1&>); |
| 154 | } |
| 155 | |
| 156 | test<std::vector>(); |
| 157 | |
| 158 | #ifndef __cpp_lib_constexpr_deque |
| 159 | if (!TEST_IS_CONSTANT_EVALUATED) |
| 160 | #endif |
| 161 | test<std::deque>(); |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | int main(int, char**) { |
| 167 | test(); |
| 168 | #if TEST_STD_VER >= 26 |
| 169 | static_assert(test()); |
| 170 | #endif |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |