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
10
11 // <sstream>
12
13 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
14 // class basic_stringbuf
15
16 // basic_stringbuf(basic_stringbuf&& rhs);
17
18 #include <sstream>
19 #include <cassert>
20 #include <utility>
21
22 #include "make_string.h"
23 #include "test_macros.h"
24
25 #define STR(S) MAKE_STRING(CharT, S)
26
27 template <class CharT>
28 struct test_stringbuf : std::basic_stringbuf<CharT> {
29 using std::basic_stringbuf<CharT>::basic_stringbuf;
30
test_stringbuftest_stringbuf31 test_stringbuf(std::basic_stringbuf<CharT>&& other) : std::basic_stringbuf<CharT>(std::move(other)) {}
32
33 // Checks the following requirement after being moved from:
34 // The six pointers of std::basic_streambuf in *this are guaranteed to be different
35 // from the corresponding pointers in the moved-from rhs unless null.
check_different_pointerstest_stringbuf36 void check_different_pointers(test_stringbuf<CharT> const& other) const {
37 assert(this->eback() == nullptr || this->eback() != other.eback());
38 assert(this->gptr() == nullptr || this->gptr() != other.gptr());
39 assert(this->egptr() == nullptr || this->egptr() != other.egptr());
40 assert(this->pbase() == nullptr || this->pbase() != other.pbase());
41 assert(this->pptr() == nullptr || this->pptr() != other.pptr());
42 assert(this->epptr() == nullptr || this->epptr() != other.epptr());
43 }
44 };
45
46 template <class CharT>
test()47 void test() {
48 std::basic_string<CharT> strings[] = {STR(""), STR("short"), STR("loooooooooooooooooooong")};
49 for (std::basic_string<CharT> const& s : strings) {
50 using StringBuf = std::basic_stringbuf<CharT>;
51 {
52 test_stringbuf<CharT> buf1(s);
53 test_stringbuf<CharT> buf(std::move(static_cast<StringBuf&>(buf1)));
54 assert(buf.str() == s);
55 assert(buf1.str().empty());
56 buf.check_different_pointers(buf1);
57 }
58 {
59 test_stringbuf<CharT> buf1(s, std::ios_base::in);
60 test_stringbuf<CharT> buf(std::move(static_cast<StringBuf&>(buf1)));
61 assert(buf.str() == s);
62 assert(buf1.str().empty());
63 buf.check_different_pointers(buf1);
64 }
65 {
66 test_stringbuf<CharT> buf1(s, std::ios_base::out);
67 test_stringbuf<CharT> buf(std::move(static_cast<StringBuf&>(buf1)));
68 assert(buf.str() == s);
69 assert(buf1.str().empty());
70 buf.check_different_pointers(buf1);
71 }
72 {
73 test_stringbuf<CharT> buf1;
74 test_stringbuf<CharT> buf(std::move(static_cast<StringBuf&>(buf1)));
75 assert(buf.str().empty());
76 assert(buf1.str().empty());
77 buf.check_different_pointers(buf1);
78 }
79 // Use the constructor from an actual std::stringbuf, not test_stringbuf
80 {
81 StringBuf buf1(s);
82 StringBuf buf(std::move(buf1));
83 assert(buf.str() == s);
84 assert(buf1.str().empty());
85 }
86 }
87 }
88
main(int,char **)89 int main(int, char**) {
90 test<char>();
91 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
92 test<wchar_t>();
93 #endif
94 return 0;
95 }
96