[PATCH] lib/tests: add string_get_size() boundary tests

Shuvam Pandey posted 1 patch 1 month, 3 weeks ago
lib/tests/string_helpers_kunit.c | 33 ++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
[PATCH] lib/tests: add string_get_size() boundary tests
Posted by Shuvam Pandey 1 month, 3 weeks ago
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_bQ@mail.gmail.com
Link: https://lore.kernel.org/r/696d25567aadbb8ba324d4a980c15eb5cf4ccef6.camel@HansenPartnership.com
Signed-off-by: Shuvam Pandey <shuvampandey1@gmail.com>
---
 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)
Re: [PATCH] lib/tests: add string_get_size() boundary tests
Posted by Andy Shevchenko 1 month, 3 weeks ago
On Wed, Apr 22, 2026 at 02:04:06AM +0545, Shuvam Pandey wrote:
> 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.

Always keen to see more test cases and hence almost automatic green light
from me!

Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] lib/tests: add string_get_size() boundary tests
Posted by Josh Law 1 month, 3 weeks ago
Hi Shuvam!

I see that there already is discussion going on around patches
like this (from your Link: fields)

So I am going to try and keep this
as low drama as possible 

Okay, the one thing I will say about this, is I particularly like
these tests. They are very very smart and "gnarly" :-)

Like for example, 

+	/* 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");

That is a INCREDIBLY smart edge case.  (I probably wouldn't think of this)

Or even

+	/* 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");

Again, smart. I like these tests.


So, you (more) than deserve these tags, as I tested it too :) :

Reviewed-by: Josh Law <joshlaw48@gmail.com>
Tested-by: Josh Law <joshlaw48@gmail.com>