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// UNSUPPORTED: availability-filesystem-missing
13
14// The string reported on errors changed, which makes those tests fail when run
15// against a built library that doesn't contain 0aa637b2037d.
16// XFAIL: using-built-library-before-llvm-13
17
18// <filesystem>
19
20// uintmax_t file_size(const path& p);
21// uintmax_t file_size(const path& p, std::error_code& ec) noexcept;
22
23#include <filesystem>
24#include <type_traits>
25#include <cassert>
26
27#include "assert_macros.h"
28#include "test_macros.h"
29#include "filesystem_test_helper.h"
30namespace fs = std::filesystem;
31using namespace fs;
32
33static void signature_test()
34{
35 const path p; ((void)p);
36 std::error_code ec; ((void)ec);
37 ASSERT_SAME_TYPE(decltype(file_size(p)), std::uintmax_t);
38 ASSERT_SAME_TYPE(decltype(file_size(p, ec)), std::uintmax_t);
39 ASSERT_NOT_NOEXCEPT(file_size(p: p));
40 ASSERT_NOEXCEPT(file_size(p: p, ec&: ec));
41}
42
43static void file_size_empty_test()
44{
45 static_test_env static_env;
46 const path p = static_env.EmptyFile;
47 assert(file_size(p) == 0);
48 std::error_code ec;
49 assert(file_size(p, ec) == 0);
50}
51
52static void file_size_non_empty()
53{
54 scoped_test_env env;
55 const path p = env.create_file("file", 42);
56 assert(file_size(p) == 42);
57 std::error_code ec;
58 assert(file_size(p, ec) == 42);
59}
60
61static void symlink_test_case()
62{
63 static_test_env static_env;
64 const path p = static_env.File;
65 const path p2 = static_env.SymlinkToFile;
66 assert(file_size(p) == file_size(p2));
67}
68
69static void file_size_error_cases()
70{
71 static_test_env static_env;
72 struct {
73 path p;
74 std::errc expected_err;
75 } TestCases[] = {
76 {static_env.Dir, std::errc::is_a_directory},
77 {static_env.SymlinkToDir, std::errc::is_a_directory},
78 {static_env.BadSymlink, std::errc::no_such_file_or_directory},
79 {static_env.DNE, std::errc::no_such_file_or_directory},
80 {"", std::errc::no_such_file_or_directory}};
81 const std::uintmax_t expect = static_cast<std::uintmax_t>(-1);
82 for (auto& TC : TestCases) {
83 std::error_code ec = GetTestEC();
84 assert(file_size(TC.p, ec) == expect);
85 assert(ErrorIs(ec, TC.expected_err));
86
87 ExceptionChecker Checker(TC.p, TC.expected_err, "file_size");
88 TEST_VALIDATE_EXCEPTION(filesystem_error, Checker, file_size(TC.p));
89 }
90}
91
92int main(int, char**) {
93 signature_test();
94 file_size_empty_test();
95 file_size_non_empty();
96 symlink_test_case();
97 file_size_error_cases();
98 return 0;
99}
100

source code of libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.file_size/file_size.pass.cpp