| 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 | // void insert_range(R&& rg); |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <deque> |
| 18 | #include <flat_set> |
| 19 | #include <functional> |
| 20 | #include <ranges> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "MinSequenceContainer.h" |
| 24 | #include "../helpers.h" |
| 25 | #include "MoveOnly.h" |
| 26 | #include "test_macros.h" |
| 27 | #include "test_iterators.h" |
| 28 | #include "min_allocator.h" |
| 29 | |
| 30 | // test constraint container-compatible-range |
| 31 | template <class M, class R> |
| 32 | concept CanInsertRange = requires(M m, R&& r) { m.insert_range(std::forward<R>(r)); }; |
| 33 | |
| 34 | using Set = std::flat_multiset<int, double>; |
| 35 | |
| 36 | static_assert(CanInsertRange<Set, std::ranges::subrange<int*>>); |
| 37 | static_assert(CanInsertRange<Set, std::ranges::subrange<short*>>); |
| 38 | static_assert(!CanInsertRange<Set, std::ranges::subrange<std::pair<int, int>*>>); |
| 39 | static_assert(!CanInsertRange<Set, std::ranges::subrange<std::pair<short, short>*>>); |
| 40 | |
| 41 | template <class KeyContainer> |
| 42 | void test_one() { |
| 43 | using Key = typename KeyContainer::value_type; |
| 44 | |
| 45 | { |
| 46 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
| 47 | using It = forward_iterator<const int*>; |
| 48 | M m = {10, 10, 8, 5, 2, 1, 1}; |
| 49 | int ar[] = {3, 1, 4, 1, 5, 9}; |
| 50 | std::ranges::subrange r = {It(ar), It(ar + 6)}; |
| 51 | static_assert(std::ranges::common_range<decltype(r)>); |
| 52 | m.insert_range(r); |
| 53 | assert((m == M{1, 1, 1, 1, 2, 3, 4, 5, 5, 8, 9, 10, 10})); |
| 54 | } |
| 55 | { |
| 56 | using M = std::flat_multiset<Key, std::greater<>, KeyContainer>; |
| 57 | using It = cpp20_input_iterator<const int*>; |
| 58 | M m = {10, 10, 8, 5, 2, 1, 1}; |
| 59 | int ar[] = {3, 1, 4, 1, 5, 9}; |
| 60 | std::ranges::subrange r = {It(ar), sentinel_wrapper<It>(It(ar + 6))}; |
| 61 | static_assert(!std::ranges::common_range<decltype(r)>); |
| 62 | m.insert_range(r); |
| 63 | assert((m == M{1, 1, 1, 1, 2, 3, 4, 5, 5, 8, 9, 10, 10})); |
| 64 | } |
| 65 | { |
| 66 | // was empty |
| 67 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
| 68 | M m; |
| 69 | int ar[] = {3, 1, 4, 1, 5, 9}; |
| 70 | m.insert_range(ar); |
| 71 | assert((m == M{1, 1, 3, 4, 5, 9})); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void test() { |
| 76 | test_one<std::vector<int>>(); |
| 77 | test_one<std::deque<int>>(); |
| 78 | test_one<MinSequenceContainer<int>>(); |
| 79 | test_one<std::vector<int, min_allocator<int>>>(); |
| 80 | { |
| 81 | // Items are forwarded correctly from the input range. |
| 82 | MoveOnly a[] = {3, 1, 4, 1, 5}; |
| 83 | std::flat_multiset<MoveOnly> m; |
| 84 | m.insert_range(a | std::views::as_rvalue); |
| 85 | MoveOnly expected[] = {1, 1, 3, 4, 5}; |
| 86 | assert(std::ranges::equal(m, expected)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void test_exception() { |
| 91 | auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); }; |
| 92 | test_insert_range_exception_guarantee(insert_function&: insert_func); |
| 93 | } |
| 94 | |
| 95 | int main(int, char**) { |
| 96 | test(); |
| 97 | test_exception(); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |