rename.pass.cpp (3747B)
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 // void rename(const path& old_p, const path& new_p); 15 // void rename(const path& old_p, const path& new_p, error_code& ec) noexcept; 16 17 #include "filesystem_include.hpp" 18 19 #include "test_macros.h" 20 #include "rapid-cxx-test.hpp" 21 #include "filesystem_test_helper.hpp" 22 23 using namespace fs; 24 25 TEST_SUITE(filesystem_rename_test_suite) 26 27 TEST_CASE(test_signatures) 28 { 29 const path p; ((void)p); 30 std::error_code ec; ((void)ec); 31 ASSERT_SAME_TYPE(decltype(fs::rename(p, p)), void); 32 ASSERT_SAME_TYPE(decltype(fs::rename(p, p, ec)), void); 33 34 ASSERT_NOT_NOEXCEPT(fs::rename(p, p)); 35 ASSERT_NOEXCEPT(fs::rename(p, p, ec)); 36 } 37 38 TEST_CASE(test_error_reporting) 39 { 40 auto checkThrow = [](path const& f, path const& t, const std::error_code& ec) 41 { 42 #ifndef TEST_HAS_NO_EXCEPTIONS 43 try { 44 fs::rename(f, t); 45 return false; 46 } catch (filesystem_error const& err) { 47 return err.path1() == f 48 && err.path2() == t 49 && err.code() == ec; 50 } 51 #else 52 ((void)f); ((void)t); ((void)ec); 53 return true; 54 #endif 55 }; 56 scoped_test_env env; 57 const path dne = env.make_env_path("dne"); 58 const path file = env.create_file("file1", 42); 59 const path dir = env.create_dir("dir1"); 60 struct TestCase { 61 path from; 62 path to; 63 } cases[] = { 64 {dne, dne}, 65 {file, dir}, 66 {dir, file} 67 }; 68 for (auto& TC : cases) { 69 auto from_before = status(TC.from); 70 auto to_before = status(TC.to); 71 std::error_code ec; 72 rename(TC.from, TC.to, ec); 73 TEST_REQUIRE(ec); 74 TEST_CHECK(from_before.type() == status(TC.from).type()); 75 TEST_CHECK(to_before.type() == status(TC.to).type()); 76 TEST_CHECK(checkThrow(TC.from, TC.to, ec)); 77 } 78 } 79 80 TEST_CASE(basic_rename_test) 81 { 82 scoped_test_env env; 83 84 const std::error_code set_ec = std::make_error_code(std::errc::address_in_use); 85 const path file = env.create_file("file1", 42); 86 { // same file 87 std::error_code ec = set_ec; 88 rename(file, file, ec); 89 TEST_CHECK(!ec); 90 TEST_CHECK(is_regular_file(file)); 91 TEST_CHECK(file_size(file) == 42); 92 } 93 const path sym = env.create_symlink(file, "sym"); 94 { // file -> symlink 95 std::error_code ec = set_ec; 96 rename(file, sym, ec); 97 TEST_CHECK(!ec); 98 TEST_CHECK(!exists(file)); 99 TEST_CHECK(is_regular_file(symlink_status(sym))); 100 TEST_CHECK(file_size(sym) == 42); 101 } 102 const path file2 = env.create_file("file2", 42); 103 const path file3 = env.create_file("file3", 100); 104 { // file -> file 105 std::error_code ec = set_ec; 106 rename(file2, file3, ec); 107 TEST_CHECK(!ec); 108 TEST_CHECK(!exists(file2)); 109 TEST_CHECK(is_regular_file(file3)); 110 TEST_CHECK(file_size(file3) == 42); 111 } 112 const path dne = env.make_env_path("dne"); 113 const path bad_sym = env.create_symlink(dne, "bad_sym"); 114 const path bad_sym_dest = env.make_env_path("bad_sym2"); 115 { // bad-symlink 116 std::error_code ec = set_ec; 117 rename(bad_sym, bad_sym_dest, ec); 118 TEST_CHECK(!ec); 119 TEST_CHECK(!exists(symlink_status(bad_sym))); 120 TEST_CHECK(is_symlink(bad_sym_dest)); 121 TEST_CHECK(read_symlink(bad_sym_dest) == dne); 122 } 123 } 124 125 TEST_SUITE_END()