[libcxx-commits] [libcxx] 78ba1e9 - [libcxx] [test] Move use of statvfs to helper header

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 21 23:12:05 PDT 2020


Author: Martin Storsjö
Date: 2020-10-22T09:00:57+03:00
New Revision: 78ba1e93a6f25c0b4266e96e5628b325a5da31b6

URL: https://github.com/llvm/llvm-project/commit/78ba1e93a6f25c0b4266e96e5628b325a5da31b6
DIFF: https://github.com/llvm/llvm-project/commit/78ba1e93a6f25c0b4266e96e5628b325a5da31b6.diff

LOG: [libcxx] [test] Move use of statvfs to helper header

Implement the corresponding thing using windows functions as well.

Differential Revision: https://reviews.llvm.org/D89864

Added: 
    

Modified: 
    libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp
    libcxx/test/support/filesystem_test_helper.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp
index f607b44e544d..c0317966d4fe 100644
--- a/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.space/space.pass.cpp
@@ -14,7 +14,6 @@
 // space_info space(const path& p, error_code& ec) noexcept;
 
 #include "filesystem_include.h"
-#include <sys/statvfs.h>
 
 #include "test_macros.h"
 #include "rapid-cxx-test.h"
@@ -85,23 +84,12 @@ TEST_CASE(basic_space_test)
     // All the test cases should reside on the same filesystem and therefore
     // should have the same expected result. Compute this expected result
     // one and check that it looks semi-sane.
-    struct statvfs expect;
-    TEST_REQUIRE(::statvfs(static_env.Dir.string().c_str(), &expect) != -1);
-    TEST_CHECK(expect.f_bavail > 0);
-    TEST_CHECK(expect.f_bfree > 0);
-    TEST_CHECK(expect.f_bsize > 0);
-    TEST_CHECK(expect.f_blocks > 0);
-    TEST_REQUIRE(expect.f_frsize > 0);
-    auto do_mult = [&](std::uintmax_t val) {
-        std::uintmax_t fsize = expect.f_frsize;
-        std::uintmax_t new_val = val * fsize;
-        TEST_CHECK(new_val / fsize == val); // Test for overflow
-        return new_val;
-    };
     const std::uintmax_t bad_value = static_cast<std::uintmax_t>(-1);
-    const std::uintmax_t expect_capacity = do_mult(expect.f_blocks);
-    const std::uintmax_t expect_free = do_mult(expect.f_bfree);
-    const std::uintmax_t expect_avail = do_mult(expect.f_bavail);
+    std::uintmax_t expect_capacity;
+    std::uintmax_t expect_free;
+    std::uintmax_t expect_avail;
+    TEST_REQUIRE(utils::space(static_env.Dir.string(), expect_capacity,
+                              expect_free, expect_avail));
 
     // Other processes running on the operating system may have changed
     // the amount of space available. Check that these are within tolerances.

diff  --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 704e2b26a0b4..ac2014fa34d1 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -6,6 +6,7 @@
 #include <sys/stat.h> // for stat, mkdir, mkfifo
 #ifndef _WIN32
 #include <unistd.h> // for ftruncate, link, symlink, getcwd, chdir
+#include <sys/statvfs.h>
 #else
 #include <io.h>
 #include <direct.h>
@@ -52,6 +53,21 @@ namespace utils {
     inline int unsetenv(const char *var) {
         return ::_putenv((std::string(var) + "=").c_str());
     }
+    inline bool space(std::string path, std::uintmax_t &capacity,
+                      std::uintmax_t &free, std::uintmax_t &avail) {
+        ULARGE_INTEGER FreeBytesAvailableToCaller, TotalNumberOfBytes,
+                       TotalNumberOfFreeBytes;
+        if (!GetDiskFreeSpaceExA(path.c_str(), &FreeBytesAvailableToCaller,
+                                 &TotalNumberOfBytes, &TotalNumberOfFreeBytes))
+          return false;
+        capacity = TotalNumberOfBytes.QuadPart;
+        free = TotalNumberOfFreeBytes.QuadPart;
+        avail = FreeBytesAvailableToCaller.QuadPart;
+        assert(capacity > 0);
+        assert(free > 0);
+        assert(avail > 0);
+        return true;
+    }
 #else
     using ::mkdir;
     using ::ftruncate;
@@ -63,6 +79,27 @@ namespace utils {
     inline int unsetenv(const char *var) {
         return ::unsetenv(var);
     }
+    inline bool space(std::string path, std::uintmax_t &capacity,
+                      std::uintmax_t &free, std::uintmax_t &avail) {
+        struct statvfs expect;
+        if (::statvfs(path.c_str(), &expect) == -1)
+          return false;
+        assert(expect.f_bavail > 0);
+        assert(expect.f_bfree > 0);
+        assert(expect.f_bsize > 0);
+        assert(expect.f_blocks > 0);
+        assert(expect.f_frsize > 0);
+        auto do_mult = [&](std::uintmax_t val) {
+            std::uintmax_t fsize = expect.f_frsize;
+            std::uintmax_t new_val = val * fsize;
+            assert(new_val / fsize == val); // Test for overflow
+            return new_val;
+        };
+        capacity = do_mult(expect.f_blocks);
+        free = do_mult(expect.f_bfree);
+        avail = do_mult(expect.f_bavail);
+        return true;
+    }
 #endif
 
     inline std::string getcwd() {


        


More information about the libcxx-commits mailing list