Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/test/integration/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_integration_test(
getenv_test.cpp
DEPENDS
libc.src.stdlib.getenv
libc.src.string.strcmp
ENV
FRANCE=Paris
GERMANY=Berlin
Expand Down
45 changes: 13 additions & 32 deletions libc/test/integration/src/stdlib/getenv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(nullptr)) &&
(rhs != static_cast<char *>(nullptr))) ||
((lhs != static_cast<char *>(nullptr)) &&
(rhs == static_cast<char *>(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<char *>(nullptr)));
ASSERT_TRUE(
my_streq(LIBC_NAMESPACE::getenv("="), static_cast<char *>(nullptr)));
ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("MISSING ENV VARIABLE"),
static_cast<char *>(nullptr)));
ASSERT_FALSE(
my_streq(LIBC_NAMESPACE::getenv("PATH"), static_cast<char *>(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<char *>(nullptr)));
ASSERT_TRUE(my_streq(LIBC_NAMESPACE::getenv("FRANCE1"),
static_cast<char *>(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;
}
Loading