Home | History | Annotate | Download | only in fs.op.hard_lk_ct
      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 // uintmax_t hard_link_count(const path& p);
     15 // uintmax_t hard_link_count(const path& p, std::error_code& ec) noexcept;
     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 
     25 using namespace std::experimental::filesystem;
     26 
     27 TEST_SUITE(hard_link_count_test_suite)
     28 
     29 TEST_CASE(signature_test)
     30 {
     31     const path p; ((void)p);
     32     std::error_code ec; ((void)ec);
     33     ASSERT_SAME_TYPE(decltype(hard_link_count(p)), uintmax_t);
     34     ASSERT_SAME_TYPE(decltype(hard_link_count(p, ec)), uintmax_t);
     35     ASSERT_NOT_NOEXCEPT(hard_link_count(p));
     36     ASSERT_NOEXCEPT(hard_link_count(p, ec));
     37 }
     38 
     39 TEST_CASE(hard_link_count_for_file)
     40 {
     41     TEST_CHECK(hard_link_count(StaticEnv::File) == 1);
     42     std::error_code ec;
     43     TEST_CHECK(hard_link_count(StaticEnv::File, ec) == 1);
     44 }
     45 
     46 TEST_CASE(hard_link_count_for_directory)
     47 {
     48     uintmax_t DirExpect = 3; // hard link from . .. and Dir2
     49     uintmax_t Dir3Expect = 2; // hard link from . ..
     50     uintmax_t DirExpectAlt = DirExpect;
     51     uintmax_t Dir3ExpectAlt = Dir3Expect;
     52 #if defined(__APPLE__)
     53     // Filesystems formatted with case sensitive hfs+ behave unixish as
     54     // expected. Normal hfs+ filesystems report the number of directory
     55     // entries instead.
     56     DirExpectAlt = 5; // .  ..  Dir2  file1  file2
     57     Dir3Expect = 3; // .  ..  file5
     58 #endif
     59     TEST_CHECK(hard_link_count(StaticEnv::Dir) == DirExpect ||
     60                hard_link_count(StaticEnv::Dir) == DirExpectAlt ||
     61                hard_link_count(StaticEnv::Dir) == 1);
     62     TEST_CHECK(hard_link_count(StaticEnv::Dir3) == Dir3Expect ||
     63                hard_link_count(StaticEnv::Dir3) == Dir3ExpectAlt ||
     64                hard_link_count(StaticEnv::Dir3) == 1);
     65 
     66     std::error_code ec;
     67     TEST_CHECK(hard_link_count(StaticEnv::Dir, ec) == DirExpect ||
     68                hard_link_count(StaticEnv::Dir, ec) == DirExpectAlt ||
     69                hard_link_count(StaticEnv::Dir) == 1);
     70     TEST_CHECK(hard_link_count(StaticEnv::Dir3, ec) == Dir3Expect ||
     71                hard_link_count(StaticEnv::Dir3, ec) == Dir3ExpectAlt ||
     72                hard_link_count(StaticEnv::Dir3) == 1);
     73 }
     74 TEST_CASE(hard_link_count_increments_test)
     75 {
     76     scoped_test_env env;
     77     const path file = env.create_file("file", 42);
     78     TEST_CHECK(hard_link_count(file) == 1);
     79 
     80     env.create_hardlink(file, "file_hl");
     81     TEST_CHECK(hard_link_count(file) == 2);
     82 }
     83 
     84 
     85 TEST_CASE(hard_link_count_error_cases)
     86 {
     87     const path testCases[] = {
     88         StaticEnv::BadSymlink,
     89         StaticEnv::DNE
     90     };
     91     const uintmax_t expect = static_cast<uintmax_t>(-1);
     92     for (auto& TC : testCases) {
     93         std::error_code ec;
     94         TEST_CHECK(hard_link_count(TC, ec) == expect);
     95         TEST_CHECK(ec);
     96     }
     97 }
     98 
     99 TEST_SUITE_END()
    100