1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // XFAIL: LIBCXX-AIX-FIXME
12 
13 // <filesystem>
14 
15 // bool equivalent(path const& lhs, path const& rhs);
16 // bool equivalent(path const& lhs, path const& rhs, std::error_code& ec) noexcept;
17 
18 #include "filesystem_include.h"
19 #include <type_traits>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "rapid-cxx-test.h"
24 #include "filesystem_test_helper.h"
25 
26 using namespace fs;
27 
28 TEST_SUITE(equivalent_test_suite)
29 
30 TEST_CASE(signature_test) {
31   const path p;
32   ((void)p);
33   std::error_code ec;
34   ((void)ec);
35   ASSERT_NOEXCEPT(equivalent(p, p, ec));
36   ASSERT_NOT_NOEXCEPT(equivalent(p, p));
37 }
38 
39 TEST_CASE(equivalent_test) {
40   static_test_env static_env;
41   struct TestCase {
42     path lhs;
43     path rhs;
44     bool expect;
45   };
46   const TestCase testCases[] = {
47       {static_env.Dir, static_env.Dir, true},
48       {static_env.File, static_env.Dir, false},
49       {static_env.Dir, static_env.SymlinkToDir, true},
50       {static_env.Dir, static_env.SymlinkToFile, false},
51       {static_env.File, static_env.File, true},
52       {static_env.File, static_env.SymlinkToFile, true},
53   };
54   for (auto& TC : testCases) {
55     std::error_code ec;
56     TEST_CHECK(equivalent(TC.lhs, TC.rhs, ec) == TC.expect);
57     TEST_CHECK(!ec);
58   }
59 }
60 
61 TEST_CASE(equivalent_reports_error_if_input_dne) {
62   static_test_env static_env;
63   const path E = static_env.File;
64   const path DNE = static_env.DNE;
65   { // Test that an error is reported when either of the paths don't exist
66     std::error_code ec = GetTestEC();
67     TEST_CHECK(equivalent(E, DNE, ec) == false);
68     TEST_CHECK(ec);
69     TEST_CHECK(ec != GetTestEC());
70   }
71   {
72     std::error_code ec = GetTestEC();
73     TEST_CHECK(equivalent(DNE, E, ec) == false);
74     TEST_CHECK(ec);
75     TEST_CHECK(ec != GetTestEC());
76   }
77   {
78     TEST_CHECK_THROW(filesystem_error, equivalent(DNE, E));
79     TEST_CHECK_THROW(filesystem_error, equivalent(E, DNE));
80   }
81   { // Test that an exception is thrown if both paths do not exist.
82     TEST_CHECK_THROW(filesystem_error, equivalent(DNE, DNE));
83   }
84   {
85     std::error_code ec = GetTestEC();
86     TEST_CHECK(equivalent(DNE, DNE, ec) == false);
87     TEST_CHECK(ec);
88     TEST_CHECK(ec != GetTestEC());
89   }
90 }
91 
92 TEST_CASE(equivalent_hardlink_succeeds) {
93   scoped_test_env env;
94   path const file = env.create_file("file", 42);
95   const path hl1 = env.create_hardlink(file, "hl1");
96   const path hl2 = env.create_hardlink(file, "hl2");
97   TEST_CHECK(equivalent(file, hl1));
98   TEST_CHECK(equivalent(file, hl2));
99   TEST_CHECK(equivalent(hl1, hl2));
100 }
101 
102 #ifndef _WIN32
103 TEST_CASE(equivalent_is_other_succeeds) {
104   scoped_test_env env;
105   path const file = env.create_file("file", 42);
106   const path fifo1 = env.create_fifo("fifo1");
107   const path fifo2 = env.create_fifo("fifo2");
108   // Required to test behavior for inputs where is_other(p) is true.
109   TEST_REQUIRE(is_other(fifo1));
110   TEST_CHECK(!equivalent(file, fifo1));
111   TEST_CHECK(!equivalent(fifo2, file));
112   TEST_CHECK(!equivalent(fifo1, fifo2));
113   TEST_CHECK(equivalent(fifo1, fifo1));
114 }
115 #endif
116 
117 TEST_SUITE_END()
118