//===----------------------------------------------------------------------===// // // 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 // // template R> // void insert_range(R&& rg); #include #include #include #include #include #include #include "MinSequenceContainer.h" #include "../helpers.h" #include "MoveOnly.h" #include "test_macros.h" #include "test_iterators.h" #include "min_allocator.h" // test constraint container-compatible-range template concept CanInsertRange = requires(M m, R&& r) { m.insert_range(std::forward(r)); }; using Set = std::flat_multiset; static_assert(CanInsertRange>); static_assert(CanInsertRange>); static_assert(!CanInsertRange*>>); static_assert(!CanInsertRange*>>); template constexpr void test_one() { using Key = typename KeyContainer::value_type; { using M = std::flat_multiset, KeyContainer>; using It = forward_iterator; M m = {10, 10, 8, 5, 2, 1, 1}; int ar[] = {3, 1, 4, 1, 5, 9}; std::ranges::subrange r = {It(ar), It(ar + 6)}; static_assert(std::ranges::common_range); m.insert_range(r); assert((m == M{1, 1, 1, 1, 2, 3, 4, 5, 5, 8, 9, 10, 10})); } { using M = std::flat_multiset, KeyContainer>; using It = cpp20_input_iterator; M m = {10, 10, 8, 5, 2, 1, 1}; int ar[] = {3, 1, 4, 1, 5, 9}; std::ranges::subrange r = {It(ar), sentinel_wrapper(It(ar + 6))}; static_assert(!std::ranges::common_range); m.insert_range(r); assert((m == M{1, 1, 1, 1, 2, 3, 4, 5, 5, 8, 9, 10, 10})); } { // was empty using M = std::flat_multiset, KeyContainer>; M m; int ar[] = {3, 1, 4, 1, 5, 9}; m.insert_range(ar); assert((m == M{1, 1, 3, 4, 5, 9})); } } constexpr bool test() { test_one>(); #ifndef __cpp_lib_constexpr_deque if (!TEST_IS_CONSTANT_EVALUATED) #endif test_one>(); test_one>(); test_one>>(); { // Items are forwarded correctly from the input range. MoveOnly a[] = {3, 1, 4, 1, 5}; std::flat_multiset m; m.insert_range(a | std::views::as_rvalue); MoveOnly expected[] = {1, 1, 3, 4, 5}; assert(std::ranges::equal(m, expected)); } return true; } void test_exception() { auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); }; test_insert_range_exception_guarantee(insert_func); } int main(int, char**) { test(); #if TEST_STD_VER >= 26 static_assert(test()); #endif test_exception(); return 0; }