From efa1266f10e90c0c46f47cc06645422142cb2d9f Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Wed, 3 Sep 2025 12:42:10 +0800 Subject: common -> base in test dir. --- test/base/platform/unix/UnixFileStreamTest.cpp | 30 ++++++++++++++++++++ test/base/platform/unix/UnixFileTest.cpp | 38 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/base/platform/unix/UnixFileStreamTest.cpp create mode 100644 test/base/platform/unix/UnixFileTest.cpp (limited to 'test/base/platform/unix') diff --git a/test/base/platform/unix/UnixFileStreamTest.cpp b/test/base/platform/unix/UnixFileStreamTest.cpp new file mode 100644 index 00000000..417fccc2 --- /dev/null +++ b/test/base/platform/unix/UnixFileStreamTest.cpp @@ -0,0 +1,30 @@ +#include "cru/base/platform/unix/UnixFileStream.h" + +#include + +#include +#include + +TEST_CASE("UnixFileStream Work", "[stream]") { + using namespace cru; + using namespace cru::io; + using namespace cru::platform::unix; + + auto temp_file_path = + (std::filesystem::temp_directory_path() / "cru_test_temp.XXXXXX") + .generic_string(); + mkstemp(temp_file_path.data()); + + UnixFileStream file(temp_file_path.c_str(), O_WRONLY | O_CREAT); + file.Write("abc", 3); + file.Close(); + + UnixFileStream file2(temp_file_path.c_str(), O_RDONLY); + auto buffer = std::make_unique(3); + file2.Read(buffer.get(), 3); + REQUIRE(std::string_view(reinterpret_cast(buffer.get()), 3) == + "abc"); + file2.Close(); + + std::filesystem::remove(temp_file_path); +} diff --git a/test/base/platform/unix/UnixFileTest.cpp b/test/base/platform/unix/UnixFileTest.cpp new file mode 100644 index 00000000..d5bba0db --- /dev/null +++ b/test/base/platform/unix/UnixFileTest.cpp @@ -0,0 +1,38 @@ + +#include "cru/base/platform/unix/UnixFileStream.h" + +#include + +#include +#include + +TEST_CASE("UnixFile Work", "[unix]") { + using namespace cru; + using namespace cru::platform::unix; + + auto calledTimes = 0; + auto mockClose = [&calledTimes](int _) { + calledTimes += 1; + return 0; + }; + + auto temp_file_path = + (std::filesystem::temp_directory_path() / "cru_test_temp.XXXXXX") + .generic_string(); + mkstemp(temp_file_path.data()); + + auto fdNumber = ::open(temp_file_path.c_str(), O_WRONLY | O_CREAT); + + { + UnixFileDescriptor fd(fdNumber, true, std::move(mockClose)); + REQUIRE(calledTimes == 0); + UnixFileDescriptor fd2(std::move(fd)); + REQUIRE(calledTimes == 0); + UnixFileDescriptor fd3; + fd3 = std::move(fd2); + REQUIRE(calledTimes == 0); + } + REQUIRE(calledTimes == 1); + + std::filesystem::remove(temp_file_path); +} -- cgit v1.2.3