| 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 | // flat_set(initializer_list<value_type> il, const key_compare& comp = key_compare()); |
| 14 | // template<class Alloc> |
| 15 | // flat_set(initializer_list<value_type> il, const Alloc& a); |
| 16 | // template<class Alloc> |
| 17 | // flat_set(initializer_list<value_type> il, const key_compare& comp, const Alloc& a); |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <deque> |
| 21 | #include <flat_set> |
| 22 | #include <functional> |
| 23 | #include <type_traits> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "test_macros.h" |
| 27 | #include "min_allocator.h" |
| 28 | #include "test_allocator.h" |
| 29 | |
| 30 | #include "../../../test_compare.h" |
| 31 | |
| 32 | struct DefaultCtableComp { |
| 33 | explicit DefaultCtableComp() { default_constructed_ = true; } |
| 34 | bool operator()(int, int) const { return false; } |
| 35 | bool default_constructed_ = false; |
| 36 | }; |
| 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_set<int, C, V1>; |
| 49 | using M2 = std::flat_set<int, C, V2>; |
| 50 | using IL = std::initializer_list<int>; |
| 51 | static_assert(std::is_constructible_v<M1, IL, const A1&>); |
| 52 | static_assert(std::is_constructible_v<M2, IL, const A2&>); |
| 53 | static_assert(!std::is_constructible_v<M1, IL, const A2&>); |
| 54 | static_assert(!std::is_constructible_v<M2, IL, const A1&>); |
| 55 | |
| 56 | static_assert(std::is_constructible_v<M1, IL, const C&, const A1&>); |
| 57 | static_assert(std::is_constructible_v<M2, IL, const C&, const A2&>); |
| 58 | static_assert(!std::is_constructible_v<M1, IL, const C&, const A2&>); |
| 59 | static_assert(!std::is_constructible_v<M2, IL, const C&, const A1&>); |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | // initializer_list<value_type> needs to match exactly |
| 64 | using M = std::flat_set<int>; |
| 65 | using C = typename M::key_compare; |
| 66 | static_assert(std::is_constructible_v<M, std::initializer_list<int>>); |
| 67 | static_assert(std::is_constructible_v<M, std::initializer_list<int>, C>); |
| 68 | static_assert(std::is_constructible_v<M, std::initializer_list<int>, C, std::allocator<int>>); |
| 69 | static_assert(std::is_constructible_v<M, std::initializer_list<int>, std::allocator<int>>); |
| 70 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>>); |
| 71 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C>); |
| 72 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C, std::allocator<int>>); |
| 73 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, std::allocator<int>>); |
| 74 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>>); |
| 75 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C>); |
| 76 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, C, std::allocator<int>>); |
| 77 | static_assert(!std::is_constructible_v<M, std::initializer_list<const int>, std::allocator<int>>); |
| 78 | } |
| 79 | |
| 80 | int expected[] = {1, 2, 3, 5}; |
| 81 | { |
| 82 | // flat_set(initializer_list<value_type>); |
| 83 | using M = std::flat_set<int>; |
| 84 | std::initializer_list<int> il = {5, 2, 2, 3, 1, 3}; |
| 85 | M m(il); |
| 86 | assert(std::equal(m.begin(), m.end(), expected, expected + 4)); |
| 87 | } |
| 88 | { |
| 89 | // flat_set(initializer_list<value_type>); |
| 90 | // explicit(false) |
| 91 | using M = std::flat_set<int>; |
| 92 | M m = {5, 2, 2, 3, 1, 3}; |
| 93 | assert(std::equal(m.begin(), m.end(), expected, expected + 4)); |
| 94 | } |
| 95 | { |
| 96 | // flat_set(initializer_list<value_type>); |
| 97 | using M = std::flat_set<int, std::greater<int>, std::deque<int, min_allocator<int>>>; |
| 98 | M m = {5, 2, 2, 3, 1, 3}; |
| 99 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 100 | } |
| 101 | { |
| 102 | using A = explicit_allocator<int>; |
| 103 | { |
| 104 | // flat_set(initializer_list<value_type>); |
| 105 | // different comparator |
| 106 | using M = std::flat_set<int, DefaultCtableComp, std::vector<int, A>>; |
| 107 | M m = {1, 2, 3}; |
| 108 | assert(m.size() == 1); |
| 109 | LIBCPP_ASSERT(*m.begin() == 1); |
| 110 | assert(m.key_comp().default_constructed_); |
| 111 | } |
| 112 | { |
| 113 | // flat_set(initializer_list<value_type>, const Allocator&); |
| 114 | using M = std::flat_set<int, std::greater<int>, std::deque<int, A>>; |
| 115 | A a; |
| 116 | M m({5, 2, 2, 3, 1, 3}, a); |
| 117 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 118 | } |
| 119 | } |
| 120 | { |
| 121 | // flat_set(initializer_list<value_type>, const key_compare&); |
| 122 | using C = test_less<int>; |
| 123 | using M = std::flat_set<int, C>; |
| 124 | auto m = M({5, 2, 2, 3, 1, 3}, C(10)); |
| 125 | assert(std::equal(m.begin(), m.end(), expected, expected + 4)); |
| 126 | assert(m.key_comp() == C(10)); |
| 127 | |
| 128 | // explicit(false) |
| 129 | M m2 = {{5, 2, 2, 1, 3, 3}, C(10)}; |
| 130 | assert(m2 == m); |
| 131 | assert(m2.key_comp() == C(10)); |
| 132 | } |
| 133 | { |
| 134 | // flat_set(initializer_list<value_type>, const key_compare&); |
| 135 | // Sorting uses the comparator that was passed in |
| 136 | using M = std::flat_set<int, std::function<bool(int, int)>, std::deque<int, min_allocator<int>>>; |
| 137 | auto m = M({5, 2, 2, 1, 3, 1}, std::greater<int>()); |
| 138 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 139 | assert(m.key_comp()(2, 1) == true); |
| 140 | } |
| 141 | { |
| 142 | // flat_set(initializer_list<value_type> il, const key_compare& comp, const Alloc& a); |
| 143 | using A = explicit_allocator<int>; |
| 144 | using M = std::flat_set<int, std::greater<int>, std::deque<int, A>>; |
| 145 | A a; |
| 146 | M m({5, 2, 2, 3, 1, 3}, {}, a); |
| 147 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | int main(int, char**) { |
| 152 | test(); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |