| 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 | // template<container-compatible-range<value_type> R> |
| 14 | // flat_set(from_range_t, R&&) |
| 15 | // template<container-compatible-range<value_type> R> |
| 16 | // flat_set(from_range_t, R&&, const key_compare&) |
| 17 | // template<container-compatible-range<value_type> R, class Alloc> |
| 18 | // flat_set(from_range_t, R&&, const Alloc&); |
| 19 | // template<container-compatible-range<value_type> R, class Alloc> |
| 20 | // flat_set(from_range_t, R&&, const key_compare&, const Alloc&); |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <deque> |
| 24 | #include <flat_set> |
| 25 | #include <functional> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "min_allocator.h" |
| 30 | #include "test_allocator.h" |
| 31 | #include "test_iterators.h" |
| 32 | #include "test_macros.h" |
| 33 | #include "../../../test_compare.h" |
| 34 | |
| 35 | // test constraint container-compatible-range |
| 36 | |
| 37 | template <class V> |
| 38 | using RangeOf = std::ranges::subrange<V*>; |
| 39 | using Set = std::flat_set<int>; |
| 40 | |
| 41 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>>); |
| 42 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>>); |
| 43 | static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>>); |
| 44 | |
| 45 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>>); |
| 46 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::less<int>>); |
| 47 | static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>>); |
| 48 | |
| 49 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::allocator<int>>); |
| 50 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::allocator<int>>); |
| 51 | static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::allocator<int>>); |
| 52 | |
| 53 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>); |
| 54 | static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>); |
| 55 | static_assert( |
| 56 | !std:: |
| 57 | is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>, std::allocator<int>>); |
| 58 | |
| 59 | void test() { |
| 60 | { |
| 61 | // The constructors in this subclause shall not participate in overload |
| 62 | // resolution unless uses_allocator_v<container_type, Alloc> is true. |
| 63 | |
| 64 | using C = test_less<int>; |
| 65 | using A1 = test_allocator<int>; |
| 66 | using A2 = other_allocator<int>; |
| 67 | using V1 = std::vector<int, A1>; |
| 68 | using V2 = std::vector<int, A2>; |
| 69 | using M1 = std::flat_set<int, C, V1>; |
| 70 | using M2 = std::flat_set<int, C, V2>; |
| 71 | static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const A1&>); |
| 72 | static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const A2&>); |
| 73 | static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const A2&>); |
| 74 | static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const A1&>); |
| 75 | |
| 76 | static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A1&>); |
| 77 | static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A2&>); |
| 78 | static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A2&>); |
| 79 | static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A1&>); |
| 80 | } |
| 81 | |
| 82 | int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3}; |
| 83 | int expected[] = {1, 2, 3}; |
| 84 | { |
| 85 | // flat_set(from_range_t, R&&) |
| 86 | // input_range && !common |
| 87 | using M = std::flat_set<int>; |
| 88 | using Iter = cpp20_input_iterator<const int*>; |
| 89 | using Sent = sentinel_wrapper<Iter>; |
| 90 | using R = std::ranges::subrange<Iter, Sent>; |
| 91 | auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))); |
| 92 | assert(std::ranges::equal(m, expected)); |
| 93 | LIBCPP_ASSERT(std::ranges::equal(m, expected)); |
| 94 | |
| 95 | // explicit(false) |
| 96 | M m2 = {std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))}; |
| 97 | assert(m2 == m); |
| 98 | } |
| 99 | { |
| 100 | // flat_set(from_range_t, R&&) |
| 101 | // greater |
| 102 | using M = std::flat_set<int, std::greater<int>, std::deque<int, min_allocator<int>>>; |
| 103 | using Iter = cpp20_input_iterator<const int*>; |
| 104 | using Sent = sentinel_wrapper<Iter>; |
| 105 | using R = std::ranges::subrange<Iter, Sent>; |
| 106 | auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))); |
| 107 | assert(std::ranges::equal(m, std::deque<int, min_allocator<int>>{3, 2, 1})); |
| 108 | } |
| 109 | { |
| 110 | // flat_set(from_range_t, R&&) |
| 111 | // contiguous range |
| 112 | using M = std::flat_set<int>; |
| 113 | using R = std::ranges::subrange<const int*>; |
| 114 | auto m = M(std::from_range, R(ar, ar + 9)); |
| 115 | assert(std::ranges::equal(m, expected)); |
| 116 | } |
| 117 | { |
| 118 | // flat_set(from_range_t, R&&, const key_compare&) |
| 119 | using C = test_less<int>; |
| 120 | using M = std::flat_set<int, C, std::vector<int>>; |
| 121 | using R = std::ranges::subrange<const int*>; |
| 122 | auto m = M(std::from_range, R(ar, ar + 9), C(3)); |
| 123 | assert(std::ranges::equal(m, expected)); |
| 124 | assert(m.key_comp() == C(3)); |
| 125 | |
| 126 | // explicit(false) |
| 127 | M m2 = {std::from_range, R(ar, ar + 9), C(3)}; |
| 128 | assert(m2 == m); |
| 129 | assert(m2.key_comp() == C(3)); |
| 130 | } |
| 131 | { |
| 132 | // flat_set(from_range_t, R&&, const Allocator&) |
| 133 | using A1 = test_allocator<int>; |
| 134 | using M = std::flat_set<int, std::less<int>, std::vector<int, A1>>; |
| 135 | using R = std::ranges::subrange<const int*>; |
| 136 | auto m = M(std::from_range, R(ar, ar + 9), A1(5)); |
| 137 | assert(std::ranges::equal(m, expected)); |
| 138 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 139 | } |
| 140 | { |
| 141 | // flat_set(from_range_t, R&&, const Allocator&) |
| 142 | // explicit(false) |
| 143 | using A1 = test_allocator<int>; |
| 144 | using M = std::flat_set<int, std::less<int>, std::deque<int, A1>>; |
| 145 | using R = std::ranges::subrange<const int*>; |
| 146 | M m = {std::from_range, R(ar, ar + 9), A1(5)}; // implicit ctor |
| 147 | assert(std::ranges::equal(m, expected)); |
| 148 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 149 | } |
| 150 | { |
| 151 | // flat_set(from_range_t, R&&, const key_compare&, const Allocator&) |
| 152 | using C = test_less<int>; |
| 153 | using A1 = test_allocator<int>; |
| 154 | using M = std::flat_set<int, C, std::vector<int, A1>>; |
| 155 | using R = std::ranges::subrange<const int*>; |
| 156 | auto m = M(std::from_range, R(ar, ar + 9), C(3), A1(5)); |
| 157 | assert(std::ranges::equal(m, expected)); |
| 158 | assert(m.key_comp() == C(3)); |
| 159 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 160 | } |
| 161 | { |
| 162 | // flat_set(from_range_t, R&&, const key_compare&, const Allocator&) |
| 163 | // explicit(false) |
| 164 | using A1 = test_allocator<int>; |
| 165 | using M = std::flat_set<int, std::less<int>, std::deque<int, A1>>; |
| 166 | using R = std::ranges::subrange<const int*>; |
| 167 | M m = {std::from_range, R(ar, ar + 9), {}, A1(5)}; // implicit ctor |
| 168 | assert(std::ranges::equal(m, expected)); |
| 169 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | int main(int, char**) { |
| 174 | test(); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |