Home | History | Annotate | Download | only in fs.op.space
      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 // space_info space(const path& p);
     15 // space_info space(const path& p, error_code& ec) noexcept;
     16 
     17 #include <experimental/filesystem>
     18 #include <sys/statvfs.h>
     19 
     20 #include "test_macros.h"
     21 #include "rapid-cxx-test.hpp"
     22 #include "filesystem_test_helper.hpp"
     23 
     24 using namespace std::experimental::filesystem;
     25 
     26 bool EqualDelta(std::uintmax_t x, std::uintmax_t y, std::uintmax_t delta) {
     27     if (x >= y) {
     28         return (x - y) <= delta;
     29     } else {
     30         return (y - x) <= delta;
     31     }
     32 }
     33 
     34 TEST_SUITE(filesystem_space_test_suite)
     35 
     36 TEST_CASE(signature_test)
     37 {
     38     const path p; ((void)p);
     39     std::error_code ec; ((void)ec);
     40     ASSERT_SAME_TYPE(decltype(space(p)), space_info);
     41     ASSERT_SAME_TYPE(decltype(space(p, ec)), space_info);
     42     ASSERT_NOT_NOEXCEPT(space(p));
     43     ASSERT_NOEXCEPT(space(p, ec));
     44 }
     45 
     46 TEST_CASE(test_error_reporting)
     47 {
     48     auto checkThrow = [](path const& f, const std::error_code& ec)
     49     {
     50 #ifndef TEST_HAS_NO_EXCEPTIONS
     51         try {
     52             space(f);
     53             return false;
     54         } catch (filesystem_error const& err) {
     55             return err.path1() == f
     56                 && err.path2() == ""
     57                 && err.code() == ec;
     58         }
     59 #else
     60         ((void)f); ((void)ec);
     61         return true;
     62 #endif
     63     };
     64     const path cases[] = {
     65         "",
     66         StaticEnv::DNE,
     67         StaticEnv::BadSymlink
     68     };
     69     for (auto& p : cases) {
     70         const auto expect = static_cast<std::uintmax_t>(-1);
     71         std::error_code ec;
     72         space_info info = space(p, ec);
     73         TEST_CHECK(ec);
     74         TEST_CHECK(info.capacity == expect);
     75         TEST_CHECK(info.free == expect);
     76         TEST_CHECK(info.available == expect);
     77         TEST_CHECK(checkThrow(p, ec));
     78     }
     79 }
     80 
     81 TEST_CASE(basic_space_test)
     82 {
     83     // All the test cases should reside on the same filesystem and therefore
     84     // should have the same expected result. Compute this expected result
     85     // one and check that it looks semi-sane.
     86     struct statvfs expect;
     87     TEST_REQUIRE(::statvfs(StaticEnv::Dir.c_str(), &expect) != -1);
     88     TEST_CHECK(expect.f_bavail > 0);
     89     TEST_CHECK(expect.f_bfree > 0);
     90     TEST_CHECK(expect.f_bsize > 0);
     91     TEST_CHECK(expect.f_blocks > 0);
     92     TEST_REQUIRE(expect.f_frsize > 0);
     93     auto do_mult = [&](std::uintmax_t val) {
     94         std::uintmax_t fsize = expect.f_frsize;
     95         std::uintmax_t new_val = val * fsize;
     96         TEST_CHECK(new_val / fsize == val); // Test for overflow
     97         return new_val;
     98     };
     99     const std::uintmax_t bad_value = static_cast<std::uintmax_t>(-1);
    100     const std::uintmax_t expect_capacity = do_mult(expect.f_blocks);
    101     const std::uintmax_t expect_free = do_mult(expect.f_bfree);
    102     const std::uintmax_t expect_avail = do_mult(expect.f_bavail);
    103 
    104     // Other processes running on the operating system may have changed
    105     // the amount of space available. Check that these are within tolerances.
    106     // Currently 5% of capacity
    107     const std::uintmax_t delta = expect_capacity / 20;
    108     const path cases[] = {
    109         StaticEnv::File,
    110         StaticEnv::Dir,
    111         StaticEnv::Dir2,
    112         StaticEnv::SymlinkToFile,
    113         StaticEnv::SymlinkToDir
    114     };
    115     for (auto& p : cases) {
    116         std::error_code ec = GetTestEC();
    117         space_info info = space(p, ec);
    118         TEST_CHECK(!ec);
    119         TEST_CHECK(info.capacity != bad_value);
    120         TEST_CHECK(expect_capacity == info.capacity);
    121         TEST_CHECK(info.free != bad_value);
    122         TEST_CHECK(EqualDelta(expect_free, info.free, delta));
    123         TEST_CHECK(info.available != bad_value);
    124         TEST_CHECK(EqualDelta(expect_avail, info.available, delta));
    125     }
    126 }
    127 
    128 TEST_SUITE_END()
    129