blob: 55f6c5dd6d6ae694b653f9fc78b0c5d192de62cd [file] [log] [blame]
William A. Kennington III4f5711c2022-08-12 17:01:53 -07001#include <gtest/gtest.h>
2
William A. Kennington IIIe7743452022-08-12 17:39:44 -07003#include <filesystem>
William A. Kennington III74755ce2022-08-19 17:14:52 -07004#include <fmt/compile.h>
William A. Kennington IIIe7743452022-08-12 17:39:44 -07005#include <memory>
William A. Kennington III4f5711c2022-08-12 17:01:53 -07006#include <stdplus/fd/fmt.hpp>
7#include <stdplus/fd/managed.hpp>
William A. Kennington IIIe7743452022-08-12 17:39:44 -07008#include <stdplus/gtest/tmp.hpp>
William A. Kennington III4f5711c2022-08-12 17:01:53 -07009#include <stdplus/util/cexec.hpp>
10#include <sys/mman.h>
11
William A. Kennington III74755ce2022-08-19 17:14:52 -070012#include <string>
13#include <string_view>
14
William A. Kennington III4f5711c2022-08-12 17:01:53 -070015namespace stdplus
16{
17namespace fd
18{
19
William A. Kennington III74755ce2022-08-19 17:14:52 -070020using fmt::operator""_cf;
21using std::literals::string_view_literals::operator""sv;
22
William A. Kennington III4f5711c2022-08-12 17:01:53 -070023TEST(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 III74755ce2022-08-19 17:14:52 -070029 buf.append("hi\n"sv);
William A. Kennington III4f5711c2022-08-12 17:01:53 -070030 EXPECT_EQ(0, fd.lseek(0, Whence::Cur));
31 buf.flush();
32
William A. Kennington III74755ce2022-08-19 17:14:52 -070033 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 III4f5711c2022-08-12 17:01:53 -070038
William A. Kennington III74755ce2022-08-19 17:14:52 -070039 buf.append(FMT_STRING("hi\n"));
40 EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070041 }
William A. Kennington III74755ce2022-08-19 17:14:52 -070042 EXPECT_EQ(4109, fd.lseek(0, Whence::Cur));
William A. Kennington III4f5711c2022-08-12 17:01:53 -070043}
44
William A. Kennington IIIe7743452022-08-12 17:39:44 -070045class 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
66TEST_F(FormatToFileTest, NoCommit)
67{
68 file->append("hi\n");
69 EXPECT_EQ(0, std::filesystem::file_size(tmpname));
70}
71
72TEST_F(FormatToFileTest, Basic)
73{
74 file->append("hi\n");
William A. Kennington III74755ce2022-08-19 17:14:52 -070075 file->append("hi\n"sv);
76 file->append(FMT_STRING("hi\n"));
77 file->append(FMT_COMPILE("hi\n"));
William A. Kennington IIIe7743452022-08-12 17:39:44 -070078 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 III74755ce2022-08-19 17:14:52 -070082 EXPECT_EQ(12, std::filesystem::file_size(filename));
William A. Kennington IIIe7743452022-08-12 17:39:44 -070083}
84
William A. Kennington III4f5711c2022-08-12 17:01:53 -070085} // namespace fd
86} // namespace stdplus