Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/util/include/func_test_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class BaseRunFuncTests : public ::testing::TestWithParam<FuncTestParam<InType, O

ValidateTestName(test_name);

const auto test_env_scope = ppc::util::test::MakePerTestEnvForCurrentGTest(test_name);

if (IsTestDisabled(test_name)) {
GTEST_SKIP();
}
Expand Down
2 changes: 2 additions & 0 deletions modules/util/include/perf_test_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class BaseRunPerfTests : public ::testing::TestWithParam<PerfTestParam<InType, O
GTEST_SKIP();
}

const auto test_env_scope = ppc::util::test::MakePerTestEnvForCurrentGTest(test_name);

task_ = task_getter(GetTestInputData());
ppc::performance::Perf perf(task_);
ppc::performance::PerfAttr perf_attr;
Expand Down
62 changes: 62 additions & 0 deletions modules/util/include/util.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#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>
Expand All @@ -17,6 +23,9 @@
# 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.
Expand Down Expand Up @@ -90,4 +99,57 @@ inline std::shared_ptr<nlohmann::json> InitJSONPtr() {

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
6 changes: 3 additions & 3 deletions scripts/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def run_processes(self, additional_mpi_args):
self.__run_exec(
mpi_running
+ [str(self.work_dir / "ppc_func_tests")]
+ self.__get_gtest_settings(1, "_" + task_type)
+ self.__get_gtest_settings(1, "_" + task_type + "_")
)

def run_performance(self):
Expand All @@ -152,13 +152,13 @@ def run_performance(self):
self.__run_exec(
mpi_running
+ [str(self.work_dir / "ppc_perf_tests")]
+ self.__get_gtest_settings(1, "_" + task_type)
+ self.__get_gtest_settings(1, "_" + task_type + "_")
)

for task_type in ["omp", "seq", "stl", "tbb"]:
self.__run_exec(
[str(self.work_dir / "ppc_perf_tests")]
+ self.__get_gtest_settings(1, "_" + task_type)
+ self.__get_gtest_settings(1, "_" + task_type + "_")
)


Expand Down
Loading