Debian Codegraph @

libstdc++-v3/testsuite/27_io/filesystem/operations/resize_file.cc

raw
1 // Copyright (C) 2019-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
20
21 // C++17 30.10.15.33 Resize file [fs.op.resize_file]
22
23 #include <filesystem>
24 #include <string>
25 #include <fstream>
26 #include <testsuite_fs.h>
27 #include <testsuite_hooks.h>
28
29 void
30 test01()
31 {
32 auto p = __gnu_test::nonexistent_path();
33 std::error_code ec;
34 resize_file(p, 0, ec);
35 VERIFY( ec );
36 ec = {};
37 resize_file(p, 1, ec);
38 VERIFY( ec );
39
40 __gnu_test::scoped_file f(p);
41 std::ofstream{p} << "some text";
42 std::ifstream fin;
43 std::string input;
44
45 #ifdef _GLIBCXX_HAVE_TRUNCATE
46 resize_file(p, 4, ec);
47 VERIFY( !ec );
48 fin.open(p);
49 getline(fin, input);
50 VERIFY( input.length() == 4 );
51 fin.close();
52
53 resize_file(p, 2);
54 fin.open(p);
55 getline(fin, input);
56 VERIFY( input.length() == 2 );
57 fin.close();
58 #endif
59
60 resize_file(p, 0, ec);
61 VERIFY( !ec );
62 fin.open(p);
63 getline(fin, input);
64 VERIFY( input.length() == 0 );
65 fin.close();
66 }
67
68 int
69 main()
70 {
71 test01();
72 }