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 basic_string& assign_range(R&& rg); // C++23
13 
14 #include <string>
15 
16 #include "../../../../containers/sequences/insert_range_sequence_containers.h"
17 #include "test_macros.h"
18 #include "asan_testing.h"
19 
20 // Tested cases:
21 // - different kinds of assignments (assigning an {empty/one-element/mid-sized/long range} to an
22 //   {empty/one-element/full} container);
23 // - an exception is thrown when allocating new elements.
24 
test_constexpr()25 constexpr bool test_constexpr() {
26   for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() {
27     test_sequence_assign_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
28         []([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
29   });
30 
31   return true;
32 }
33 
main(int,char **)34 int main(int, char**) {
35   static_assert(test_constraints_assign_range<std::basic_string, char, int>());
36 
37   for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() {
38     test_sequence_assign_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
39         []([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
40   });
41   static_assert(test_constexpr());
42 
43   { // Check that `assign_range` returns a reference to the string.
44     std::string c;
45     static_assert(std::is_lvalue_reference_v<decltype(c.assign_range(FullContainer_Begin_EmptyRange<char>.input))>);
46     assert(&c.assign_range(FullContainer_Begin_EmptyRange<char>.input) == &c);
47     LIBCPP_ASSERT(is_string_asan_correct(c));
48     assert(&c.assign_range(FullContainer_Begin_OneElementRange<char>.input) == &c);
49     LIBCPP_ASSERT(is_string_asan_correct(c));
50     assert(&c.assign_range(FullContainer_Begin_MidRange<char>.input) == &c);
51     LIBCPP_ASSERT(is_string_asan_correct(c));
52     assert(&c.assign_range(FullContainer_Begin_LongRange<char>.input) == &c);
53     LIBCPP_ASSERT(is_string_asan_correct(c));
54   }
55 
56   // Note: `test_assign_range_exception_safety_throwing_copy` doesn't apply because copying chars cannot throw.
57   {
58 #if !defined(TEST_HAS_NO_EXCEPTIONS)
59     // Note: the input string must be long enough to prevent SSO, otherwise the allocator won't be used.
60     std::string in(64, 'a');
61 
62     try {
63       ThrowingAllocator<char> alloc;
64 
65       globalMemCounter.reset();
66       std::basic_string<char, std::char_traits<char>, ThrowingAllocator<char>> c(alloc);
67       c.assign_range(in);
68       assert(false); // The function call above should throw.
69 
70     } catch (int) {
71       assert(globalMemCounter.new_called == globalMemCounter.delete_called);
72     }
73 #endif
74   }
75 
76   return 0;
77 }
78