xref: /llvm-project/libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.assign/move.pass.cpp (revision d6a92611ebb55bcca6231d2bdc5749eaecf349d6)
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& operator=(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 
31   // Checks the following requirement after being moved from:
32   //    The six pointers of std::basic_streambuf in *this are guaranteed to be different
33   //    from the corresponding pointers in the moved-from rhs unless null.
check_different_pointerstest_stringbuf34   void check_different_pointers(test_stringbuf<CharT> const& other) const {
35     assert(this->eback() == nullptr || this->eback() != other.eback());
36     assert(this->gptr() == nullptr || this->gptr() != other.gptr());
37     assert(this->egptr() == nullptr || this->egptr() != other.egptr());
38     assert(this->pbase() == nullptr || this->pbase() != other.pbase());
39     assert(this->pptr() == nullptr || this->pptr() != other.pptr());
40     assert(this->epptr() == nullptr || this->epptr() != other.epptr());
41   }
42 };
43 
44 template <class CharT>
test()45 void test() {
46   std::basic_string<CharT> strings[] = {STR(""), STR("short"), STR("loooooooooooooooooooong")};
47   for (std::basic_string<CharT> const& s : strings) {
48     {
49       test_stringbuf<CharT> buf1(s);
50       test_stringbuf<CharT> buf;
51       buf = std::move(buf1);
52       assert(buf.str() == s);
53       buf.check_different_pointers(buf1);
54     }
55     {
56       test_stringbuf<CharT> buf1(s, std::ios_base::in);
57       test_stringbuf<CharT> buf;
58       buf = std::move(buf1);
59       assert(buf.str() == s);
60       buf.check_different_pointers(buf1);
61     }
62     {
63       test_stringbuf<CharT> buf1(s, std::ios_base::out);
64       test_stringbuf<CharT> buf;
65       buf = std::move(buf1);
66       assert(buf.str() == s);
67       buf.check_different_pointers(buf1);
68     }
69     {
70       test_stringbuf<CharT> buf1;
71       test_stringbuf<CharT> buf;
72       buf = std::move(buf1);
73       buf.check_different_pointers(buf1);
74     }
75     // Use the assignment operator on an actual std::stringbuf, not test_stringbuf
76     {
77       std::basic_stringbuf<CharT> buf1(s);
78       std::basic_stringbuf<CharT> buf;
79       buf = std::move(buf1);
80       assert(buf.str() == s);
81     }
82   }
83 }
84 
main(int,char **)85 int main(int, char**) {
86   test<char>();
87 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
88   test<wchar_t>();
89 #endif
90   return 0;
91 }
92