forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.hpp
More file actions
155 lines (130 loc) · 4.14 KB
/
util.hpp
File metadata and controls
155 lines (130 loc) · 4.14 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#include <algorithm>
#include <atomic>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
#include <system_error>
#include <typeinfo>
#ifdef __GNUG__
# include <cxxabi.h>
#endif
#include "nlohmann/json_fwd.hpp"
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4459)
#endif
#include <gtest/gtest.h>
#include <libenvpp/detail/environment.hpp>
#include <nlohmann/json.hpp>
/// @brief JSON namespace used for settings and config parsing.
using NlohmannJsonParseError = nlohmann::json::parse_error;
/// @brief JSON namespace used for settings and config typing.
using NlohmannJsonTypeError = nlohmann::json::type_error;
#ifdef _MSC_VER
# pragma warning(pop)
#endif
namespace ppc::util {
/// @brief Utility class for tracking destructor failure across tests.
/// @details Provides thread-safe methods to set, unset, and check the failure flag.
class DestructorFailureFlag {
public:
/// @brief Marks that a destructor failure has occurred.
static void Set() {
failure_flag.store(true);
}
/// @brief Clears the destructor failure flag.
static void Unset() {
failure_flag.store(false);
}
/// @brief Checks if a destructor failure was recorded.
/// @return True if failure occurred, false otherwise.
static bool Get() {
return failure_flag.load();
}
private:
inline static std::atomic<bool> failure_flag{false};
};
enum class GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
std::string GetAbsoluteTaskPath(const std::string &id_path, const std::string &relative_path);
int GetNumThreads();
int GetNumProc();
double GetTaskMaxTime();
double GetPerfMaxTime();
template <typename T>
std::string GetNamespace() {
std::string name = typeid(T).name();
#ifdef __GNUC__
int status = 0;
std::unique_ptr<char, void (*)(void *)> demangled{abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status),
std::free};
name = (status == 0) ? demangled.get() : name;
#endif
#ifdef _MSC_VER
const std::string prefixes[] = {"class ", "struct ", "enum ", "union "};
for (const auto &prefix : prefixes) {
if (name.starts_with(prefix)) {
name = name.substr(prefix.size());
break;
}
}
name.erase(0, name.find_first_not_of(' '));
#endif
auto pos = name.rfind("::");
return (pos != std::string::npos) ? name.substr(0, pos) : std::string{};
}
inline std::shared_ptr<nlohmann::json> InitJSONPtr() {
return std::make_shared<nlohmann::json>();
}
bool IsUnderMpirun();
namespace test {
[[nodiscard]] inline std::string SanitizeToken(std::string_view token_sv) {
std::string token{token_sv};
auto is_allowed = [](char c) {
return std::isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '-' || c == '.';
};
std::ranges::replace(token, ' ', '_');
for (char &ch : token) {
if (!is_allowed(ch)) {
ch = '_';
}
}
return token;
}
class ScopedPerTestEnv {
public:
explicit ScopedPerTestEnv(const std::string &token)
: set_uid_("PPC_TEST_UID", token), set_tmp_("PPC_TEST_TMPDIR", CreateTmpDir(token)) {}
private:
static std::string CreateTmpDir(const std::string &token) {
namespace fs = std::filesystem;
const fs::path tmp = fs::temp_directory_path() / (std::string("ppc_test_") + token);
std::error_code ec;
fs::create_directories(tmp, ec);
(void)ec;
return tmp.string();
}
env::detail::set_scoped_environment_variable set_uid_;
env::detail::set_scoped_environment_variable set_tmp_;
};
[[nodiscard]] inline std::string MakeCurrentGTestToken(std::string_view fallback_name) {
const auto *unit = ::testing::UnitTest::GetInstance();
const auto *info = (unit != nullptr) ? unit->current_test_info() : nullptr;
std::ostringstream os;
if (info != nullptr) {
os << info->test_suite_name() << "." << info->name();
} else {
os << fallback_name;
}
return SanitizeToken(os.str());
}
inline ScopedPerTestEnv MakePerTestEnvForCurrentGTest(std::string_view fallback_name) {
return ScopedPerTestEnv(MakeCurrentGTestToken(fallback_name));
}
} // namespace test
} // namespace ppc::util