//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 // // void replace(key_container_type&& key_cont, mapped_container_type&& mapped_cont); #include #include #include #include #include #include "MinSequenceContainer.h" #include "../helpers.h" #include "test_macros.h" #include "min_allocator.h" template concept CanReplace = requires(T t, Args&&... args) { t.replace(std::forward(args)...); }; using Map = std::flat_map; static_assert(CanReplace, std::vector>); static_assert(!CanReplace&, std::vector>); static_assert(!CanReplace, const std::vector&>); static_assert(!CanReplace&, const std::vector&>); template constexpr void test() { using Key = typename KeyContainer::value_type; using Value = typename ValueContainer::value_type; using M = std::flat_map, KeyContainer, ValueContainer>; M m = M({1, 2, 3}, {4, 5, 6}); KeyContainer new_keys = {7, 8}; ValueContainer new_values = {9, 10}; auto expected_keys = new_keys; auto expected_values = new_values; m.replace(std::move(new_keys), std::move(new_values)); assert(m.size() == 2); assert(std::ranges::equal(m.keys(), expected_keys)); assert(std::ranges::equal(m.values(), expected_values)); } constexpr bool test() { test, std::vector>(); #ifndef __cpp_lib_constexpr_deque if (!TEST_IS_CONSTANT_EVALUATED) #endif { test, std::vector>(); } test, MinSequenceContainer>(); test>, std::vector>>(); if (!TEST_IS_CONSTANT_EVALUATED) { #ifndef TEST_HAS_NO_EXCEPTIONS using KeyContainer = std::vector; using ValueContainer = ThrowOnMoveContainer; using M = std::flat_map; M m; m.emplace(1, 1); m.emplace(2, 2); try { KeyContainer new_keys{3, 4}; ValueContainer new_values{5, 6}; m.replace(std::move(new_keys), std::move(new_values)); assert(false); } catch (int) { check_invariant(m); // In libc++, we clear the map LIBCPP_ASSERT(m.size() == 0); } #endif } return true; } int main(int, char**) { test(); #if TEST_STD_VER >= 26 static_assert(test()); #endif return 0; }