diff options
Diffstat (limited to 'test/test_file.cpp')
-rw-r--r-- | test/test_file.cpp | 80 |
1 files changed, 71 insertions, 9 deletions
diff --git a/test/test_file.cpp b/test/test_file.cpp index 27844d8..62df1ae 100644 --- a/test/test_file.cpp +++ b/test/test_file.cpp @@ -17,6 +17,7 @@ #include "fud_file.hpp" #include "fud_string.hpp" +#include "fud_vector.hpp" #include "test_common.hpp" #include "gtest/gtest.h" @@ -28,6 +29,17 @@ namespace fud { +auto rmFile(const auto& filename) -> int +{ + auto result = unlink(filename.c_str()); + if (result == -1) { + if (errno == ENOENT) { + return 0; + } + } + return result; +} + TEST(FudFile, Basic) { constexpr const char* testDirCName = "/tmp/fud_directory_test"; @@ -47,15 +59,6 @@ TEST(FudFile, Basic) String testName1{String::makeFromCStrings(testDirCName, "/", "test1").takeOkay()}; String testName2{String::makeFromCStrings(testDirCName, "/", "test2").takeOkay()}; - auto rmFile = [](const auto& filename) -> int { - auto result = unlink(filename.c_str()); - if (result == -1) { - if (errno == ENOENT) { - return 0; - } - } - return result; - }; ASSERT_EQ(rmFile(testName1), 0); ASSERT_EQ(rmFile(testName2), 0); @@ -94,4 +97,63 @@ TEST(FudFile, Basic) EXPECT_EQ(fileResult.getError(), FudStatus::Failure); } +TEST(FudBufferedFile, OpenReadWrite) +{ + constexpr const char* testDirCName = "/tmp/fud_directory_test"; + const auto testDirName{String::makeFromCString(testDirCName).takeOkay()}; + ASSERT_TRUE(testDirName.utf8Valid()); + constexpr mode_t pathMode = 0777; + + ASSERT_EQ(removeRecursive(testDirName), FudStatus::Success); + + auto mkdirResult = mkdir(testDirName.c_str(), pathMode); + EXPECT_EQ(mkdirResult, 0); + if (mkdirResult != 0) { + ASSERT_EQ(removeRecursive(testDirName), FudStatus::Success); + return; + } + + String testName{String::makeFromCStrings(testDirCName, "/", "test1").takeOkay()}; + auto fileResult{RegularFile::create( + testName.asView(), + FileAccessMode::ReadWrite, + OpenFlags{OpenFlagEnum::Truncate}, + Permissions{PermReadWrite, PermReadWrite, PermReadWrite}, + true, + NullOpt)}; + ASSERT_TRUE(fileResult.isOkay()); + + ASSERT_GT(testName.length(), 8); + + auto vectorResult{Vector<std::byte>::withSize(8)}; + ASSERT_TRUE(vectorResult.isOkay()); + auto bufferedFile{BufferedRegularFile::make(fileResult.takeOkay(), vectorResult.takeOkay())}; + + ASSERT_EQ(bufferedFile.file().size().getOkayOr(std::numeric_limits<size_t>::max()), 0); + + auto writeResult = bufferedFile.write( + reinterpret_cast<const std::byte*>(testName.data()), + testName.size(), + NullOpt); + DrainResult expected{testName.size(), FudStatus::Success}; + ASSERT_EQ(writeResult, expected); + + DrainResult nullExpected{0, FudStatus::Success}; + ASSERT_EQ(bufferedFile.flush(), nullExpected); + ASSERT_EQ(bufferedFile.seekStart(), FudStatus::Success); + + Vector<utf8> output{Vector<utf8>::withSize(testName.size()).takeOkay()}; + auto readResult = bufferedFile.read( + reinterpret_cast<std::byte*>(output.data()), + testName.size(), + testName.size()); + ASSERT_EQ(readResult, expected); + + EXPECT_EQ(output.size(), testName.size()); + EXPECT_EQ(0, compareMem(output.data(), output.size(), testName.data(), testName.size()).takeOkayOr(-1)); + + EXPECT_EQ(bufferedFile.close(true), FudStatus::Success); + ASSERT_EQ(rmFile(testName), 0); +} + } // namespace fud |