[CodeGen][NewPM] Port RegisterCoalescer to NPM (#124698)
[llvm-project.git] / libcxx / test / std / containers / container.adaptors / flat.map / flat.map.modifiers / replace.pass.cpp
blob5ca811d76152014f6ca938fbe757e27c51f31971
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // <flat_map>
13 // void replace(key_container_type&& key_cont, mapped_container_type&& mapped_cont);
15 #include <algorithm>
16 #include <deque>
17 #include <concepts>
18 #include <flat_map>
19 #include <functional>
21 #include "MinSequenceContainer.h"
22 #include "../helpers.h"
23 #include "test_macros.h"
24 #include "min_allocator.h"
26 template <class T, class... Args>
27 concept CanReplace = requires(T t, Args&&... args) { t.replace(std::forward<Args>(args)...); };
29 using Map = std::flat_map<int, int>;
30 static_assert(CanReplace<Map, std::vector<int>, std::vector<int>>);
31 static_assert(!CanReplace<Map, const std::vector<int>&, std::vector<int>>);
32 static_assert(!CanReplace<Map, std::vector<int>, const std::vector<int>&>);
33 static_assert(!CanReplace<Map, const std::vector<int>&, const std::vector<int>&>);
35 template <class KeyContainer, class ValueContainer>
36 void test() {
37 using Key = typename KeyContainer::value_type;
38 using Value = typename ValueContainer::value_type;
39 using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;
41 M m = M({1, 2, 3}, {4, 5, 6});
42 KeyContainer new_keys = {7, 8};
43 ValueContainer new_values = {9, 10};
44 auto expected_keys = new_keys;
45 auto expected_values = new_values;
46 m.replace(std::move(new_keys), std::move(new_values));
47 assert(m.size() == 2);
48 assert(std::ranges::equal(m.keys(), expected_keys));
49 assert(std::ranges::equal(m.values(), expected_values));
52 int main(int, char**) {
53 test<std::vector<int>, std::vector<double>>();
54 test<std::deque<int>, std::vector<double>>();
55 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();
56 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();
59 #ifndef TEST_HAS_NO_EXCEPTIONS
60 using KeyContainer = std::vector<int>;
61 using ValueContainer = ThrowOnMoveContainer<int>;
62 using M = std::flat_map<int, int, std::ranges::less, KeyContainer, ValueContainer>;
64 M m;
65 m.emplace(1, 1);
66 m.emplace(2, 2);
67 try {
68 KeyContainer new_keys{3, 4};
69 ValueContainer new_values{5, 6};
70 m.replace(std::move(new_keys), std::move(new_values));
71 assert(false);
72 } catch (int) {
73 check_invariant(m);
74 // In libc++, we clear the map
75 LIBCPP_ASSERT(m.size() == 0);
77 #endif
79 return 0;