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 equivalent(path const& lhs, path const& rhs);
15// bool equivalent(path const& lhs, path const& rhs, 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
25using namespace std::experimental::filesystem;
26
27TEST_SUITE(equivalent_test_suite)
28
29TEST_CASE(signature_test)
30{
31    const path p; ((void)p);
32    std::error_code ec; ((void)ec);
33    ASSERT_NOEXCEPT(equivalent(p, p, ec));
34    ASSERT_NOT_NOEXCEPT(equivalent(p, p));
35}
36
37TEST_CASE(equivalent_test)
38{
39    struct TestCase {
40        path lhs;
41        path rhs;
42        bool expect;
43    };
44    const TestCase testCases[] = {
45        {StaticEnv::Dir, StaticEnv::Dir, true},
46        {StaticEnv::File, StaticEnv::Dir, false},
47        {StaticEnv::Dir, StaticEnv::SymlinkToDir, true},
48        {StaticEnv::Dir, StaticEnv::SymlinkToFile, false},
49        {StaticEnv::File, StaticEnv::File, true},
50        {StaticEnv::File, StaticEnv::SymlinkToFile, true},
51    };
52    for (auto& TC : testCases) {
53        std::error_code ec;
54        TEST_CHECK(equivalent(TC.lhs, TC.rhs, ec) == TC.expect);
55        TEST_CHECK(!ec);
56    }
57}
58
59TEST_CASE(equivalent_reports_double_dne)
60{
61    const path E = StaticEnv::File;
62    const path DNE = StaticEnv::DNE;
63    { // Test that no exception is thrown if one of the paths exists
64        TEST_CHECK(equivalent(E, DNE) == false);
65        TEST_CHECK(equivalent(DNE, E) == false);
66    }
67    { // Test that an exception is thrown if both paths do not exist.
68        TEST_CHECK_THROW(filesystem_error, equivalent(DNE, DNE));
69    }
70    {
71        std::error_code ec;
72        TEST_CHECK(equivalent(DNE, DNE, ec) == false);
73        TEST_CHECK(ec);
74    }
75}
76
77TEST_CASE(equivalent_is_other_succeeds)
78{
79    scoped_test_env env;
80    path const file = env.create_file("file", 42);
81    const path hl1 = env.create_hardlink(file, "hl1");
82    const path hl2 = env.create_hardlink(file, "hl2");
83    TEST_CHECK(equivalent(file, hl1));
84    TEST_CHECK(equivalent(file, hl2));
85    TEST_CHECK(equivalent(hl1, hl2));
86}
87
88TEST_SUITE_END()
89