Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.modifiers / string_append / append_range.pass.cpp
blob68327f65bdb381a4f547fd99c010c73427577fa4
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 // template<container-compatible-range<charT> R>
12 // constexpr basic_string& append_range(R&& rg); // C++23
14 #include <string>
16 #include "../../../../containers/sequences/insert_range_sequence_containers.h"
17 #include "test_macros.h"
19 // Tested cases:
20 // - different kinds of insertions (appending an {empty/one-element/mid-sized/long range} into an
21 // {empty/one-element/full} container);
22 // - an exception is thrown when allocating new elements.
24 constexpr bool test_constexpr() {
25 for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() {
26 test_sequence_append_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
27 LIBCPP_ASSERT(c.__invariants());
28 });
29 });
31 return true;
34 int main(int, char**) {
35 static_assert(test_constraints_append_range<std::basic_string, char, int>());
37 for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() {
38 test_sequence_append_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>([](auto&& c) {
39 LIBCPP_ASSERT(c.__invariants());
40 });
41 });
42 static_assert(test_constexpr());
44 { // Check that `append_range` returns a reference to the string.
45 std::string c;
46 static_assert(std::is_lvalue_reference_v<decltype(c.append_range(FullContainer_Begin_EmptyRange<char>.input))>);
47 assert(&c.append_range(FullContainer_Begin_EmptyRange<char>.input) == &c);
48 assert(&c.append_range(FullContainer_Begin_OneElementRange<char>.input) == &c);
49 assert(&c.append_range(FullContainer_Begin_MidRange<char>.input) == &c);
50 assert(&c.append_range(FullContainer_Begin_LongRange<char>.input) == &c);
53 // Note: `test_append_range_exception_safety_throwing_copy` doesn't apply because copying chars cannot throw.
55 #if !defined(TEST_HAS_NO_EXCEPTIONS)
56 // Note: the input string must be long enough to prevent SSO, otherwise the allocator won't be used.
57 std::string in(64, 'a');
59 try {
60 ThrowingAllocator<char> alloc;
62 globalMemCounter.reset();
63 std::basic_string<char, std::char_traits<char>, ThrowingAllocator<char>> c(alloc);
64 c.append_range(in);
65 assert(false); // The function call above should throw.
67 } catch (int) {
68 assert(globalMemCounter.new_called == globalMemCounter.delete_called);
70 #endif
73 return 0;