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
10 // TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
11 // UNSUPPORTED: availability-pmr-missing
12 
13 // This test ensures that we properly propagate allocators from stringstream's
14 // inner string object to the new string returned from .str().
15 // `str() const&` is specified to preserve the allocator (not copy the string).
16 // `str() &&` isn't specified, but should preserve the allocator (move the string).
17 
18 #include <cassert>
19 #include <memory>
20 #include <memory_resource>
21 #include <sstream>
22 #include <string>
23 #include <string_view>
24 #include <type_traits>
25 
26 #include "make_string.h"
27 #include "test_allocator.h"
28 #include "test_macros.h"
29 
30 template <class CharT>
test_soccc_behavior()31 void test_soccc_behavior() {
32   using Alloc = SocccAllocator<CharT>;
33   using SS    = std::basic_stringstream<CharT, std::char_traits<CharT>, Alloc>;
34   using S     = std::basic_string<CharT, std::char_traits<CharT>, Alloc>;
35   {
36     SS ss = SS(std::ios_base::out, Alloc(10));
37 
38     // [stringbuf.members]/6 specifies that the allocator is copied,
39     // not select_on_container_copy_construction'ed.
40     //
41     S copied = ss.str();
42     assert(copied.get_allocator().count_ == 10);
43     assert(ss.rdbuf()->get_allocator().count_ == 10);
44     assert(copied.empty());
45 
46     // sanity-check that SOCCC does in fact work
47     assert(S(copied).get_allocator().count_ == 11);
48 
49     // [stringbuf.members]/10 doesn't specify the allocator to use,
50     // but copying the allocator as-if-by moving the string makes sense.
51     //
52     S moved = std::move(ss).str();
53     assert(moved.get_allocator().count_ == 10);
54     assert(ss.rdbuf()->get_allocator().count_ == 10);
55     assert(moved.empty());
56   }
57 }
58 
59 template <class CharT,
60           class Base = std::basic_stringbuf<CharT, std::char_traits<CharT>, std::pmr::polymorphic_allocator<CharT>>>
61 struct StringBuf : Base {
StringBufStringBuf62   explicit StringBuf(std::pmr::memory_resource* mr) : Base(std::ios_base::in, mr) {}
public_setgStringBuf63   void public_setg(int a, int b, int c) {
64     CharT* p = this->eback();
65     assert(this->view().data() == p);
66     this->setg(p + a, p + b, p + c);
67     assert(this->eback() == p + a);
68     assert(this->view().data() == p + a);
69   }
70 };
71 
72 template <class CharT>
test_allocation_is_pilfered()73 void test_allocation_is_pilfered() {
74   using SS = std::basic_stringstream<CharT, std::char_traits<CharT>, std::pmr::polymorphic_allocator<CharT>>;
75   using S  = std::pmr::basic_string<CharT>;
76   alignas(void*) char buf[80 * sizeof(CharT)];
77   const CharT* initial =
78       MAKE_CSTRING(CharT, "a very long string that exceeds the small string optimization buffer length");
79   {
80     std::pmr::set_default_resource(std::pmr::null_memory_resource());
81     auto mr1 = std::pmr::monotonic_buffer_resource(buf, sizeof(buf), std::pmr::null_memory_resource());
82     SS ss    = SS(S(initial, &mr1));
83     S s      = std::move(ss).str();
84     assert(s == initial);
85   }
86   {
87     // Try moving-out-of a stringbuf whose view() is not the entire string.
88     // This is libc++'s behavior; libstdc++ doesn't allow such stringbufs to be created.
89     //
90     std::pmr::set_default_resource(std::pmr::null_memory_resource());
91     auto mr1 = std::pmr::monotonic_buffer_resource(buf, sizeof(buf), std::pmr::null_memory_resource());
92     auto src = StringBuf<CharT>(&mr1);
93     src.str(S(initial, &mr1));
94     src.public_setg(2, 6, 40);
95     SS ss(std::ios_base::in, &mr1);
96     *ss.rdbuf() = std::move(src);
97     LIBCPP_ASSERT(ss.view() == std::basic_string_view<CharT>(initial).substr(2, 38));
98     S s = std::move(ss).str();
99     LIBCPP_ASSERT(s == std::basic_string_view<CharT>(initial).substr(2, 38));
100   }
101 }
102 
103 template <class CharT>
test_no_foreign_allocations()104 void test_no_foreign_allocations() {
105   using SS = std::basic_stringstream<CharT, std::char_traits<CharT>, std::pmr::polymorphic_allocator<CharT>>;
106   using S  = std::pmr::basic_string<CharT>;
107   const CharT* initial =
108       MAKE_CSTRING(CharT, "a very long string that exceeds the small string optimization buffer length");
109   {
110     std::pmr::set_default_resource(std::pmr::null_memory_resource());
111     auto mr1 = std::pmr::monotonic_buffer_resource(std::pmr::new_delete_resource());
112     auto ss  = SS(S(initial, &mr1));
113     assert(ss.rdbuf()->get_allocator().resource() == &mr1);
114 
115     // [stringbuf.members]/6 specifies that the result of `str() const &`
116     // does NOT use the default allocator; it uses the original allocator.
117     //
118     S copied = ss.str();
119     assert(copied.get_allocator().resource() == &mr1);
120     assert(ss.rdbuf()->get_allocator().resource() == &mr1);
121     assert(copied == initial);
122 
123     // [stringbuf.members]/10 doesn't specify the allocator to use,
124     // but copying the allocator as-if-by moving the string makes sense.
125     //
126     S moved = std::move(ss).str();
127     assert(moved.get_allocator().resource() == &mr1);
128     assert(ss.rdbuf()->get_allocator().resource() == &mr1);
129     assert(moved == initial);
130   }
131 }
132 
main(int,char **)133 int main(int, char**) {
134   test_soccc_behavior<char>();
135   test_allocation_is_pilfered<char>();
136   test_no_foreign_allocations<char>();
137 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
138   test_soccc_behavior<wchar_t>();
139   test_allocation_is_pilfered<wchar_t>();
140   test_no_foreign_allocations<wchar_t>();
141 #endif
142 
143   return 0;
144 }
145