| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-additional-options "-pthread" { target pthread } } |
| 19 | // { dg-do run { target c++20 } } |
| 20 | // { dg-require-effective-target cxx11_abi } |
| 21 | |
| 22 | #include <sstream> |
| 23 | #include <string_view> |
| 24 | #include <syncstream> |
| 25 | |
| 26 | #include <testsuite_allocator.h> |
| 27 | #include <testsuite_hooks.h> |
| 28 | |
| 29 | void |
| 30 | test01() // construction |
| 31 | { |
| 32 | { |
| 33 | std::syncbuf s1; |
| 34 | VERIFY( s1.get_wrapped() == nullptr ); |
| 35 | |
| 36 | std::stringbuf b; |
| 37 | std::syncbuf s2(&b); |
| 38 | VERIFY( s2.get_wrapped() == &b ); |
| 39 | } |
| 40 | |
| 41 | { |
| 42 | using alloc_type = __gnu_test::uneq_allocator<char>; |
| 43 | using sbuf_t = std::basic_syncbuf<char, std::char_traits<char>, |
| 44 | alloc_type>; |
| 45 | |
| 46 | sbuf_t b; |
| 47 | |
| 48 | alloc_type aa; |
| 49 | sbuf_t s1(&b, aa); |
| 50 | VERIFY( aa == s1.get_allocator() ); |
| 51 | |
| 52 | alloc_type aaa(42); |
| 53 | sbuf_t s2(&b, aaa); |
| 54 | VERIFY( aaa == s2.get_allocator() ); |
| 55 | |
| 56 | VERIFY( s1.get_allocator() != s2.get_allocator() ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | test02() // moving |
| 62 | { |
| 63 | { |
| 64 | std::stringbuf b; |
| 65 | std::syncbuf s1(&b); |
| 66 | |
| 67 | std::syncbuf s2(std::move(s1)); |
| 68 | |
| 69 | VERIFY( s1.get_wrapped() == nullptr ); |
| 70 | VERIFY( s2.get_wrapped() == &b ); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | std::stringbuf b; |
| 75 | std::syncbuf s1(&b); |
| 76 | |
| 77 | std::syncbuf s2; |
| 78 | s2 = std::move(s1); |
| 79 | |
| 80 | VERIFY( s1.get_wrapped() == nullptr ); |
| 81 | VERIFY( s2.get_wrapped() == &b ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | test03() // swaping |
| 87 | { |
| 88 | std::stringbuf b; |
| 89 | std::syncbuf s1(&b); |
| 90 | |
| 91 | std::syncbuf s2; |
| 92 | std::swap(s1, s2); |
| 93 | |
| 94 | VERIFY( s1.get_wrapped() == nullptr ); |
| 95 | VERIFY( s2.get_wrapped() == &b ); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | test04() // emitting |
| 100 | { |
| 101 | { |
| 102 | std::stringbuf b; |
| 103 | std::syncbuf s(&b); |
| 104 | |
| 105 | const std::string_view txt("This is a test"); |
| 106 | s.sputn(txt.data(), txt.size()); |
| 107 | |
| 108 | VERIFY( b.str() != txt ); |
| 109 | VERIFY( s.pubsync() == 0 ); |
| 110 | VERIFY( b.str() != txt ); |
| 111 | |
| 112 | VERIFY( s.emit() ); |
| 113 | VERIFY( b.str() == txt ); |
| 114 | } |
| 115 | |
| 116 | { |
| 117 | std::stringbuf b; |
| 118 | std::syncbuf s(&b); |
| 119 | s.set_emit_on_sync(true); |
| 120 | |
| 121 | const std::string_view txt("This is a test"); |
| 122 | s.sputn(txt.data(), txt.size()); |
| 123 | |
| 124 | VERIFY( s.pubsync() == 0 ); |
| 125 | VERIFY( b.str() == txt ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | int main() |
| 130 | { |
| 131 | test01(); |
| 132 | test02(); |
| 133 | test03(); |
| 134 | test04(); |
| 135 | return 0; |
| 136 | } |