aboutsummaryrefslogtreecommitdiff
path: root/test/std/experimental/filesystem/fs.op.funcs/fs.op.hard_lk_ct/hard_link_count.pass.cpp
blob: 6b542a5b67a58a666b2e2a1a6d5d057119eb966b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <experimental/filesystem>

// uintmax_t hard_link_count(const path& p);
// uintmax_t hard_link_count(const path& p, std::error_code& ec) noexcept;

#include <experimental/filesystem>
#include <type_traits>
#include <cassert>

#include "test_macros.h"
#include "rapid-cxx-test.hpp"
#include "filesystem_test_helper.hpp"

using namespace std::experimental::filesystem;

TEST_SUITE(hard_link_count_test_suite)

TEST_CASE(signature_test)
{
    const path p; ((void)p);
    std::error_code ec; ((void)ec);
    ASSERT_SAME_TYPE(decltype(hard_link_count(p)), uintmax_t);
    ASSERT_SAME_TYPE(decltype(hard_link_count(p, ec)), uintmax_t);
    ASSERT_NOT_NOEXCEPT(hard_link_count(p));
    ASSERT_NOEXCEPT(hard_link_count(p, ec));
}

TEST_CASE(hard_link_count_for_file)
{
    TEST_CHECK(hard_link_count(StaticEnv::File) == 1);
    std::error_code ec;
    TEST_CHECK(hard_link_count(StaticEnv::File, ec) == 1);
}

TEST_CASE(hard_link_count_for_directory)
{
    uintmax_t DirExpect = 3; // hard link from . .. and Dir2
    uintmax_t Dir3Expect = 2; // hard link from . ..
    uintmax_t DirExpectAlt = DirExpect;
    uintmax_t Dir3ExpectAlt = Dir3Expect;
#if defined(__APPLE__)
    // Filesystems formatted with case sensitive hfs+ behave unixish as
    // expected. Normal hfs+ filesystems report the number of directory
    // entries instead.
    DirExpectAlt = 5; // .  ..  Dir2  file1  file2
    Dir3Expect = 3; // .  ..  file5
#endif
    TEST_CHECK(hard_link_count(StaticEnv::Dir) == DirExpect ||
               hard_link_count(StaticEnv::Dir) == DirExpectAlt ||
               hard_link_count(StaticEnv::Dir) == 1);
    TEST_CHECK(hard_link_count(StaticEnv::Dir3) == Dir3Expect ||
               hard_link_count(StaticEnv::Dir3) == Dir3ExpectAlt ||
               hard_link_count(StaticEnv::Dir3) == 1);

    std::error_code ec;
    TEST_CHECK(hard_link_count(StaticEnv::Dir, ec) == DirExpect ||
               hard_link_count(StaticEnv::Dir, ec) == DirExpectAlt ||
               hard_link_count(StaticEnv::Dir) == 1);
    TEST_CHECK(hard_link_count(StaticEnv::Dir3, ec) == Dir3Expect ||
               hard_link_count(StaticEnv::Dir3, ec) == Dir3ExpectAlt ||
               hard_link_count(StaticEnv::Dir3) == 1);
}
TEST_CASE(hard_link_count_increments_test)
{
    scoped_test_env env;
    const path file = env.create_file("file", 42);
    TEST_CHECK(hard_link_count(file) == 1);

    env.create_hardlink(file, "file_hl");
    TEST_CHECK(hard_link_count(file) == 2);
}


TEST_CASE(hard_link_count_error_cases)
{
    const path testCases[] = {
        StaticEnv::BadSymlink,
        StaticEnv::DNE
    };
    const uintmax_t expect = static_cast<uintmax_t>(-1);
    for (auto& TC : testCases) {
        std::error_code ec;
        TEST_CHECK(hard_link_count(TC, ec) == expect);
        TEST_CHECK(ec);
    }
}

TEST_SUITE_END()