From: Joel Rosdahl Date: Mon, 14 Nov 2022 08:55:09 +0000 (+0100) Subject: enhance: Add Util::format_human_readable_diff X-Git-Tag: v4.8~95 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf78bc995e765767b42942a834fcd7e81b392fd7;p=thirdparty%2Fccache.git enhance: Add Util::format_human_readable_diff --- diff --git a/src/Util.cpp b/src/Util.cpp index 3aecbaa1e..844e5497a 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -555,6 +555,13 @@ format_base32hex(const uint8_t* data, size_t size) return result; } +std::string +format_human_readable_diff(int64_t diff) +{ + const char* sign = diff == 0 ? "" : (diff > 0 ? "+" : "-"); + return FMT("{}{}", sign, format_human_readable_size(std::abs(diff))); +} + std::string format_human_readable_size(uint64_t size) { diff --git a/src/Util.hpp b/src/Util.hpp index e29aff3d4..1bd57b0df 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -143,6 +143,9 @@ std::string format_base16(const uint8_t* data, size_t size); // padding characters will be added. std::string format_base32hex(const uint8_t* data, size_t size); +// Format `diff` as a human-readable string. +std::string format_human_readable_diff(int64_t diff); + // Format `size` as a human-readable string. std::string format_human_readable_size(uint64_t size); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 8cb331b4a..a45d7c57a 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -256,6 +256,34 @@ TEST_CASE("Util::format_base32hex") CHECK(Util::format_base32hex(input, 6) == "cpnmuoj1e8"); } +TEST_CASE("Util::format_human_readable_diff") +{ + CHECK(Util::format_human_readable_diff(0) == "0 bytes"); + CHECK(Util::format_human_readable_diff(1) == "+1 byte"); + CHECK(Util::format_human_readable_diff(42) == "+42 bytes"); + CHECK(Util::format_human_readable_diff(1949) == "+1.9 kB"); + CHECK(Util::format_human_readable_diff(1951) == "+2.0 kB"); + CHECK(Util::format_human_readable_diff(499.7 * 1000) == "+499.7 kB"); + CHECK(Util::format_human_readable_diff(1000 * 1000) == "+1.0 MB"); + CHECK(Util::format_human_readable_diff(1234 * 1000) == "+1.2 MB"); + CHECK(Util::format_human_readable_diff(438.5 * 1000 * 1000) == "+438.5 MB"); + CHECK(Util::format_human_readable_diff(1000 * 1000 * 1000) == "+1.0 GB"); + CHECK(Util::format_human_readable_diff(17.11 * 1000 * 1000 * 1000) + == "+17.1 GB"); + + CHECK(Util::format_human_readable_diff(-1) == "-1 byte"); + CHECK(Util::format_human_readable_diff(-42) == "-42 bytes"); + CHECK(Util::format_human_readable_diff(-1949) == "-1.9 kB"); + CHECK(Util::format_human_readable_diff(-1951) == "-2.0 kB"); + CHECK(Util::format_human_readable_diff(-499.7 * 1000) == "-499.7 kB"); + CHECK(Util::format_human_readable_diff(-1000 * 1000) == "-1.0 MB"); + CHECK(Util::format_human_readable_diff(-1234 * 1000) == "-1.2 MB"); + CHECK(Util::format_human_readable_diff(-438.5 * 1000 * 1000) == "-438.5 MB"); + CHECK(Util::format_human_readable_diff(-1000 * 1000 * 1000) == "-1.0 GB"); + CHECK(Util::format_human_readable_diff(-17.11 * 1000 * 1000 * 1000) + == "-17.1 GB"); +} + TEST_CASE("Util::format_human_readable_size") { CHECK(Util::format_human_readable_size(0) == "0 bytes");