| 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 <class InputIterator> |
| 14 | // flat_set(InputIterator first, InputIterator last, const key_compare& comp = key_compare()); |
| 15 | // template<class InputIterator, class Allocator> |
| 16 | // flat_set(InputIterator first, InputIterator last, const Allocator& a); |
| 17 | // template<class InputIterator, class Allocator> |
| 18 | // flat_set(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a); |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <deque> |
| 22 | #include <flat_set> |
| 23 | #include <functional> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "min_allocator.h" |
| 27 | #include "test_allocator.h" |
| 28 | #include "test_iterators.h" |
| 29 | #include "test_macros.h" |
| 30 | #include "../../../test_compare.h" |
| 31 | |
| 32 | void test() { |
| 33 | { |
| 34 | // The constructors in this subclause shall not participate in overload |
| 35 | // resolution unless uses_allocator_v<container_type, Alloc> is true. |
| 36 | |
| 37 | using C = test_less<int>; |
| 38 | using A1 = test_allocator<int>; |
| 39 | using A2 = other_allocator<int>; |
| 40 | using V1 = std::vector<int, A1>; |
| 41 | using V2 = std::vector<int, A2>; |
| 42 | using M1 = std::flat_set<int, C, V1>; |
| 43 | using M2 = std::flat_set<int, C, V2>; |
| 44 | using Iter1 = typename M1::iterator; |
| 45 | using Iter2 = typename M2::iterator; |
| 46 | static_assert(std::is_constructible_v<M1, Iter1, Iter1, const A1&>); |
| 47 | static_assert(std::is_constructible_v<M2, Iter2, Iter2, const A2&>); |
| 48 | static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const A2&>); |
| 49 | static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const A1&>); |
| 50 | |
| 51 | static_assert(std::is_constructible_v<M1, Iter1, Iter1, const C&, const A1&>); |
| 52 | static_assert(std::is_constructible_v<M2, Iter2, Iter2, const C&, const A2&>); |
| 53 | static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const C&, const A2&>); |
| 54 | static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const C&, const A1&>); |
| 55 | } |
| 56 | |
| 57 | int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3}; |
| 58 | int expected[] = {1, 2, 3}; |
| 59 | { |
| 60 | // flat_set(InputIterator , InputIterator) |
| 61 | // cpp17_input_iterator |
| 62 | using M = std::flat_set<int>; |
| 63 | auto m = M(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9)); |
| 64 | assert(std::ranges::equal(m, expected)); |
| 65 | |
| 66 | // explicit(false) |
| 67 | M m2 = {cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9)}; |
| 68 | assert(m2 == m); |
| 69 | } |
| 70 | { |
| 71 | // flat_set(InputIterator , InputIterator) |
| 72 | // greater |
| 73 | using M = std::flat_set<int, std::greater<int>, std::deque<int, min_allocator<int>>>; |
| 74 | auto m = M(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9)); |
| 75 | assert(std::ranges::equal(m, std::deque<int, min_allocator<int>>{3, 2, 1})); |
| 76 | } |
| 77 | { |
| 78 | // flat_set(InputIterator , InputIterator) |
| 79 | // Test when the operands are of array type (also contiguous iterator type) |
| 80 | using M = std::flat_set<int, std::greater<int>, std::vector<int, min_allocator<int>>>; |
| 81 | auto m = M(ar, ar); |
| 82 | assert(m.empty()); |
| 83 | } |
| 84 | { |
| 85 | // flat_set(InputIterator , InputIterator, const key_compare&) |
| 86 | using C = test_less<int>; |
| 87 | using M = std::flat_set<int, C, std::vector<int>>; |
| 88 | auto m = M(ar, ar + 9, C(3)); |
| 89 | assert(std::ranges::equal(m, expected)); |
| 90 | assert(m.key_comp() == C(3)); |
| 91 | |
| 92 | // explicit(false) |
| 93 | M m2 = {ar, ar + 9, C(3)}; |
| 94 | assert(m2 == m); |
| 95 | assert(m2.key_comp() == C(3)); |
| 96 | } |
| 97 | { |
| 98 | // flat_set(InputIterator , InputIterator, const Allocator&) |
| 99 | using A1 = test_allocator<int>; |
| 100 | using M = std::flat_set<int, std::less<int>, std::vector<int, A1>>; |
| 101 | auto m = M(ar, ar + 9, A1(5)); |
| 102 | assert(std::ranges::equal(m, expected)); |
| 103 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 104 | } |
| 105 | { |
| 106 | // flat_set(InputIterator , InputIterator, const Allocator&) |
| 107 | // explicit(false) |
| 108 | using A1 = test_allocator<int>; |
| 109 | using M = std::flat_set<int, std::less<int>, std::vector<int, A1>>; |
| 110 | M m = {ar, ar + 9, A1(5)}; // implicit ctor |
| 111 | assert(std::ranges::equal(m, expected)); |
| 112 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 113 | } |
| 114 | { |
| 115 | // flat_set(InputIterator , InputIterator, const key_compare&, const Allocator&) |
| 116 | using C = test_less<int>; |
| 117 | using A1 = test_allocator<int>; |
| 118 | using M = std::flat_set<int, C, std::vector<int, A1>>; |
| 119 | auto m = M(ar, ar + 9, C(3), A1(5)); |
| 120 | assert(std::ranges::equal(m, expected)); |
| 121 | assert(m.key_comp() == C(3)); |
| 122 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 123 | } |
| 124 | { |
| 125 | // flat_set(InputIterator , InputIterator, const key_compare&, const Allocator&) |
| 126 | // explicit(false) |
| 127 | using A1 = test_allocator<int>; |
| 128 | using M = std::flat_set<int, std::less<int>, std::deque<int, A1>>; |
| 129 | M m = {ar, ar + 9, {}, A1(5)}; // implicit ctor |
| 130 | assert(std::ranges::equal(m, expected)); |
| 131 | LIBCPP_ASSERT(std::ranges::equal(m, expected)); |
| 132 | assert(std::move(m).extract().get_allocator() == A1(5)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | int main(int, char**) { |
| 137 | test(); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |