This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.
I tested this using:
$ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 scanf
Failure output before this series: [ 383.100048] test_scanf: vsscanf("1574 9 64ca 935b 7 142d ff58 0", "%4hx %1hx %4hx %4hx %1hx %4hx %4hx %1hx", ...) expected 2472240330 got 1690959881 [ 383.102843] test_scanf: vsscanf("f12:2:d:2:c166:1:36b:1906", "%3hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...) expected 131085 got 851970 [ 383.105376] test_scanf: vsscanf("4,b2fe,3,593,6,0,3bde,0", "%1hx,%4hx,%1hx,%3hx,%1hx,%1hx,%4hx,%1hx", ...) expected 93519875 got 242430 [ 383.105659] test_scanf: vsscanf("6-1-2-1-d9e6-f-93e-e567", "%1hx-%1hx-%1hx-%1hx-%4hx-%1hx-%3hx-%4hx", ...) expected 65538 got 131073 [ 383.106127] test_scanf: vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...) expected 125069 got 3901554741 [ 383.106235] test_scanf: vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...) expected 571539457 got 106936 ... [ 383.106398] test_scanf: failed 6 out of 2545 tests
Failure output after this series: # numbers_list_field_width_val_width: ASSERTION FAILED at lib/scanf_kunit.c:94 lib/scanf_kunit.c:555: vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...) expected 837828163 got 1044578334 not ok 1 " " # numbers_list_field_width_val_width: ASSERTION FAILED at lib/scanf_kunit.c:94 lib/scanf_kunit.c:555: vsscanf("dc2:1c:0:3531:2621:5172:1:7", "%3hx:%2hx:%1hx:%4hx:%4hx:%4hx:%1hx:%1hx", ...) expected 892403712 got 28 not ok 2 ":" # numbers_list_field_width_val_width: ASSERTION FAILED at lib/scanf_kunit.c:94 lib/scanf_kunit.c:555: vsscanf("e083,8f6e,b,70ca,1,1,aab1,10e4", "%4hx,%4hx,%1hx,%4hx,%1hx,%1hx,%4hx,%4hx", ...) expected 1892286475 got 757614 not ok 3 "," # numbers_list_field_width_val_width: ASSERTION FAILED at lib/scanf_kunit.c:94 lib/scanf_kunit.c:555: vsscanf("2e72-8435-1-2fc-7cbd-c2f1-7158-2b41", "%4hx-%4hx-%1hx-%3hx-%4hx-%4hx-%4hx-%4hx", ...) expected 50069505 got 99381 not ok 4 "-" # numbers_list_field_width_val_width: ASSERTION FAILED at lib/scanf_kunit.c:94 lib/scanf_kunit.c:555: vsscanf("403/0/17/1/11e7/1/1fe8/34ba", "%3hx/%1hx/%2hx/%1hx/%4hx/%1hx/%4hx/%4hx", ...) expected 65559 got 1507328 not ok 5 "/"
Signed-off-by: Tamir Duberstein tamird@gmail.com --- Changes in v9: - Use __scanf correctly on `_test`. (Thanks to Petr Mladek). - Split header `#include` cleanup into separate patch. (Petr Mladek) - In "scanf: break kunit into test cases", revert to KUNIT_FAIL. (Petr Mladek) - Rebase on linux-next. - Add provisional patch "scanf: further break kunit into test cases". Please feel free to take this series without this patch if you prefer. - Link to v8: https://lore.kernel.org/r/20250214-scanf-kunit-convert-v8-0-5ea50f95f83c@gma...
Changes in v8: - Expand "scanf: remove redundant debug logs" commit message. (Andy Shevchenko) - Add patch "implicate test line in failure messages". - Rebase on linux-next, move scanf_kunit.c into lib/tests/. - Link to v7: https://lore.kernel.org/r/20250211-scanf-kunit-convert-v7-0-c057f0a3d9d8@gma...
Changes in v7: - Remove redundant debug logs. (Petr Mladek) - Drop Petr's Acked-by. - Use original test assertions as KUNIT_*_EQ_MSG produces hard-to-parse messages. The new failure output is: - Link to v6: https://lore.kernel.org/r/20250210-scanf-kunit-convert-v6-0-4d583d07f92d@gma...
Changes in v6: - s/at boot/at runtime/ for consistency with the printf series. - Go back to kmalloc. (Geert Uytterhoeven) - Link to v5: https://lore.kernel.org/r/20250210-scanf-kunit-convert-v5-0-8e64f3a7de99@gma...
Changes in v5: - Remove extraneous trailing newlines from failure messages. - Replace `pr_debug` with `kunit_printk`. - Use static char arrays instead of kmalloc. - Drop KUnit boilerplate from CONFIG_SCANF_KUNIT_TEST help text. - Drop arch changes. - Link to v4: https://lore.kernel.org/r/20250207-scanf-kunit-convert-v4-0-a23e2afaede8@gma...
Changes in v4: - Bake `test` into various macros, greatly reducing diff noise. - Revert control flow changes. - Link to v3: https://lore.kernel.org/r/20250204-scanf-kunit-convert-v3-0-386d7c3ee714@gma...
Changes in v3: - Reduce diff noise in lib/Makefile. (Petr Mladek) - Split `scanf_test` into a few test cases. New output: : =================== scanf (10 subtests) ==================== : [PASSED] numbers_simple : ====================== numbers_list ======================= : [PASSED] delim=" " : [PASSED] delim=":" : [PASSED] delim="," : [PASSED] delim="-" : [PASSED] delim="/" : ================== [PASSED] numbers_list =================== : ============ numbers_list_field_width_typemax ============= : [PASSED] delim=" " : [PASSED] delim=":" : [PASSED] delim="," : [PASSED] delim="-" : [PASSED] delim="/" : ======== [PASSED] numbers_list_field_width_typemax ========= : =========== numbers_list_field_width_val_width ============ : [PASSED] delim=" " : [PASSED] delim=":" : [PASSED] delim="," : [PASSED] delim="-" : [PASSED] delim="/" : ======= [PASSED] numbers_list_field_width_val_width ======== : [PASSED] numbers_slice : [PASSED] numbers_prefix_overflow : [PASSED] test_simple_strtoull : [PASSED] test_simple_strtoll : [PASSED] test_simple_strtoul : [PASSED] test_simple_strtol : ====================== [PASSED] scanf ====================== : ============================================================ : Testing complete. Ran 22 tests: passed: 22 : Elapsed time: 5.517s total, 0.001s configuring, 5.440s building, 0.067s running - Link to v2: https://lore.kernel.org/r/20250203-scanf-kunit-convert-v2-1-277a618d804e@gma...
Changes in v2: - Rename lib/{test_scanf.c => scanf_kunit.c}. (Andy Shevchenko) - Link to v1: https://lore.kernel.org/r/20250131-scanf-kunit-convert-v1-1-0976524f0eba@gma...
--- Tamir Duberstein (6): scanf: implicate test line in failure messages scanf: remove redundant debug logs scanf: convert self-test to KUnit scanf: break kunit into test cases scanf: tidy header `#include`s scanf: further break kunit into test cases
MAINTAINERS | 2 +- lib/Kconfig.debug | 12 +- lib/Makefile | 1 - lib/tests/Makefile | 1 + lib/{test_scanf.c => tests/scanf_kunit.c} | 360 ++++++++++++++++-------------- tools/testing/selftests/lib/Makefile | 2 +- tools/testing/selftests/lib/config | 1 - tools/testing/selftests/lib/scanf.sh | 4 - 8 files changed, 201 insertions(+), 182 deletions(-) --- base-commit: 7ec162622e66a4ff886f8f28712ea1b13069e1aa change-id: 20250131-scanf-kunit-convert-f70dc33bb34c
Best regards,
This improves the failure output by pointing to the failing line at the top level of the test.
Reviewed-by: Petr Mladek pmladek@suse.com Tested-by: Petr Mladek pmladek@suse.com Signed-off-by: Tamir Duberstein tamird@gmail.com --- lib/test_scanf.c | 66 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 33 deletions(-)
diff --git a/lib/test_scanf.c b/lib/test_scanf.c index 44f8508c9d88..e65b10c3dc11 100644 --- a/lib/test_scanf.c +++ b/lib/test_scanf.c @@ -24,12 +24,12 @@ static char *test_buffer __initdata; static char *fmt_buffer __initdata; static struct rnd_state rnd_state __initdata;
-typedef int (*check_fn)(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap); +typedef int (*check_fn)(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap);
-static void __scanf(4, 6) __init -_test(check_fn fn, const void *check_data, const char *string, const char *fmt, - int n_args, ...) +static void __scanf(6, 8) __init +_test(const char *file, const int line, check_fn fn, const void *check_data, const char *string, + const char *fmt, int n_args, ...) { va_list ap, ap_copy; int ret; @@ -42,12 +42,12 @@ _test(check_fn fn, const void *check_data, const char *string, const char *fmt, va_end(ap_copy);
if (ret != n_args) { - pr_warn("vsscanf("%s", "%s", ...) returned %d expected %d\n", - string, fmt, ret, n_args); + pr_warn("%s:%d: vsscanf("%s", "%s", ...) returned %d expected %d\n", + file, line, string, fmt, ret, n_args); goto fail; }
- ret = (*fn)(check_data, string, fmt, n_args, ap); + ret = (*fn)(file, line, check_data, string, fmt, n_args, ap); if (ret) goto fail;
@@ -67,88 +67,88 @@ do { \ typeof(*expect) got = *va_arg(ap, typeof(expect)); \ pr_debug("\t" arg_fmt "\n", got); \ if (got != *expect) { \ - pr_warn("vsscanf("%s", "%s", ...) expected " arg_fmt " got " arg_fmt "\n", \ - str, fmt, *expect, got); \ + pr_warn("%s:%d, vsscanf("%s", "%s", ...) expected " arg_fmt " got " arg_fmt "\n", \ + file, line, str, fmt, *expect, got); \ return 1; \ } \ } \ return 0; \ } while (0)
-static int __init check_ull(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_ull(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const unsigned long long *pval = check_data;
_check_numbers_template("%llu", pval, string, fmt, n_args, ap); }
-static int __init check_ll(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_ll(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const long long *pval = check_data;
_check_numbers_template("%lld", pval, string, fmt, n_args, ap); }
-static int __init check_ulong(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_ulong(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const unsigned long *pval = check_data;
_check_numbers_template("%lu", pval, string, fmt, n_args, ap); }
-static int __init check_long(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_long(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const long *pval = check_data;
_check_numbers_template("%ld", pval, string, fmt, n_args, ap); }
-static int __init check_uint(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_uint(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const unsigned int *pval = check_data;
_check_numbers_template("%u", pval, string, fmt, n_args, ap); }
-static int __init check_int(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_int(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const int *pval = check_data;
_check_numbers_template("%d", pval, string, fmt, n_args, ap); }
-static int __init check_ushort(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_ushort(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const unsigned short *pval = check_data;
_check_numbers_template("%hu", pval, string, fmt, n_args, ap); }
-static int __init check_short(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_short(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const short *pval = check_data;
_check_numbers_template("%hd", pval, string, fmt, n_args, ap); }
-static int __init check_uchar(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_uchar(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const unsigned char *pval = check_data;
_check_numbers_template("%hhu", pval, string, fmt, n_args, ap); }
-static int __init check_char(const void *check_data, const char *string, - const char *fmt, int n_args, va_list ap) +static int __init check_char(const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const signed char *pval = check_data;
@@ -196,7 +196,7 @@ do { \ T result = ~expect_val; /* should be overwritten */ \ \ snprintf(test_buffer, BUF_SIZE, gen_fmt, expect_val); \ - _test(fn, &expect_val, test_buffer, "%" scan_fmt, 1, &result); \ + _test(__FILE__, __LINE__, fn, &expect_val, test_buffer, "%" scan_fmt, 1, &result); \ } while (0)
#define simple_numbers_loop(T, gen_fmt, scan_fmt, fn) \ @@ -344,7 +344,7 @@ static void __init append_delim(char *str_buf, int *str_buf_pos, int str_buf_len #define test_array_8(fn, check_data, string, fmt, arr) \ do { \ BUILD_BUG_ON(ARRAY_SIZE(arr) != 8); \ - _test(fn, check_data, string, fmt, 8, \ + _test(__FILE__, __LINE__, fn, check_data, string, fmt, 8, \ &(arr)[0], &(arr)[1], &(arr)[2], &(arr)[3], \ &(arr)[4], &(arr)[5], &(arr)[6], &(arr)[7]); \ } while (0) @@ -608,7 +608,7 @@ do { \ const T expect[2] = { expect0, expect1 }; \ T result[2] = { (T)~expect[0], (T)~expect[1] }; \ \ - _test(fn, &expect, str, scan_fmt, n_args, &result[0], &result[1]); \ + _test(__FILE__, __LINE__, fn, &expect, str, scan_fmt, n_args, &result[0], &result[1]); \ } while (0)
/*
Remove `pr_debug` calls which emit information already contained in `pr_warn` calls that occur on test failure. This reduces unhelpful test verbosity.
Note that a `pr_debug` removed from `_check_numbers_template` appears to have been the only guard against silent false positives, but in fact this condition is handled in `_test`; it is only possible for `n_args` to be `0` in `_check_numbers_template` if the test explicitly expects it *and* `vsscanf` returns `0`, matching the expectation.
Reviewed-by: Petr Mladek pmladek@suse.com Signed-off-by: Tamir Duberstein tamird@gmail.com --- lib/test_scanf.c | 3 --- 1 file changed, 3 deletions(-)
diff --git a/lib/test_scanf.c b/lib/test_scanf.c index e65b10c3dc11..cb0b57dc7383 100644 --- a/lib/test_scanf.c +++ b/lib/test_scanf.c @@ -62,10 +62,8 @@ _test(const char *file, const int line, check_fn fn, const void *check_data, con
#define _check_numbers_template(arg_fmt, expect, str, fmt, n_args, ap) \ do { \ - pr_debug(""%s", "%s" ->\n", str, fmt); \ for (; n_args > 0; n_args--, expect++) { \ typeof(*expect) got = *va_arg(ap, typeof(expect)); \ - pr_debug("\t" arg_fmt "\n", got); \ if (got != *expect) { \ pr_warn("%s:%d, vsscanf("%s", "%s", ...) expected " arg_fmt " got " arg_fmt "\n", \ file, line, str, fmt, *expect, got); \ @@ -689,7 +687,6 @@ do { \ total_tests++; \ len = snprintf(test_buffer, BUF_SIZE, gen_fmt, expect); \ got = (fn)(test_buffer, &endp, base); \ - pr_debug(#fn "("%s", %d) -> " gen_fmt "\n", test_buffer, base, got); \ if (got != (expect)) { \ fail = true; \ pr_warn(#fn "("%s", %d): got " gen_fmt " expected " gen_fmt "\n", \
Convert the scanf() self-test to a KUnit test.
In the interest of keeping the patch reasonably-sized this doesn't refactor the tests into proper parameterized tests - it's all one big test case.
Reviewed-by: David Gow davidgow@google.com Reviewed-by: Petr Mladek pmladek@suse.com Tested-by: Petr Mladek pmladek@suse.com Signed-off-by: Tamir Duberstein tamird@gmail.com --- MAINTAINERS | 2 +- lib/Kconfig.debug | 12 +- lib/Makefile | 1 - lib/tests/Makefile | 1 + lib/{test_scanf.c => tests/scanf_kunit.c} | 252 +++++++++++++++--------------- tools/testing/selftests/lib/Makefile | 2 +- tools/testing/selftests/lib/config | 1 - tools/testing/selftests/lib/scanf.sh | 4 - 8 files changed, 134 insertions(+), 141 deletions(-)
diff --git a/MAINTAINERS b/MAINTAINERS index 4e9e0e52f92e..f5e2c420ed5d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -25599,8 +25599,8 @@ R: Sergey Senozhatsky senozhatsky@chromium.org S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git F: Documentation/core-api/printk-formats.rst +F: lib/tests/scanf_kunit.c F: lib/test_printf.c -F: lib/test_scanf.c F: lib/vsprintf.c
VT1211 HARDWARE MONITOR DRIVER diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ebb5b190e9f9..af6262f84af6 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2436,6 +2436,15 @@ config ASYNC_RAID6_TEST config TEST_HEXDUMP tristate "Test functions located in the hexdump module at runtime"
+config SCANF_KUNIT_TEST + tristate "KUnit test scanf() family of functions at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Enable this option to test the scanf functions at runtime. + + If unsure, say N. + config STRING_KUNIT_TEST tristate "KUnit test string functions at runtime" if !KUNIT_ALL_TESTS depends on KUNIT @@ -2452,9 +2461,6 @@ config TEST_KSTRTOX config TEST_PRINTF tristate "Test printf() family of functions at runtime"
-config TEST_SCANF - tristate "Test scanf() family of functions at runtime" - config TEST_BITMAP tristate "Test bitmap_*() family of functions at runtime" help diff --git a/lib/Makefile b/lib/Makefile index 7bab71e59019..821dd8447f11 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -78,7 +78,6 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o obj-$(CONFIG_TEST_PRINTF) += test_printf.o -obj-$(CONFIG_TEST_SCANF) += test_scanf.o
obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o ifeq ($(CONFIG_CC_IS_CLANG)$(CONFIG_KASAN),yy) diff --git a/lib/tests/Makefile b/lib/tests/Makefile index 8961fbcff7a4..db9b685f2d88 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare) obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o +obj-$(CONFIG_SCANF_KUNIT_TEST) += scanf_kunit.o obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o obj-$(CONFIG_SLUB_KUNIT_TEST) += slub_kunit.o obj-$(CONFIG_TEST_SORT) += test_sort.o diff --git a/lib/test_scanf.c b/lib/tests/scanf_kunit.c similarity index 78% rename from lib/test_scanf.c rename to lib/tests/scanf_kunit.c index cb0b57dc7383..94eb5ec80fdb 100644 --- a/lib/test_scanf.c +++ b/lib/tests/scanf_kunit.c @@ -3,60 +3,44 @@ * Test cases for sscanf facility. */
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt - +#include <kunit/test.h> #include <linux/bitops.h> -#include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/overflow.h> -#include <linux/printk.h> #include <linux/prandom.h> #include <linux/slab.h> #include <linux/string.h>
-#include "../tools/testing/selftests/kselftest_module.h" - #define BUF_SIZE 1024
-KSTM_MODULE_GLOBALS(); -static char *test_buffer __initdata; -static char *fmt_buffer __initdata; -static struct rnd_state rnd_state __initdata; +static char *test_buffer; +static char *fmt_buffer; +static struct rnd_state rnd_state;
-typedef int (*check_fn)(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap); +typedef void (*check_fn)(struct kunit *test, const char *file, const int line, + const void *check_data, const char *string, const char *fmt, int n_args, + va_list ap);
-static void __scanf(6, 8) __init -_test(const char *file, const int line, check_fn fn, const void *check_data, const char *string, - const char *fmt, int n_args, ...) +static void __scanf(7, 9) +_test(struct kunit *test, const char *file, const int line, check_fn fn, const void *check_data, + const char *string, const char *fmt, int n_args, ...) { va_list ap, ap_copy; int ret;
- total_tests++; - va_start(ap, n_args); va_copy(ap_copy, ap); ret = vsscanf(string, fmt, ap_copy); va_end(ap_copy);
if (ret != n_args) { - pr_warn("%s:%d: vsscanf("%s", "%s", ...) returned %d expected %d\n", - file, line, string, fmt, ret, n_args); - goto fail; + KUNIT_FAIL(test, "%s:%d: vsscanf("%s", "%s", ...) returned %d expected %d", + file, line, string, fmt, ret, n_args); + } else { + (*fn)(test, file, line, check_data, string, fmt, n_args, ap); }
- ret = (*fn)(file, line, check_data, string, fmt, n_args, ap); - if (ret) - goto fail; - - va_end(ap); - - return; - -fail: - failed_tests++; va_end(ap); }
@@ -65,88 +49,92 @@ do { \ for (; n_args > 0; n_args--, expect++) { \ typeof(*expect) got = *va_arg(ap, typeof(expect)); \ if (got != *expect) { \ - pr_warn("%s:%d, vsscanf("%s", "%s", ...) expected " arg_fmt " got " arg_fmt "\n", \ - file, line, str, fmt, *expect, got); \ - return 1; \ + KUNIT_FAIL(test, \ + "%s:%d: vsscanf("%s", "%s", ...) expected " arg_fmt " got " arg_fmt, \ + file, line, str, fmt, *expect, got); \ + return; \ } \ } \ - return 0; \ } while (0)
-static int __init check_ull(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_ull(struct kunit *test, const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const unsigned long long *pval = check_data;
_check_numbers_template("%llu", pval, string, fmt, n_args, ap); }
-static int __init check_ll(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_ll(struct kunit *test, const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const long long *pval = check_data;
_check_numbers_template("%lld", pval, string, fmt, n_args, ap); }
-static int __init check_ulong(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_ulong(struct kunit *test, const char *file, const int line, + const void *check_data, const char *string, const char *fmt, int n_args, + va_list ap) { const unsigned long *pval = check_data;
_check_numbers_template("%lu", pval, string, fmt, n_args, ap); }
-static int __init check_long(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_long(struct kunit *test, const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const long *pval = check_data;
_check_numbers_template("%ld", pval, string, fmt, n_args, ap); }
-static int __init check_uint(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_uint(struct kunit *test, const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const unsigned int *pval = check_data;
_check_numbers_template("%u", pval, string, fmt, n_args, ap); }
-static int __init check_int(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_int(struct kunit *test, const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const int *pval = check_data;
_check_numbers_template("%d", pval, string, fmt, n_args, ap); }
-static int __init check_ushort(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_ushort(struct kunit *test, const char *file, const int line, + const void *check_data, const char *string, const char *fmt, int n_args, + va_list ap) { const unsigned short *pval = check_data;
_check_numbers_template("%hu", pval, string, fmt, n_args, ap); }
-static int __init check_short(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_short(struct kunit *test, const char *file, const int line, + const void *check_data, const char *string, const char *fmt, int n_args, + va_list ap) { const short *pval = check_data;
_check_numbers_template("%hd", pval, string, fmt, n_args, ap); }
-static int __init check_uchar(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_uchar(struct kunit *test, const char *file, const int line, + const void *check_data, const char *string, const char *fmt, int n_args, + va_list ap) { const unsigned char *pval = check_data;
_check_numbers_template("%hhu", pval, string, fmt, n_args, ap); }
-static int __init check_char(const char *file, const int line, const void *check_data, - const char *string, const char *fmt, int n_args, va_list ap) +static void check_char(struct kunit *test, const char *file, const int line, const void *check_data, + const char *string, const char *fmt, int n_args, va_list ap) { const signed char *pval = check_data;
@@ -154,7 +142,7 @@ static int __init check_char(const char *file, const int line, const void *check }
/* Selection of interesting numbers to test, copied from test-kstrtox.c */ -static const unsigned long long numbers[] __initconst = { +static const unsigned long long numbers[] = { 0x0ULL, 0x1ULL, 0x7fULL, @@ -194,7 +182,7 @@ do { \ T result = ~expect_val; /* should be overwritten */ \ \ snprintf(test_buffer, BUF_SIZE, gen_fmt, expect_val); \ - _test(__FILE__, __LINE__, fn, &expect_val, test_buffer, "%" scan_fmt, 1, &result); \ + _test(test, __FILE__, __LINE__, fn, &expect_val, test_buffer, "%" scan_fmt, 1, &result);\ } while (0)
#define simple_numbers_loop(T, gen_fmt, scan_fmt, fn) \ @@ -212,7 +200,7 @@ do { \ } \ } while (0)
-static void __init numbers_simple(void) +static void numbers_simple(struct kunit *test) { simple_numbers_loop(unsigned long long, "%llu", "llu", check_ull); simple_numbers_loop(long long, "%lld", "lld", check_ll); @@ -265,14 +253,14 @@ static void __init numbers_simple(void) * the raw prandom*() functions (Not mathematically rigorous!!). * Variabilty of length and value is more important than perfect randomness. */ -static u32 __init next_test_random(u32 max_bits) +static u32 next_test_random(u32 max_bits) { u32 n_bits = hweight32(prandom_u32_state(&rnd_state)) % (max_bits + 1);
return prandom_u32_state(&rnd_state) & GENMASK(n_bits, 0); }
-static unsigned long long __init next_test_random_ull(void) +static unsigned long long next_test_random_ull(void) { u32 rand1 = prandom_u32_state(&rnd_state); u32 n_bits = (hweight32(rand1) * 3) % 64; @@ -309,7 +297,7 @@ do { \ * updating buf_pos and returning the number of characters appended. * On error buf_pos is not changed and return value is 0. */ -static int __init __printf(4, 5) +static int __printf(4, 5) append_fmt(char *buf, int *buf_pos, int buf_len, const char *val_fmt, ...) { va_list ap; @@ -331,7 +319,7 @@ append_fmt(char *buf, int *buf_pos, int buf_len, const char *val_fmt, ...) * Convenience function to append the field delimiter string * to both the value string and format string buffers. */ -static void __init append_delim(char *str_buf, int *str_buf_pos, int str_buf_len, +static void append_delim(char *str_buf, int *str_buf_pos, int str_buf_len, char *fmt_buf, int *fmt_buf_pos, int fmt_buf_len, const char *delim_str) { @@ -342,7 +330,7 @@ static void __init append_delim(char *str_buf, int *str_buf_pos, int str_buf_len #define test_array_8(fn, check_data, string, fmt, arr) \ do { \ BUILD_BUG_ON(ARRAY_SIZE(arr) != 8); \ - _test(__FILE__, __LINE__, fn, check_data, string, fmt, 8, \ + _test(test, __FILE__, __LINE__, fn, check_data, string, fmt, 8, \ &(arr)[0], &(arr)[1], &(arr)[2], &(arr)[3], \ &(arr)[4], &(arr)[5], &(arr)[6], &(arr)[7]); \ } while (0) @@ -396,7 +384,7 @@ do { \ test_array_8(fn, expect, test_buffer, fmt_buffer, result); \ } while (0)
-static void __init numbers_list_ll(const char *delim) +static void numbers_list_ll(struct kunit *test, const char *delim) { numbers_list_8(unsigned long long, "%llu", delim, "llu", check_ull); numbers_list_8(long long, "%lld", delim, "lld", check_ll); @@ -406,7 +394,7 @@ static void __init numbers_list_ll(const char *delim) numbers_list_8(long long, "0x%llx", delim, "lli", check_ll); }
-static void __init numbers_list_l(const char *delim) +static void numbers_list_l(struct kunit *test, const char *delim) { numbers_list_8(unsigned long, "%lu", delim, "lu", check_ulong); numbers_list_8(long, "%ld", delim, "ld", check_long); @@ -416,7 +404,7 @@ static void __init numbers_list_l(const char *delim) numbers_list_8(long, "0x%lx", delim, "li", check_long); }
-static void __init numbers_list_d(const char *delim) +static void numbers_list_d(struct kunit *test, const char *delim) { numbers_list_8(unsigned int, "%u", delim, "u", check_uint); numbers_list_8(int, "%d", delim, "d", check_int); @@ -426,7 +414,7 @@ static void __init numbers_list_d(const char *delim) numbers_list_8(int, "0x%x", delim, "i", check_int); }
-static void __init numbers_list_h(const char *delim) +static void numbers_list_h(struct kunit *test, const char *delim) { numbers_list_8(unsigned short, "%hu", delim, "hu", check_ushort); numbers_list_8(short, "%hd", delim, "hd", check_short); @@ -436,7 +424,7 @@ static void __init numbers_list_h(const char *delim) numbers_list_8(short, "0x%hx", delim, "hi", check_short); }
-static void __init numbers_list_hh(const char *delim) +static void numbers_list_hh(struct kunit *test, const char *delim) { numbers_list_8(unsigned char, "%hhu", delim, "hhu", check_uchar); numbers_list_8(signed char, "%hhd", delim, "hhd", check_char); @@ -446,16 +434,16 @@ static void __init numbers_list_hh(const char *delim) numbers_list_8(signed char, "0x%hhx", delim, "hhi", check_char); }
-static void __init numbers_list(const char *delim) +static void numbers_list(struct kunit *test, const char *delim) { - numbers_list_ll(delim); - numbers_list_l(delim); - numbers_list_d(delim); - numbers_list_h(delim); - numbers_list_hh(delim); + numbers_list_ll(test, delim); + numbers_list_l(test, delim); + numbers_list_d(test, delim); + numbers_list_h(test, delim); + numbers_list_hh(test, delim); }
-static void __init numbers_list_field_width_ll(const char *delim) +static void numbers_list_field_width_ll(struct kunit *test, const char *delim) { numbers_list_fix_width(unsigned long long, "%llu", delim, 20, "llu", check_ull); numbers_list_fix_width(long long, "%lld", delim, 20, "lld", check_ll); @@ -465,7 +453,7 @@ static void __init numbers_list_field_width_ll(const char *delim) numbers_list_fix_width(long long, "0x%llx", delim, 18, "lli", check_ll); }
-static void __init numbers_list_field_width_l(const char *delim) +static void numbers_list_field_width_l(struct kunit *test, const char *delim) { #if BITS_PER_LONG == 64 numbers_list_fix_width(unsigned long, "%lu", delim, 20, "lu", check_ulong); @@ -484,7 +472,7 @@ static void __init numbers_list_field_width_l(const char *delim) #endif }
-static void __init numbers_list_field_width_d(const char *delim) +static void numbers_list_field_width_d(struct kunit *test, const char *delim) { numbers_list_fix_width(unsigned int, "%u", delim, 10, "u", check_uint); numbers_list_fix_width(int, "%d", delim, 11, "d", check_int); @@ -494,7 +482,7 @@ static void __init numbers_list_field_width_d(const char *delim) numbers_list_fix_width(int, "0x%x", delim, 10, "i", check_int); }
-static void __init numbers_list_field_width_h(const char *delim) +static void numbers_list_field_width_h(struct kunit *test, const char *delim) { numbers_list_fix_width(unsigned short, "%hu", delim, 5, "hu", check_ushort); numbers_list_fix_width(short, "%hd", delim, 6, "hd", check_short); @@ -504,7 +492,7 @@ static void __init numbers_list_field_width_h(const char *delim) numbers_list_fix_width(short, "0x%hx", delim, 6, "hi", check_short); }
-static void __init numbers_list_field_width_hh(const char *delim) +static void numbers_list_field_width_hh(struct kunit *test, const char *delim) { numbers_list_fix_width(unsigned char, "%hhu", delim, 3, "hhu", check_uchar); numbers_list_fix_width(signed char, "%hhd", delim, 4, "hhd", check_char); @@ -518,16 +506,16 @@ static void __init numbers_list_field_width_hh(const char *delim) * List of numbers separated by delim. Each field width specifier is the * maximum possible digits for the given type and base. */ -static void __init numbers_list_field_width_typemax(const char *delim) +static void numbers_list_field_width_typemax(struct kunit *test, const char *delim) { - numbers_list_field_width_ll(delim); - numbers_list_field_width_l(delim); - numbers_list_field_width_d(delim); - numbers_list_field_width_h(delim); - numbers_list_field_width_hh(delim); + numbers_list_field_width_ll(test, delim); + numbers_list_field_width_l(test, delim); + numbers_list_field_width_d(test, delim); + numbers_list_field_width_h(test, delim); + numbers_list_field_width_hh(test, delim); }
-static void __init numbers_list_field_width_val_ll(const char *delim) +static void numbers_list_field_width_val_ll(struct kunit *test, const char *delim) { numbers_list_val_width(unsigned long long, "%llu", delim, "llu", check_ull); numbers_list_val_width(long long, "%lld", delim, "lld", check_ll); @@ -537,7 +525,7 @@ static void __init numbers_list_field_width_val_ll(const char *delim) numbers_list_val_width(long long, "0x%llx", delim, "lli", check_ll); }
-static void __init numbers_list_field_width_val_l(const char *delim) +static void numbers_list_field_width_val_l(struct kunit *test, const char *delim) { numbers_list_val_width(unsigned long, "%lu", delim, "lu", check_ulong); numbers_list_val_width(long, "%ld", delim, "ld", check_long); @@ -547,7 +535,7 @@ static void __init numbers_list_field_width_val_l(const char *delim) numbers_list_val_width(long, "0x%lx", delim, "li", check_long); }
-static void __init numbers_list_field_width_val_d(const char *delim) +static void numbers_list_field_width_val_d(struct kunit *test, const char *delim) { numbers_list_val_width(unsigned int, "%u", delim, "u", check_uint); numbers_list_val_width(int, "%d", delim, "d", check_int); @@ -557,7 +545,7 @@ static void __init numbers_list_field_width_val_d(const char *delim) numbers_list_val_width(int, "0x%x", delim, "i", check_int); }
-static void __init numbers_list_field_width_val_h(const char *delim) +static void numbers_list_field_width_val_h(struct kunit *test, const char *delim) { numbers_list_val_width(unsigned short, "%hu", delim, "hu", check_ushort); numbers_list_val_width(short, "%hd", delim, "hd", check_short); @@ -567,7 +555,7 @@ static void __init numbers_list_field_width_val_h(const char *delim) numbers_list_val_width(short, "0x%hx", delim, "hi", check_short); }
-static void __init numbers_list_field_width_val_hh(const char *delim) +static void numbers_list_field_width_val_hh(struct kunit *test, const char *delim) { numbers_list_val_width(unsigned char, "%hhu", delim, "hhu", check_uchar); numbers_list_val_width(signed char, "%hhd", delim, "hhd", check_char); @@ -581,13 +569,13 @@ static void __init numbers_list_field_width_val_hh(const char *delim) * List of numbers separated by delim. Each field width specifier is the * exact length of the corresponding value digits in the string being scanned. */ -static void __init numbers_list_field_width_val_width(const char *delim) +static void numbers_list_field_width_val_width(struct kunit *test, const char *delim) { - numbers_list_field_width_val_ll(delim); - numbers_list_field_width_val_l(delim); - numbers_list_field_width_val_d(delim); - numbers_list_field_width_val_h(delim); - numbers_list_field_width_val_hh(delim); + numbers_list_field_width_val_ll(test, delim); + numbers_list_field_width_val_l(test, delim); + numbers_list_field_width_val_d(test, delim); + numbers_list_field_width_val_h(test, delim); + numbers_list_field_width_val_hh(test, delim); }
/* @@ -596,9 +584,9 @@ static void __init numbers_list_field_width_val_width(const char *delim) * of digits. For example the hex values c0,3,bf01,303 would have a * string representation of "c03bf01303" and extracted with "%2x%1x%4x%3x". */ -static void __init numbers_slice(void) +static void numbers_slice(struct kunit *test) { - numbers_list_field_width_val_width(""); + numbers_list_field_width_val_width(test, ""); }
#define test_number_prefix(T, str, scan_fmt, expect0, expect1, n_args, fn) \ @@ -606,14 +594,14 @@ do { \ const T expect[2] = { expect0, expect1 }; \ T result[2] = { (T)~expect[0], (T)~expect[1] }; \ \ - _test(__FILE__, __LINE__, fn, &expect, str, scan_fmt, n_args, &result[0], &result[1]); \ + _test(test, __FILE__, __LINE__, fn, &expect, str, scan_fmt, n_args, &result[0], &result[1]);\ } while (0)
/* * Number prefix is >= field width. * Expected behaviour is derived from testing userland sscanf. */ -static void __init numbers_prefix_overflow(void) +static void numbers_prefix_overflow(struct kunit *test) { /* * Negative decimal with a field of width 1, should quit scanning @@ -682,24 +670,17 @@ do { \ T got; \ char *endp; \ int len; \ - bool fail = false; \ \ - total_tests++; \ len = snprintf(test_buffer, BUF_SIZE, gen_fmt, expect); \ got = (fn)(test_buffer, &endp, base); \ if (got != (expect)) { \ - fail = true; \ - pr_warn(#fn "("%s", %d): got " gen_fmt " expected " gen_fmt "\n", \ - test_buffer, base, got, expect); \ + KUNIT_FAIL(test, #fn "("%s", %d): got " gen_fmt " expected " gen_fmt, \ + test_buffer, base, got, expect); \ } else if (endp != test_buffer + len) { \ - fail = true; \ - pr_warn(#fn "("%s", %d) startp=0x%px got endp=0x%px expected 0x%px\n", \ - test_buffer, base, test_buffer, \ - test_buffer + len, endp); \ + KUNIT_FAIL(test, #fn "("%s", %d) startp=0x%px got endp=0x%px expected 0x%px", \ + test_buffer, base, test_buffer, \ + test_buffer + len, endp); \ } \ - \ - if (fail) \ - failed_tests++; \ } while (0)
#define test_simple_strtoxx(T, fn, gen_fmt, base) \ @@ -715,7 +696,7 @@ do { \ } \ } while (0)
-static void __init test_simple_strtoull(void) +static void test_simple_strtoull(struct kunit *test) { test_simple_strtoxx(unsigned long long, simple_strtoull, "%llu", 10); test_simple_strtoxx(unsigned long long, simple_strtoull, "%llu", 0); @@ -724,7 +705,7 @@ static void __init test_simple_strtoull(void) test_simple_strtoxx(unsigned long long, simple_strtoull, "0x%llx", 0); }
-static void __init test_simple_strtoll(void) +static void test_simple_strtoll(struct kunit *test) { test_simple_strtoxx(long long, simple_strtoll, "%lld", 10); test_simple_strtoxx(long long, simple_strtoll, "%lld", 0); @@ -733,7 +714,7 @@ static void __init test_simple_strtoll(void) test_simple_strtoxx(long long, simple_strtoll, "0x%llx", 0); }
-static void __init test_simple_strtoul(void) +static void test_simple_strtoul(struct kunit *test) { test_simple_strtoxx(unsigned long, simple_strtoul, "%lu", 10); test_simple_strtoxx(unsigned long, simple_strtoul, "%lu", 0); @@ -742,7 +723,7 @@ static void __init test_simple_strtoul(void) test_simple_strtoxx(unsigned long, simple_strtoul, "0x%lx", 0); }
-static void __init test_simple_strtol(void) +static void test_simple_strtol(struct kunit *test) { test_simple_strtoxx(long, simple_strtol, "%ld", 10); test_simple_strtoxx(long, simple_strtol, "%ld", 0); @@ -752,35 +733,35 @@ static void __init test_simple_strtol(void) }
/* Selection of common delimiters/separators between numbers in a string. */ -static const char * const number_delimiters[] __initconst = { +static const char * const number_delimiters[] = { " ", ":", ",", "-", "/", };
-static void __init test_numbers(void) +static void test_numbers(struct kunit *test) { int i;
/* String containing only one number. */ - numbers_simple(); + numbers_simple(test);
/* String with multiple numbers separated by delimiter. */ for (i = 0; i < ARRAY_SIZE(number_delimiters); i++) { - numbers_list(number_delimiters[i]); + numbers_list(test, number_delimiters[i]);
/* Field width may be longer than actual field digits. */ - numbers_list_field_width_typemax(number_delimiters[i]); + numbers_list_field_width_typemax(test, number_delimiters[i]);
/* Each field width exactly length of actual field digits. */ - numbers_list_field_width_val_width(number_delimiters[i]); + numbers_list_field_width_val_width(test, number_delimiters[i]); }
/* Slice continuous sequence of digits using field widths. */ - numbers_slice(); + numbers_slice(test);
- numbers_prefix_overflow(); + numbers_prefix_overflow(test); }
-static void __init selftest(void) +static void scanf_test(struct kunit *test) { test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL); if (!test_buffer) @@ -794,18 +775,29 @@ static void __init selftest(void)
prandom_seed_state(&rnd_state, 3141592653589793238ULL);
- test_numbers(); + test_numbers(test);
- test_simple_strtoull(); - test_simple_strtoll(); - test_simple_strtoul(); - test_simple_strtol(); + test_simple_strtoull(test); + test_simple_strtoll(test); + test_simple_strtoul(test); + test_simple_strtol(test);
kfree(fmt_buffer); kfree(test_buffer); }
-KSTM_MODULE_LOADERS(test_scanf); +static struct kunit_case scanf_test_cases[] = { + KUNIT_CASE(scanf_test), + {} +}; + +static struct kunit_suite scanf_test_suite = { + .name = "scanf", + .test_cases = scanf_test_cases, +}; + +kunit_test_suite(scanf_test_suite); + MODULE_AUTHOR("Richard Fitzgerald rf@opensource.cirrus.com"); MODULE_DESCRIPTION("Test cases for sscanf facility"); MODULE_LICENSE("GPL v2"); diff --git a/tools/testing/selftests/lib/Makefile b/tools/testing/selftests/lib/Makefile index 66dcbe2e39fa..dda63c32c0f0 100644 --- a/tools/testing/selftests/lib/Makefile +++ b/tools/testing/selftests/lib/Makefile @@ -4,5 +4,5 @@ # No binaries, but make sure arg-less "make" doesn't trigger "run_tests" all:
-TEST_PROGS := printf.sh bitmap.sh scanf.sh +TEST_PROGS := printf.sh bitmap.sh include ../lib.mk diff --git a/tools/testing/selftests/lib/config b/tools/testing/selftests/lib/config index 306a3d4dca98..1d17fa85de8f 100644 --- a/tools/testing/selftests/lib/config +++ b/tools/testing/selftests/lib/config @@ -1,4 +1,3 @@ CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_BITOPS=m diff --git a/tools/testing/selftests/lib/scanf.sh b/tools/testing/selftests/lib/scanf.sh deleted file mode 100755 index b59b8ba561c3..000000000000 --- a/tools/testing/selftests/lib/scanf.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: GPL-2.0 -# Tests the scanf infrastructure using test_scanf kernel module. -$(dirname $0)/../kselftest/module.sh "scanf" test_scanf
Use `suite_init` and move some tests into `scanf_test_cases`. This gives us nicer output in the event of a failure.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Tamir Duberstein tamird@gmail.com --- lib/tests/scanf_kunit.c | 82 ++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 35 deletions(-)
diff --git a/lib/tests/scanf_kunit.c b/lib/tests/scanf_kunit.c index 94eb5ec80fdb..e9a36ed80575 100644 --- a/lib/tests/scanf_kunit.c +++ b/lib/tests/scanf_kunit.c @@ -434,8 +434,11 @@ static void numbers_list_hh(struct kunit *test, const char *delim) numbers_list_8(signed char, "0x%hhx", delim, "hhi", check_char); }
-static void numbers_list(struct kunit *test, const char *delim) +static void numbers_list(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_ll(test, delim); numbers_list_l(test, delim); numbers_list_d(test, delim); @@ -506,8 +509,11 @@ static void numbers_list_field_width_hh(struct kunit *test, const char *delim) * List of numbers separated by delim. Each field width specifier is the * maximum possible digits for the given type and base. */ -static void numbers_list_field_width_typemax(struct kunit *test, const char *delim) +static void numbers_list_field_width_typemax(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_field_width_ll(test, delim); numbers_list_field_width_l(test, delim); numbers_list_field_width_d(test, delim); @@ -569,8 +575,11 @@ static void numbers_list_field_width_val_hh(struct kunit *test, const char *deli * List of numbers separated by delim. Each field width specifier is the * exact length of the corresponding value digits in the string being scanned. */ -static void numbers_list_field_width_val_width(struct kunit *test, const char *delim) +static void numbers_list_field_width_val_width(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_field_width_val_ll(test, delim); numbers_list_field_width_val_l(test, delim); numbers_list_field_width_val_d(test, delim); @@ -586,7 +595,12 @@ static void numbers_list_field_width_val_width(struct kunit *test, const char *d */ static void numbers_slice(struct kunit *test) { - numbers_list_field_width_val_width(test, ""); + const char *delim = ""; + + KUNIT_ASSERT_PTR_EQ(test, test->param_value, NULL); + test->param_value = &delim; + + numbers_list_field_width_val_width(test); }
#define test_number_prefix(T, str, scan_fmt, expect0, expect1, n_args, fn) \ @@ -737,62 +751,60 @@ static const char * const number_delimiters[] = { " ", ":", ",", "-", "/", };
-static void test_numbers(struct kunit *test) +static void number_delimiter_param_desc(const char * const *param, + char *desc) { - int i; + snprintf(desc, KUNIT_PARAM_DESC_SIZE, ""%s"", *param); +}
- /* String containing only one number. */ - numbers_simple(test); +KUNIT_ARRAY_PARAM(number_delimiters, number_delimiters, number_delimiter_param_desc);
+static struct kunit_case scanf_test_cases[] = { + KUNIT_CASE(numbers_simple), /* String with multiple numbers separated by delimiter. */ - for (i = 0; i < ARRAY_SIZE(number_delimiters); i++) { - numbers_list(test, number_delimiters[i]); - - /* Field width may be longer than actual field digits. */ - numbers_list_field_width_typemax(test, number_delimiters[i]); - - /* Each field width exactly length of actual field digits. */ - numbers_list_field_width_val_width(test, number_delimiters[i]); - } - + KUNIT_CASE_PARAM(numbers_list, number_delimiters_gen_params), + /* Field width may be longer than actual field digits. */ + KUNIT_CASE_PARAM(numbers_list_field_width_typemax, number_delimiters_gen_params), + /* Each field width exactly length of actual field digits. */ + KUNIT_CASE_PARAM(numbers_list_field_width_val_width, number_delimiters_gen_params), /* Slice continuous sequence of digits using field widths. */ - numbers_slice(test); + KUNIT_CASE(numbers_slice), + KUNIT_CASE(numbers_prefix_overflow),
- numbers_prefix_overflow(test); -} + KUNIT_CASE(test_simple_strtoull), + KUNIT_CASE(test_simple_strtoll), + KUNIT_CASE(test_simple_strtoul), + KUNIT_CASE(test_simple_strtol), + {} +};
-static void scanf_test(struct kunit *test) +static int scanf_suite_init(struct kunit_suite *suite) { test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL); if (!test_buffer) - return; + return -ENOMEM;
fmt_buffer = kmalloc(BUF_SIZE, GFP_KERNEL); if (!fmt_buffer) { kfree(test_buffer); - return; + return -ENOMEM; }
prandom_seed_state(&rnd_state, 3141592653589793238ULL);
- test_numbers(test); - - test_simple_strtoull(test); - test_simple_strtoll(test); - test_simple_strtoul(test); - test_simple_strtol(test); + return 0; +}
+static void scanf_suite_exit(struct kunit_suite *suite) +{ kfree(fmt_buffer); kfree(test_buffer); }
-static struct kunit_case scanf_test_cases[] = { - KUNIT_CASE(scanf_test), - {} -}; - static struct kunit_suite scanf_test_suite = { .name = "scanf", + .suite_init = scanf_suite_init, + .suite_exit = scanf_suite_exit, .test_cases = scanf_test_cases, };
On Fri 2025-03-07 06:27:37, Tamir Duberstein wrote:
Use `suite_init` and move some tests into `scanf_test_cases`. This gives us nicer output in the event of a failure.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Tamir Duberstein tamird@gmail.com
Reviewed-by: Petr Mladek pmladek@suse.com Tested-by: Petr Mladek pmladek@suse.com
Best Regards, Petr
Remove the unused `kernel.h`. Replace `string.h` with `sprintf.h` as the former doesn't seem to be used directly.
Signed-off-by: Tamir Duberstein tamird@gmail.com --- lib/tests/scanf_kunit.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/lib/tests/scanf_kunit.c b/lib/tests/scanf_kunit.c index e9a36ed80575..d96d7ca85b52 100644 --- a/lib/tests/scanf_kunit.c +++ b/lib/tests/scanf_kunit.c @@ -5,12 +5,11 @@
#include <kunit/test.h> #include <linux/bitops.h> -#include <linux/kernel.h> #include <linux/module.h> #include <linux/overflow.h> #include <linux/prandom.h> #include <linux/slab.h> -#include <linux/string.h> +#include <linux/sprintf.h>
#define BUF_SIZE 1024
On Fri, Mar 07, 2025 at 06:27:38AM -0500, Tamir Duberstein wrote:
Remove the unused `kernel.h`. Replace `string.h` with `sprintf.h` as the former doesn't seem to be used directly.
...
#include <kunit/test.h>
+ array_size.h
#include <linux/bitops.h>
+ bug.h // BUILD_BUG_ON() + errno.h // actually asm/errno.h, but in C code the linux/* will suffice
-#include <linux/kernel.h>
This was used like a "proxy" header to a lot, see around.
#include <linux/module.h> #include <linux/overflow.h> #include <linux/prandom.h> #include <linux/slab.h> -#include <linux/string.h> +#include <linux/sprintf.h>
+ types.h // u32, __scanf, ...
...
Note,the above I made based on the context of your changes, I haven't checked the resulting file in full, please do it yourself.
On Fri, Mar 07, 2025 at 07:34:29PM +0200, Andy Shevchenko wrote:
On Fri, Mar 07, 2025 at 06:27:38AM -0500, Tamir Duberstein wrote:
...
#include <kunit/test.h>
- array_size.h
#include <linux/bitops.h>
- bug.h // BUILD_BUG_ON()
Actually if it's only BUILD_BUG_ON(), then we better use build_bug.h.
- errno.h // actually asm/errno.h, but in C code the linux/* will suffice
-#include <linux/kernel.h>
This was used like a "proxy" header to a lot, see around.
#include <linux/module.h> #include <linux/overflow.h> #include <linux/prandom.h> #include <linux/slab.h> -#include <linux/string.h> +#include <linux/sprintf.h>
- types.h // u32, __scanf, ...
...
Note,the above I made based on the context of your changes, I haven't checked the resulting file in full, please do it yourself.
On Fri, Mar 7, 2025 at 12:40 PM Andy Shevchenko andriy.shevchenko@linux.intel.com wrote:
On Fri, Mar 07, 2025 at 07:34:29PM +0200, Andy Shevchenko wrote:
On Fri, Mar 07, 2025 at 06:27:38AM -0500, Tamir Duberstein wrote:
...
#include <kunit/test.h>
- array_size.h
#include <linux/bitops.h>
- bug.h // BUILD_BUG_ON()
Actually if it's only BUILD_BUG_ON(), then we better use build_bug.h.
- errno.h // actually asm/errno.h, but in C code the linux/* will suffice
-#include <linux/kernel.h>
This was used like a "proxy" header to a lot, see around.
#include <linux/module.h> #include <linux/overflow.h> #include <linux/prandom.h> #include <linux/slab.h> -#include <linux/string.h> +#include <linux/sprintf.h>
- types.h // u32, __scanf, ...
...
Note,the above I made based on the context of your changes, I haven't checked the resulting file in full, please do it yourself.
-- With Best Regards, Andy Shevchenko
If it's easier to take this series without this patch, please do so -- my tools aren't giving me any help with IWYU and I'm not familiar enough with the kernel to do a thorough manual job. As this is not core to the goals of this series, I'd prefer not to hold it up over it.
Thanks.
Tamir
On Fri 2025-03-07 06:27:38, Tamir Duberstein wrote:
Remove the unused `kernel.h`. Replace `string.h` with `sprintf.h` as the former doesn't seem to be used directly.
--- a/lib/tests/scanf_kunit.c +++ b/lib/tests/scanf_kunit.c @@ -5,12 +5,11 @@ #include <kunit/test.h> #include <linux/bitops.h> -#include <linux/kernel.h> #include <linux/module.h> #include <linux/overflow.h> #include <linux/prandom.h> #include <linux/slab.h> -#include <linux/string.h> +#include <linux/sprintf.h> #define BUF_SIZE 1024
The change makes sense and looks good to me. I would go with it.
Reviewed-by: Petr Mladek pmladek@suse.com Tested-by: Petr Mladek pmladek@suse.com
Further improvements might be done later in a separate patchset...
Best Regards, Petr
This gives us more granular test output.
Output before:
KTAP version 1 1..1 KTAP version 1 # Subtest: scanf # module: scanf_kunit 1..10 ok 1 numbers_simple KTAP version 1 # Subtest: numbers_list ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list: pass:5 fail:0 skip:0 total:5 ok 2 numbers_list KTAP version 1 # Subtest: numbers_list_field_width_typemax ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_typemax: pass:5 fail:0 skip:0 total:5 ok 3 numbers_list_field_width_typemax KTAP version 1 # Subtest: numbers_list_field_width_val_width ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_val_width: pass:5 fail:0 skip:0 total:5 ok 4 numbers_list_field_width_val_width ok 5 numbers_slice ok 6 numbers_prefix_overflow ok 7 test_simple_strtoull ok 8 test_simple_strtoll ok 9 test_simple_strtoul ok 10 test_simple_strtol # scanf: pass:10 fail:0 skip:0 total:10 # Totals: pass:22 fail:0 skip:0 total:22 ok 1 scanf
Output after:
KTAP version 1 1..1 KTAP version 1 # Subtest: scanf # module: scanf_kunit 1..21 ok 1 numbers_simple KTAP version 1 # Subtest: numbers_list_ll ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_ll: pass:5 fail:0 skip:0 total:5 ok 2 numbers_list_ll KTAP version 1 # Subtest: numbers_list_l ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_l: pass:5 fail:0 skip:0 total:5 ok 3 numbers_list_l KTAP version 1 # Subtest: numbers_list_d ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_d: pass:5 fail:0 skip:0 total:5 ok 4 numbers_list_d KTAP version 1 # Subtest: numbers_list_h ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_h: pass:5 fail:0 skip:0 total:5 ok 5 numbers_list_h KTAP version 1 # Subtest: numbers_list_hh ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_hh: pass:5 fail:0 skip:0 total:5 ok 6 numbers_list_hh KTAP version 1 # Subtest: numbers_list_field_width_ll ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_ll: pass:5 fail:0 skip:0 total:5 ok 7 numbers_list_field_width_ll KTAP version 1 # Subtest: numbers_list_field_width_l ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_l: pass:5 fail:0 skip:0 total:5 ok 8 numbers_list_field_width_l KTAP version 1 # Subtest: numbers_list_field_width_d ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_d: pass:5 fail:0 skip:0 total:5 ok 9 numbers_list_field_width_d KTAP version 1 # Subtest: numbers_list_field_width_h ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_h: pass:5 fail:0 skip:0 total:5 ok 10 numbers_list_field_width_h KTAP version 1 # Subtest: numbers_list_field_width_hh ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_hh: pass:5 fail:0 skip:0 total:5 ok 11 numbers_list_field_width_hh KTAP version 1 # Subtest: numbers_list_field_width_val_ll ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_ll: pass:6 fail:0 skip:0 total:6 ok 12 numbers_list_field_width_val_ll KTAP version 1 # Subtest: numbers_list_field_width_val_l ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_l: pass:6 fail:0 skip:0 total:6 ok 13 numbers_list_field_width_val_l KTAP version 1 # Subtest: numbers_list_field_width_val_d ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_d: pass:6 fail:0 skip:0 total:6 ok 14 numbers_list_field_width_val_d KTAP version 1 # Subtest: numbers_list_field_width_val_h ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_h: pass:6 fail:0 skip:0 total:6 ok 15 numbers_list_field_width_val_h KTAP version 1 # Subtest: numbers_list_field_width_val_hh ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_hh: pass:6 fail:0 skip:0 total:6 ok 16 numbers_list_field_width_val_hh ok 17 numbers_prefix_overflow ok 18 test_simple_strtoull ok 19 test_simple_strtoll ok 20 test_simple_strtoul ok 21 test_simple_strtol # scanf: pass:21 fail:0 skip:0 total:21 # Totals: pass:86 fail:0 skip:0 total:86 ok 1 scanf
Signed-off-by: Tamir Duberstein tamird@gmail.com --- lib/tests/scanf_kunit.c | 170 ++++++++++++++++++++++++++---------------------- 1 file changed, 94 insertions(+), 76 deletions(-)
diff --git a/lib/tests/scanf_kunit.c b/lib/tests/scanf_kunit.c index d96d7ca85b52..2193937039a4 100644 --- a/lib/tests/scanf_kunit.c +++ b/lib/tests/scanf_kunit.c @@ -383,8 +383,11 @@ do { \ test_array_8(fn, expect, test_buffer, fmt_buffer, result); \ } while (0)
-static void numbers_list_ll(struct kunit *test, const char *delim) +static void numbers_list_ll(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_8(unsigned long long, "%llu", delim, "llu", check_ull); numbers_list_8(long long, "%lld", delim, "lld", check_ll); numbers_list_8(long long, "%lld", delim, "lli", check_ll); @@ -393,8 +396,11 @@ static void numbers_list_ll(struct kunit *test, const char *delim) numbers_list_8(long long, "0x%llx", delim, "lli", check_ll); }
-static void numbers_list_l(struct kunit *test, const char *delim) +static void numbers_list_l(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_8(unsigned long, "%lu", delim, "lu", check_ulong); numbers_list_8(long, "%ld", delim, "ld", check_long); numbers_list_8(long, "%ld", delim, "li", check_long); @@ -403,8 +409,11 @@ static void numbers_list_l(struct kunit *test, const char *delim) numbers_list_8(long, "0x%lx", delim, "li", check_long); }
-static void numbers_list_d(struct kunit *test, const char *delim) +static void numbers_list_d(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_8(unsigned int, "%u", delim, "u", check_uint); numbers_list_8(int, "%d", delim, "d", check_int); numbers_list_8(int, "%d", delim, "i", check_int); @@ -413,8 +422,11 @@ static void numbers_list_d(struct kunit *test, const char *delim) numbers_list_8(int, "0x%x", delim, "i", check_int); }
-static void numbers_list_h(struct kunit *test, const char *delim) +static void numbers_list_h(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_8(unsigned short, "%hu", delim, "hu", check_ushort); numbers_list_8(short, "%hd", delim, "hd", check_short); numbers_list_8(short, "%hd", delim, "hi", check_short); @@ -423,8 +435,11 @@ static void numbers_list_h(struct kunit *test, const char *delim) numbers_list_8(short, "0x%hx", delim, "hi", check_short); }
-static void numbers_list_hh(struct kunit *test, const char *delim) +static void numbers_list_hh(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_8(unsigned char, "%hhu", delim, "hhu", check_uchar); numbers_list_8(signed char, "%hhd", delim, "hhd", check_char); numbers_list_8(signed char, "%hhd", delim, "hhi", check_char); @@ -433,20 +448,11 @@ static void numbers_list_hh(struct kunit *test, const char *delim) numbers_list_8(signed char, "0x%hhx", delim, "hhi", check_char); }
-static void numbers_list(struct kunit *test) +static void numbers_list_field_width_ll(struct kunit *test) { const char * const *param = test->param_value; const char *delim = *param;
- numbers_list_ll(test, delim); - numbers_list_l(test, delim); - numbers_list_d(test, delim); - numbers_list_h(test, delim); - numbers_list_hh(test, delim); -} - -static void numbers_list_field_width_ll(struct kunit *test, const char *delim) -{ numbers_list_fix_width(unsigned long long, "%llu", delim, 20, "llu", check_ull); numbers_list_fix_width(long long, "%lld", delim, 20, "lld", check_ll); numbers_list_fix_width(long long, "%lld", delim, 20, "lli", check_ll); @@ -455,8 +461,11 @@ static void numbers_list_field_width_ll(struct kunit *test, const char *delim) numbers_list_fix_width(long long, "0x%llx", delim, 18, "lli", check_ll); }
-static void numbers_list_field_width_l(struct kunit *test, const char *delim) +static void numbers_list_field_width_l(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + #if BITS_PER_LONG == 64 numbers_list_fix_width(unsigned long, "%lu", delim, 20, "lu", check_ulong); numbers_list_fix_width(long, "%ld", delim, 20, "ld", check_long); @@ -474,8 +483,11 @@ static void numbers_list_field_width_l(struct kunit *test, const char *delim) #endif }
-static void numbers_list_field_width_d(struct kunit *test, const char *delim) +static void numbers_list_field_width_d(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_fix_width(unsigned int, "%u", delim, 10, "u", check_uint); numbers_list_fix_width(int, "%d", delim, 11, "d", check_int); numbers_list_fix_width(int, "%d", delim, 11, "i", check_int); @@ -484,8 +496,11 @@ static void numbers_list_field_width_d(struct kunit *test, const char *delim) numbers_list_fix_width(int, "0x%x", delim, 10, "i", check_int); }
-static void numbers_list_field_width_h(struct kunit *test, const char *delim) +static void numbers_list_field_width_h(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_fix_width(unsigned short, "%hu", delim, 5, "hu", check_ushort); numbers_list_fix_width(short, "%hd", delim, 6, "hd", check_short); numbers_list_fix_width(short, "%hd", delim, 6, "hi", check_short); @@ -494,8 +509,11 @@ static void numbers_list_field_width_h(struct kunit *test, const char *delim) numbers_list_fix_width(short, "0x%hx", delim, 6, "hi", check_short); }
-static void numbers_list_field_width_hh(struct kunit *test, const char *delim) +static void numbers_list_field_width_hh(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_fix_width(unsigned char, "%hhu", delim, 3, "hhu", check_uchar); numbers_list_fix_width(signed char, "%hhd", delim, 4, "hhd", check_char); numbers_list_fix_width(signed char, "%hhd", delim, 4, "hhi", check_char); @@ -504,24 +522,11 @@ static void numbers_list_field_width_hh(struct kunit *test, const char *delim) numbers_list_fix_width(signed char, "0x%hhx", delim, 4, "hhi", check_char); }
-/* - * List of numbers separated by delim. Each field width specifier is the - * maximum possible digits for the given type and base. - */ -static void numbers_list_field_width_typemax(struct kunit *test) +static void numbers_list_field_width_val_ll(struct kunit *test) { const char * const *param = test->param_value; const char *delim = *param;
- numbers_list_field_width_ll(test, delim); - numbers_list_field_width_l(test, delim); - numbers_list_field_width_d(test, delim); - numbers_list_field_width_h(test, delim); - numbers_list_field_width_hh(test, delim); -} - -static void numbers_list_field_width_val_ll(struct kunit *test, const char *delim) -{ numbers_list_val_width(unsigned long long, "%llu", delim, "llu", check_ull); numbers_list_val_width(long long, "%lld", delim, "lld", check_ll); numbers_list_val_width(long long, "%lld", delim, "lli", check_ll); @@ -530,8 +535,11 @@ static void numbers_list_field_width_val_ll(struct kunit *test, const char *deli numbers_list_val_width(long long, "0x%llx", delim, "lli", check_ll); }
-static void numbers_list_field_width_val_l(struct kunit *test, const char *delim) +static void numbers_list_field_width_val_l(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_val_width(unsigned long, "%lu", delim, "lu", check_ulong); numbers_list_val_width(long, "%ld", delim, "ld", check_long); numbers_list_val_width(long, "%ld", delim, "li", check_long); @@ -540,8 +548,11 @@ static void numbers_list_field_width_val_l(struct kunit *test, const char *delim numbers_list_val_width(long, "0x%lx", delim, "li", check_long); }
-static void numbers_list_field_width_val_d(struct kunit *test, const char *delim) +static void numbers_list_field_width_val_d(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_val_width(unsigned int, "%u", delim, "u", check_uint); numbers_list_val_width(int, "%d", delim, "d", check_int); numbers_list_val_width(int, "%d", delim, "i", check_int); @@ -550,8 +561,11 @@ static void numbers_list_field_width_val_d(struct kunit *test, const char *delim numbers_list_val_width(int, "0x%x", delim, "i", check_int); }
-static void numbers_list_field_width_val_h(struct kunit *test, const char *delim) +static void numbers_list_field_width_val_h(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_val_width(unsigned short, "%hu", delim, "hu", check_ushort); numbers_list_val_width(short, "%hd", delim, "hd", check_short); numbers_list_val_width(short, "%hd", delim, "hi", check_short); @@ -560,8 +574,11 @@ static void numbers_list_field_width_val_h(struct kunit *test, const char *delim numbers_list_val_width(short, "0x%hx", delim, "hi", check_short); }
-static void numbers_list_field_width_val_hh(struct kunit *test, const char *delim) +static void numbers_list_field_width_val_hh(struct kunit *test) { + const char * const *param = test->param_value; + const char *delim = *param; + numbers_list_val_width(unsigned char, "%hhu", delim, "hhu", check_uchar); numbers_list_val_width(signed char, "%hhd", delim, "hhd", check_char); numbers_list_val_width(signed char, "%hhd", delim, "hhi", check_char); @@ -570,38 +587,6 @@ static void numbers_list_field_width_val_hh(struct kunit *test, const char *deli numbers_list_val_width(signed char, "0x%hhx", delim, "hhi", check_char); }
-/* - * List of numbers separated by delim. Each field width specifier is the - * exact length of the corresponding value digits in the string being scanned. - */ -static void numbers_list_field_width_val_width(struct kunit *test) -{ - const char * const *param = test->param_value; - const char *delim = *param; - - numbers_list_field_width_val_ll(test, delim); - numbers_list_field_width_val_l(test, delim); - numbers_list_field_width_val_d(test, delim); - numbers_list_field_width_val_h(test, delim); - numbers_list_field_width_val_hh(test, delim); -} - -/* - * Slice a continuous string of digits without field delimiters, containing - * numbers of varying length, using the field width to extract each group - * of digits. For example the hex values c0,3,bf01,303 would have a - * string representation of "c03bf01303" and extracted with "%2x%1x%4x%3x". - */ -static void numbers_slice(struct kunit *test) -{ - const char *delim = ""; - - KUNIT_ASSERT_PTR_EQ(test, test->param_value, NULL); - test->param_value = &delim; - - numbers_list_field_width_val_width(test); -} - #define test_number_prefix(T, str, scan_fmt, expect0, expect1, n_args, fn) \ do { \ const T expect[2] = { expect0, expect1 }; \ @@ -758,16 +743,49 @@ static void number_delimiter_param_desc(const char * const *param,
KUNIT_ARRAY_PARAM(number_delimiters, number_delimiters, number_delimiter_param_desc);
+static const void *optional_number_delimiters_gen_params(const void *prev, char *desc) +{ + static const char * const blank = ""; + + if (prev == NULL) { + number_delimiter_param_desc(&blank, desc); + return ␣ + } + if (prev == &blank) + return number_delimiters_gen_params(NULL, desc); + return number_delimiters_gen_params(prev, desc); +} + static struct kunit_case scanf_test_cases[] = { KUNIT_CASE(numbers_simple), /* String with multiple numbers separated by delimiter. */ - KUNIT_CASE_PARAM(numbers_list, number_delimiters_gen_params), - /* Field width may be longer than actual field digits. */ - KUNIT_CASE_PARAM(numbers_list_field_width_typemax, number_delimiters_gen_params), - /* Each field width exactly length of actual field digits. */ - KUNIT_CASE_PARAM(numbers_list_field_width_val_width, number_delimiters_gen_params), - /* Slice continuous sequence of digits using field widths. */ - KUNIT_CASE(numbers_slice), + KUNIT_CASE_PARAM(numbers_list_ll, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_l, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_d, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_h, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_hh, number_delimiters_gen_params), + + /* + * Lists of numbers separated by delim. Each field width specifier is the + * maximum possible digits for the given type and base. + */ + KUNIT_CASE_PARAM(numbers_list_field_width_ll, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_l, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_d, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_h, number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_hh, number_delimiters_gen_params), + + /* + * Lists of numbers separated by delim. Each field width specifier is the + * exact length of the corresponding value digits in the string being scanned. + * The empty string is included in the list of delimiters. + */ + KUNIT_CASE_PARAM(numbers_list_field_width_val_ll, optional_number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_val_l, optional_number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_val_d, optional_number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_val_h, optional_number_delimiters_gen_params), + KUNIT_CASE_PARAM(numbers_list_field_width_val_hh, optional_number_delimiters_gen_params), + KUNIT_CASE(numbers_prefix_overflow),
KUNIT_CASE(test_simple_strtoull),
On Fri 2025-03-07 06:27:39, Tamir Duberstein wrote:
This gives us more granular test output.
Output before:
KTAP version 1 1..1 KTAP version 1 # Subtest: scanf # module: scanf_kunit 1..10 ok 1 numbers_simple KTAP version 1 # Subtest: numbers_list ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list: pass:5 fail:0 skip:0 total:5 ok 2 numbers_list KTAP version 1 # Subtest: numbers_list_field_width_typemax ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_typemax: pass:5 fail:0 skip:0 total:5 ok 3 numbers_list_field_width_typemax KTAP version 1 # Subtest: numbers_list_field_width_val_width ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_val_width: pass:5 fail:0 skip:0 total:5 ok 4 numbers_list_field_width_val_width ok 5 numbers_slice ok 6 numbers_prefix_overflow ok 7 test_simple_strtoull ok 8 test_simple_strtoll ok 9 test_simple_strtoul ok 10 test_simple_strtol # scanf: pass:10 fail:0 skip:0 total:10 # Totals: pass:22 fail:0 skip:0 total:22 ok 1 scanf
Output after:
KTAP version 1 1..1 KTAP version 1 # Subtest: scanf # module: scanf_kunit 1..21 ok 1 numbers_simple KTAP version 1 # Subtest: numbers_list_ll ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_ll: pass:5 fail:0 skip:0 total:5 ok 2 numbers_list_ll KTAP version 1 # Subtest: numbers_list_l ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_l: pass:5 fail:0 skip:0 total:5 ok 3 numbers_list_l KTAP version 1 # Subtest: numbers_list_d ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_d: pass:5 fail:0 skip:0 total:5 ok 4 numbers_list_d KTAP version 1 # Subtest: numbers_list_h ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_h: pass:5 fail:0 skip:0 total:5 ok 5 numbers_list_h KTAP version 1 # Subtest: numbers_list_hh ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_hh: pass:5 fail:0 skip:0 total:5 ok 6 numbers_list_hh KTAP version 1 # Subtest: numbers_list_field_width_ll ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_ll: pass:5 fail:0 skip:0 total:5 ok 7 numbers_list_field_width_ll KTAP version 1 # Subtest: numbers_list_field_width_l ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_l: pass:5 fail:0 skip:0 total:5 ok 8 numbers_list_field_width_l KTAP version 1 # Subtest: numbers_list_field_width_d ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_d: pass:5 fail:0 skip:0 total:5 ok 9 numbers_list_field_width_d KTAP version 1 # Subtest: numbers_list_field_width_h ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_h: pass:5 fail:0 skip:0 total:5 ok 10 numbers_list_field_width_h KTAP version 1 # Subtest: numbers_list_field_width_hh ok 1 " " ok 2 ":" ok 3 "," ok 4 "-" ok 5 "/" # numbers_list_field_width_hh: pass:5 fail:0 skip:0 total:5 ok 11 numbers_list_field_width_hh KTAP version 1 # Subtest: numbers_list_field_width_val_ll ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_ll: pass:6 fail:0 skip:0 total:6 ok 12 numbers_list_field_width_val_ll KTAP version 1 # Subtest: numbers_list_field_width_val_l ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_l: pass:6 fail:0 skip:0 total:6 ok 13 numbers_list_field_width_val_l KTAP version 1 # Subtest: numbers_list_field_width_val_d ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_d: pass:6 fail:0 skip:0 total:6 ok 14 numbers_list_field_width_val_d KTAP version 1 # Subtest: numbers_list_field_width_val_h ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_h: pass:6 fail:0 skip:0 total:6 ok 15 numbers_list_field_width_val_h KTAP version 1 # Subtest: numbers_list_field_width_val_hh ok 1 "" ok 2 " " ok 3 ":" ok 4 "," ok 5 "-" ok 6 "/" # numbers_list_field_width_val_hh: pass:6 fail:0 skip:0 total:6 ok 16 numbers_list_field_width_val_hh ok 17 numbers_prefix_overflow ok 18 test_simple_strtoull ok 19 test_simple_strtoll ok 20 test_simple_strtoul ok 21 test_simple_strtol # scanf: pass:21 fail:0 skip:0 total:21 # Totals: pass:86 fail:0 skip:0 total:86 ok 1 scanf
Signed-off-by: Tamir Duberstein tamird@gmail.com
Thanks for trying this. But I personally do not find this as a big win. It causes that the test log is longer than one screen and thus harder to review.
It might be fine when running only this test and running it only once. But I think that it is quite common to run more tests and then it is easy to get lost.
I guess that there are tools for handling the KTAP format which might make this easier but...
So, I would prefer to avoid this patch.
Best Regards, Petr
On Fri, Mar 14, 2025 at 9:21 AM Petr Mladek pmladek@suse.com wrote:
Thanks for trying this. But I personally do not find this as a big win. It causes that the test log is longer than one screen and thus harder to review.
It might be fine when running only this test and running it only once. But I think that it is quite common to run more tests and then it is easy to get lost.
I guess that there are tools for handling the KTAP format which might make this easier but...
So, I would prefer to avoid this patch.
Best Regards, Petr
Fine with me, and thanks for having a look! I've rebased this on Kees' for-next/move-kunit-tests, shall I send v10 with this patch dropped?
On Fri 2025-03-07 06:27:33, Tamir Duberstein wrote:
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.
Tamir Duberstein (6): scanf: implicate test line in failure messages scanf: remove redundant debug logs scanf: convert self-test to KUnit scanf: break kunit into test cases
Kees, could you please take the above 5 patches as well via the tree moving the KUNIT tests to lib/tests ?
They seem to be ready for linux-next and the next merge window.
scanf: tidy header `#include`s
This one is a bit controversial and might be added later.
scanf: further break kunit into test cases
This one was just an attempt. But I personally think that it is not worth it.
Best Regards, Petr
On Fri, Mar 14, 2025 at 02:29:40PM +0100, Petr Mladek wrote:
On Fri 2025-03-07 06:27:33, Tamir Duberstein wrote:
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.
Tamir Duberstein (6): scanf: implicate test line in failure messages scanf: remove redundant debug logs scanf: convert self-test to KUnit scanf: break kunit into test cases
Kees, could you please take the above 5 patches as well via the tree moving the KUNIT tests to lib/tests ?
I think you mean 4? Sure!
-Kees
They seem to be ready for linux-next and the next merge window.
scanf: tidy header `#include`s
This one is a bit controversial and might be added later.
scanf: further break kunit into test cases
This one was just an attempt. But I personally think that it is not worth it.
Best Regards, Petr
On Fri, Mar 14, 2025 at 4:45 PM Kees Cook kees@kernel.org wrote:
On Fri, Mar 14, 2025 at 02:29:40PM +0100, Petr Mladek wrote:
On Fri 2025-03-07 06:27:33, Tamir Duberstein wrote:
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.
Tamir Duberstein (6): scanf: implicate test line in failure messages scanf: remove redundant debug logs scanf: convert self-test to KUnit scanf: break kunit into test cases
Kees, could you please take the above 5 patches as well via the tree moving the KUNIT tests to lib/tests ?
I think you mean 4? Sure!
4 by my count as well :)
Please let me know if you'd like me to respin with conflicts resolved!
On Fri 2025-03-14 16:50:43, Tamir Duberstein wrote:
On Fri, Mar 14, 2025 at 4:45 PM Kees Cook kees@kernel.org wrote:
On Fri, Mar 14, 2025 at 02:29:40PM +0100, Petr Mladek wrote:
On Fri 2025-03-07 06:27:33, Tamir Duberstein wrote:
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.
Tamir Duberstein (6): scanf: implicate test line in failure messages scanf: remove redundant debug logs scanf: convert self-test to KUnit scanf: break kunit into test cases
Kees, could you please take the above 5 patches as well via the tree moving the KUNIT tests to lib/tests ?
I think you mean 4? Sure!
4 by my count as well :)
Please let me know if you'd like me to respin with conflicts resolved!
Yes, I meant 4. I am sorry for the confusion.
And the list of pushed patches in the later reply looks good.
Thanks for help.
Best Regards, Petr
On Fri, 07 Mar 2025 06:27:33 -0500, Tamir Duberstein wrote:
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.
I tested this using:
[...]
(I had to rebase on the printf move, but I think it got it all.)
Applied to for-next/move-kunit-tests, thanks!
[1/6] scanf: implicate test line in failure messages https://git.kernel.org/kees/c/5866730da723 [2/6] scanf: remove redundant debug logs https://git.kernel.org/kees/c/6340d61b9005 [3/6] scanf: convert self-test to KUnit https://git.kernel.org/kees/c/97c1f302f2bc [4/6] scanf: break kunit into test cases https://git.kernel.org/kees/c/d62f8c95470c
Take care,
On Fri, Mar 14, 2025 at 5:00 PM Kees Cook kees@kernel.org wrote:
On Fri, 07 Mar 2025 06:27:33 -0500, Tamir Duberstein wrote:
This is one of just 3 remaining "Test Module" kselftests (the others being bitmap and printf), the rest having been converted to KUnit. In addition to the enclosed patch, please consider this an RFC on the removal of the "Test Module" kselftest machinery.
I tested this using:
[...]
(I had to rebase on the printf move, but I think it got it all.)
Applied to for-next/move-kunit-tests, thanks!
[1/6] scanf: implicate test line in failure messages https://git.kernel.org/kees/c/5866730da723 [2/6] scanf: remove redundant debug logs https://git.kernel.org/kees/c/6340d61b9005 [3/6] scanf: convert self-test to KUnit https://git.kernel.org/kees/c/97c1f302f2bc [4/6] scanf: break kunit into test cases https://git.kernel.org/kees/c/d62f8c95470c
Looks good to me. Thanks Kees!
linux-kselftest-mirror@lists.linaro.org