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