| 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 | // void insert(InputIterator first, InputIterator last); |
| 15 | |
| 16 | #include <flat_set> |
| 17 | #include <algorithm> |
| 18 | #include <cassert> |
| 19 | #include <functional> |
| 20 | #include <deque> |
| 21 | |
| 22 | #include "MinSequenceContainer.h" |
| 23 | #include "../helpers.h" |
| 24 | #include "test_macros.h" |
| 25 | #include "test_iterators.h" |
| 26 | #include "min_allocator.h" |
| 27 | |
| 28 | // test constraint InputIterator |
| 29 | template <class M, class... Args> |
| 30 | concept CanInsert = requires(M m, Args&&... args) { m.insert(std::forward<Args>(args)...); }; |
| 31 | |
| 32 | using Set = std::flat_multiset<int>; |
| 33 | |
| 34 | static_assert(CanInsert<Set, int*, int*>); |
| 35 | static_assert(CanInsert<Set, cpp17_input_iterator<int*>, cpp17_input_iterator<int*>>); |
| 36 | static_assert(!CanInsert<Set, int, int>); |
| 37 | static_assert(!CanInsert<Set, cpp20_input_iterator<int*>, cpp20_input_iterator<int*>>); |
| 38 | |
| 39 | template <class KeyContainer> |
| 40 | void test_one() { |
| 41 | using M = std::flat_multiset<int, std::less<int>, KeyContainer>; |
| 42 | |
| 43 | int ar1[] = { |
| 44 | 2, |
| 45 | 2, |
| 46 | 2, |
| 47 | 1, |
| 48 | 1, |
| 49 | 1, |
| 50 | 3, |
| 51 | 3, |
| 52 | 3, |
| 53 | }; |
| 54 | int ar2[] = { |
| 55 | 4, |
| 56 | 4, |
| 57 | 4, |
| 58 | 1, |
| 59 | 1, |
| 60 | 1, |
| 61 | 0, |
| 62 | 0, |
| 63 | 0, |
| 64 | }; |
| 65 | |
| 66 | M m; |
| 67 | m.insert(cpp17_input_iterator<int*>(ar1), cpp17_input_iterator<int*>(ar1 + sizeof(ar1) / sizeof(ar1[0]))); |
| 68 | assert(m.size() == 9); |
| 69 | M expected{1, 1, 1, 2, 2, 2, 3, 3, 3}; |
| 70 | assert(m == expected); |
| 71 | |
| 72 | m.insert(cpp17_input_iterator<int*>(ar2), cpp17_input_iterator<int*>(ar2 + sizeof(ar2) / sizeof(ar2[0]))); |
| 73 | assert(m.size() == 18); |
| 74 | M expected2{0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4}; |
| 75 | assert(m == expected2); |
| 76 | } |
| 77 | |
| 78 | void test() { |
| 79 | test_one<std::vector<int>>(); |
| 80 | test_one<std::deque<int>>(); |
| 81 | test_one<MinSequenceContainer<int>>(); |
| 82 | test_one<std::vector<int, min_allocator<int>>>(); |
| 83 | { |
| 84 | std::flat_multiset<int, std::less<int>, SillyReserveVector<int>> m{1, 2}; |
| 85 | std::vector<int> v{3, 4}; |
| 86 | m.insert(v.begin(), v.end()); |
| 87 | assert(std::ranges::equal(m, std::vector<int>{1, 2, 3, 4})); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void test_exception() { |
| 92 | auto insert_func = [](auto& m, const auto& newValues) { m.insert(newValues.begin(), newValues.end()); }; |
| 93 | test_insert_range_exception_guarantee(insert_function&: insert_func); |
| 94 | } |
| 95 | |
| 96 | int main(int, char**) { |
| 97 | test(); |
| 98 | test_exception(); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |