Skip to content

Commit 72676bb

Browse files
vittyvktorvalds
authored andcommitted
lib/test-string_helpers.c: fix and improve string_get_size() tests
Recently added commit 564b026 ("string_helpers: fix precision loss for some inputs") fixed precision issues for string_get_size() and broke tests. Fix and improve them: test both STRING_UNITS_2 and STRING_UNITS_10 at a time, better failure reporting, test small an huge values. Fixes: 564b026 ("string_helpers: fix precision loss for some inputs") Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk> Cc: James Bottomley <James.Bottomley@HansenPartnership.com> Cc: James Bottomley <JBottomley@Odin.com> Cc: "James E.J. Bottomley" <jejb@parisc-linux.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e3ae195 commit 72676bb

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

lib/test-string_helpers.c

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,36 +327,67 @@ static __init void test_string_escape(const char *name,
327327
}
328328

329329
#define string_get_size_maxbuf 16
330-
#define test_string_get_size_one(size, blk_size, units, exp_result) \
330+
#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \
331331
do { \
332-
BUILD_BUG_ON(sizeof(exp_result) >= string_get_size_maxbuf); \
333-
__test_string_get_size((size), (blk_size), (units), \
334-
(exp_result)); \
332+
BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \
333+
BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
334+
__test_string_get_size((size), (blk_size), (exp_result10), \
335+
(exp_result2)); \
335336
} while (0)
336337

337338

338-
static __init void __test_string_get_size(const u64 size, const u64 blk_size,
339-
const enum string_size_units units,
340-
const char *exp_result)
339+
static __init void test_string_get_size_check(const char *units,
340+
const char *exp,
341+
char *res,
342+
const u64 size,
343+
const u64 blk_size)
341344
{
342-
char buf[string_get_size_maxbuf];
343-
344-
string_get_size(size, blk_size, units, buf, sizeof(buf));
345-
if (!memcmp(buf, exp_result, strlen(exp_result) + 1))
345+
if (!memcmp(res, exp, strlen(exp) + 1))
346346
return;
347347

348-
buf[sizeof(buf) - 1] = '\0';
349-
pr_warn("Test 'test_string_get_size_one' failed!\n");
350-
pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %d\n",
348+
res[string_get_size_maxbuf - 1] = '\0';
349+
350+
pr_warn("Test 'test_string_get_size' failed!\n");
351+
pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n",
351352
size, blk_size, units);
352-
pr_warn("expected: '%s', got '%s'\n", exp_result, buf);
353+
pr_warn("expected: '%s', got '%s'\n", exp, res);
354+
}
355+
356+
static __init void __test_string_get_size(const u64 size, const u64 blk_size,
357+
const char *exp_result10,
358+
const char *exp_result2)
359+
{
360+
char buf10[string_get_size_maxbuf];
361+
char buf2[string_get_size_maxbuf];
362+
363+
string_get_size(size, blk_size, STRING_UNITS_10, buf10, sizeof(buf10));
364+
string_get_size(size, blk_size, STRING_UNITS_2, buf2, sizeof(buf2));
365+
366+
test_string_get_size_check("STRING_UNITS_10", exp_result10, buf10,
367+
size, blk_size);
368+
369+
test_string_get_size_check("STRING_UNITS_2", exp_result2, buf2,
370+
size, blk_size);
353371
}
354372

355373
static __init void test_string_get_size(void)
356374
{
357-
test_string_get_size_one(16384, 512, STRING_UNITS_2, "8.00 MiB");
358-
test_string_get_size_one(8192, 4096, STRING_UNITS_10, "32.7 MB");
359-
test_string_get_size_one(1, 512, STRING_UNITS_10, "512 B");
375+
/* small values */
376+
test_string_get_size_one(0, 512, "0 B", "0 B");
377+
test_string_get_size_one(1, 512, "512 B", "512 B");
378+
test_string_get_size_one(1100, 1, "1.10 kB", "1.07 KiB");
379+
380+
/* normal values */
381+
test_string_get_size_one(16384, 512, "8.39 MB", "8.00 MiB");
382+
test_string_get_size_one(500118192, 512, "256 GB", "238 GiB");
383+
test_string_get_size_one(8192, 4096, "33.6 MB", "32.0 MiB");
384+
385+
/* weird block sizes */
386+
test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
387+
388+
/* huge values */
389+
test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
390+
test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
360391
}
361392

362393
static int __init test_string_helpers_init(void)

0 commit comments

Comments
 (0)