From a63365574db1c8500b2e88856ca5691bd1ff81e1 Mon Sep 17 00:00:00 2001 From: Jeff Bailey Date: Sun, 12 Oct 2025 09:03:04 +0000 Subject: [PATCH 1/3] [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. --- .../integration/src/stdlib/CMakeLists.txt | 1 + .../integration/src/stdlib/getenv_test.cpp | 45 ++++++------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/libc/test/integration/src/stdlib/CMakeLists.txt b/libc/test/integration/src/stdlib/CMakeLists.txt index 1773d9fc9f0f5..caa2a0b96288e 100644 --- a/libc/test/integration/src/stdlib/CMakeLists.txt +++ b/libc/test/integration/src/stdlib/CMakeLists.txt @@ -12,6 +12,7 @@ add_integration_test( getenv_test.cpp DEPENDS libc.src.stdlib.getenv + libc.src.string.strcmp ENV FRANCE=Paris GERMANY=Berlin diff --git a/libc/test/integration/src/stdlib/getenv_test.cpp b/libc/test/integration/src/stdlib/getenv_test.cpp index 72dcea0ddf1f1..b909df94c7fb6 100644 --- a/libc/test/integration/src/stdlib/getenv_test.cpp +++ b/libc/test/integration/src/stdlib/getenv_test.cpp @@ -7,43 +7,24 @@ //===----------------------------------------------------------------------===// #include "src/stdlib/getenv.h" +#include "src/string/strcmp.h" #include "test/IntegrationTest/test.h" -static bool my_streq(const char *lhs, const char *rhs) { - if (lhs == rhs) - return true; - if (((lhs == static_cast(nullptr)) && - (rhs != static_cast(nullptr))) || - ((lhs != static_cast(nullptr)) && - (rhs == static_cast(nullptr)))) { - return false; - } - const char *l, *r; - for (l = lhs, r = rhs; *l != '\0' && *r != '\0'; ++l, ++r) - if (*l != *r) - return false; - - return *l == '\0' && *r == '\0'; -} - TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv, [[maybe_unused]] char **envp) { - ASSERT_TRUE( - my_streq(LIBC_NAMESPACE::getenv(""), static_cast(nullptr))); - ASSERT_TRUE( - my_streq(LIBC_NAMESPACE::getenv("="), static_cast(nullptr))); - ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE"), - static_cast(nullptr))); - ASSERT_FALSE( - my_streq(LIBC_NAMESPACE::getenv("PATH"), static_cast(nullptr))); - ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("FRANCE"), "Paris")); - ASSERT_FALSE(my_streq(LIBC_NAMESPACE::getenv("FRANCE"), "Berlin")); - ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("GERMANY"), "Berlin")); - ASSERT_TRUE( - my_streq(LIBC_NAMESPACE::getenv("FRANC"), static_cast(nullptr))); - ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("FRANCE1"), - static_cast(nullptr))); + ASSERT_TRUE(LIBC_NAMESPACE::getenv("") == nullptr); + ASSERT_TRUE(LIBC_NAMESPACE::getenv("=") == nullptr); + ASSERT_TRUE(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE") == nullptr); + ASSERT_FALSE(LIBC_NAMESPACE::getenv("PATH") == nullptr); + ASSERT_EQ(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Paris"), + 0); + ASSERT_NE(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Berlin"), + 0); + ASSERT_EQ(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("GERMANY"), "Berlin"), + 0); + ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANC") == nullptr); + ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANCE1") == nullptr); return 0; } From faa226feae379d2d209fe1ff893cf0fda6ae0c2e Mon Sep 17 00:00:00 2001 From: Jeff Bailey Date: Thu, 13 Nov 2025 11:59:38 +0000 Subject: [PATCH 2/3] [libc][stdlib] Update getenv_test to use inline_strcmp for string comparison --- libc/test/integration/src/stdlib/CMakeLists.txt | 1 - libc/test/integration/src/stdlib/getenv_test.cpp | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libc/test/integration/src/stdlib/CMakeLists.txt b/libc/test/integration/src/stdlib/CMakeLists.txt index caa2a0b96288e..1773d9fc9f0f5 100644 --- a/libc/test/integration/src/stdlib/CMakeLists.txt +++ b/libc/test/integration/src/stdlib/CMakeLists.txt @@ -12,7 +12,6 @@ add_integration_test( getenv_test.cpp DEPENDS libc.src.stdlib.getenv - libc.src.string.strcmp ENV FRANCE=Paris GERMANY=Berlin diff --git a/libc/test/integration/src/stdlib/getenv_test.cpp b/libc/test/integration/src/stdlib/getenv_test.cpp index b909df94c7fb6..0706fc7ce4f2a 100644 --- a/libc/test/integration/src/stdlib/getenv_test.cpp +++ b/libc/test/integration/src/stdlib/getenv_test.cpp @@ -7,21 +7,22 @@ //===----------------------------------------------------------------------===// #include "src/stdlib/getenv.h" -#include "src/string/strcmp.h" +#include "src/string/memory_utils/inline_strcmp.h" #include "test/IntegrationTest/test.h" TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv, [[maybe_unused]] char **envp) { + auto comp = [](char l, char r) -> int { return l - r; }; ASSERT_TRUE(LIBC_NAMESPACE::getenv("") == nullptr); ASSERT_TRUE(LIBC_NAMESPACE::getenv("=") == nullptr); ASSERT_TRUE(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE") == nullptr); ASSERT_FALSE(LIBC_NAMESPACE::getenv("PATH") == nullptr); - ASSERT_EQ(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Paris"), + ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Paris", comp), 0); - ASSERT_NE(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Berlin"), + ASSERT_NE(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Berlin", comp), 0); - ASSERT_EQ(LIBC_NAMESPACE::strcmp(LIBC_NAMESPACE::getenv("GERMANY"), "Berlin"), + ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("GERMANY"), "Berlin", comp), 0); ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANC") == nullptr); ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANCE1") == nullptr); From 81410472dc85a70d64f8332193c17fb0440a1969 Mon Sep 17 00:00:00 2001 From: Jeff Bailey Date: Thu, 13 Nov 2025 12:07:44 +0000 Subject: [PATCH 3/3] Formatting fixes --- libc/test/integration/src/stdlib/getenv_test.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libc/test/integration/src/stdlib/getenv_test.cpp b/libc/test/integration/src/stdlib/getenv_test.cpp index 0706fc7ce4f2a..49c68f7463ab9 100644 --- a/libc/test/integration/src/stdlib/getenv_test.cpp +++ b/libc/test/integration/src/stdlib/getenv_test.cpp @@ -18,11 +18,14 @@ TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv, ASSERT_TRUE(LIBC_NAMESPACE::getenv("=") == nullptr); ASSERT_TRUE(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE") == nullptr); ASSERT_FALSE(LIBC_NAMESPACE::getenv("PATH") == nullptr); - ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Paris", comp), + ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"), + "Paris", comp), 0); - ASSERT_NE(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"), "Berlin", comp), + ASSERT_NE(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("FRANCE"), + "Berlin", comp), 0); - ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("GERMANY"), "Berlin", comp), + ASSERT_EQ(LIBC_NAMESPACE::inline_strcmp(LIBC_NAMESPACE::getenv("GERMANY"), + "Berlin", comp), 0); ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANC") == nullptr); ASSERT_TRUE(LIBC_NAMESPACE::getenv("FRANCE1") == nullptr);