From 3a23709eef020393cfcbb7bf763766346c478387 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 27 Aug 2020 09:22:50 +0200 Subject: [PATCH] Make Util::format_human_readable_size format kB --- src/Util.cpp | 4 +++- unittest/test_Util.cpp | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) 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"); -- 2.47.3