fd/fmt: Add buffered formatting to fd
This makes it possible to trivially write formatted data to a file
descriptor with built-in buffering to reduce the number of syscalls.
Change-Id: Ib66c062b65e2a611f13be570c2ed5fe5eb208fb7
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/fd/fmt.cpp b/test/fd/fmt.cpp
new file mode 100644
index 0000000..726b5e5
--- /dev/null
+++ b/test/fd/fmt.cpp
@@ -0,0 +1,35 @@
+#include <gtest/gtest.h>
+
+#include <stdplus/fd/fmt.hpp>
+#include <stdplus/fd/managed.hpp>
+#include <stdplus/util/cexec.hpp>
+#include <sys/mman.h>
+
+namespace stdplus
+{
+namespace fd
+{
+
+TEST(FormatBuffer, Basic)
+{
+ auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
+ {
+ FormatBuffer buf(fd, 4096);
+ buf.append("hi\n");
+ EXPECT_EQ(0, fd.lseek(0, Whence::Cur));
+ buf.flush();
+
+ EXPECT_EQ(3, fd.lseek(0, Whence::Cur));
+ buf.append("{}", std::string(2050, 'a'));
+ EXPECT_EQ(3, fd.lseek(0, Whence::Cur));
+ buf.append("{}", std::string(2050, 'a'));
+ EXPECT_EQ(4103, fd.lseek(0, Whence::Cur));
+
+ buf.append("hi\n");
+ EXPECT_EQ(4103, fd.lseek(0, Whence::Cur));
+ }
+ EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
+}
+
+} // namespace fd
+} // namespace stdplus