string_get_size() has several non-obvious outputs around decimal and
binary unit boundaries because the unit is chosen before the final
rounding step. Decimal values just below 1 MB can print as "1000 kB";
binary values below 1 KiB stay in bytes; and rounded binary values can
print as "1000 KiB" or "1024 KiB" before the input reaches the next
binary unit.

Add KUnit coverage for these current outputs and nearby threshold values
so the existing behavior is explicit.

Link: 
https://lore.kernel.org/r/CAHp75VfB0Y7K=chk5wpktzcgvfcn5x7zhneq6qsrirdghs5...@mail.gmail.com
Link: 
https://lore.kernel.org/r/696d25567aadbb8ba324d4a980c15eb5cf4ccef6.ca...@hansenpartnership.com
Signed-off-by: Shuvam Pandey <[email protected]>
---
 lib/tests/string_helpers_kunit.c | 33 ++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_kunit.c
index c853046183d24..92454837f3ddd 100644
--- a/lib/tests/string_helpers_kunit.c
+++ b/lib/tests/string_helpers_kunit.c
@@ -558,6 +558,39 @@ static void test_get_size(struct kunit *test)
        /* weird block sizes */
        test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
 
+       /* decimal boundary around 1 MB */
+       test_string_get_size_one(999499, 1, "999 kB", "976 KiB");
+       test_string_get_size_one(999500, 1, "1000 kB", "976 KiB");
+       test_string_get_size_one(999999, 1, "1000 kB", "977 KiB");
+       test_string_get_size_one(1000000, 1, "1.00 MB", "977 KiB");
+       test_string_get_size_one(1000001, 1, "1.00 MB", "977 KiB");
+
+       /* binary values stay in bytes until the 1 KiB boundary */
+       test_string_get_size_one(1000, 1, "1.00 kB", "1000 B");
+       test_string_get_size_one(1018, 1, "1.02 kB", "1018 B");
+       test_string_get_size_one(1023, 1, "1.02 kB", "1023 B");
+       test_string_get_size_one(1024, 1, "1.02 kB", "1.00 KiB");
+       test_string_get_size_one(1025, 1, "1.03 kB", "1.00 KiB");
+
+       /* binary rounding around 1000 KiB */
+       test_string_get_size_one(1023487, 1, "1.02 MB", "999 KiB");
+       test_string_get_size_one(1023488, 1, "1.02 MB", "1000 KiB");
+       test_string_get_size_one(1024000, 1, "1.02 MB", "1000 KiB");
+       test_string_get_size_one(1024511, 1, "1.02 MB", "1000 KiB");
+       test_string_get_size_one(1024512, 1, "1.02 MB", "1001 KiB");
+
+       /* binary boundary around 1 MiB */
+       test_string_get_size_one(1048063, 1, "1.05 MB", "1023 KiB");
+       test_string_get_size_one(1048064, 1, "1.05 MB", "1024 KiB");
+       test_string_get_size_one(1048575, 1, "1.05 MB", "1024 KiB");
+       test_string_get_size_one(1048576, 1, "1.05 MB", "1.00 MiB");
+       test_string_get_size_one(1048577, 1, "1.05 MB", "1.00 MiB");
+
+       /* binary boundary around 2 MiB */
+       test_string_get_size_one(2097151, 1, "2.10 MB", "2.00 MiB");
+       test_string_get_size_one(2097152, 1, "2.10 MB", "2.00 MiB");
+       test_string_get_size_one(2097153, 1, "2.10 MB", "2.00 MiB");
+
        /* huge values */
        test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
        test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
-- 
2.50.1 (Apple Git-155)



Reply via email to