Home | History | Annotate | Download | only in fs.op.equivalent
      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 
     25 using namespace std::experimental::filesystem;
     26 
     27 TEST_SUITE(equivalent_test_suite)
     28 
     29 TEST_CASE(signature_test) {
     30   const path p;
     31   ((void)p);
     32   std::error_code ec;
     33   ((void)ec);
     34   ASSERT_NOEXCEPT(equivalent(p, p, ec));
     35   ASSERT_NOT_NOEXCEPT(equivalent(p, p));
     36 }
     37 
     38 TEST_CASE(equivalent_test) {
     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 
     59 TEST_CASE(equivalent_reports_error_if_input_dne) {
     60   const path E = StaticEnv::File;
     61   const path DNE = StaticEnv::DNE;
     62   { // Test that an error is reported when either of the paths don't exist
     63     std::error_code ec = GetTestEC();
     64     TEST_CHECK(equivalent(E, DNE, ec) == false);
     65     TEST_CHECK(ec);
     66     TEST_CHECK(ec != GetTestEC());
     67   }
     68   {
     69     std::error_code ec = GetTestEC();
     70     TEST_CHECK(equivalent(DNE, E, ec) == false);
     71     TEST_CHECK(ec);
     72     TEST_CHECK(ec != GetTestEC());
     73   }
     74   {
     75     TEST_CHECK_THROW(filesystem_error, equivalent(DNE, E));
     76     TEST_CHECK_THROW(filesystem_error, equivalent(E, DNE));
     77   }
     78   { // Test that an exception is thrown if both paths do not exist.
     79     TEST_CHECK_THROW(filesystem_error, equivalent(DNE, DNE));
     80   }
     81   {
     82     std::error_code ec = GetTestEC();
     83     TEST_CHECK(equivalent(DNE, DNE, ec) == false);
     84     TEST_CHECK(ec);
     85     TEST_CHECK(ec != GetTestEC());
     86   }
     87 }
     88 
     89 TEST_CASE(equivalent_hardlink_succeeds) {
     90   scoped_test_env env;
     91   path const file = env.create_file("file", 42);
     92   const path hl1 = env.create_hardlink(file, "hl1");
     93   const path hl2 = env.create_hardlink(file, "hl2");
     94   TEST_CHECK(equivalent(file, hl1));
     95   TEST_CHECK(equivalent(file, hl2));
     96   TEST_CHECK(equivalent(hl1, hl2));
     97 }
     98 
     99 TEST_CASE(equivalent_is_other_succeeds) {
    100   scoped_test_env env;
    101   path const file = env.create_file("file", 42);
    102   const path fifo1 = env.create_fifo("fifo1");
    103   const path fifo2 = env.create_fifo("fifo2");
    104   // Required to test behavior for inputs where is_other(p) is true.
    105   TEST_REQUIRE(is_other(fifo1));
    106   TEST_CHECK(!equivalent(file, fifo1));
    107   TEST_CHECK(!equivalent(fifo2, file));
    108   TEST_CHECK(!equivalent(fifo1, fifo2));
    109   TEST_CHECK(equivalent(fifo1, fifo1));
    110 }
    111 
    112 TEST_SUITE_END()
    113