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

source code of libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/replace.pass.cpp