test/fd: Split out some utility functions
Change-Id: Ic87a40a82111676988344702db60ca96ef74a0de
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/fd/util.hpp b/test/fd/util.hpp
new file mode 100644
index 0000000..ff34d00
--- /dev/null
+++ b/test/fd/util.hpp
@@ -0,0 +1,30 @@
+#include <sys/mman.h>
+
+#include <stdplus/fd/managed.hpp>
+#include <stdplus/fd/ops.hpp>
+#include <stdplus/util/cexec.hpp>
+
+#include <algorithm>
+#include <string_view>
+
+namespace stdplus::fd
+{
+
+inline ManagedFd makeMemfd(std::string_view contents)
+{
+ auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
+ write(fd, contents);
+ lseek(fd, 0, Whence::Set);
+ return fd;
+}
+
+constexpr auto readSv(std::string_view s) noexcept
+{
+ return [s](std::span<std::byte> buf) {
+ auto end = std::copy_n(reinterpret_cast<const std::byte*>(s.data()),
+ std::min(buf.size(), s.size()), buf.begin());
+ return std::span(buf.begin(), end);
+ };
+}
+
+} // namespace stdplus::fd