Debian Codegraph @

libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.resize_file/resize_file.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12
13 // <filesystem>
14
15 // void resize_file(const path& p, uintmax_t new_size);
16 // void resize_file(const path& p, uintmax_t new_size, error_code& ec) noexcept;
17
18 #include <filesystem>
19
20 #include "test_macros.h"
21 #include "filesystem_test_helper.h"
22 namespace fs = std::filesystem;
23 using namespace fs;
24
25 static void test_signatures()
26 {
27 const path p; ((void)p);
28 std::uintmax_t i; ((void)i);
29 std::error_code ec; ((void)ec);
30
31 ASSERT_SAME_TYPE(decltype(fs::resize_file(p, i)), void);
32 ASSERT_SAME_TYPE(decltype(fs::resize_file(p, i, ec)), void);
33
34 ASSERT_NOT_NOEXCEPT(fs::resize_file(p, i));
35 ASSERT_NOEXCEPT(fs::resize_file(p, i, ec));
36 }
37
38 static void test_error_reporting()
39 {
40 auto checkThrow = [](path const& f, std::uintmax_t s, const std::error_code& ec)
41 {
42 #ifndef TEST_HAS_NO_EXCEPTIONS
43 try {
44 fs::resize_file(f, s);
45 return false;
46 } catch (filesystem_error const& err) {
47 return err.path1() == f
48 && err.path2() == ""
49 && err.code() == ec;
50 }
51 #else
52 ((void)f); ((void)s); ((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 bad_sym = env.create_symlink(dne, "sym");
59 const path dir = env.create_dir("dir1");
60 const path cases[] = {
61 dne, bad_sym, dir
62 };
63 for (auto& p : cases) {
64 std::error_code ec;
65 resize_file(p, 42, ec);
66 assert(ec);
67 assert(checkThrow(p, 42, ec));
68 }
69 }
70
71 static void basic_resize_file_test()
72 {
73 scoped_test_env env;
74 const path file1 = env.create_file("file1", 42);
75 const auto set_ec = std::make_error_code(std::errc::address_in_use);
76 { // grow file
77 const std::uintmax_t new_s = 100;
78 std::error_code ec = set_ec;
79 resize_file(file1, new_s, ec);
80 assert(!ec);
81 assert(file_size(file1) == new_s);
82 }
83 { // shrink file
84 const std::uintmax_t new_s = 1;
85 std::error_code ec = set_ec;
86 resize_file(file1, new_s, ec);
87 assert(!ec);
88 assert(file_size(file1) == new_s);
89 }
90 { // shrink file to zero
91 const std::uintmax_t new_s = 0;
92 std::error_code ec = set_ec;
93 resize_file(file1, new_s, ec);
94 assert(!ec);
95 assert(file_size(file1) == new_s);
96 }
97 const path sym = env.create_symlink(file1, "sym");
98 { // grow file via symlink
99 const std::uintmax_t new_s = 1024;
100 std::error_code ec = set_ec;
101 resize_file(sym, new_s, ec);
102 assert(!ec);
103 assert(file_size(file1) == new_s);
104 }
105 }
106
107 int main(int, char**) {
108 test_signatures();
109 test_error_reporting();
110 basic_resize_file_test();
111 return 0;
112 }