William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 1 | #include <fmt/compile.h> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 2 | #include <sys/mman.h> |
| 3 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 4 | #include <stdplus/fd/fmt.hpp> |
| 5 | #include <stdplus/fd/managed.hpp> |
| 6 | #include <stdplus/util/cexec.hpp> |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 7 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <string_view> |
| 10 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 13 | namespace stdplus |
| 14 | { |
| 15 | namespace fd |
| 16 | { |
| 17 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 18 | using std::literals::string_view_literals::operator""sv; |
| 19 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 20 | TEST(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 III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 26 | buf.append("hi\n"sv); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 27 | EXPECT_EQ(0, fd.lseek(0, Whence::Cur)); |
| 28 | buf.flush(); |
| 29 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 30 | 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 III | 1cad7d7 | 2023-06-06 13:50:01 -0700 | [diff] [blame] | 33 | buf.append(FMT_COMPILE("{}"), std::string(2050, 'a')); |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 34 | EXPECT_EQ(4106, fd.lseek(0, Whence::Cur)); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 35 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 36 | buf.append(FMT_STRING("hi\n")); |
| 37 | EXPECT_EQ(4106, fd.lseek(0, Whence::Cur)); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 38 | } |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 39 | EXPECT_EQ(4109, fd.lseek(0, Whence::Cur)); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 40 | } |
| 41 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 42 | } // namespace fd |
| 43 | } // namespace stdplus |