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

source code of libcxx/test/std/containers/container.adaptors/flat.set/helpers.h