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 fmt::operator""_cf; |
| 19 | using std::literals::string_view_literals::operator""sv; |
| 20 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 21 | TEST(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 III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 27 | buf.append("hi\n"sv); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 28 | EXPECT_EQ(0, fd.lseek(0, Whence::Cur)); |
| 29 | buf.flush(); |
| 30 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 31 | 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 III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 36 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 37 | buf.append(FMT_STRING("hi\n")); |
| 38 | EXPECT_EQ(4106, fd.lseek(0, Whence::Cur)); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 39 | } |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame] | 40 | EXPECT_EQ(4109, fd.lseek(0, Whence::Cur)); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 41 | } |
| 42 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 43 | } // namespace fd |
| 44 | } // namespace stdplus |