diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2022-11-07 21:04:01 +0100 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2022-11-27 21:33:49 +0100 |
commit | 6e2ffd10df8b71c775925b4bd54838a9181fc328 (patch) | |
tree | d8e8429637be8567a2e9ffc96bc6f97cc0404e8d | |
parent | f7c1ca19be1dc57172a00f2da6307a78822c4e99 (diff) | |
download | ccache-6e2ffd10df8b71c775925b4bd54838a9181fc328.tar.gz |
feat: Improve Util::format_human_readable_size for small sizes
-rw-r--r-- | src/Util.cpp | 6 | ||||
-rw-r--r-- | unittest/test_Util.cpp | 11 |
2 files changed, 10 insertions, 7 deletions
diff --git a/src/Util.cpp b/src/Util.cpp index e802a83b..3aecbaa1 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 b71dfdb3..8cb331b4 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"); |