| 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 | // <sstream> |
| 10 | |
| 11 | // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 12 | // class basic_stringbuf |
| 13 | |
| 14 | // void str(const basic_string<charT,traits,Allocator>& s); |
| 15 | |
| 16 | #include <sstream> |
| 17 | #include <string> |
| 18 | #include <cassert> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | struct StringBuf : std::stringbuf { |
| 23 | explicit StringBuf(const char* s, std::ios_base::openmode mode) : basic_stringbuf(s, mode) {} |
| 24 | void public_setg(int a, int b, int c) { |
| 25 | char* p = eback(); |
| 26 | this->setg(gbeg: p + a, gnext: p + b, gend: p + c); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | static void test_altered_sequence_pointers() { |
| 31 | { |
| 32 | StringBuf src("hello world" , std::ios_base::in); |
| 33 | src.public_setg(a: 4, b: 6, c: 9); |
| 34 | std::stringbuf dest; |
| 35 | dest = std::move(src); |
| 36 | std::string str = dest.str(); |
| 37 | assert(5 <= str.size() && str.size() <= 11); |
| 38 | LIBCPP_ASSERT(str == "o wor" ); |
| 39 | LIBCPP_ASSERT(dest.str() == "o wor" ); |
| 40 | } |
| 41 | { |
| 42 | StringBuf src("hello world" , std::ios_base::in); |
| 43 | src.public_setg(a: 4, b: 6, c: 9); |
| 44 | std::stringbuf dest; |
| 45 | dest.swap(rhs&: src); |
| 46 | std::string str = dest.str(); |
| 47 | assert(5 <= str.size() && str.size() <= 11); |
| 48 | LIBCPP_ASSERT(str == "o wor" ); |
| 49 | LIBCPP_ASSERT(dest.str() == "o wor" ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | int main(int, char**) |
| 54 | { |
| 55 | test_altered_sequence_pointers(); |
| 56 | { |
| 57 | std::stringbuf buf("testing" ); |
| 58 | assert(buf.str() == "testing" ); |
| 59 | buf.str(s: "another test" ); |
| 60 | assert(buf.str() == "another test" ); |
| 61 | } |
| 62 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 63 | { |
| 64 | std::wstringbuf buf(L"testing" ); |
| 65 | assert(buf.str() == L"testing" ); |
| 66 | buf.str(s: L"another test" ); |
| 67 | assert(buf.str() == L"another test" ); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |