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_directory(file_status s) noexcept
15// bool is_directory(path const& p);
16// bool is_directory(path const& p, std::error_code& ec) noexcept;
17
18#include <experimental/filesystem>
19#include <type_traits>
20#include <cassert>
21
22#include "test_macros.h"
23#include "rapid-cxx-test.hpp"
24#include "filesystem_test_helper.hpp"
25
26using namespace std::experimental::filesystem;
27
28TEST_SUITE(is_directory_test_suite)
29
30TEST_CASE(signature_test)
31{
32    file_status s; ((void)s);
33    const path p; ((void)p);
34    std::error_code ec; ((void)ec);
35    ASSERT_NOEXCEPT(is_directory(s));
36    ASSERT_NOEXCEPT(is_directory(p, ec));
37    ASSERT_NOT_NOEXCEPT(is_directory(p));
38}
39
40TEST_CASE(is_directory_status_test)
41{
42    struct TestCase {
43        file_type type;
44        bool expect;
45    };
46    const TestCase testCases[] = {
47        {file_type::none, false},
48        {file_type::not_found, false},
49        {file_type::regular, false},
50        {file_type::directory, true},
51        {file_type::symlink, false},
52        {file_type::block, false},
53        {file_type::character, false},
54        {file_type::fifo, false},
55        {file_type::socket, false},
56        {file_type::unknown, false}
57    };
58    for (auto& TC : testCases) {
59        file_status s(TC.type);
60        TEST_CHECK(is_directory(s) == TC.expect);
61    }
62}
63
64TEST_CASE(test_exist_not_found)
65{
66    const path p = StaticEnv::DNE;
67    TEST_CHECK(is_directory(p) == false);
68}
69
70TEST_CASE(static_env_test)
71{
72    TEST_CHECK(is_directory(StaticEnv::Dir));
73    TEST_CHECK(is_directory(StaticEnv::SymlinkToDir));
74    TEST_CHECK(!is_directory(StaticEnv::File));
75}
76
77TEST_CASE(test_is_directory_fails)
78{
79    scoped_test_env env;
80    const path dir = env.create_dir("dir");
81    const path dir2 = env.create_dir("dir/dir2");
82    permissions(dir, perms::none);
83
84    std::error_code ec;
85    TEST_CHECK(is_directory(dir2, ec) == false);
86    TEST_CHECK(ec);
87
88    TEST_CHECK_THROW(filesystem_error, is_directory(dir2));
89}
90
91TEST_SUITE_END()
92