aboutsummaryrefslogtreecommitdiff
path: root/test/common/platform/unix/UnixFileStreamTest.cpp
blob: c27e485b1e2eb656ae352020cbf5cd20dfb74f50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "cru/common/io/OpenFileFlag.h"
#include "cru/common/platform/unix/UnixFileStream.h"

#include <catch2/catch_test_macros.hpp>

#include <fcntl.h>
#include <filesystem>

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();
  mktemp(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(), OpenFileFlags::Read);
  auto buffer = std::make_unique<std::byte[]>(3);
  file2.Read(buffer.get(), 3);
  REQUIRE(std::string_view(reinterpret_cast<const char*>(buffer.get()), 3) ==
          "abc");
  file2.Close();

  std::filesystem::remove(temp_file_path);
}