scanf: convert self-test to KUnit
Subsystems (4)
v4 cover scanf: convert self-test to KUnit
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
---
Changes in v4:
- Bake test into various macros, greatly reducing diff noise.
- Revert control flow changes.
- Link to v3: https://lore.kernel.org/r/[email protected]
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/[email protected]
Changes in v2:
- Rename lib/{test_scanf.c => scanf_kunit.c}. (Andy Shevchenko)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Tamir Duberstein (2):
scanf: convert self-test to KUnit
scanf: break kunit into test cases
MAINTAINERS | 2 +-
arch/m68k/configs/amiga_defconfig | 1 -
arch/m68k/configs/apollo_defconfig | 1 -
arch/m68k/configs/atari_defconfig | 1 -
arch/m68k/configs/bvme6000_defconfig | 1 -
arch/m68k/configs/hp300_defconfig | 1 -
arch/m68k/configs/mac_defconfig | 1 -
arch/m68k/configs/multi_defconfig | 1 -
arch/m68k/configs/mvme147_defconfig | 1 -
arch/m68k/configs/mvme16x_defconfig | 1 -
arch/m68k/configs/q40_defconfig | 1 -
arch/m68k/configs/sun3_defconfig | 1 -
arch/m68k/configs/sun3x_defconfig | 1 -
arch/powerpc/configs/ppc64_defconfig | 1 -
lib/Kconfig.debug | 20 ++-
lib/Makefile | 2 +-
lib/{test_scanf.c => scanf_kunit.c} | 260 +++++++++++++++++------------------
tools/testing/selftests/lib/Makefile | 2 +-
tools/testing/selftests/lib/config | 1 -
tools/testing/selftests/lib/scanf.sh | 4 -
20 files changed, 149 insertions(+), 155 deletions(-)---
base-commit: a86bf2283d2c9769205407e2b54777c03d012939
change-id: 20250131-scanf-kunit-convert-f70dc33bb34c
Best regards,
Signature · 1 line
Tamir Duberstein <[email protected]>
PATCH v4 1/2 scanf: convert self-test to KUnit
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.
---
MAINTAINERS | 2 +-
arch/m68k/configs/amiga_defconfig | 1 -
arch/m68k/configs/apollo_defconfig | 1 -
arch/m68k/configs/atari_defconfig | 1 -
arch/m68k/configs/bvme6000_defconfig | 1 -
arch/m68k/configs/hp300_defconfig | 1 -
arch/m68k/configs/mac_defconfig | 1 -
arch/m68k/configs/multi_defconfig | 1 -
arch/m68k/configs/mvme147_defconfig | 1 -
arch/m68k/configs/mvme16x_defconfig | 1 -
arch/m68k/configs/q40_defconfig | 1 -
arch/m68k/configs/sun3_defconfig | 1 -
arch/m68k/configs/sun3x_defconfig | 1 -
arch/powerpc/configs/ppc64_defconfig | 1 -
lib/Kconfig.debug | 20 +++-
lib/Makefile | 2 +-
lib/{test_scanf.c => scanf_kunit.c} | 220 ++++++++++++++++-------------------
tools/testing/selftests/lib/Makefile | 2 +-
tools/testing/selftests/lib/config | 1 -
tools/testing/selftests/lib/scanf.sh | 4 -
20 files changed, 123 insertions(+), 141 deletions(-)MAINTAINERS +1 -1
@@ -25368,8 +25368,8 @@ R: Sergey Senozhatsky <[email protected]> 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/scanf_kunit.c F: lib/test_printf.c -F: lib/test_scanf.c F: lib/vsprintf.c VT1211 HARDWARE MONITOR DRIVER
arch/m68k/configs/amiga_defconfig +0 -1
@@ -623,7 +623,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/apollo_defconfig +0 -1
@@ -580,7 +580,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/atari_defconfig +0 -1
@@ -600,7 +600,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/bvme6000_defconfig +0 -1
@@ -572,7 +572,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/hp300_defconfig +0 -1
@@ -582,7 +582,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/mac_defconfig +0 -1
@@ -599,7 +599,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/multi_defconfig +0 -1
@@ -686,7 +686,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/mvme147_defconfig +0 -1
@@ -572,7 +572,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/mvme16x_defconfig +0 -1
@@ -573,7 +573,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/q40_defconfig +0 -1
@@ -589,7 +589,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/sun3_defconfig +0 -1
@@ -569,7 +569,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/m68k/configs/sun3x_defconfig +0 -1
@@ -570,7 +570,6 @@ CONFIG_ASYNC_RAID6_TEST=m CONFIG_TEST_HEXDUMP=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
arch/powerpc/configs/ppc64_defconfig +0 -1
@@ -445,7 +445,6 @@ CONFIG_STRING_SELFTEST=m CONFIG_TEST_STRING_HELPERS=m CONFIG_TEST_KSTRTOX=m CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_TEST_UUID=m CONFIG_TEST_XARRAY=m
lib/Kconfig.debug +17 -3
@@ -2427,6 +2427,23 @@ 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 boot. + + KUnit tests run during boot and output the results to the debug log + in TAP format (http://testanything.org/). Only useful for kernel devs + running the KUnit test harness, and not intended for inclusion into a + production build. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + config STRING_KUNIT_TEST tristate "KUnit test string functions at runtime" if !KUNIT_ALL_TESTS depends on KUNIT
@@ -2443,9 +2460,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
lib/Makefile +1 -1
@@ -85,7 +85,7 @@ 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_SCANF_KUNIT_TEST) += scanf_kunit.o obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o ifeq ($(CONFIG_CC_IS_CLANG)$(CONFIG_KASAN),yy)
lib/scanf_kunit.c renamed from lib/test_scanf.c +103 -117
559 lines not on this page · raw message
tools/testing/selftests/lib/Makefile +1 -1
@@ -4,5 +4,5 @@ # No binaries, but make sure arg-less "make" doesn't trigger "run_tests" all: -TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh scanf.sh +TEST_PROGS := printf.sh bitmap.sh prime_numbers.sh include ../lib.mk
tools/testing/selftests/lib/config +0 -1
@@ -1,5 +1,4 @@ CONFIG_TEST_PRINTF=m -CONFIG_TEST_SCANF=m CONFIG_TEST_BITMAP=m CONFIG_PRIME_NUMBERS=m CONFIG_TEST_BITOPS=m
tools/testing/selftests/lib/scanf.sh deleted +0 -4
@@ -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
Signature · 1 line
2.48.1
Quoted 6 lines · Tamir Duberstein
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.
Apologies for the version churn, I'll have to send one more which removes spurious trailing newlines (not necessary in kunit) and converts logging macros like pr_info to kunit_info.
I'll wait a few days in case folks have other comments.
PATCH v4 2/2 scanf: break kunit into test cases
In reply to Tamir DubersteinUse suite_{init,exit} and move some tests into scanf_test_cases. This gives us nicer output in the event of a failure.
--- lib/scanf_kunit.c | 82 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 35 deletions(-)
lib/scanf_kunit.c +47 -35
@@ -430,8 +430,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);
@@ -502,8 +505,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);
@@ -565,8 +571,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);
@@ -582,7 +591,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) \
@@ -734,62 +748,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 ,"delim=\"%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 -1 ; fmt_buffer =kmalloc (BUF_SIZE ,GFP_KERNEL );if (!fmt_buffer) {kfree (test_buffer); -return ; +return -1 ; }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, };
Signature · 1 line
2.48.1