blob: 770bc994b4506ff5f205f88d2adf6ab9b70116a5 [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 fmt::operator""_cf;
19using std::literals::string_view_literals::operator""sv;
20
William A. Kennington III4f5711c2022-08-12 17:01:53 -070021TEST(FormatBuffer, Basic)
22{
23 auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
24 {
25 FormatBuffer buf(fd, 4096);
26 buf.append("hi\n");
William A. Kennington III74755ce2022-08-19 17:14:52 -070027 buf.append("hi\n"sv);
William A. Kennington III4f5711c2022-08-12 17:01:53 -070028 EXPECT_EQ(0, fd.lseek(0, Whence::Cur));
29 buf.flush();
30
William A. Kennington III74755ce2022-08-19 17:14:52 -070031 EXPECT_EQ(6, fd.lseek(0, Whence::Cur));
32 buf.append(FMT_COMPILE("{}"), std::string(2050, 'a'));
33 EXPECT_EQ(6, fd.lseek(0, Whence::Cur));
34 buf.append("{}"_cf, std::string(2050, 'a'));
35 EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070036
William A. Kennington III74755ce2022-08-19 17:14:52 -070037 buf.append(FMT_STRING("hi\n"));
38 EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070039 }
William A. Kennington III74755ce2022-08-19 17:14:52 -070040 EXPECT_EQ(4109, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070041}
42
William A. Kennington III4f5711c2022-08-12 17:01:53 -070043} // namespace fd
44} // namespace stdplus