xref: /llvm-project/libc/test/integration/src/stdlib/getenv_test.cpp (revision a5cb6edb475bd41a3e3ced3fa9323096535c2da9)
1 //===-- Unittests for getenv ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdlib/getenv.h"
10 
11 #include "utils/IntegrationTest/test.h"
12 
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 
30 int main(int argc, char **argv, char **envp) {
31   ASSERT_TRUE(my_streq(__llvm_libc::getenv(""), static_cast<char *>(nullptr)));
32   ASSERT_TRUE(my_streq(__llvm_libc::getenv("="), static_cast<char *>(nullptr)));
33   ASSERT_TRUE(my_streq(__llvm_libc::getenv("MISSING ENV VARIABLE"),
34                        static_cast<char *>(nullptr)));
35   ASSERT_FALSE(
36       my_streq(__llvm_libc::getenv("PATH"), static_cast<char *>(nullptr)));
37   ASSERT_TRUE(my_streq(__llvm_libc::getenv("FRANCE"), "Paris"));
38   ASSERT_FALSE(my_streq(__llvm_libc::getenv("FRANCE"), "Berlin"));
39   ASSERT_TRUE(my_streq(__llvm_libc::getenv("GERMANY"), "Berlin"));
40   ASSERT_TRUE(
41       my_streq(__llvm_libc::getenv("FRANC"), static_cast<char *>(nullptr)));
42   ASSERT_TRUE(
43       my_streq(__llvm_libc::getenv("FRANCE1"), static_cast<char *>(nullptr)));
44 
45   return 0;
46 }
47