aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-11-12 18:45:32 +0000
committerJonathan Wakely <jwakely@redhat.com>2021-11-13 11:45:31 +0000
commita30a2e43e4a357919ecfa916451966f8e32b5176 (patch)
treee7e8c7967c511027f3880259c7ab2b15ac901b9d /libstdc++-v3/testsuite
parentecdf414bd89e6ba251f6b3f494407139b4dbae0e (diff)
downloadgcc-a30a2e43e4a357919ecfa916451966f8e32b5176.zip
gcc-a30a2e43e4a357919ecfa916451966f8e32b5176.tar.gz
gcc-a30a2e43e4a357919ecfa916451966f8e32b5176.tar.bz2
libstdc++: Implement std::spanstream for C++23
This implements the <spanstream> header, as proposed for C++23 by P0448R4. libstdc++-v3/ChangeLog: * include/Makefile.am: Add spanstream header. * include/Makefile.in: Regenerate. * include/precompiled/stdc++.h: Add spanstream header. * include/std/version (__cpp_lib_spanstream): Define. * include/std/spanstream: New file. * testsuite/27_io/spanstream/1.cc: New test. * testsuite/27_io/spanstream/version.cc: New test.
Diffstat (limited to 'libstdc++-v3/testsuite')
-rw-r--r--libstdc++-v3/testsuite/27_io/spanstream/1.cc53
-rw-r--r--libstdc++-v3/testsuite/27_io/spanstream/version.cc10
2 files changed, 63 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/27_io/spanstream/1.cc b/libstdc++-v3/testsuite/27_io/spanstream/1.cc
new file mode 100644
index 0000000..b66ee60
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/spanstream/1.cc
@@ -0,0 +1,53 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do run { target c++23 } }
+
+#include <spanstream>
+
+#ifndef __cpp_lib_spanstream
+# error "Feature-test macro for spanstream missing in <spanstream>"
+#elif __cpp_lib_spanstream != 202106L
+# error "Feature-test macro for spanstream has wrong value in <spanstream>"
+#endif
+
+#include <testsuite_hooks.h>
+
+using std::ispanstream;
+using std::ospanstream;
+using std::span;
+
+void
+test_input()
+{
+ // reading input from a fixed pre-arranged character buffer
+ char input[] = "10 20 30";
+ ispanstream is{span<char>{input}};
+ int i;
+ is >> i;
+ VERIFY(10 == i);
+ is >> i;
+ VERIFY(20 == i);
+ is >> i;
+ VERIFY(30 == i);
+ is >>i;
+ VERIFY(!is);
+}
+
+void
+test_output()
+{
+ // writing to a fixed pre-arranged character buffer
+ char output[30]{}; // zero-initialize array
+ ospanstream os{span<char>{output}};
+ os << 10 << 20 << 30;
+ auto const sp = os.span();
+ VERIFY(6 == sp.size());
+ VERIFY("102030" == std::string(sp.data(), sp.size()));
+ VERIFY(static_cast<void*>(output) == sp.data()); // no copying of underlying data!
+ VERIFY("102030" == std::string(output)); // initialization guaranteed NUL termination
+}
+
+int main()
+{
+ test_input();
+ test_output();
+}
diff --git a/libstdc++-v3/testsuite/27_io/spanstream/version.cc b/libstdc++-v3/testsuite/27_io/spanstream/version.cc
new file mode 100644
index 0000000..6261755
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/spanstream/version.cc
@@ -0,0 +1,10 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <version>
+
+#ifndef __cpp_lib_spanstream
+# error "Feature-test macro for spanstream missing in <version>"
+#elif __cpp_lib_spanstream != 202106L
+# error "Feature-test macro for spanstream has wrong value in <version>"
+#endif