diff options
Diffstat (limited to 'lib/test-string_helpers.c')
| -rw-r--r-- | lib/test-string_helpers.c | 67 |
1 files changed, 49 insertions, 18 deletions
diff --git a/lib/test-string_helpers.c b/lib/test-string_helpers.c index 98866a770770..25b5cbfb7615 100644 --- a/lib/test-string_helpers.c +++ b/lib/test-string_helpers.c | |||
| @@ -327,36 +327,67 @@ out: | |||
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | #define string_get_size_maxbuf 16 | 329 | #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) \ |
| 331 | do { \ | 331 | do { \ |
| 332 | BUILD_BUG_ON(sizeof(exp_result) >= string_get_size_maxbuf); \ | 332 | BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \ |
| 333 | __test_string_get_size((size), (blk_size), (units), \ | 333 | BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \ |
| 334 | (exp_result)); \ | 334 | __test_string_get_size((size), (blk_size), (exp_result10), \ |
| 335 | (exp_result2)); \ | ||
| 335 | } while (0) | 336 | } while (0) |
| 336 | 337 | ||
| 337 | 338 | ||
| 338 | static __init void __test_string_get_size(const u64 size, const u64 blk_size, | 339 | static __init void test_string_get_size_check(const char *units, |
| 339 | const enum string_size_units units, | 340 | const char *exp, |
| 340 | const char *exp_result) | 341 | char *res, |
| 342 | const u64 size, | ||
| 343 | const u64 blk_size) | ||
| 341 | { | 344 | { |
| 342 | char buf[string_get_size_maxbuf]; | 345 | if (!memcmp(res, exp, strlen(exp) + 1)) |
| 343 | |||
| 344 | string_get_size(size, blk_size, units, buf, sizeof(buf)); | ||
| 345 | if (!memcmp(buf, exp_result, strlen(exp_result) + 1)) | ||
| 346 | return; | 346 | return; |
| 347 | 347 | ||
| 348 | buf[sizeof(buf) - 1] = '\0'; | 348 | res[string_get_size_maxbuf - 1] = '\0'; |
| 349 | pr_warn("Test 'test_string_get_size_one' failed!\n"); | 349 | |
| 350 | pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %d\n", | 350 | pr_warn("Test 'test_string_get_size' failed!\n"); |
| 351 | pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n", | ||
| 351 | size, blk_size, units); | 352 | 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); | ||
| 353 | } | 371 | } |
| 354 | 372 | ||
| 355 | static __init void test_string_get_size(void) | 373 | static __init void test_string_get_size(void) |
| 356 | { | 374 | { |
| 357 | test_string_get_size_one(16384, 512, STRING_UNITS_2, "8.00 MiB"); | 375 | /* small values */ |
| 358 | test_string_get_size_one(8192, 4096, STRING_UNITS_10, "32.7 MB"); | 376 | test_string_get_size_one(0, 512, "0 B", "0 B"); |
| 359 | test_string_get_size_one(1, 512, STRING_UNITS_10, "512 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"); | ||
| 360 | } | 391 | } |
| 361 | 392 | ||
| 362 | static int __init test_string_helpers_init(void) | 393 | static int __init test_string_helpers_init(void) |
