• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 exists(file_status s) noexcept
15 // bool exists(path const& p);
16 // bool exists(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 
26 using namespace std::experimental::filesystem;
27 
28 TEST_SUITE(exists_test_suite)
29 
TEST_CASE(signature_test)30 TEST_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(exists(s));
36     ASSERT_NOEXCEPT(exists(p, ec));
37     ASSERT_NOT_NOEXCEPT(exists(p));
38 }
39 
TEST_CASE(exists_status_test)40 TEST_CASE(exists_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, true},
50         {file_type::directory, true},
51         {file_type::symlink, true},
52         {file_type::block, true},
53         {file_type::character, true},
54         {file_type::fifo, true},
55         {file_type::socket, true},
56         {file_type::unknown, true}
57     };
58     for (auto& TC : testCases) {
59         file_status s(TC.type);
60         TEST_CHECK(exists(s) == TC.expect);
61     }
62 }
63 
TEST_CASE(test_exist_not_found)64 TEST_CASE(test_exist_not_found)
65 {
66     const path p = StaticEnv::DNE;
67     TEST_CHECK(exists(p) == false);
68 
69     std::error_code ec = GetTestEC();
70     TEST_CHECK(exists(p, ec) == false);
71     TEST_CHECK(!ec);
72 }
73 
TEST_CASE(test_exists_fails)74 TEST_CASE(test_exists_fails)
75 {
76     scoped_test_env env;
77     const path dir = env.create_dir("dir");
78     const path file = env.create_file("dir/file", 42);
79     permissions(dir, perms::none);
80 
81     std::error_code ec;
82     TEST_CHECK(exists(file, ec) == false);
83     TEST_CHECK(ec);
84 
85     TEST_CHECK_THROW(filesystem_error, exists(file));
86 }
87 
TEST_CASE(test_name_too_long)88 TEST_CASE(test_name_too_long) {
89     std::string long_name(2500, 'a');
90     const path file(long_name);
91 
92     std::error_code ec;
93     TEST_CHECK(exists(file, ec) == false);
94     TEST_CHECK(ec);
95 }
96 
97 TEST_SUITE_END()
98