//===----------------------------------------------------------------------===// // // 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 // // class flat_map // pair insert( value_type&& v); #include #include #include #include "MinSequenceContainer.h" #include "MoveOnly.h" #include "min_allocator.h" #include "test_macros.h" #include "../helpers.h" template constexpr void do_insert_rv_test() { using M = Container; using P = Pair; using R = std::pair; M m; std::same_as decltype(auto) r = m.insert(P(2, 2)); assert(r.second); assert(r.first == m.begin()); assert(m.size() == 1); assert(r.first->first == 2); assert(r.first->second == 2); r = m.insert(P(1, 1)); assert(r.second); assert(r.first == m.begin()); assert(m.size() == 2); assert(r.first->first == 1); assert(r.first->second == 1); r = m.insert(P(3, 3)); assert(r.second); assert(r.first == std::ranges::prev(m.end())); assert(m.size() == 3); assert(r.first->first == 3); assert(r.first->second == 3); r = m.insert(P(3, 3)); assert(!r.second); assert(r.first == std::ranges::prev(m.end())); assert(m.size() == 3); assert(r.first->first == 3); assert(r.first->second == 3); } template constexpr void test() { using Key = typename KeyContainer::value_type; using Value = typename ValueContainer::value_type; using M = std::flat_map; using P = std::pair; using CP = std::pair; do_insert_rv_test(); do_insert_rv_test(); } 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>>(); { using M = std::flat_map; using R = std::pair; M m; R r = m.insert({2, MoveOnly(2)}); assert(r.second); assert(r.first == m.begin()); assert(m.size() == 1); assert(r.first->first == 2); assert(r.first->second == 2); r = m.insert({1, MoveOnly(1)}); assert(r.second); assert(r.first == m.begin()); assert(m.size() == 2); assert(r.first->first == 1); assert(r.first->second == 1); r = m.insert({3, MoveOnly(3)}); assert(r.second); assert(r.first == std::ranges::prev(m.end())); assert(m.size() == 3); assert(r.first->first == 3); assert(r.first->second == 3); r = m.insert({3, MoveOnly(3)}); assert(!r.second); assert(r.first == std::ranges::prev(m.end())); assert(m.size() == 3); assert(r.first->first == 3); assert(r.first->second == 3); } if (!TEST_IS_CONSTANT_EVALUATED) { auto insert_func = [](auto& m, auto key_arg, auto value_arg) { using FlatMap = std::decay_t; using value_type = typename FlatMap::value_type; value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); m.insert(std::move(p)); }; test_emplace_exception_guarantee(insert_func); } return true; } int main(int, char**) { test(); #if TEST_STD_VER >= 26 static_assert(test()); #endif return 0; }