|
| 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