xref: /aosp_15_r20/external/llvm-libc/test/integration/src/unistd/getcwd_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for getcwd ----------------------------------------------===//
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/__support/CPP/string_view.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/stdlib/getenv.h"
12 #include "src/unistd/getcwd.h"
13 
14 #include "test/IntegrationTest/test.h"
15 
16 #include <stdlib.h> // For malloc and free
17 
18 using LIBC_NAMESPACE::cpp::string_view;
19 
TEST_MAIN(int argc,char ** argv,char ** envp)20 TEST_MAIN(int argc, char **argv, char **envp) {
21   char buffer[1024];
22   ASSERT_TRUE(string_view(LIBC_NAMESPACE::getenv("PWD")) ==
23               LIBC_NAMESPACE::getcwd(buffer, 1024));
24 
25   // nullptr buffer
26   char *cwd = LIBC_NAMESPACE::getcwd(nullptr, 0);
27   ASSERT_TRUE(string_view(LIBC_NAMESPACE::getenv("PWD")) == cwd);
28   free(cwd);
29 
30   // Bad size
31   cwd = LIBC_NAMESPACE::getcwd(buffer, 0);
32   ASSERT_TRUE(cwd == nullptr);
33   ASSERT_ERRNO_EQ(EINVAL);
34   LIBC_NAMESPACE::libc_errno = 0;
35 
36   // Insufficient size
37   cwd = LIBC_NAMESPACE::getcwd(buffer, 2);
38   ASSERT_TRUE(cwd == nullptr);
39   int err = LIBC_NAMESPACE::libc_errno;
40   ASSERT_EQ(err, ERANGE);
41 
42   return 0;
43 }
44