William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 1 | #include <gtest/gtest.h> |
| 2 | |
William A. Kennington III | e774345 | 2022-08-12 17:39:44 -0700 | [diff] [blame] | 3 | #include <filesystem> |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 4 | #include <fmt/compile.h> |
William A. Kennington III | e774345 | 2022-08-12 17:39:44 -0700 | [diff] [blame] | 5 | #include <memory> |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 6 | #include <stdplus/fd/fmt.hpp> |
| 7 | #include <stdplus/fd/managed.hpp> |
William A. Kennington III | e774345 | 2022-08-12 17:39:44 -0700 | [diff] [blame] | 8 | #include <stdplus/gtest/tmp.hpp> |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 9 | #include <stdplus/util/cexec.hpp> |
| 10 | #include <sys/mman.h> |
| 11 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 12 | #include <string> |
| 13 | #include <string_view> |
| 14 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 15 | namespace stdplus |
| 16 | { |
| 17 | namespace fd |
| 18 | { |
| 19 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 20 | using fmt::operator""_cf; |
| 21 | using std::literals::string_view_literals::operator""sv; |
| 22 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 23 | TEST(FormatBuffer, Basic) |
| 24 | { |
| 25 | auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create")); |
| 26 | { |
| 27 | FormatBuffer buf(fd, 4096); |
| 28 | buf.append("hi\n"); |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 29 | buf.append("hi\n"sv); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 30 | EXPECT_EQ(0, fd.lseek(0, Whence::Cur)); |
| 31 | buf.flush(); |
| 32 | |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 33 | EXPECT_EQ(6, fd.lseek(0, Whence::Cur)); |
| 34 | buf.append(FMT_COMPILE("{}"), std::string(2050, 'a')); |
| 35 | EXPECT_EQ(6, fd.lseek(0, Whence::Cur)); |
| 36 | buf.append("{}"_cf, std::string(2050, 'a')); |
| 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 | buf.append(FMT_STRING("hi\n")); |
| 40 | EXPECT_EQ(4106, fd.lseek(0, Whence::Cur)); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 41 | } |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 42 | EXPECT_EQ(4109, fd.lseek(0, Whence::Cur)); |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 43 | } |
| 44 | |
William A. Kennington III | e774345 | 2022-08-12 17:39:44 -0700 | [diff] [blame] | 45 | class FormatToFileTest : public gtest::TestWithTmp |
| 46 | { |
| 47 | protected: |
| 48 | std::string tmpname; |
| 49 | std::unique_ptr<FormatToFile> file; |
| 50 | |
| 51 | FormatToFileTest() : |
| 52 | tmpname(fmt::format("{}/tmp.XXXXXX", CaseTmpDir())), |
| 53 | file(std::make_unique<FormatToFile>(tmpname)) |
| 54 | { |
| 55 | tmpname = file->getTmpname(); |
| 56 | EXPECT_TRUE(std::filesystem::exists(tmpname)); |
| 57 | } |
| 58 | |
| 59 | ~FormatToFileTest() noexcept(true) |
| 60 | { |
| 61 | file.reset(); |
| 62 | EXPECT_FALSE(std::filesystem::exists(tmpname)); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | TEST_F(FormatToFileTest, NoCommit) |
| 67 | { |
| 68 | file->append("hi\n"); |
| 69 | EXPECT_EQ(0, std::filesystem::file_size(tmpname)); |
| 70 | } |
| 71 | |
| 72 | TEST_F(FormatToFileTest, Basic) |
| 73 | { |
| 74 | file->append("hi\n"); |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 75 | file->append("hi\n"sv); |
| 76 | file->append(FMT_STRING("hi\n")); |
| 77 | file->append(FMT_COMPILE("hi\n")); |
William A. Kennington III | e774345 | 2022-08-12 17:39:44 -0700 | [diff] [blame] | 78 | EXPECT_EQ(0, std::filesystem::file_size(tmpname)); |
| 79 | auto filename = fmt::format("{}/out", CaseTmpDir()); |
| 80 | file->commit(filename); |
| 81 | EXPECT_FALSE(std::filesystem::exists(tmpname)); |
William A. Kennington III | 74755ce | 2022-08-19 17:14:52 -0700 | [diff] [blame^] | 82 | EXPECT_EQ(12, std::filesystem::file_size(filename)); |
William A. Kennington III | e774345 | 2022-08-12 17:39:44 -0700 | [diff] [blame] | 83 | } |
| 84 | |
William A. Kennington III | 4f5711c | 2022-08-12 17:01:53 -0700 | [diff] [blame] | 85 | } // namespace fd |
| 86 | } // namespace stdplus |