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 // void resize_file(const path& p, uintmax_t new_size); 15 // void resize_file(const path& p, uintmax_t new_size, error_code& ec) noexcept; 16 17 #include <experimental/filesystem> 18 19 #include "test_macros.h" 20 #include "rapid-cxx-test.hpp" 21 #include "filesystem_test_helper.hpp" 22 23 using namespace std::experimental::filesystem; 24 namespace fs = std::experimental::filesystem; 25 26 TEST_SUITE(filesystem_resize_file_test_suite) 27 28 TEST_CASE(test_signatures) 29 { 30 const path p; ((void)p); 31 std::uintmax_t i; ((void)i); 32 std::error_code ec; ((void)ec); 33 34 ASSERT_SAME_TYPE(decltype(fs::resize_file(p, i)), void); 35 ASSERT_SAME_TYPE(decltype(fs::resize_file(p, i, ec)), void); 36 37 ASSERT_NOT_NOEXCEPT(fs::resize_file(p, i)); 38 ASSERT_NOEXCEPT(fs::resize_file(p, i, ec)); 39 } 40 41 TEST_CASE(test_error_reporting) 42 { 43 auto checkThrow = [](path const& f, std::uintmax_t s, const std::error_code& ec) 44 { 45 #ifndef TEST_HAS_NO_EXCEPTIONS 46 try { 47 fs::resize_file(f, s); 48 return false; 49 } catch (filesystem_error const& err) { 50 return err.path1() == f 51 && err.path2() == "" 52 && err.code() == ec; 53 } 54 #else 55 ((void)f); ((void)s); ((void)ec); 56 return true; 57 #endif 58 }; 59 scoped_test_env env; 60 const path dne = env.make_env_path("dne"); 61 const path bad_sym = env.create_symlink(dne, "sym"); 62 const path dir = env.create_dir("dir1"); 63 const path cases[] = { 64 dne, bad_sym, dir 65 }; 66 for (auto& p : cases) { 67 std::error_code ec; 68 resize_file(p, 42, ec); 69 TEST_REQUIRE(ec); 70 TEST_CHECK(checkThrow(p, 42, ec)); 71 } 72 } 73 74 TEST_CASE(basic_resize_file_test) 75 { 76 scoped_test_env env; 77 const path file1 = env.create_file("file1", 42); 78 const auto set_ec = std::make_error_code(std::errc::address_in_use); 79 { // grow file 80 const std::uintmax_t new_s = 100; 81 std::error_code ec = set_ec; 82 resize_file(file1, new_s, ec); 83 TEST_CHECK(!ec); 84 TEST_CHECK(file_size(file1) == new_s); 85 } 86 { // shrink file 87 const std::uintmax_t new_s = 1; 88 std::error_code ec = set_ec; 89 resize_file(file1, new_s, ec); 90 TEST_CHECK(!ec); 91 TEST_CHECK(file_size(file1) == new_s); 92 } 93 { // shrink file to zero 94 const std::uintmax_t new_s = 0; 95 std::error_code ec = set_ec; 96 resize_file(file1, new_s, ec); 97 TEST_CHECK(!ec); 98 TEST_CHECK(file_size(file1) == new_s); 99 } 100 const path sym = env.create_symlink(file1, "sym"); 101 { // grow file via symlink 102 const std::uintmax_t new_s = 1024; 103 std::error_code ec = set_ec; 104 resize_file(sym, new_s, ec); 105 TEST_CHECK(!ec); 106 TEST_CHECK(file_size(file1) == new_s); 107 } 108 } 109 110 TEST_SUITE_END() 111