libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

assign.pass.cpp (3539B)


      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 // <filesystem>
     13 
     14 // class directory_entry
     15 
     16 // directory_entry& operator=(directory_entry const&) = default;
     17 // directory_entry& operator=(directory_entry&&) noexcept = default;
     18 // void assign(path const&);
     19 // void replace_filename(path const&);
     20 
     21 #include "filesystem_include.hpp"
     22 #include <type_traits>
     23 #include <cassert>
     24 
     25 #include "test_macros.h"
     26 #include "rapid-cxx-test.hpp"
     27 #include "filesystem_test_helper.hpp"
     28 
     29 TEST_SUITE(directory_entry_mods_suite)
     30 
     31 TEST_CASE(test_path_assign_method) {
     32   using namespace fs;
     33   const path p("foo/bar/baz");
     34   const path p2("abc");
     35   directory_entry e(p);
     36   {
     37     static_assert(std::is_same<decltype(e.assign(p)), void>::value,
     38                   "return type should be void");
     39     static_assert(noexcept(e.assign(p)) == false,
     40                   "operation must not be noexcept");
     41   }
     42   {
     43     TEST_CHECK(e.path() == p);
     44     e.assign(p2);
     45     TEST_CHECK(e.path() == p2 && e.path() != p);
     46     e.assign(p);
     47     TEST_CHECK(e.path() == p && e.path() != p2);
     48   }
     49 }
     50 
     51 TEST_CASE(test_path_assign_ec_method) {
     52   using namespace fs;
     53   const path p("foo/bar/baz");
     54   const path p2("abc");
     55   {
     56     std::error_code ec;
     57     directory_entry e(p);
     58     static_assert(std::is_same<decltype(e.assign(p, ec)), void>::value,
     59                   "return type should be void");
     60     static_assert(noexcept(e.assign(p, ec)) == false,
     61                   "operation must not be noexcept");
     62   }
     63   {
     64     directory_entry ent(p);
     65     std::error_code ec = GetTestEC();
     66     ent.assign(p2, ec);
     67     TEST_CHECK(ErrorIs(ec, std::errc::no_such_file_or_directory));
     68     TEST_CHECK(ent.path() == p2);
     69   }
     70 }
     71 
     72 TEST_CASE(test_assign_calls_refresh) {
     73   using namespace fs;
     74   scoped_test_env env;
     75   const path dir = env.create_dir("dir");
     76   const path file = env.create_file("dir/file", 42);
     77   const path sym = env.create_symlink("dir/file", "sym");
     78 
     79   {
     80     directory_entry ent;
     81     ent.assign(file);
     82 
     83     // removing the file demonstrates that the values where cached previously.
     84     LIBCPP_ONLY(remove(file));
     85 
     86     TEST_CHECK(ent.is_regular_file());
     87   }
     88   env.create_file("dir/file", 101);
     89   {
     90     directory_entry ent;
     91     ent.assign(sym);
     92 
     93     LIBCPP_ONLY(remove(file));
     94     LIBCPP_ONLY(remove(sym));
     95 
     96     TEST_CHECK(ent.is_symlink());
     97     TEST_CHECK(ent.is_regular_file());
     98   }
     99 }
    100 
    101 TEST_CASE(test_assign_propagates_error) {
    102   using namespace fs;
    103   scoped_test_env env;
    104   const path dir = env.create_dir("dir");
    105   const path file = env.create_file("dir/file", 42);
    106   const path sym_out_of_dir = env.create_symlink("dir/file", "sym");
    107   const path file_out_of_dir = env.create_file("file1");
    108   const path sym_in_dir = env.create_symlink("file1", "dir/sym1");
    109 
    110   permissions(dir, perms::none);
    111 
    112   {
    113     directory_entry ent;
    114     std::error_code ec = GetTestEC();
    115     ent.assign(file, ec);
    116     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
    117   }
    118   {
    119     directory_entry ent;
    120     std::error_code ec = GetTestEC();
    121     ent.assign(sym_in_dir, ec);
    122     TEST_CHECK(ErrorIs(ec, std::errc::permission_denied));
    123   }
    124   {
    125     directory_entry ent;
    126     std::error_code ec = GetTestEC();
    127     ent.assign(sym_out_of_dir, ec);
    128     TEST_CHECK(!ec);
    129   }
    130 }
    131 
    132 TEST_SUITE_END()