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 | // void replace(container_type&& key_cont); |
14 | |
15 | #include <algorithm> |
16 | #include <deque> |
17 | #include <concepts> |
18 | #include <flat_set> |
19 | #include <functional> |
20 | |
21 | #include "MinSequenceContainer.h" |
22 | #include "../helpers.h" |
23 | #include "test_macros.h" |
24 | #include "min_allocator.h" |
25 | |
26 | template <class T, class... Args> |
27 | concept CanReplace = requires(T t, Args&&... args) { t.replace(std::forward<Args>(args)...); }; |
28 | |
29 | using Set = std::flat_multiset<int, int>; |
30 | static_assert(CanReplace<Set, std::vector<int>>); |
31 | static_assert(!CanReplace<Set, const std::vector<int>&>); |
32 | |
33 | template <class KeyContainer> |
34 | void test_one() { |
35 | using Key = typename KeyContainer::value_type; |
36 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
37 | { |
38 | // was empty |
39 | M m; |
40 | KeyContainer new_keys = {7, 7, 8}; |
41 | auto expected_keys = new_keys; |
42 | m.replace(std::move(new_keys)); |
43 | assert(m.size() == 3); |
44 | assert(std::ranges::equal(m, expected_keys)); |
45 | } |
46 | { |
47 | M m = M({1, 1, 2, 2, 3}); |
48 | KeyContainer new_keys = {7, 7, 8, 8}; |
49 | auto expected_keys = new_keys; |
50 | m.replace(std::move(new_keys)); |
51 | assert(m.size() == 4); |
52 | assert(std::ranges::equal(m, expected_keys)); |
53 | } |
54 | } |
55 | |
56 | void test() { |
57 | test_one<std::vector<int>>(); |
58 | test_one<std::deque<int>>(); |
59 | test_one<MinSequenceContainer<int>>(); |
60 | test_one<std::vector<int, min_allocator<int>>>(); |
61 | } |
62 | |
63 | void test_exception() { |
64 | #ifndef TEST_HAS_NO_EXCEPTIONS |
65 | using KeyContainer = ThrowOnMoveContainer<int>; |
66 | using M = std::flat_multiset<int, std::ranges::less, KeyContainer>; |
67 | |
68 | M m; |
69 | m.emplace(1); |
70 | m.emplace(2); |
71 | try { |
72 | KeyContainer new_keys{3, 4}; |
73 | m.replace(std::move(new_keys)); |
74 | assert(false); |
75 | } catch (int) { |
76 | check_invariant(m); |
77 | // In libc++, we clear the set |
78 | LIBCPP_ASSERT(m.size() == 0); |
79 | } |
80 | #endif |
81 | } |
82 | |
83 | int main(int, char**) { |
84 | test(); |
85 | test_exception(); |
86 | |
87 | return 0; |
88 | } |
89 | |