Skip to content

Commit 5dac017

Browse files
authored
[libc][stdlib] Simplify getenv_test by using strcmp instead of custom helper
Replace the custom my_streq() helper function with LIBC_NAMESPACE::strcmp(), simplifying the test code and making it more consistent with other integration tests in the codebase. Changes: - Removed 18-line my_streq() helper function - Added #include "src/string/strcmp.h" - Updated string comparisons: * Null checks: Direct comparison with nullptr * Equality checks: ASSERT_EQ(strcmp(...), 0) * Inequality checks: ASSERT_NE(strcmp(...), 0) - Added libc.src.string.strcmp dependency in CMakeLists.txt This reduces the test file from 49 to 31 lines while maintaining the same test coverage. The strcmp-based approach is cleaner and aligns with the pattern used in the environment_management branch tests (setenv_test, unsetenv_test, putenv_test). Testing: Verified that all test assertions pass with the new implementation.
1 parent 9f06843 commit 5dac017

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

libc/test/integration/src/stdlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_integration_test(
1212
getenv_test.cpp
1313
DEPENDS
1414
libc.src.stdlib.getenv
15+
libc.src.string.strcmp
1516
ENV
1617
FRANCE=Paris
1718
GERMANY=Berlin

libc/test/integration/src/stdlib/getenv_test.cpp

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,24 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdlib/getenv.h"
10+
#include "src/string/strcmp.h"
1011

1112
#include "test/IntegrationTest/test.h"
1213

13-
static bool my_streq(const char *lhs, const char *rhs) {
14-
if (lhs == rhs)
15-
return true;
16-
if (((lhs == static_cast<char *>(nullptr)) &&
17-
(rhs != static_cast<char *>(nullptr))) ||
18-
((lhs != static_cast<char *>(nullptr)) &&
19-
(rhs == static_cast<char *>(nullptr)))) {
20-
return false;
21-
}
22-
const char *l, *r;
23-
for (l = lhs, r = rhs; *l != '\0' && *r != '\0'; ++l, ++r)
24-
if (*l != *r)
25-
return false;
26-
27-
return *l == '\0' && *r == '\0';
28-
}
29-
3014
TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
3115
[[maybe_unused]] char **envp) {
32-
ASSERT_TRUE(
33-
my_streq(LIBC_NAMESPACE::getenv(""), static_cast<char *>(nullptr)));
34-
ASSERT_TRUE(
35-
my_streq(LIBC_NAMESPACE::getenv("="), static_cast<char *>(nullptr)));
36-
ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE"),
37-
static_cast<char *>(nullptr)));
38-
ASSERT_FALSE(
39-
my_streq(LIBC_NAMESPACE::getenv("PATH"), static_cast<char *>(nullptr)));
40-
ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("FRANCE"), "Paris"));
41-
ASSERT_FALSE(my_streq(LIBC_NAMESPACE::getenv("FRANCE"), "Berlin"));
42-
ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("GERMANY"), "Berlin"));
43-
ASSERT_TRUE(
44-
my_streq(LIBC_NAMESPACE::getenv("FRANC"), static_cast<char *>(nullptr)));
45-
ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("FRANCE1"),
46-
static_cast<char *>(nullptr)));
16+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("") == nullptr);
17+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("=") == nullptr);
18+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE") == nullptr);
19+
ASSERT_FALSE(LIBC_NAMESPACE::getenv("PATH") == nullptr);
20+
ASSERT_EQ(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Paris"),
21+
0);
22+
ASSERT_NE(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Berlin"),
23+
0);
24+
ASSERT_EQ(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("GERMANY"), "Berlin"),
25+
0);
26+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANC") == nullptr);
27+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANCE1") == nullptr);
4728

4829
return 0;
4930
}

0 commit comments

Comments
 (0)