diff mbox series

[v6,2/2] scanf: break kunit into test cases

Message ID 20250210-scanf-kunit-convert-v6-2-4d583d07f92d@gmail.com (mailing list archive)
State New
Headers show
Series scanf: convert self-test to KUnit | expand

Commit Message

Tamir Duberstein Feb. 10, 2025, 6:13 p.m. UTC
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/scanf_kunit.c | 88 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 48 insertions(+), 40 deletions(-)

Comments

Petr Mladek Feb. 11, 2025, 11:54 a.m. UTC | #1
On Mon 2025-02-10 13:13:49, 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.

Hmm, simulate the following failure in the original test module:


diff --git a/lib/test_scanf.c b/lib/test_scanf.c
index 44f8508c9d88..3ec12328cc4c 100644
--- a/lib/test_scanf.c
+++ b/lib/test_scanf.c
@@ -564,7 +564,7 @@ static void __init numbers_list_field_width_val_h(const char *delim)
 	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);
-	numbers_list_val_width(unsigned short,	"%hx",	     delim, "hx", check_ushort);
+	numbers_list_val_width(unsigned short,	"%hx",	     delim, "hx", check_uint);
 	numbers_list_val_width(unsigned short,	"0x%hx",     delim, "hx", check_ushort);
 	numbers_list_val_width(short,		"0x%hx",     delim, "hi", check_short);
 }

and I got:


[  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


When I tried to do the same in the new module:

diff --git a/lib/scanf_kunit.c b/lib/scanf_kunit.c
index e45f3c4f0437..692eb8cbbeab 100644
--- a/lib/scanf_kunit.c
+++ b/lib/scanf_kunit.c
@@ -546,7 +546,7 @@ 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);
 	numbers_list_val_width(short,		"%hd",	     delim, "hi", check_short);
-	numbers_list_val_width(unsigned short,	"%hx",	     delim, "hx", check_ushort);
+	numbers_list_val_width(unsigned short,	"%hx",	     delim, "hx", check_uint);
 	numbers_list_val_width(unsigned short,	"0x%hx",     delim, "hx", check_ushort);
 	numbers_list_val_width(short,		"0x%hx",     delim, "hi", check_short);
 }

then I got:

[ 6625.895391] KTAP version 1
[ 6625.895928] 1..1
[ 6625.896494]     KTAP version 1
[ 6625.896852]     # Subtest: scanf
[ 6625.897191]     # module: scanf_kunit
[ 6625.897198]     1..10
[ 6625.903479]     ok 1 numbers_simple
[ 6625.903490]         KTAP version 1
[ 6625.904352]         # Subtest: numbers_list
[ 6625.905623]         ok 1 " "
[ 6625.907654]         ok 2 ":"
[ 6625.909654]         ok 3 ","
[ 6625.911564]         ok 4 "-"
[ 6625.913632]         ok 5 "/"
[ 6625.914020]     # numbers_list: pass:5 fail:0 skip:0 total:5
[ 6625.914370]     ok 2 numbers_list
[ 6625.914964]         KTAP version 1
[ 6625.915871]         # Subtest: numbers_list_field_width_typemax
[ 6625.917527]         ok 1 " "
[ 6625.919366]         ok 2 ":"
[ 6625.921586]         ok 3 ","
[ 6625.923240]         ok 4 "-"
[ 6625.925226]         ok 5 "/"
[ 6625.925622]     # numbers_list_field_width_typemax: pass:5 fail:0 skip:0 total:5
[ 6625.925973]     ok 3 numbers_list_field_width_typemax
[ 6625.926709]         KTAP version 1
[ 6625.927613]         # Subtest: numbers_list_field_width_val_width
[ 6625.928546]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1044578334 (0x3e43001e)
                       *pval == 837828163 (0x31f03e43)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
[ 6625.929225]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 837828163 (0x31f03e43)
                       *pval == 0 (0x0)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
[ 6625.932202]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 12784 (0x31f0)
                       *pval == 2624608151 (0x9c705797)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
[ 6625.934982]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 0 (0x0)
                       *pval == 1966080 (0x1e0000)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
[ 6625.935004]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1469513728 (0x57970000)
                       *pval == 837828163 (0x31f03e43)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
[ 6625.935025]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 2624608151 (0x9c705797)
                       *pval == 0 (0x0)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
[ 6625.935046]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 469802096 (0x1c009c70)
                       *pval == 2624608151 (0x9c705797)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
[ 6625.938161]         not ok 1 " "
[ 6625.952074]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 131073 (0x20001)
                       *pval == 65538 (0x10002)
               vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
[ 6625.952098]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 65538 (0x10002)
                       *pval == 1038822 (0xfd9e6)
               vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
[ 6625.952121]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3655729153 (0xd9e60001)
                       *pval == 3848735038 (0xe567093e)
               vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
[ 6625.952143]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1038822 (0xfd9e6)
                       *pval == 65542 (0x10006)
               vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
[ 6625.960548]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 155058191 (0x93e000f)
                       *pval == 65538 (0x10002)
               vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
[ 6625.960579]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3848735038 (0xe567093e)
                       *pval == 1038822 (0xfd9e6)
               vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
[ 6625.960604]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 50390375 (0x300e567)
                       *pval == 3848735038 (0xe567093e)
               vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
[ 6625.969351]         not ok 2 ":"
[ 6625.969860]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3589275648 (0xd5f00000)
                       *pval == 3768047088 (0xe097d5f0)
               vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
[ 6625.972121]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3768047088 (0xe097d5f0)
                       *pval == 878575619 (0x345e0003)
               vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
[ 6625.972139]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 254103 (0x3e097)
                       *pval == 2832859455 (0xa8da013f)
               vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
[ 6625.976800]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 878575619 (0x345e0003)
                       *pval == 0 (0x0)
               vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
[ 6625.976819]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 20919390 (0x13f345e)
                       *pval == 3768047088 (0xe097d5f0)
               vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
[ 6625.976836]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 2832859455 (0xa8da013f)
                       *pval == 878575619 (0x345e0003)
               vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
[ 6625.976853]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3691030746 (0xdc00a8da)
                       *pval == 2832859455 (0xa8da013f)
               vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
[ 6625.979505]         not ok 3 ","
[ 6625.989303]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1475084289 (0x57ec0001)
                       *pval == 3243268076 (0xc15057ec)
               vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
[ 6625.989324]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3243268076 (0xc15057ec)
                       *pval == 21 (0x15)
               vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
[ 6625.989343]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1425744 (0x15c150)
                       *pval == 786439 (0xc0007)
               vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
[ 6625.994177]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 21 (0x15)
                       *pval == 65537 (0x10001)
               vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
[ 6625.994196]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 458752 (0x70000)
                       *pval == 3243268076 (0xc15057ec)
               vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
[ 6625.994214]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 786439 (0xc0007)
                       *pval == 21 (0x15)
               vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
[ 6625.994232]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3858759692 (0xe600000c)
                       *pval == 786439 (0xc0007)
               vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
[ 6626.003487]         not ok 4 "-"
[ 6626.004021]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 3901554741 (0xe88d0035)
                       *pval == 125069 (0x1e88d)
               vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
[ 6626.006206]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 125069 (0x1e88d)
                       *pval == 1821114368 (0x6c8c0000)
               vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
[ 6626.006226]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1 (0x1)
                       *pval == 65543 (0x10007)
               vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
[ 6626.006243]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1821114368 (0x6c8c0000)
                       *pval == 3502806 (0x3572d6)
               vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
[ 6626.006261]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 486540 (0x76c8c)
                       *pval == 125069 (0x1e88d)
               vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
[ 6626.015404]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 65543 (0x10007)
                       *pval == 1821114368 (0x6c8c0000)
               vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
[ 6626.015423]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1828716545 (0x6d000001)
                       *pval == 65543 (0x10007)
               vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
[ 6626.018041]         not ok 5 "/"
[ 6626.022529]     # numbers_list_field_width_val_width: pass:0 fail:5 skip:0 total:5
[ 6626.022535]     not ok 4 numbers_list_field_width_val_width
[ 6626.023220]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 106936 (0x1a1b8)
                       *pval == 571539457 (0x22110001)
               vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
[ 6626.024017]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 571539457 (0x22110001)
                       *pval == 81562 (0x13e9a)
               vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
[ 6626.024035]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1050288657 (0x3e9a2211)
                       *pval == 91449567 (0x57368df)
               vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
[ 6626.024053]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 81562 (0x13e9a)
                       *pval == 2713242046 (0xa1b8c9be)
               vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
[ 6626.024073]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1759444993 (0x68df0001)
                       *pval == 571539457 (0x22110001)
               vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
[ 6626.024091]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 91449567 (0x57368df)
                       *pval == 81562 (0x13e9a)
               vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
[ 6626.024108]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1107297651 (0x42000573)
                       *pval == 91449567 (0x57368df)
               vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
[ 6626.026607]     not ok 5 numbers_slice
[ 6626.039801]     ok 6 numbers_prefix_overflow
[ 6626.040522]     ok 7 test_simple_strtoull
[ 6626.041433]     ok 8 test_simple_strtoll
[ 6626.042409]     ok 9 test_simple_strtoul
[ 6626.043153]     ok 10 test_simple_strtol
[ 6626.043161] # scanf: pass:8 fail:2 skip:0 total:10
[ 6626.043166] # Totals: pass:16 fail:6 skip:0 total:22
[ 6626.043170] not ok 1 scanf


I like that the log better points to the test:

[ 6625.927613]         # Subtest: numbers_list_field_width_val_width

But

[ 6625.928546]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
                   Expected got == *pval, but
                       got == 1044578334 (0x3e43001e)
                       *pval == 837828163 (0x31f03e43)
               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)

is much harder to parse than the original

[  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


Also I wonder why the scanned string is different:

               vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vs.

[  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
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I would expect that the 1st error would be on the same string
in both tests. I wonder why it differs.

Best Regards,
Petr
Tamir Duberstein Feb. 11, 2025, 3:11 p.m. UTC | #2
On Tue, Feb 11, 2025 at 6:54 AM Petr Mladek <pmladek@suse.com> wrote:
>
> On Mon 2025-02-10 13:13:49, 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.
>
> Hmm, simulate the following failure in the original test module:
>
>
> diff --git a/lib/test_scanf.c b/lib/test_scanf.c
> index 44f8508c9d88..3ec12328cc4c 100644
> --- a/lib/test_scanf.c
> +++ b/lib/test_scanf.c
> @@ -564,7 +564,7 @@ static void __init numbers_list_field_width_val_h(const char *delim)
>         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);
> -       numbers_list_val_width(unsigned short,  "%hx",       delim, "hx", check_ushort);
> +       numbers_list_val_width(unsigned short,  "%hx",       delim, "hx", check_uint);
>         numbers_list_val_width(unsigned short,  "0x%hx",     delim, "hx", check_ushort);
>         numbers_list_val_width(short,           "0x%hx",     delim, "hi", check_short);
>  }
>
> and I got:
>
>
> [  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
>
>
> When I tried to do the same in the new module:
>
> diff --git a/lib/scanf_kunit.c b/lib/scanf_kunit.c
> index e45f3c4f0437..692eb8cbbeab 100644
> --- a/lib/scanf_kunit.c
> +++ b/lib/scanf_kunit.c
> @@ -546,7 +546,7 @@ 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);
>         numbers_list_val_width(short,           "%hd",       delim, "hi", check_short);
> -       numbers_list_val_width(unsigned short,  "%hx",       delim, "hx", check_ushort);
> +       numbers_list_val_width(unsigned short,  "%hx",       delim, "hx", check_uint);
>         numbers_list_val_width(unsigned short,  "0x%hx",     delim, "hx", check_ushort);
>         numbers_list_val_width(short,           "0x%hx",     delim, "hi", check_short);
>  }
>
> then I got:
>
> [ 6625.895391] KTAP version 1
> [ 6625.895928] 1..1
> [ 6625.896494]     KTAP version 1
> [ 6625.896852]     # Subtest: scanf
> [ 6625.897191]     # module: scanf_kunit
> [ 6625.897198]     1..10
> [ 6625.903479]     ok 1 numbers_simple
> [ 6625.903490]         KTAP version 1
> [ 6625.904352]         # Subtest: numbers_list
> [ 6625.905623]         ok 1 " "
> [ 6625.907654]         ok 2 ":"
> [ 6625.909654]         ok 3 ","
> [ 6625.911564]         ok 4 "-"
> [ 6625.913632]         ok 5 "/"
> [ 6625.914020]     # numbers_list: pass:5 fail:0 skip:0 total:5
> [ 6625.914370]     ok 2 numbers_list
> [ 6625.914964]         KTAP version 1
> [ 6625.915871]         # Subtest: numbers_list_field_width_typemax
> [ 6625.917527]         ok 1 " "
> [ 6625.919366]         ok 2 ":"
> [ 6625.921586]         ok 3 ","
> [ 6625.923240]         ok 4 "-"
> [ 6625.925226]         ok 5 "/"
> [ 6625.925622]     # numbers_list_field_width_typemax: pass:5 fail:0 skip:0 total:5
> [ 6625.925973]     ok 3 numbers_list_field_width_typemax
> [ 6625.926709]         KTAP version 1
> [ 6625.927613]         # Subtest: numbers_list_field_width_val_width
> [ 6625.928546]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1044578334 (0x3e43001e)
>                        *pval == 837828163 (0x31f03e43)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
> [ 6625.929225]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 837828163 (0x31f03e43)
>                        *pval == 0 (0x0)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
> [ 6625.932202]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 12784 (0x31f0)
>                        *pval == 2624608151 (0x9c705797)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
> [ 6625.934982]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 0 (0x0)
>                        *pval == 1966080 (0x1e0000)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
> [ 6625.935004]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1469513728 (0x57970000)
>                        *pval == 837828163 (0x31f03e43)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
> [ 6625.935025]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 2624608151 (0x9c705797)
>                        *pval == 0 (0x0)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
> [ 6625.935046]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 469802096 (0x1c009c70)
>                        *pval == 2624608151 (0x9c705797)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
> [ 6625.938161]         not ok 1 " "
> [ 6625.952074]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 131073 (0x20001)
>                        *pval == 65538 (0x10002)
>                vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
> [ 6625.952098]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 65538 (0x10002)
>                        *pval == 1038822 (0xfd9e6)
>                vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
> [ 6625.952121]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3655729153 (0xd9e60001)
>                        *pval == 3848735038 (0xe567093e)
>                vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
> [ 6625.952143]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1038822 (0xfd9e6)
>                        *pval == 65542 (0x10006)
>                vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
> [ 6625.960548]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 155058191 (0x93e000f)
>                        *pval == 65538 (0x10002)
>                vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
> [ 6625.960579]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3848735038 (0xe567093e)
>                        *pval == 1038822 (0xfd9e6)
>                vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
> [ 6625.960604]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 50390375 (0x300e567)
>                        *pval == 3848735038 (0xe567093e)
>                vsscanf("6:1:2:1:d9e6:f:93e:e567", "%1hx:%1hx:%1hx:%1hx:%4hx:%1hx:%3hx:%4hx", ...)
> [ 6625.969351]         not ok 2 ":"
> [ 6625.969860]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3589275648 (0xd5f00000)
>                        *pval == 3768047088 (0xe097d5f0)
>                vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
> [ 6625.972121]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3768047088 (0xe097d5f0)
>                        *pval == 878575619 (0x345e0003)
>                vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
> [ 6625.972139]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 254103 (0x3e097)
>                        *pval == 2832859455 (0xa8da013f)
>                vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
> [ 6625.976800]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 878575619 (0x345e0003)
>                        *pval == 0 (0x0)
>                vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
> [ 6625.976819]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 20919390 (0x13f345e)
>                        *pval == 3768047088 (0xe097d5f0)
>                vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
> [ 6625.976836]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 2832859455 (0xa8da013f)
>                        *pval == 878575619 (0x345e0003)
>                vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
> [ 6625.976853]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3691030746 (0xdc00a8da)
>                        *pval == 2832859455 (0xa8da013f)
>                vsscanf("0,0,d5f0,e097,3,345e,13f,a8da", "%1hx,%1hx,%4hx,%4hx,%1hx,%4hx,%3hx,%4hx", ...)
> [ 6625.979505]         not ok 3 ","
> [ 6625.989303]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1475084289 (0x57ec0001)
>                        *pval == 3243268076 (0xc15057ec)
>                vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
> [ 6625.989324]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3243268076 (0xc15057ec)
>                        *pval == 21 (0x15)
>                vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
> [ 6625.989343]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1425744 (0x15c150)
>                        *pval == 786439 (0xc0007)
>                vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
> [ 6625.994177]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 21 (0x15)
>                        *pval == 65537 (0x10001)
>                vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
> [ 6625.994196]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 458752 (0x70000)
>                        *pval == 3243268076 (0xc15057ec)
>                vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
> [ 6625.994214]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 786439 (0xc0007)
>                        *pval == 21 (0x15)
>                vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
> [ 6625.994232]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3858759692 (0xe600000c)
>                        *pval == 786439 (0xc0007)
>                vsscanf("1-1-57ec-c150-15-0-7-c", "%1hx-%1hx-%4hx-%4hx-%2hx-%1hx-%1hx-%1hx", ...)
> [ 6626.003487]         not ok 4 "-"
> [ 6626.004021]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 3901554741 (0xe88d0035)
>                        *pval == 125069 (0x1e88d)
>                vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
> [ 6626.006206]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 125069 (0x1e88d)
>                        *pval == 1821114368 (0x6c8c0000)
>                vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
> [ 6626.006226]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1 (0x1)
>                        *pval == 65543 (0x10007)
>                vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
> [ 6626.006243]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1821114368 (0x6c8c0000)
>                        *pval == 3502806 (0x3572d6)
>                vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
> [ 6626.006261]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 486540 (0x76c8c)
>                        *pval == 125069 (0x1e88d)
>                vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
> [ 6626.015404]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 65543 (0x10007)
>                        *pval == 1821114368 (0x6c8c0000)
>                vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
> [ 6626.015423]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1828716545 (0x6d000001)
>                        *pval == 65543 (0x10007)
>                vsscanf("72d6/35/e88d/1/0/6c8c/7/1", "%4hx/%2hx/%4hx/%1hx/%1hx/%4hx/%1hx/%1hx", ...)
> [ 6626.018041]         not ok 5 "/"
> [ 6626.022529]     # numbers_list_field_width_val_width: pass:0 fail:5 skip:0 total:5
> [ 6626.022535]     not ok 4 numbers_list_field_width_val_width
> [ 6626.023220]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 106936 (0x1a1b8)
>                        *pval == 571539457 (0x22110001)
>                vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
> [ 6626.024017]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 571539457 (0x22110001)
>                        *pval == 81562 (0x13e9a)
>                vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
> [ 6626.024035]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1050288657 (0x3e9a2211)
>                        *pval == 91449567 (0x57368df)
>                vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
> [ 6626.024053]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 81562 (0x13e9a)
>                        *pval == 2713242046 (0xa1b8c9be)
>                vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
> [ 6626.024073]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1759444993 (0x68df0001)
>                        *pval == 571539457 (0x22110001)
>                vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
> [ 6626.024091]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 91449567 (0x57368df)
>                        *pval == 81562 (0x13e9a)
>                vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
> [ 6626.024108]     # numbers_slice: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1107297651 (0x42000573)
>                        *pval == 91449567 (0x57368df)
>                vsscanf("c9bea1b8122113e9a168df573", "%4hx%4hx%1hx%4hx%4hx%1hx%4hx%3hx", ...)
> [ 6626.026607]     not ok 5 numbers_slice
> [ 6626.039801]     ok 6 numbers_prefix_overflow
> [ 6626.040522]     ok 7 test_simple_strtoull
> [ 6626.041433]     ok 8 test_simple_strtoll
> [ 6626.042409]     ok 9 test_simple_strtoul
> [ 6626.043153]     ok 10 test_simple_strtol
> [ 6626.043161] # scanf: pass:8 fail:2 skip:0 total:10
> [ 6626.043166] # Totals: pass:16 fail:6 skip:0 total:22
> [ 6626.043170] not ok 1 scanf
>
>
> I like that the log better points to the test:
>
> [ 6625.927613]         # Subtest: numbers_list_field_width_val_width
>
> But
>
> [ 6625.928546]     # numbers_list_field_width_val_width: EXPECTATION FAILED at lib/scanf_kunit.c:91
>                    Expected got == *pval, but
>                        got == 1044578334 (0x3e43001e)
>                        *pval == 837828163 (0x31f03e43)
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
>
> is much harder to parse than the original

Yeah, I find that hard to look at as well. I'll revert this to the
original format.

> [  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
>
>
> Also I wonder why the scanned string is different:
>
>                vsscanf("0 1e 3e43 31f0 0 0 5797 9c70", "%1hx %2hx %4hx %4hx %1hx %1hx %4hx %4hx", ...)
>                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> vs.
>
> [  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
>                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> I would expect that the 1st error would be on the same string
> in both tests. I wonder why it differs.

This is just because the order in which the tests run has changed.
These strings come from a PRNG. Before the last patch the tests did
(pseudocode):

for (delim : delims) {
  for (test : tests) {
    test(delim)
  }
}

After the last patch the inner and outer loops have traded places.

>
> Best Regards,
> Petr
diff mbox series

Patch

diff --git a/lib/scanf_kunit.c b/lib/scanf_kunit.c
index dfd29b103053..7e2e7d891e41 100644
--- a/lib/scanf_kunit.c
+++ b/lib/scanf_kunit.c
@@ -4,14 +4,10 @@ 
  */
 
 #include <kunit/test.h>
-#include <linux/bitops.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 <linux/sprintf.h>
 
 #define BUF_SIZE 1024
 
@@ -428,8 +424,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);
@@ -500,8 +499,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);
@@ -563,8 +565,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);
@@ -580,7 +585,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)	\
@@ -732,62 +742,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,
 };