blob: bdac7a4cc30f5b816515f7f80b101cfda54d97f2 [file] [log] [blame]
William A. Kennington III74755ce2022-08-19 17:14:52 -07001#include <fmt/compile.h>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002#include <sys/mman.h>
3
William A. Kennington III4f5711c2022-08-12 17:01:53 -07004#include <stdplus/fd/fmt.hpp>
5#include <stdplus/fd/managed.hpp>
6#include <stdplus/util/cexec.hpp>
William A. Kennington III4f5711c2022-08-12 17:01:53 -07007
William A. Kennington III74755ce2022-08-19 17:14:52 -07008#include <string>
9#include <string_view>
10
Patrick Williamsd1984dd2023-05-10 16:12:44 -050011#include <gtest/gtest.h>
12
William A. Kennington III4f5711c2022-08-12 17:01:53 -070013namespace stdplus
14{
15namespace fd
16{
17
William A. Kennington III74755ce2022-08-19 17:14:52 -070018using std::literals::string_view_literals::operator""sv;
19
William A. Kennington III4f5711c2022-08-12 17:01:53 -070020TEST(FormatBuffer, Basic)
21{
22 auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
23 {
24 FormatBuffer buf(fd, 4096);
William A. Kennington III8bd92512023-07-24 18:09:55 -070025 buf.appends("hi\n", "hi\n"sv);
William A. Kennington III4f5711c2022-08-12 17:01:53 -070026 EXPECT_EQ(0, fd.lseek(0, Whence::Cur));
27 buf.flush();
28
William A. Kennington III74755ce2022-08-19 17:14:52 -070029 EXPECT_EQ(6, fd.lseek(0, Whence::Cur));
30 buf.append(FMT_COMPILE("{}"), std::string(2050, 'a'));
31 EXPECT_EQ(6, fd.lseek(0, Whence::Cur));
William A. Kennington III8bd92512023-07-24 18:09:55 -070032 buf.append("{}", std::string(2050, 'a'));
William A. Kennington III74755ce2022-08-19 17:14:52 -070033 EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070034
William A. Kennington III8bd92512023-07-24 18:09:55 -070035 buf.appends("hi\n");
William A. Kennington III74755ce2022-08-19 17:14:52 -070036 EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070037 }
William A. Kennington III74755ce2022-08-19 17:14:52 -070038 EXPECT_EQ(4109, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070039}
40
William A. Kennington III4f5711c2022-08-12 17:01:53 -070041} // namespace fd
42} // namespace stdplus