8#include <catch_amalgamated.h>
17namespace test_helpers {
29 : path_{std::filesystem::temp_directory_path() /
30 (std::string{
"deep_learning_visualizer_"} + std::string{name} +
32 std::filesystem::remove(path_);
36 std::filesystem::remove(path_);
39 temp_file(
const temp_file &) =
delete;
40 temp_file &operator=(
const temp_file &) =
delete;
41 temp_file(temp_file &&) =
delete;
42 temp_file &operator=(temp_file &&) =
delete;
48 std::ofstream file{path_};
55 void write_bytes(
const std::vector<std::uint8_t> &bytes)
const {
56 std::ofstream file{path_, std::ios::binary};
57 file.write(
reinterpret_cast<const char *
>(bytes.data()),
58 static_cast<std::streamsize
>(bytes.size()));
64 [[nodiscard]]
const std::filesystem::path &
path()
const {
69 std::filesystem::path path_;
82inline std::vector<std::uint8_t>
84 const std::vector<std::int64_t> &dimensions) {
85 std::vector<std::uint8_t> header{
86 0x00, 0x00, data_type,
static_cast<std::uint8_t
>(dimensions.size())};
88 for (
auto dimension : dimensions) {
89 for (
int shift{24}; shift >= 0; shift -= 8) {
90 header.push_back(
static_cast<std::uint8_t
>(
91 (
static_cast<std::uint64_t
>(dimension) >> shift) & 0xFFU));
106inline Catch::Matchers::WithinAbsMatcher
within_abs(
float target,
107 double epsilon = 1e-5) {
108 return Catch::Matchers::WithinAbs(
static_cast<double>(target), epsilon);
A scratch file in the system temporary directory which is removed once the test finishes,...
Definition test_helpers.h:23
const std::filesystem::path & path() const
Definition test_helpers.h:64
temp_file(std::string_view name)
Definition test_helpers.h:28
void write_bytes(const std::vector< std::uint8_t > &bytes) const
Replaces the contents of the file with the given raw bytes.
Definition test_helpers.h:55
void write_text(std::string_view text) const
Replaces the contents of the file with the given text.
Definition test_helpers.h:47
std::vector< std::uint8_t > idx_header(std::uint8_t data_type, const std::vector< std::int64_t > &dimensions)
Builds the header of an IDX file, which is a null magic number, a one byte data type,...
Definition test_helpers.h:83
Catch::Matchers::WithinAbsMatcher within_abs(float target, double epsilon=1e-5)
Matches a float against a target within an absolute tolerance.
Definition test_helpers.h:106