Skip to content

Enable util_tests.cpp on Windows #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions modules/core/util/func_tests/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

#include "core/util/include/util.hpp"

#ifdef _WIN32
#include <Windows.h>
inline int unsetenv(const char* name) { return SetEnvironmentVariableA(name, NULL) ? 0 : -1; }
inline int setenv(const char* name, const char* value, int /*overwrite*/) {
return SetEnvironmentVariableA(name, value) ? 0 : -1;
}
#endif

TEST(util_tests, check_unset_env) {
#ifndef _WIN32
int save_var = ppc::util::GetPPCNumThreads();

unsetenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe)

EXPECT_EQ(ppc::util::GetPPCNumThreads(), 1);

setenv("PPC_NUM_THREADS", std::to_string(save_var).c_str(), 1); // NOLINT(concurrency-mt-unsafe)
#else
GTEST_SKIP();
#endif
}

TEST(util_tests, check_set_env) {
#ifndef _WIN32
int save_var = ppc::util::GetPPCNumThreads();

const int num_threads = static_cast<int>(std::thread::hardware_concurrency());
Expand All @@ -30,7 +33,4 @@ TEST(util_tests, check_set_env) {
EXPECT_EQ(ppc::util::GetPPCNumThreads(), num_threads);

setenv("PPC_NUM_THREADS", std::to_string(save_var).c_str(), 1); // NOLINT(concurrency-mt-unsafe)
#else
GTEST_SKIP();
#endif
}
28 changes: 9 additions & 19 deletions modules/core/util/src/util.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
#define _CRT_SECURE_NO_WARNINGS
#include "core/util/include/util.hpp"

#include <cstdlib>
#ifdef _WIN32
#include <cstdint>
#include <iostream>
#include <memory>
#include <vector>
#endif

#include <filesystem>
#include <string>

Expand All @@ -17,17 +11,13 @@ std::string ppc::util::GetAbsolutePath(const std::string &relative_path) {
}

int ppc::util::GetPPCNumThreads() {
#ifdef _WIN32
size_t len;
char omp_env[100];
errno_t err = getenv_s(&len, omp_env, sizeof(omp_env), "PPC_NUM_THREADS");
if (err != 0 || len == 0) {
omp_env[0] = '\0';
const char *env = std::getenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe)
if ((env != nullptr) && (*env != 0)) {
char *endptr = nullptr;
long val = std::strtol(env, &endptr, 10);
if (endptr != env && val > 0) {
return static_cast<int>(val);
}
}
int num_threads = std::atoi(omp_env);
#else
const char *omp_env = std::getenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe)
int num_threads = (omp_env != nullptr) ? std::atoi(omp_env) : 1;
#endif
return num_threads;
return 1;
}
Loading