Skip to content

Commit 56eef98

Browse files
authored
[libc][stdlib] Simplify getenv_test by using strcmp instead of custom helper (#163055)
[libc][stdlib] Simplify getenv_test by using inline_strcmp instead of custom helper Replace the custom `my_streq` helper function with LLVM libc's `inline_strcmp` utility from `src/string/memory_utils/inline_strcmp.h`. Changes: - Remove 18-line custom `my_streq` implementation - Use `inline_strcmp` with a simple comparator lambda for string comparisons - Replace `my_streq(..., nullptr)` checks with direct `== nullptr` comparisons - Maintain identical test coverage while reducing code duplication Benefits: - Uses existing, well-tested LLVM libc infrastructure - Clearer test assertions with standard comparison functions - More concise code (reduced from ~48 to ~33 lines) - Consistent with LLVM libc coding practices The test continues to verify: - Empty string handling - Invalid name handling ('=') - Missing environment variable queries - Correct retrieval of existing variables (FRANCE, GERMANY, PATH) - Partial name matching behavior (FRANC vs FRANCE, FRANCE1 vs FRANCE)
1 parent f969c86 commit 56eef98

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

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

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

99
#include "src/stdlib/getenv.h"
10+
#include "src/string/memory_utils/inline_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+
auto comp = [](char l, char r) -> int { return l - r; };
17+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("") == nullptr);
18+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("=") == nullptr);
19+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE") == nullptr);
20+
ASSERT_FALSE(LIBC_NAMESPACE::getenv("PATH") == nullptr);
21+
ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"),
22+
"Paris", comp),
23+
0);
24+
ASSERT_NE(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"),
25+
"Berlin", comp),
26+
0);
27+
ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("GERMANY"),
28+
"Berlin", comp),
29+
0);
30+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANC") == nullptr);
31+
ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANCE1") == nullptr);
4732

4833
return 0;
4934
}

0 commit comments

Comments
 (0)