Deep Learning Visualizer
Interactive visualization of basic applications of deep learning
Loading...
Searching...
No Matches
test_helpers.h
Go to the documentation of this file.
1#pragma once
2
8#include <catch_amalgamated.h>
9
10#include <cstdint>
11#include <filesystem>
12#include <fstream>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace test_helpers {
18
23class temp_file {
24public:
28 explicit temp_file(std::string_view name)
29 : path_{std::filesystem::temp_directory_path() /
30 (std::string{"deep_learning_visualizer_"} + std::string{name} +
31 ".tmp")} {
32 std::filesystem::remove(path_);
33 }
34
35 ~temp_file() {
36 std::filesystem::remove(path_);
37 }
38
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;
43
47 void write_text(std::string_view text) const {
48 std::ofstream file{path_};
49 file << text;
50 }
51
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()));
59 }
60
64 [[nodiscard]] const std::filesystem::path &path() const {
65 return path_;
66 }
67
68private:
69 std::filesystem::path path_;
70};
71
82inline std::vector<std::uint8_t>
83idx_header(std::uint8_t data_type,
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())};
87
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));
92 }
93 }
94
95 return header;
96}
97
106inline Catch::Matchers::WithinAbsMatcher within_abs(float target,
107 double epsilon = 1e-5) {
108 return Catch::Matchers::WithinAbs(static_cast<double>(target), epsilon);
109}
110
111} // namespace test_helpers
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