| 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 |
// template<container-compatible-range<charT> R> |
| 12 |
// constexpr iterator insert_range(const_iterator p, R&& rg); // C++23 |
| 13 |
|
| 14 |
#include <string> |
| 15 |
|
| 16 |
#include "../../../../containers/sequences/insert_range_sequence_containers.h" |
| 17 |
#include "test_macros.h" |
| 18 |
|
| 19 |
// Tested cases: |
| 20 |
// - different kinds of insertions (inserting an {empty/one-element/mid-sized/long range} into an |
| 21 |
// {empty/one-element/full} container at the {beginning/middle/end}); |
| 22 |
// - an exception is thrown when allocating new elements. |
| 23 |
|
| 24 |
constexpr bool test_constexpr() { |
| 25 |
for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() { |
| 26 |
test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>( |
| 27 |
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); }); |
| 28 |
}); |
| 29 |
|
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
int main(int, char**) { |
| 34 |
static_assert(test_constraints_insert_range<std::basic_string, char, int>()); |
| 35 |
|
| 36 |
for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() { |
| 37 |
test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>( |
| 38 |
[]([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); }); |
| 39 |
}); |
| 40 |
static_assert(test_constexpr()); |
| 41 |
|
| 42 |
// Note: `test_insert_range_exception_safety_throwing_copy` doesn't apply because copying chars cannot throw. |
| 43 |
{ |
| 44 |
#if !defined(TEST_HAS_NO_EXCEPTIONS) |
| 45 |
// Note: the input string must be long enough to prevent SSO, otherwise the allocator won't be used. |
| 46 |
std::string in(64, 'a'); |
| 47 |
|
| 48 |
try { |
| 49 |
ThrowingAllocator<char> alloc; |
| 50 |
|
| 51 |
globalMemCounter.reset(); |
| 52 |
std::basic_string<char, std::char_traits<char>, ThrowingAllocator<char>> c(alloc); |
| 53 |
c.insert_range(c.end(), in); |
| 54 |
assert(false); // The function call above should throw. |
| 55 |
|
| 56 |
} catch (int) { |
| 57 |
assert(globalMemCounter.new_called == globalMemCounter.delete_called); |
| 58 |
} |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |