1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// UNSUPPORTED: c++98, c++03
11
12// <experimental/filesystem>
13
14// bool is_empty(path const& p);
15// bool is_empty(path const& p, std::error_code& ec);
16
17#include <experimental/filesystem>
18#include <type_traits>
19#include <cassert>
20
21#include "test_macros.h"
22#include "rapid-cxx-test.hpp"
23#include "filesystem_test_helper.hpp"
24
25using namespace std::experimental::filesystem;
26
27TEST_SUITE(is_empty_test_suite)
28
29TEST_CASE(signature_test)
30{
31    const path p; ((void)p);
32    std::error_code ec; ((void)ec);
33    ASSERT_NOT_NOEXCEPT(is_empty(p, ec));
34    ASSERT_NOT_NOEXCEPT(is_empty(p));
35}
36
37TEST_CASE(test_exist_not_found)
38{
39    const path p = StaticEnv::DNE;
40    std::error_code ec;
41    TEST_CHECK(is_empty(p, ec) == false);
42    TEST_CHECK(ec);
43    TEST_CHECK_THROW(filesystem_error, is_empty(p));
44}
45
46TEST_CASE(test_is_empty_directory)
47{
48    TEST_CHECK(!is_empty(StaticEnv::Dir));
49    TEST_CHECK(!is_empty(StaticEnv::SymlinkToDir));
50}
51
52TEST_CASE(test_is_empty_directory_dynamic)
53{
54    scoped_test_env env;
55    TEST_CHECK(is_empty(env.test_root));
56    env.create_file("foo", 42);
57    TEST_CHECK(!is_empty(env.test_root));
58}
59
60TEST_CASE(test_is_empty_file)
61{
62    TEST_CHECK(is_empty(StaticEnv::EmptyFile));
63    TEST_CHECK(!is_empty(StaticEnv::NonEmptyFile));
64}
65
66TEST_CASE(test_is_empty_fails)
67{
68    scoped_test_env env;
69    const path dir = env.create_dir("dir");
70    const path dir2 = env.create_dir("dir/dir2");
71    permissions(dir, perms::none);
72
73    std::error_code ec;
74    TEST_CHECK(is_empty(dir2, ec) == false);
75    TEST_CHECK(ec);
76
77    TEST_CHECK_THROW(filesystem_error, is_empty(dir2));
78}
79
80TEST_CASE(test_directory_access_denied)
81{
82    scoped_test_env env;
83    const path dir = env.create_dir("dir");
84    const path file1 = env.create_file("dir/file", 42);
85    permissions(dir, perms::none);
86
87    std::error_code ec = GetTestEC();
88    TEST_CHECK(is_empty(dir, ec) == false);
89    TEST_CHECK(ec);
90    TEST_CHECK(ec != GetTestEC());
91
92    TEST_CHECK_THROW(filesystem_error, is_empty(dir));
93}
94
95
96TEST_CASE(test_fifo_fails)
97{
98    scoped_test_env env;
99    const path fifo = env.create_fifo("fifo");
100
101    std::error_code ec = GetTestEC();
102    TEST_CHECK(is_empty(fifo, ec) == false);
103    TEST_CHECK(ec);
104    TEST_CHECK(ec != GetTestEC());
105
106    TEST_CHECK_THROW(filesystem_error, is_empty(fifo));
107}
108
109TEST_SUITE_END()
110