| 1 | // { dg-do run { target c++23 } } |
| 2 | // { dg-add-options no_pch } |
| 3 | |
| 4 | #include <spanstream> |
| 5 | |
| 6 | #ifndef __cpp_lib_spanstream |
| 7 | # error "Feature-test macro for spanstream missing in <spanstream>" |
| 8 | #elif __cpp_lib_spanstream != 202106L |
| 9 | # error "Feature-test macro for spanstream has wrong value in <spanstream>" |
| 10 | #endif |
| 11 | |
| 12 | #include <testsuite_hooks.h> |
| 13 | |
| 14 | using std::ispanstream; |
| 15 | using std::ospanstream; |
| 16 | using std::span; |
| 17 | |
| 18 | void |
| 19 | test_input() |
| 20 | { |
| 21 | // reading input from a fixed pre-arranged character buffer |
| 22 | char input[] = "10 20 30"; |
| 23 | ispanstream is{span<char>{input}}; |
| 24 | int i; |
| 25 | is >> i; |
| 26 | VERIFY(10 == i); |
| 27 | is >> i; |
| 28 | VERIFY(20 == i); |
| 29 | is >> i; |
| 30 | VERIFY(30 == i); |
| 31 | is >>i; |
| 32 | VERIFY(!is); |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | test_output() |
| 37 | { |
| 38 | // writing to a fixed pre-arranged character buffer |
| 39 | char output[30]{}; // zero-initialize array |
| 40 | ospanstream os{span<char>{output}}; |
| 41 | os << 10 << 20 << 30; |
| 42 | auto const sp = os.span(); |
| 43 | VERIFY(6 == sp.size()); |
| 44 | VERIFY("102030" == std::string(sp.data(), sp.size())); |
| 45 | VERIFY(static_cast<void*>(output) == sp.data()); // no copying of underlying data! |
| 46 | VERIFY("102030" == std::string(output)); // initialization guaranteed NUL termination |
| 47 | } |
| 48 | |
| 49 | int main() |
| 50 | { |
| 51 | test_input(); |
| 52 | test_output(); |
| 53 | } |