aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-09-02 22:29:11 +0800
committerYuqian Yang <crupest@crupest.life>2025-09-03 01:52:38 +0800
commit545a638929218a83d194402b3d52f5bffd87d9eb (patch)
treef659f671fec17f3ef7dced31f3a1f59673d18690 /test
parent5035f18f44f675af2faa4019b6de14b3f3aab270 (diff)
downloadcru-545a638929218a83d194402b3d52f5bffd87d9eb.tar.gz
cru-545a638929218a83d194402b3d52f5bffd87d9eb.tar.bz2
cru-545a638929218a83d194402b3d52f5bffd87d9eb.zip
UnixFileDescriptor.
Diffstat (limited to 'test')
-rw-r--r--test/common/CMakeLists.txt1
-rw-r--r--test/common/platform/unix/UnixFileTest.cpp38
2 files changed, 39 insertions, 0 deletions
diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt
index 61222a68..683c8295 100644
--- a/test/common/CMakeLists.txt
+++ b/test/common/CMakeLists.txt
@@ -25,6 +25,7 @@ target_compile_definitions(CruBaseTest PRIVATE
if (UNIX AND NOT EMSCRIPTEN)
target_sources(CruBaseTest PRIVATE
+ platform/unix/UnixFileTest.cpp
platform/unix/UnixFileStreamTest.cpp
)
endif()
diff --git a/test/common/platform/unix/UnixFileTest.cpp b/test/common/platform/unix/UnixFileTest.cpp
new file mode 100644
index 00000000..d5bba0db
--- /dev/null
+++ b/test/common/platform/unix/UnixFileTest.cpp
@@ -0,0 +1,38 @@
+
+#include "cru/base/platform/unix/UnixFileStream.h"
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <fcntl.h>
+#include <filesystem>
+
+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);
+}