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 // uintmax_t file_size(const path& p);
15 // uintmax_t file_size(const path& p, std::error_code& ec) noexcept;
16
17 #include "filesystem_include.hpp"
18 #include <type_traits>
19 #include <cassert>
20
21 #include "test_macros.h"
22 #include "rapid-cxx-test.hpp"
23 #include "filesystem_test_helper.hpp"
24
25 using namespace fs;
26
27 TEST_SUITE(file_size_test_suite)
28
TEST_CASE(signature_test)29 TEST_CASE(signature_test)
30 {
31 const path p; ((void)p);
32 std::error_code ec; ((void)ec);
33 ASSERT_SAME_TYPE(decltype(file_size(p)), uintmax_t);
34 ASSERT_SAME_TYPE(decltype(file_size(p, ec)), uintmax_t);
35 ASSERT_NOT_NOEXCEPT(file_size(p));
36 ASSERT_NOEXCEPT(file_size(p, ec));
37 }
38
TEST_CASE(file_size_empty_test)39 TEST_CASE(file_size_empty_test)
40 {
41 const path p = StaticEnv::EmptyFile;
42 TEST_CHECK(file_size(p) == 0);
43 std::error_code ec;
44 TEST_CHECK(file_size(p, ec) == 0);
45 }
46
TEST_CASE(file_size_non_empty)47 TEST_CASE(file_size_non_empty)
48 {
49 scoped_test_env env;
50 const path p = env.create_file("file", 42);
51 TEST_CHECK(file_size(p) == 42);
52 std::error_code ec;
53 TEST_CHECK(file_size(p, ec) == 42);
54 }
55
TEST_CASE(symlink_test_case)56 TEST_CASE(symlink_test_case)
57 {
58 const path p = StaticEnv::File;
59 const path p2 = StaticEnv::SymlinkToFile;
60 TEST_CHECK(file_size(p) == file_size(p2));
61 }
62
TEST_CASE(file_size_error_cases)63 TEST_CASE(file_size_error_cases)
64 {
65 struct {
66 path p;
67 std::errc expected_err;
68 } TestCases[] = {
69 {StaticEnv::Dir, std::errc::is_a_directory},
70 {StaticEnv::SymlinkToDir, std::errc::is_a_directory},
71 {StaticEnv::BadSymlink, std::errc::no_such_file_or_directory},
72 {StaticEnv::DNE, std::errc::no_such_file_or_directory},
73 {"", std::errc::no_such_file_or_directory}};
74 const uintmax_t expect = static_cast<uintmax_t>(-1);
75 for (auto& TC : TestCases) {
76 std::error_code ec = GetTestEC();
77 TEST_CHECK(file_size(TC.p, ec) == expect);
78 TEST_CHECK(ErrorIs(ec, TC.expected_err));
79
80 ExceptionChecker Checker(TC.p, TC.expected_err, "file_size");
81 TEST_CHECK_THROW_RESULT(filesystem_error, Checker, file_size(TC.p));
82 }
83 }
84
85 TEST_SUITE_END()
86