| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <flat_set> |
| 4 | |
| 5 | #if __cpp_lib_flat_set != 202207L |
| 6 | # error "Feature-test macro __cpp_lib_flat_set has wrong value in <flat_set>" |
| 7 | #endif |
| 8 | |
| 9 | #include <deque> |
| 10 | #include <ranges> |
| 11 | #include <vector> |
| 12 | #include <testsuite_allocator.h> |
| 13 | #include <testsuite_hooks.h> |
| 14 | |
| 15 | template<template<typename> class KeyContainer> |
| 16 | void |
| 17 | test01() |
| 18 | { |
| 19 | std::flat_set<int, std::less<int>, KeyContainer<int>> m; |
| 20 | static_assert( std::ranges::random_access_range<decltype(m)> ); |
| 21 | |
| 22 | m.insert(1); |
| 23 | m.insert(2); |
| 24 | m.insert(3); |
| 25 | m.insert(1); |
| 26 | m.insert(2); |
| 27 | m.insert(3); |
| 28 | m.insert(0); |
| 29 | VERIFY( m.size() == 4 ); |
| 30 | VERIFY( std::ranges::equal(m, (int[]){0, 1, 2, 3}) ); |
| 31 | |
| 32 | for (int i = 0; i < 4; i++) |
| 33 | { |
| 34 | m.clear(); |
| 35 | m.insert(m.end(), (i + 0) % 4); |
| 36 | m.insert(m.end(), (i + 1) % 4); |
| 37 | m.insert(m.end(), (i + 2) % 4); |
| 38 | m.insert(m.end(), (i + 3) % 4); |
| 39 | m.insert(m.begin() + i, 1); |
| 40 | m.insert(m.begin() + i, 2); |
| 41 | m.insert(m.begin() + i, 3); |
| 42 | m.insert(m.begin() + i, 0); |
| 43 | VERIFY( std::ranges::equal(m, (int[]){0, 1, 2, 3}) ); |
| 44 | } |
| 45 | |
| 46 | m.clear(); |
| 47 | m = {10,10}; |
| 48 | VERIFY( m.size() == 1 ); |
| 49 | m.insert({11, 12, 11}); |
| 50 | VERIFY( m.size() == 3 ); |
| 51 | VERIFY( m.end()[-1] == 12 ); |
| 52 | |
| 53 | auto m_copy = m; |
| 54 | VERIFY( m_copy == m ); |
| 55 | m_copy.operator=({12, 11, 10}); |
| 56 | VERIFY( m_copy == m ); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | test02() |
| 61 | { |
| 62 | std::flat_set<int, std::greater<int>> m; |
| 63 | static_assert( std::ranges::random_access_range<decltype(m)> ); |
| 64 | |
| 65 | auto r = m.insert(1); |
| 66 | VERIFY( *r.first == 1 && r.second ); |
| 67 | r = m.insert(2); |
| 68 | VERIFY( *r.first == 2 && r.second ); |
| 69 | r = m.insert(3); |
| 70 | VERIFY( *r.first == 3 && r.second ); |
| 71 | r = m.insert(1); |
| 72 | VERIFY( *r.first == 1 && !r.second ); |
| 73 | r = m.insert(2); |
| 74 | VERIFY( *r.first == 2 && !r.second ); |
| 75 | r = m.insert(3); |
| 76 | VERIFY( *r.first == 3 && !r.second ); |
| 77 | m.insert(0); |
| 78 | VERIFY( m.size() == 4 ); |
| 79 | VERIFY( std::ranges::equal(m, (int[]){3, 2, 1, 0}) ); |
| 80 | |
| 81 | VERIFY( m.contains(3) && !m.contains(7) ); |
| 82 | VERIFY( m.count(3) == 1 ); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | test03() |
| 87 | { |
| 88 | std::flat_set<int> m; |
| 89 | m = {1, 3, 5}; |
| 90 | m.insert({7, 9}); |
| 91 | |
| 92 | auto it = m.find(0); |
| 93 | VERIFY( it == m.end() ); |
| 94 | it = m.find(9); |
| 95 | VERIFY( it == m.end()-1 ); |
| 96 | |
| 97 | const auto n = m; |
| 98 | VERIFY( m == m ); |
| 99 | VERIFY( m == n ); |
| 100 | |
| 101 | m.erase(m.begin()); |
| 102 | m.erase(5); |
| 103 | m.erase(m.end()-2, m.end()); |
| 104 | VERIFY( std::ranges::equal(m, (int[]){3}) ); |
| 105 | VERIFY( m != n ); |
| 106 | VERIFY( n < m ); |
| 107 | |
| 108 | m = n; |
| 109 | erase_if(m, [](const auto& k) { return k < 5 || k > 5; }); |
| 110 | VERIFY( std::ranges::equal(m, (int[]){5}) ); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | test04() |
| 115 | { |
| 116 | using vector = std::vector<int, __gnu_test::uneq_allocator<int>>; |
| 117 | vector v = {1, 2, 3}; |
| 118 | __gnu_test::uneq_allocator<int> alloc(42); |
| 119 | |
| 120 | using flat_set = std::flat_set<int, std::less<int>, vector>; |
| 121 | flat_set m1(alloc); |
| 122 | VERIFY( std::move(m1).extract().get_allocator().get_personality() == 42 ); |
| 123 | |
| 124 | flat_set m2(v, alloc); |
| 125 | VERIFY( std::move(m2).extract().get_allocator().get_personality() == 42 ); |
| 126 | |
| 127 | flat_set m3(std::sorted_unique_t{}, v, alloc); |
| 128 | VERIFY( std::move(m3).extract().get_allocator().get_personality() == 42 ); |
| 129 | |
| 130 | alloc = __gnu_test::uneq_allocator<int>(43); |
| 131 | flat_set m4(m3, alloc); |
| 132 | VERIFY( std::move(m4).extract().get_allocator().get_personality() == 43 ); |
| 133 | |
| 134 | alloc = __gnu_test::uneq_allocator<int>(44); |
| 135 | flat_set m5(std::move(m4), alloc); |
| 136 | VERIFY( std::move(m5).extract().get_allocator().get_personality() == 44 ); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | test05() |
| 141 | { |
| 142 | std::vector<int> v = {2, 3, 1, 5, 4}; |
| 143 | std::flat_set<int, std::less<>, std::deque<int>> m = {std::from_range, v}; |
| 144 | VERIFY( std::ranges::equal(m, (int[]){1, 2, 3, 4, 5}) ); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | test06() |
| 149 | { |
| 150 | // PR libstdc++/118156 - flat_foo::insert_range cannot handle non-common ranges |
| 151 | std::flat_set<int> s; |
| 152 | auto r = std::views::iota(1) | std::views::take(5); |
| 153 | static_assert(!std::ranges::common_range<decltype(r)>); |
| 154 | s.insert_range(r); |
| 155 | VERIFY( std::ranges::equal(s, (int[]){1, 2, 3, 4, 5}) ); |
| 156 | s.clear(); |
| 157 | s.insert_range(r | std::views::reverse); |
| 158 | VERIFY( std::ranges::equal(s, (int[]){1, 2, 3, 4, 5}) ); |
| 159 | } |
| 160 | |
| 161 | template<typename T> |
| 162 | struct NoInsertRange : std::vector<T> |
| 163 | { |
| 164 | using std::vector<T>::vector; |
| 165 | |
| 166 | template<typename It, typename R> |
| 167 | void insert_range(typename std::vector<T>::const_iterator, R&&) = delete; |
| 168 | }; |
| 169 | |
| 170 | struct NoCatIterator { |
| 171 | using difference_type = int; |
| 172 | using value_type = int; |
| 173 | |
| 174 | NoCatIterator() : v(0) {} |
| 175 | NoCatIterator(int x) : v(x) {} |
| 176 | |
| 177 | int operator*() const |
| 178 | { return v; } |
| 179 | |
| 180 | NoCatIterator& operator++() |
| 181 | { |
| 182 | ++v; |
| 183 | return *this; |
| 184 | } |
| 185 | |
| 186 | NoCatIterator operator++(int) |
| 187 | { |
| 188 | ++v; |
| 189 | return NoCatIterator(v-1); |
| 190 | } |
| 191 | |
| 192 | bool operator==(const NoCatIterator& rhs) const |
| 193 | { return v == rhs.v; } |
| 194 | |
| 195 | private: |
| 196 | int v; |
| 197 | }; |
| 198 | |
| 199 | template<> |
| 200 | struct std::iterator_traits<NoCatIterator> { |
| 201 | using difference_type = int; |
| 202 | using value_type = int; |
| 203 | using iterator_concept = std::input_iterator_tag; |
| 204 | // no iterator_category, happens also for common_iterator |
| 205 | }; |
| 206 | |
| 207 | void test07() |
| 208 | { |
| 209 | std::flat_set<int> s; |
| 210 | std::flat_set<int, std::less<int>, NoInsertRange<int>> s2; |
| 211 | |
| 212 | auto r = std::ranges::subrange<NoCatIterator>(1, 6); |
| 213 | s.insert_range(r); |
| 214 | VERIFY( std::ranges::equal(s, (int[]){1, 2, 3, 4, 5}) ); |
| 215 | s2.insert_range(r); |
| 216 | VERIFY( std::ranges::equal(s2, (int[]){1, 2, 3, 4, 5}) ); |
| 217 | |
| 218 | #ifdef __SIZEOF_INT128__ |
| 219 | // PR libstdc++/119415 - flat_foo::insert_range cannot handle common ranges |
| 220 | // on c++20 only iterators |
| 221 | auto r2 = std::views::iota(__int128(1), __int128(6)); |
| 222 | s.clear(); |
| 223 | s.insert_range(r2); |
| 224 | VERIFY( std::ranges::equal(s, (int[]){1, 2, 3, 4, 5}) ); |
| 225 | |
| 226 | s2.clear(); |
| 227 | s2.insert_range(r2); |
| 228 | VERIFY( std::ranges::equal(s2, (int[]){1, 2, 3, 4, 5}) ); |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | test08() |
| 234 | { |
| 235 | // PR libstdc++/119620 -- flat_set::emplace always constructs element on the stack |
| 236 | static int copy_counter; |
| 237 | struct A { |
| 238 | A() { } |
| 239 | A(const A&) { ++copy_counter; } |
| 240 | A& operator=(const A&) { ++copy_counter; return *this; } |
| 241 | auto operator<=>(const A&) const = default; |
| 242 | }; |
| 243 | std::flat_set<A> s; |
| 244 | A a; |
| 245 | s.emplace(a); |
| 246 | VERIFY( copy_counter == 1 ); |
| 247 | s.emplace(a); |
| 248 | VERIFY( copy_counter == 1 ); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | test09() |
| 253 | { |
| 254 | // PR libstdc++/119427 - std::erase_if(std::flat_foo) does not work |
| 255 | std::flat_set<int> s = {1,2,3,4,5}; |
| 256 | auto n = std::erase_if(s, [](int x) { return x % 2 != 0; }); |
| 257 | VERIFY( n == 3 ); |
| 258 | VERIFY( std::ranges::equal(s, (int[]){2,4}) ); |
| 259 | } |
| 260 | |
| 261 | int |
| 262 | main() |
| 263 | { |
| 264 | test01<std::vector>(); |
| 265 | test01<std::deque>(); |
| 266 | test02(); |
| 267 | test03(); |
| 268 | test04(); |
| 269 | test05(); |
| 270 | test06(); |
| 271 | test07(); |
| 272 | test08(); |
| 273 | test09(); |
| 274 | } |