Skip to content

Commit 3882703

Browse files
committed
extend test coverage: add extensive tests for util, runners, task, and refine perf module tests
1 parent e632eb1 commit 3882703

5 files changed

Lines changed: 328 additions & 5 deletions

File tree

modules/performance/tests/perf_tests.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,16 @@ TYPED_TEST(GetNamespaceTest, ExtractsNamespaceCorrectly) {
311311
std::string k_ns = ppc::util::GetNamespace<TypeParam>();
312312

313313
if constexpr (std::is_same_v<TypeParam, my::nested::Type>) {
314-
EXPECT_EQ(k_ns, "ppc::performance::my::nested");
314+
// Different compilers may represent anonymous namespaces differently
315+
// Check for essential parts: ppc::performance, my, and nested
316+
EXPECT_TRUE(k_ns.find("ppc::performance") != std::string::npos);
317+
EXPECT_TRUE(k_ns.find("my") != std::string::npos);
318+
EXPECT_TRUE(k_ns.find("nested") != std::string::npos);
315319
} else if constexpr (std::is_same_v<TypeParam, my::Another>) {
316-
EXPECT_EQ(k_ns, "ppc::performance::my");
320+
// Check for essential parts: ppc::performance and my
321+
EXPECT_TRUE(k_ns.find("ppc::performance") != std::string::npos);
322+
EXPECT_TRUE(k_ns.find("my") != std::string::npos);
323+
EXPECT_TRUE(k_ns.find("nested") == std::string::npos); // Should not contain nested
317324
} else if constexpr (std::is_same_v<TypeParam, int>) {
318325
EXPECT_EQ(k_ns, "");
319326
} else {
@@ -573,8 +580,7 @@ TEST(PerfTest, TaskRunCompletesPipelineAfterTiming) {
573580
EXPECT_EQ(postprocessing_count, 2); // Called twice
574581
}
575582

576-
namespace test_namespace {
577-
} // namespace test_namespace
583+
namespace test_namespace {} // namespace test_namespace
578584

579585
TEST(PerfTest, TemplateInstantiationWithDifferentTypes) {
580586
// Test that the Perf template can be instantiated with different types
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <nlohmann/json.hpp>
4+
5+
#include "util/include/util.hpp"
6+
7+
class RunnersAdditionalTest : public ::testing::Test {
8+
protected:
9+
void SetUp() override {
10+
// Setup for each test
11+
}
12+
13+
void TearDown() override {
14+
// Clean up after each test
15+
}
16+
};
17+
18+
// Keep only unique functionality tests - InitJSONPtr
19+
// No environment variable manipulation needed here
20+
21+
TEST_F(RunnersAdditionalTest, InitJSONPtr_BasicFunctionality) {
22+
// Test the InitJSONPtr function
23+
auto json_ptr = ppc::util::InitJSONPtr();
24+
25+
// Verify the JSON pointer is valid
26+
EXPECT_NE(json_ptr, nullptr);
27+
28+
// Test adding data to the JSON pointer - simplified to reduce complexity
29+
(*json_ptr)["test_key"] = "test_value";
30+
EXPECT_EQ((*json_ptr)["test_key"], "test_value");
31+
}
32+
33+
TEST_F(RunnersAdditionalTest, InitJSONPtr_EmptyJSON) {
34+
// Test with empty JSON
35+
auto json_ptr = ppc::util::InitJSONPtr();
36+
37+
// Should still return a valid pointer
38+
EXPECT_NE(json_ptr, nullptr);
39+
40+
// Should be empty initially
41+
EXPECT_TRUE(json_ptr->empty());
42+
}

modules/task/include/task.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ using TaskPtr = std::shared_ptr<Task<InType, OutType>>;
273273
/// @param in Input to pass to the task constructor.
274274
/// @return Shared a pointer to the newly created task.
275275
template <typename TaskType, typename InType>
276-
std::shared_ptr<TaskType> TaskGetter(InType in) {
276+
std::shared_ptr<TaskType> TaskGetter(const InType &in) {
277277
return std::make_shared<TaskType>(in);
278278
}
279279

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <memory>
4+
#include <string>
5+
#include <utility>
6+
#include <vector>
7+
8+
#include "task/include/task.hpp"
9+
10+
class TaskAdditionalTest : public ::testing::Test {
11+
protected:
12+
void SetUp() override {
13+
// Setup for each test
14+
}
15+
16+
void TearDown() override {
17+
// Clean up after each test
18+
}
19+
};
20+
21+
// Test TaskGetter function - unique functionality not covered elsewhere
22+
TEST_F(TaskAdditionalTest, TaskGetter_BasicFunctionality) {
23+
// Create a task to test with
24+
class GetterTestTask : public ppc::task::Task<std::vector<int>, std::vector<int>> {
25+
public:
26+
GetterTestTask(int value) : ppc::task::Task<std::vector<int>, std::vector<int>>(), value_(value) {}
27+
28+
bool PreProcessingImpl() override { return true; }
29+
bool ValidationImpl() override { return true; }
30+
bool RunImpl() override { return true; }
31+
bool PostProcessingImpl() override { return true; }
32+
33+
[[nodiscard]] int GetValue() const { return value_; }
34+
35+
private:
36+
int value_;
37+
};
38+
39+
// Test TaskGetter function
40+
auto getter_result = ppc::task::TaskGetter<GetterTestTask>(42);
41+
42+
EXPECT_NE(getter_result, nullptr);
43+
EXPECT_EQ(getter_result->GetValue(), 42);
44+
}
45+
46+
TEST_F(TaskAdditionalTest, TaskGetter_DifferentTaskTypes) {
47+
// Test TaskGetter with different task types
48+
class TaskType1 : public ppc::task::Task<std::vector<int>, std::vector<int>> {
49+
public:
50+
explicit TaskType1(std::string name)
51+
: ppc::task::Task<std::vector<int>, std::vector<int>>(), name_(std::move(name)) {}
52+
53+
bool PreProcessingImpl() override { return true; }
54+
bool ValidationImpl() override { return true; }
55+
bool RunImpl() override { return true; }
56+
bool PostProcessingImpl() override { return true; }
57+
58+
[[nodiscard]] std::string GetName() const { return name_; }
59+
60+
private:
61+
std::string name_;
62+
};
63+
64+
class TaskType2 : public ppc::task::Task<std::vector<int>, std::vector<int>> {
65+
public:
66+
TaskType2(double value) : ppc::task::Task<std::vector<int>, std::vector<int>>(), value_(value) {}
67+
68+
bool PreProcessingImpl() override { return true; }
69+
bool ValidationImpl() override { return true; }
70+
bool RunImpl() override { return true; }
71+
bool PostProcessingImpl() override { return true; }
72+
73+
[[nodiscard]] double GetValue() const { return value_; }
74+
75+
private:
76+
double value_;
77+
};
78+
79+
auto getter1 = ppc::task::TaskGetter<TaskType1>(std::string("test"));
80+
auto getter2 = ppc::task::TaskGetter<TaskType2>(3.14);
81+
82+
EXPECT_NE(getter1, nullptr);
83+
EXPECT_NE(getter2, nullptr);
84+
EXPECT_EQ(getter1->GetName(), "test");
85+
EXPECT_DOUBLE_EQ(getter2->GetValue(), 3.14);
86+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <libenvpp/detail/environment.hpp>
4+
#include <libenvpp/detail/testing.hpp>
5+
#include <string>
6+
7+
#include "util/include/util.hpp"
8+
9+
class UtilAdditionalTest : public ::testing::Test {
10+
protected:
11+
void SetUp() override {
12+
// No need to manually clear environment variables with libenvpp
13+
}
14+
15+
void TearDown() override {
16+
// No need to manually clear environment variables with libenvpp
17+
}
18+
};
19+
20+
// Tests for GetAbsoluteTaskPath - understand it creates full absolute paths
21+
TEST_F(UtilAdditionalTest, GetAbsoluteTaskPath_ValidPaths) {
22+
std::string result = ppc::util::GetAbsoluteTaskPath("task1", "src/main.cpp");
23+
// The function adds PPC_PATH_TO_PROJECT/tasks/task1/data/src/main.cpp
24+
// Use platform-agnostic path checking - simplified to reduce complexity
25+
EXPECT_FALSE(result.empty());
26+
EXPECT_TRUE(result.find("tasks") != std::string::npos);
27+
EXPECT_TRUE(result.find("task1") != std::string::npos);
28+
}
29+
30+
TEST_F(UtilAdditionalTest, GetAbsoluteTaskPath_EmptyIdPath) {
31+
std::string result = ppc::util::GetAbsoluteTaskPath("", "src/main.cpp");
32+
// The function adds PPC_PATH_TO_PROJECT/tasks/data/src/main.cpp
33+
EXPECT_TRUE(result.find("tasks") != std::string::npos);
34+
EXPECT_TRUE(result.find("data") != std::string::npos);
35+
EXPECT_TRUE(result.find("main.cpp") != std::string::npos);
36+
}
37+
38+
TEST_F(UtilAdditionalTest, GetAbsoluteTaskPath_EmptyRelativePath) {
39+
std::string result = ppc::util::GetAbsoluteTaskPath("task1", "");
40+
// The function adds PPC_PATH_TO_PROJECT/tasks/task1/data/
41+
EXPECT_TRUE(result.find("tasks") != std::string::npos);
42+
EXPECT_TRUE(result.find("task1") != std::string::npos);
43+
EXPECT_TRUE(result.find("data") != std::string::npos);
44+
}
45+
46+
TEST_F(UtilAdditionalTest, GetAbsoluteTaskPath_BothEmpty) {
47+
std::string result = ppc::util::GetAbsoluteTaskPath("", "");
48+
// The function adds PPC_PATH_TO_PROJECT/tasks/data/
49+
EXPECT_TRUE(result.find("tasks") != std::string::npos);
50+
EXPECT_TRUE(result.find("data") != std::string::npos);
51+
}
52+
53+
// Tests for GetNumThreads - returns 1 by default if no env var, otherwise returns env var value
54+
TEST_F(UtilAdditionalTest, GetNumThreads_EnvironmentVariableNotSet) {
55+
// Ensure PPC_NUM_THREADS is not set in the system environment
56+
env::detail::delete_environment_variable("PPC_NUM_THREADS");
57+
58+
// Create a scoped environment with no PPC_NUM_THREADS set
59+
env::scoped_test_environment test_env({});
60+
61+
int result = ppc::util::GetNumThreads();
62+
EXPECT_EQ(result, 1); // Default value when no environment variable is set
63+
}
64+
65+
TEST_F(UtilAdditionalTest, GetNumThreads_EnvironmentVariableSet) {
66+
// Create a scoped environment with PPC_NUM_THREADS=4
67+
env::scoped_test_environment test_env("PPC_NUM_THREADS", "4");
68+
69+
int result = ppc::util::GetNumThreads();
70+
EXPECT_EQ(result, 4);
71+
}
72+
73+
TEST_F(UtilAdditionalTest, GetNumThreads_EnvironmentVariableZero) {
74+
env::scoped_test_environment test_env("PPC_NUM_THREADS", "0");
75+
76+
int result = ppc::util::GetNumThreads();
77+
EXPECT_EQ(result, 0);
78+
}
79+
80+
TEST_F(UtilAdditionalTest, GetNumThreads_EnvironmentVariableNegative) {
81+
env::scoped_test_environment test_env("PPC_NUM_THREADS", "-1");
82+
83+
int result = ppc::util::GetNumThreads();
84+
EXPECT_EQ(result, -1);
85+
}
86+
87+
TEST_F(UtilAdditionalTest, GetNumThreads_EnvironmentVariableInvalid) {
88+
env::scoped_test_environment test_env("PPC_NUM_THREADS", "invalid");
89+
90+
int result = ppc::util::GetNumThreads();
91+
EXPECT_EQ(result, 1); // Returns default when parsing fails
92+
}
93+
94+
// Tests for IsUnderMpirun - checks specific environment variables from the kMpiEnvVars array
95+
TEST_F(UtilAdditionalTest, IsUnderMpirun_NoEnvironmentVariables) {
96+
// Create an empty environment to ensure no MPI vars are set
97+
env::scoped_test_environment test_env({});
98+
99+
bool result = ppc::util::IsUnderMpirun();
100+
EXPECT_FALSE(result);
101+
}
102+
103+
TEST_F(UtilAdditionalTest, IsUnderMpirun_OMPI_COMM_WORLD_SIZE) {
104+
env::scoped_test_environment test_env("OMPI_COMM_WORLD_SIZE", "4");
105+
106+
bool result = ppc::util::IsUnderMpirun();
107+
EXPECT_TRUE(result);
108+
}
109+
110+
TEST_F(UtilAdditionalTest, IsUnderMpirun_OMPI_UNIVERSE_SIZE) {
111+
env::scoped_test_environment test_env("OMPI_UNIVERSE_SIZE", "8");
112+
113+
bool result = ppc::util::IsUnderMpirun();
114+
EXPECT_TRUE(result);
115+
}
116+
117+
TEST_F(UtilAdditionalTest, IsUnderMpirun_PMI_SIZE) {
118+
env::scoped_test_environment test_env("PMI_SIZE", "2");
119+
120+
bool result = ppc::util::IsUnderMpirun();
121+
EXPECT_TRUE(result);
122+
}
123+
124+
TEST_F(UtilAdditionalTest, IsUnderMpirun_PMI_RANK) {
125+
env::scoped_test_environment test_env("PMI_RANK", "0");
126+
127+
bool result = ppc::util::IsUnderMpirun();
128+
EXPECT_TRUE(result);
129+
}
130+
131+
TEST_F(UtilAdditionalTest, IsUnderMpirun_PMI_FD) {
132+
env::scoped_test_environment test_env("PMI_FD", "3");
133+
134+
bool result = ppc::util::IsUnderMpirun();
135+
EXPECT_TRUE(result);
136+
}
137+
138+
TEST_F(UtilAdditionalTest, IsUnderMpirun_HYDRA_CONTROL_FD) {
139+
env::scoped_test_environment test_env("HYDRA_CONTROL_FD", "4");
140+
141+
bool result = ppc::util::IsUnderMpirun();
142+
EXPECT_TRUE(result);
143+
}
144+
145+
TEST_F(UtilAdditionalTest, IsUnderMpirun_PMIX_RANK) {
146+
env::scoped_test_environment test_env("PMIX_RANK", "1");
147+
148+
bool result = ppc::util::IsUnderMpirun();
149+
EXPECT_TRUE(result);
150+
}
151+
152+
TEST_F(UtilAdditionalTest, IsUnderMpirun_SLURM_PROCID) {
153+
env::scoped_test_environment test_env("SLURM_PROCID", "0");
154+
155+
bool result = ppc::util::IsUnderMpirun();
156+
EXPECT_TRUE(result);
157+
}
158+
159+
TEST_F(UtilAdditionalTest, IsUnderMpirun_MSMPI_RANK) {
160+
env::scoped_test_environment test_env("MSMPI_RANK", "2");
161+
162+
bool result = ppc::util::IsUnderMpirun();
163+
EXPECT_TRUE(result);
164+
}
165+
166+
TEST_F(UtilAdditionalTest, IsUnderMpirun_MSMPI_LOCALRANK) {
167+
env::scoped_test_environment test_env("MSMPI_LOCALRANK", "0");
168+
169+
bool result = ppc::util::IsUnderMpirun();
170+
EXPECT_TRUE(result);
171+
}
172+
173+
TEST_F(UtilAdditionalTest, IsUnderMpirun_MultipleEnvironmentVariables) {
174+
// Test with multiple MPI environment variables set
175+
env::scoped_test_environment test_env({{"OMPI_COMM_WORLD_SIZE", "4"}, {"PMI_SIZE", "4"}, {"SLURM_PROCID", "0"}});
176+
177+
bool result = ppc::util::IsUnderMpirun();
178+
EXPECT_TRUE(result);
179+
}
180+
181+
TEST_F(UtilAdditionalTest, IsUnderMpirun_EmptyEnvironmentVariable) {
182+
// Test with empty value - behavior is implementation-dependent
183+
env::scoped_test_environment test_env("OMPI_COMM_WORLD_SIZE", "");
184+
185+
bool result = ppc::util::IsUnderMpirun();
186+
// Empty values may or may not be detected as "set" depending on implementation
187+
// verify the function doesn't crash
188+
(void)result; // Suppress unused variable warning
189+
}

0 commit comments

Comments
 (0)