| 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 | |
| 11 | // <sstream> |
| 12 | |
| 13 | // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 14 | // class basic_stringbuf |
| 15 | |
| 16 | // basic_string<charT, traits, Allocator> str() &&; |
| 17 | |
| 18 | #include <sstream> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #include "make_string.h" |
| 22 | #include "test_macros.h" |
| 23 | |
| 24 | #define STR(S) MAKE_STRING(CharT, S) |
| 25 | |
| 26 | template <class CharT> |
| 27 | static void test() { |
| 28 | { |
| 29 | std::basic_stringbuf<CharT> buf(STR("testing" )); |
| 30 | std::basic_string<CharT> s = std::move(buf).str(); |
| 31 | assert(s == STR("testing" )); |
| 32 | assert(buf.view().empty()); |
| 33 | } |
| 34 | { |
| 35 | std::basic_stringbuf<CharT> buf; |
| 36 | std::basic_string<CharT> s = std::move(buf).str(); |
| 37 | assert(s.empty()); |
| 38 | assert(buf.view().empty()); |
| 39 | } |
| 40 | { |
| 41 | std::basic_stringbuf<CharT> buf(STR("a very long string that exceeds the small string optimization buffer length" )); |
| 42 | const CharT* p = buf.view().data(); |
| 43 | std::basic_string<CharT> s = std::move(buf).str(); |
| 44 | assert(s.data() == p); // the allocation was pilfered |
| 45 | assert(buf.view().empty()); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | struct StringBuf : std::stringbuf { |
| 50 | using basic_stringbuf::basic_stringbuf; |
| 51 | void public_setg(int a, int b, int c) { |
| 52 | char* p = eback(); |
| 53 | this->setg(gbeg: p + a, gnext: p + b, gend: p + c); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | static void test_altered_sequence_pointers() { |
| 58 | { |
| 59 | auto src = StringBuf("hello world" , std::ios_base::in); |
| 60 | src.public_setg(a: 4, b: 6, c: 9); |
| 61 | std::stringbuf dest; |
| 62 | dest = std::move(src); |
| 63 | std::string view = std::string(dest.view()); |
| 64 | std::string str = std::move(dest).str(); |
| 65 | assert(view == str); |
| 66 | LIBCPP_ASSERT(str == "o wor" ); |
| 67 | assert(dest.str().empty()); |
| 68 | assert(dest.view().empty()); |
| 69 | } |
| 70 | { |
| 71 | auto src = StringBuf("hello world" , std::ios_base::in); |
| 72 | src.public_setg(a: 4, b: 6, c: 9); |
| 73 | std::stringbuf dest; |
| 74 | dest.swap(rhs&: src); |
| 75 | std::string view = std::string(dest.view()); |
| 76 | std::string str = std::move(dest).str(); |
| 77 | assert(view == str); |
| 78 | LIBCPP_ASSERT(str == "o wor" ); |
| 79 | assert(dest.str().empty()); |
| 80 | assert(dest.view().empty()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int main(int, char**) { |
| 85 | test<char>(); |
| 86 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 87 | test<wchar_t>(); |
| 88 | #endif |
| 89 | test_altered_sequence_pointers(); |
| 90 | return 0; |
| 91 | } |
| 92 | |