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 
19 // Tested cases:
20 // - different kinds of assignments (assigning an {empty/one-element/mid-sized/long range} to an
21 //   {empty/one-element/full} container);
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_assign_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_assign_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_assign_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   { // Check that `assign_range` returns a reference to the string.
43     std::string c;
44     static_assert(std::is_lvalue_reference_v<decltype(c.assign_range(FullContainer_Begin_EmptyRange<char>.input))>);
45     assert(&c.assign_range(FullContainer_Begin_EmptyRange<char>.input) == &c);
46     assert(&c.assign_range(FullContainer_Begin_OneElementRange<char>.input) == &c);
47     assert(&c.assign_range(FullContainer_Begin_MidRange<char>.input) == &c);
48     assert(&c.assign_range(FullContainer_Begin_LongRange<char>.input) == &c);
49   }
50 
51   // Note: `test_assign_range_exception_safety_throwing_copy` doesn't apply because copying chars cannot throw.
52   {
53 #if !defined(TEST_HAS_NO_EXCEPTIONS)
54     // Note: the input string must be long enough to prevent SSO, otherwise the allocator won't be used.
55     std::string in(64, 'a');
56 
57     try {
58       ThrowingAllocator<char> alloc;
59 
60       globalMemCounter.reset();
61       std::basic_string<char, std::char_traits<char>, ThrowingAllocator<char>> c(alloc);
62       c.assign_range(in);
63       assert(false); // The function call above should throw.
64 
65     } catch (int) {
66       assert(globalMemCounter.new_called == globalMemCounter.delete_called);
67     }
68 #endif
69   }
70 
71   return 0;
72 }
73