Home | History | Annotate | Download | only in fs.op.file_size
      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 // <experimental/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 <experimental/filesystem>
     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 std::experimental::filesystem;
     26 
     27 TEST_SUITE(file_size_test_suite)
     28 
     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 
     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 
     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 
     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 
     63 TEST_CASE(file_size_error_cases)
     64 {
     65     const path testCases[] = {
     66         StaticEnv::Dir,
     67         StaticEnv::SymlinkToDir,
     68         StaticEnv::BadSymlink,
     69         StaticEnv::DNE
     70     };
     71     const uintmax_t expect = static_cast<uintmax_t>(-1);
     72     for (auto& TC : testCases) {
     73         std::error_code ec;
     74         TEST_CHECK(file_size(TC, ec) == expect);
     75         TEST_CHECK(ec);
     76     }
     77 }
     78 
     79 TEST_SUITE_END()
     80