libcxx

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

resize_file.pass.cpp (3116B)


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