1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12 // UNSUPPORTED: availability-filesystem-missing
14 // Starting in Android N (API 24), SELinux policy prevents the shell user from
15 // creating a FIFO file.
16 // XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{21|22|23}}
20 // bool is_empty(path const& p);
21 // bool is_empty(path const& p, std::error_code& ec);
24 #include <type_traits>
27 #include "assert_macros.h"
28 #include "test_macros.h"
29 #include "filesystem_test_helper.h"
30 namespace fs
= std::filesystem
;
33 static void signature_test()
35 const path p
; ((void)p
);
36 std::error_code ec
; ((void)ec
);
37 ASSERT_NOT_NOEXCEPT(is_empty(p
, ec
));
38 ASSERT_NOT_NOEXCEPT(is_empty(p
));
41 static void test_exist_not_found()
43 static_test_env static_env
;
44 const path p
= static_env
.DNE
;
46 assert(is_empty(p
, ec
) == false);
48 TEST_THROWS_TYPE(filesystem_error
, is_empty(p
));
51 static void test_is_empty_directory()
53 static_test_env static_env
;
54 assert(!is_empty(static_env
.Dir
));
55 assert(!is_empty(static_env
.SymlinkToDir
));
58 static void test_is_empty_directory_dynamic()
61 assert(is_empty(env
.test_root
));
62 env
.create_file("foo", 42);
63 assert(!is_empty(env
.test_root
));
66 static void test_is_empty_file()
68 static_test_env static_env
;
69 assert(is_empty(static_env
.EmptyFile
));
70 assert(!is_empty(static_env
.NonEmptyFile
));
73 static void test_is_empty_fails()
77 // Windows doesn't support setting perms::none to trigger failures
78 // reading directories; test using a special inaccessible directory
80 const path p
= GetWindowsInaccessibleDir();
84 const path dir
= env
.create_dir("dir");
85 const path p
= env
.create_dir("dir/dir2");
86 permissions(dir
, perms::none
);
90 assert(is_empty(p
, ec
) == false);
93 TEST_THROWS_TYPE(filesystem_error
, is_empty(p
));
96 static void test_directory_access_denied()
100 // Windows doesn't support setting perms::none to trigger failures
101 // reading directories; test using a special inaccessible directory
103 const path dir
= GetWindowsInaccessibleDir();
107 const path dir
= env
.create_dir("dir");
108 const path file1
= env
.create_file("dir/file", 42);
109 permissions(dir
, perms::none
);
112 std::error_code ec
= GetTestEC();
113 assert(is_empty(dir
, ec
) == false);
115 assert(ec
!= GetTestEC());
117 TEST_THROWS_TYPE(filesystem_error
, is_empty(dir
));
122 static void test_fifo_fails()
125 const path fifo
= env
.create_fifo("fifo");
127 std::error_code ec
= GetTestEC();
128 assert(is_empty(fifo
, ec
) == false);
130 assert(ec
!= GetTestEC());
132 TEST_THROWS_TYPE(filesystem_error
, is_empty(fifo
));
136 int main(int, char**) {
138 test_exist_not_found();
139 test_is_empty_directory();
140 test_is_empty_directory_dynamic();
141 test_is_empty_file();
142 test_is_empty_fails();
143 test_directory_access_denied();