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 <sstream>
15 #include <string>
16 
17 #include "../../../../containers/sequences/insert_range_sequence_containers.h"
18 #include "test_macros.h"
19 
20 // Tested cases:
21 // - different kinds of insertions (inserting an {empty/one-element/mid-sized/long range} into an
22 //   {empty/one-element/full} container at the {beginning/middle/end});
23 // - an exception is thrown when allocating new elements.
24 
25 constexpr bool test_constexpr() {
26   for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() {
27     test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
28         []([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
29   });
30 
31   { // Ensure input-only sized ranges are accepted.
32     using input_iter = cpp20_input_iterator<const char*>;
33     const char in[]{'q', 'w', 'e', 'r'};
34     std::string s = "zxcv";
35     s.insert_range(s.begin(), std::views::counted(input_iter{std::ranges::begin(in)}, std::ranges::ssize(in)));
36     assert(s == "qwerzxcv");
37   }
38 
39   return true;
40 }
41 
42 #ifndef TEST_HAS_NO_LOCALIZATION
43 void test_counted_istream_view() {
44   std::istringstream is{"qwert"};
45   auto vals     = std::views::istream<char>(is);
46   std::string s = "zxcv";
47   s.insert_range(s.begin(), std::views::counted(vals.begin(), 3));
48   assert(s == "qwezxcv");
49 }
50 #endif
51 
52 int main(int, char**) {
53   static_assert(test_constraints_insert_range<std::basic_string, char, int>());
54 
55   for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() {
56     test_sequence_insert_range<std::basic_string<char, std::char_traits<char>, Alloc>, Iter, Sent>(
57         []([[maybe_unused]] auto&& c) { LIBCPP_ASSERT(c.__invariants()); });
58   });
59   static_assert(test_constexpr());
60 
61 #ifndef TEST_HAS_NO_LOCALIZATION
62   test_counted_istream_view();
63 #endif
64 
65   // Note: `test_insert_range_exception_safety_throwing_copy` doesn't apply because copying chars cannot throw.
66   {
67 #if !defined(TEST_HAS_NO_EXCEPTIONS)
68     // Note: the input string must be long enough to prevent SSO, otherwise the allocator won't be used.
69     std::string in(64, 'a');
70 
71     try {
72       ThrowingAllocator<char> alloc;
73 
74       globalMemCounter.reset();
75       std::basic_string<char, std::char_traits<char>, ThrowingAllocator<char>> c(alloc);
76       c.insert_range(c.end(), in);
77       assert(false); // The function call above should throw.
78 
79     } catch (int) {
80       assert(globalMemCounter.new_called == globalMemCounter.delete_called);
81     }
82 #endif
83   }
84 
85   return 0;
86 }
87