| 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 | #ifndef SUPPORT_FLAT_MULTISET_HELPERS_H |
| 10 | #define SUPPORT_FLAT_MULTISET_HELPERS_H |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <cassert> |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | #include <flat_set> |
| 17 | |
| 18 | #include "../flat_helpers.h" |
| 19 | #include "test_allocator.h" |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | template <class... Args> |
| 23 | void check_invariant(const std::flat_multiset<Args...>& m) { |
| 24 | assert(std::is_sorted(m.begin(), m.end(), m.key_comp())); |
| 25 | } |
| 26 | |
| 27 | template <class F> |
| 28 | void test_emplace_exception_guarantee([[maybe_unused]] F&& emplace_function) { |
| 29 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 30 | using C = TransparentComparator; |
| 31 | { |
| 32 | // Throw on emplace the key, and underlying has strong exception guarantee |
| 33 | using KeyContainer = std::vector<int, test_allocator<int>>; |
| 34 | using M = std::flat_multiset<int, C, KeyContainer>; |
| 35 | |
| 36 | LIBCPP_STATIC_ASSERT(std::__container_traits<KeyContainer>::__emplacement_has_strong_exception_safety_guarantee); |
| 37 | |
| 38 | test_allocator_statistics stats; |
| 39 | |
| 40 | KeyContainer a({1, 1, 2, 2, 3, 4}, test_allocator<int>{&stats}); |
| 41 | [[maybe_unused]] auto expected_keys = a; |
| 42 | M m(std::sorted_equivalent, std::move(a)); |
| 43 | |
| 44 | stats.throw_after = 1; |
| 45 | try { |
| 46 | emplace_function(m, 0); |
| 47 | assert(false); |
| 48 | } catch (const std::bad_alloc&) { |
| 49 | check_invariant(m); |
| 50 | // In libc++, the flat_multiset is unchanged |
| 51 | LIBCPP_ASSERT(m.size() == 6); |
| 52 | LIBCPP_ASSERT(std::ranges::equal(m, expected_keys)); |
| 53 | } |
| 54 | } |
| 55 | { |
| 56 | // Throw on emplace the key, and underlying has no strong exception guarantee |
| 57 | using KeyContainer = EmplaceUnsafeContainer<int>; |
| 58 | using M = std::flat_multiset<int, C, KeyContainer>; |
| 59 | |
| 60 | LIBCPP_STATIC_ASSERT(!std::__container_traits<KeyContainer>::__emplacement_has_strong_exception_safety_guarantee); |
| 61 | KeyContainer a = {1, 1, 2, 2, 3, 4}; |
| 62 | M m(std::sorted_equivalent, std::move(a)); |
| 63 | try { |
| 64 | emplace_function(m, 0); |
| 65 | assert(false); |
| 66 | } catch (int) { |
| 67 | check_invariant(m); |
| 68 | // In libc++, the flat_multiset is cleared |
| 69 | LIBCPP_ASSERT(m.size() == 0); |
| 70 | } |
| 71 | } |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | template <class F> |
| 76 | void test_insert_range_exception_guarantee([[maybe_unused]] F&& insert_function) { |
| 77 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 78 | using KeyContainer = EmplaceUnsafeContainer<int>; |
| 79 | using M = std::flat_multiset<int, std::ranges::less, KeyContainer>; |
| 80 | test_allocator_statistics stats; |
| 81 | KeyContainer a{1, 2, 3, 4}; |
| 82 | M m(std::sorted_equivalent, std::move(a)); |
| 83 | |
| 84 | std::vector<int> newValues = {0, 1, 5, 6, 7, 8}; |
| 85 | stats.throw_after = 1; |
| 86 | try { |
| 87 | insert_function(m, newValues); |
| 88 | assert(false); |
| 89 | } catch (int) { |
| 90 | check_invariant(m); |
| 91 | // In libc++, we clear if anything goes wrong when inserting a range |
| 92 | LIBCPP_ASSERT(m.size() == 0); |
| 93 | } |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | template <class F> |
| 98 | void test_erase_exception_guarantee([[maybe_unused]] F&& erase_function) { |
| 99 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 100 | { |
| 101 | // key erase throws |
| 102 | using KeyContainer = ThrowOnEraseContainer<int>; |
| 103 | using M = std::flat_multiset<int, TransparentComparator, KeyContainer>; |
| 104 | |
| 105 | KeyContainer a{1, 2, 3, 4}; |
| 106 | M m(std::sorted_equivalent, std::move(a)); |
| 107 | try { |
| 108 | erase_function(m, 3); |
| 109 | assert(false); |
| 110 | } catch (int) { |
| 111 | check_invariant(m); |
| 112 | // In libc++, we clear if anything goes wrong when erasing |
| 113 | LIBCPP_ASSERT(m.size() == 0); |
| 114 | } |
| 115 | } |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | #endif // SUPPORT_FLAT_MULTISET_HELPERS_H |
| 120 | |