| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <spanstream> |
| 4 | #include <testsuite_hooks.h> |
| 5 | |
| 6 | using std::ispanstream; |
| 7 | using std::ospanstream; |
| 8 | using std::span; |
| 9 | |
| 10 | void |
| 11 | test_move() |
| 12 | { |
| 13 | char c; |
| 14 | { |
| 15 | const char str[] = "chars"; |
| 16 | std::ispanstream a(str); |
| 17 | std::ispanstream b = std::move(a); |
| 18 | VERIFY( b.span().data() == str && b.span().size() == 6 ); |
| 19 | VERIFY( b >> c ); |
| 20 | VERIFY( c == 'c' ); |
| 21 | |
| 22 | a = std::move(b); |
| 23 | VERIFY( a.span().data() == str && a.span().size() == 6 ); |
| 24 | VERIFY( a >> c >> c ); |
| 25 | VERIFY( c == 'a' ); |
| 26 | } |
| 27 | |
| 28 | { |
| 29 | char buf[10] = {}; |
| 30 | std::ospanstream a(buf); |
| 31 | std::ospanstream b = std::move(a); |
| 32 | VERIFY( b << 'c' ); |
| 33 | VERIFY( buf[0] == 'c' ); |
| 34 | VERIFY( !std::char_traits<char>::compare(buf, "c", 2) ); |
| 35 | |
| 36 | a = std::move(b); |
| 37 | VERIFY( a << 'h' << 'a' << "rs" ); |
| 38 | VERIFY( !std::char_traits<char>::compare(buf, "chars", 6) ); |
| 39 | } |
| 40 | |
| 41 | { |
| 42 | char buf[10] = {}; |
| 43 | std::spanstream a(buf); |
| 44 | std::spanstream b = std::move(a); |
| 45 | VERIFY( b.span().empty() ); |
| 46 | VERIFY( b << 'c' ); |
| 47 | VERIFY( buf[0] == 'c' ); |
| 48 | VERIFY( !std::char_traits<char>::compare(buf, "c", 2) ); |
| 49 | VERIFY( b.span().data() == buf && b.span().size() == 1 ); |
| 50 | VERIFY( b >> c ); |
| 51 | VERIFY( c == 'c' ); |
| 52 | |
| 53 | a = std::move(b); |
| 54 | VERIFY( a.span().data() == buf && a.span().size() == 1 ); |
| 55 | VERIFY( a << 'h' << 'a' << "rs" ); |
| 56 | VERIFY( !std::char_traits<char>::compare(buf, "chars", 6) ); |
| 57 | VERIFY( a.span().data() == buf && a.span().size() == 5 ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | test_swap() |
| 63 | { |
| 64 | { |
| 65 | const char str1[] = "chars"; |
| 66 | const char str2[] = "STRING"; |
| 67 | std::ispanstream a(str1); |
| 68 | std::ispanstream b(str2); |
| 69 | a.swap(b); |
| 70 | VERIFY( a.span().data() == str2 && a.span().size() == 7 ); |
| 71 | VERIFY( b.span().data() == str1 && b.span().size() == 6 ); |
| 72 | char c; |
| 73 | VERIFY( a >> c ); |
| 74 | VERIFY( c == 'S' ); |
| 75 | VERIFY( b >> c ); |
| 76 | VERIFY( c == 'c' ); |
| 77 | |
| 78 | swap(a, b); |
| 79 | VERIFY( a.span().data() == str1 && a.span().size() == 6 ); |
| 80 | VERIFY( b.span().data() == str2 && b.span().size() == 7 ); |
| 81 | VERIFY( a >> c >> c ); |
| 82 | VERIFY( c == 'a' ); |
| 83 | VERIFY( b >> c >> c ); |
| 84 | VERIFY( c == 'R' ); |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | char buf1[] = "xxxxxxxxxxxxxxx"; |
| 89 | char buf2[] = "xxxxxxxxxxxxxxx"; |
| 90 | std::ospanstream a(buf1); |
| 91 | std::ospanstream b(buf2); |
| 92 | a.swap(b); |
| 93 | VERIFY( a << "STR" ); |
| 94 | VERIFY( !std::char_traits<char>::compare(buf2, "STRx", 4) ); |
| 95 | VERIFY( b << 'c' << 'h' ); |
| 96 | VERIFY( !std::char_traits<char>::compare(buf1, "chx", 3) ); |
| 97 | |
| 98 | swap(a, b); |
| 99 | VERIFY( a.span().size() == 2 ); |
| 100 | VERIFY( b.span().size() == 3 ); |
| 101 | VERIFY( a << 'a' << "rs" ); |
| 102 | VERIFY( !std::char_traits<char>::compare(buf1, "charsx", 6) ); |
| 103 | VERIFY( b << "IN" << 'G' ); |
| 104 | VERIFY( !std::char_traits<char>::compare(buf2, "STRINGx", 7) ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | int main() |
| 109 | { |
| 110 | test_move(); |
| 111 | test_swap(); |
| 112 | } |