From: Joel Rosdahl Date: Thu, 27 Aug 2020 07:22:50 +0000 (+0200) Subject: Make Util::format_human_readable_size format kB X-Git-Tag: v4.0~165 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3a23709eef020393cfcbb7bf763766346c478387;p=thirdparty%2Fccache.git Make Util::format_human_readable_size format kB --- diff --git a/src/Util.cpp b/src/Util.cpp index ed49fb31e..15cb20cff 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -533,8 +533,10 @@ format_human_readable_size(uint64_t size) { if (size >= 1000 * 1000 * 1000) { return fmt::format("{:.1f} GB", size / ((double)(1000 * 1000 * 1000))); - } else { + } else if (size >= 1000 * 1000) { return fmt::format("{:.1f} MB", size / ((double)(1000 * 1000))); + } else { + return fmt::format("{:.1f} kB", size / 1000.0); } } diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index fedce207f..d89dd8f77 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -254,9 +254,13 @@ TEST_CASE("Util::format_hex") TEST_CASE("Util::format_human_readable_size") { - CHECK(Util::format_human_readable_size(0) == "0.0 MB"); - CHECK(Util::format_human_readable_size(49) == "0.0 MB"); - CHECK(Util::format_human_readable_size(420 * 1000) == "0.4 MB"); + CHECK(Util::format_human_readable_size(0) == "0.0 kB"); + CHECK(Util::format_human_readable_size(1) == "0.0 kB"); + CHECK(Util::format_human_readable_size(49) == "0.0 kB"); + CHECK(Util::format_human_readable_size(51) == "0.1 kB"); + CHECK(Util::format_human_readable_size(949) == "0.9 kB"); + CHECK(Util::format_human_readable_size(951) == "1.0 kB"); + CHECK(Util::format_human_readable_size(499.7 * 1000) == "499.7 kB"); CHECK(Util::format_human_readable_size(1000 * 1000) == "1.0 MB"); CHECK(Util::format_human_readable_size(1234 * 1000) == "1.2 MB"); CHECK(Util::format_human_readable_size(438.5 * 1000 * 1000) == "438.5 MB");