Skip to contentSkip to conversation

lib/tests: add string_get_size() boundary tests

PATCH In review Shuvam Pandey 3 participants 3 messages Apr 21 – Apr 22 Generic string library

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/tests: add string_get_size() boundary tests

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.

---
 lib/tests/string_helpers_kunit.c | 33 ++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
lib/tests/string_helpers_kunit.c +33 -0
index c853046183d24..92454837f3ddd 100644
@@ -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");
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by Tested-by on v1 1/1
#1 lore raw

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 on v1 1/1
#2 lore raw
In reply to Shuvam Pandey
Quoted 9 lines · Shuvam Pandey

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!

Signature · 2 lines
With Best Regards,
Andy Shevchenko