blob: 9448fed9d6ded66df98dfde2850ce5e00500a80e [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);
25 buf.append("hi\n");
William A. Kennington III74755ce2022-08-19 17:14:52 -070026 buf.append("hi\n"sv);
William A. Kennington III4f5711c2022-08-12 17:01:53 -070027 EXPECT_EQ(0, fd.lseek(0, Whence::Cur));
28 buf.flush();
29
William A. Kennington III74755ce2022-08-19 17:14:52 -070030 EXPECT_EQ(6, fd.lseek(0, Whence::Cur));
31 buf.append(FMT_COMPILE("{}"), std::string(2050, 'a'));
32 EXPECT_EQ(6, fd.lseek(0, Whence::Cur));
William A. Kennington III1cad7d72023-06-06 13:50:01 -070033 buf.append(FMT_COMPILE("{}"), std::string(2050, 'a'));
William A. Kennington III74755ce2022-08-19 17:14:52 -070034 EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070035
William A. Kennington III74755ce2022-08-19 17:14:52 -070036 buf.append(FMT_STRING("hi\n"));
37 EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070038 }
William A. Kennington III74755ce2022-08-19 17:14:52 -070039 EXPECT_EQ(4109, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070040}
41
William A. Kennington III4f5711c2022-08-12 17:01:53 -070042} // namespace fd
43} // namespace stdplus