From: Joel Rosdahl Date: Mon, 7 Nov 2022 20:04:01 +0000 (+0100) Subject: feat: Improve Util::format_human_readable_size for small sizes X-Git-Tag: v4.8~96 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e2ffd10df8b71c775925b4bd54838a9181fc328;p=thirdparty%2Fccache.git feat: Improve Util::format_human_readable_size for small sizes --- diff --git a/src/Util.cpp b/src/Util.cpp index e802a83be..3aecbaa1e 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -562,8 +562,12 @@ format_human_readable_size(uint64_t size) return FMT("{:.1f} GB", size / ((double)(1000 * 1000 * 1000))); } else if (size >= 1000 * 1000) { return FMT("{:.1f} MB", size / ((double)(1000 * 1000))); - } else { + } else if (size >= 1000) { return FMT("{:.1f} kB", size / 1000.0); + } else if (size == 1) { + return "1 byte"; + } else { + return FMT("{} bytes", size); } } diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index b71dfdb33..8cb331b4a 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -258,12 +258,11 @@ TEST_CASE("Util::format_base32hex") TEST_CASE("Util::format_human_readable_size") { - 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(0) == "0 bytes"); + CHECK(Util::format_human_readable_size(1) == "1 byte"); + CHECK(Util::format_human_readable_size(42) == "42 bytes"); + CHECK(Util::format_human_readable_size(1949) == "1.9 kB"); + CHECK(Util::format_human_readable_size(1951) == "2.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");