lib/cmdline_kunit: add a new test case for get_options()
Subsystems (3)
PATCH v2 1/5 lib/cmdline_kunit: add a new test case for get_options()
Add a test case for get_options() which is provided by cmdline.c.
--- lib/cmdline_kunit.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
lib/cmdline_kunit.c +52 -0
@@ -18,6 +18,26 @@ static const int cmdline_test_values[] = {1 ,3 ,2 ,1 ,1 ,1 ,3 ,1 , }; +static_assert (ARRAY_SIZE (cmdline_test_strings) ==ARRAY_SIZE (cmdline_test_values)); + +static const char *cmdline_test_range_strings[] = { +"-7" ,"--7" ,"-1-2" ,"7--9" , +"7-" ,"-7--9" ,"7-9," ,"9-7" , +"5-a" ,"a-5" ,"5-8" ,",8-5" , +"+,1" ,"-,4" ,"-3,0-1,6" ,"4,-" , +" +2" ," -9" ,"0-1,-3,6" ,"- 9" , +}; + +static const int cmdline_test_range_values[][16 ] = { + {1 ,-7 , }, {0 ,-0 , }, {4 ,-1 ,0 ,+1 ,2 , }, {0 ,7 , }, + {0 ,+7 , }, {0 ,-7 , }, {3 ,+7 ,8 ,+9 ,0 , }, {0 ,9 , }, + {0 ,+5 , }, {0 ,-0 , }, {4 ,+5 ,6 ,+7 ,8 , }, {0 ,0 , }, + {0 ,+0 , }, {0 ,-0 , }, {4 ,-3 ,0 ,+1 ,6 , }, {1 ,4 , }, + {0 ,+0 , }, {0 ,-0 , }, {4 ,+0 ,1 ,-3 ,6 , }, {0 ,0 , }, +}; + +static_assert (ARRAY_SIZE (cmdline_test_range_strings) ==ARRAY_SIZE (cmdline_test_range_values)); +static void cmdline_do_one_test (struct kunit *test,const char *in,int rc,int offset) {const char *fmt ="Pattern: %s" ;
@@ -84,10 +104,42 @@ static void cmdline_test_tail_int(struct kunit *test) }while (++i <ARRAY_SIZE (cmdline_test_strings)); } +static void cmdline_do_one_range_test (struct kunit *test,const char *in, +unsigned int n,const int *e) +{ +unsigned int i; +int r[16 ]; + +#define FMT "in test %u" +#define FMT2 "expected %d numbers, got %d" +#define FMT3 "at %d" +memset (r,0 ,sizeof (r)); +get_options (in,ARRAY_SIZE (r), r); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " " FMT2 , n, e[0 ], r[0 ]); +for (i =1 ; i <ARRAY_SIZE (r); i++) +KUNIT_EXPECT_EQ_MSG (test, r[i], e[i],FMT " " FMT3 , n, i); +#undef FMT3 +#undef FMT2 +#undef FMT +} + +static void cmdline_test_range (struct kunit *test) +{ +unsigned int i =0 ; + +do { +const char *str = cmdline_test_range_strings[i]; +const int *e = cmdline_test_range_values[i]; + +cmdline_do_one_range_test (test, str, i, e); + }while (++i <ARRAY_SIZE (cmdline_test_range_strings)); +} +static struct kunit_case cmdline_test_cases[] = {KUNIT_CASE (cmdline_test_noint),KUNIT_CASE (cmdline_test_lead_int),KUNIT_CASE (cmdline_test_tail_int), +KUNIT_CASE (cmdline_test_range), {} };
Signature · 1 line
2.29.2
PATCH v2 2/5 lib/cmdline: Update documentation to reflect behaviour
get_options() API has some tricks to optimize that may be not so obvious to the caller. Update documentation to reflect current behaviour.
--- lib/cmdline.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
lib/cmdline.c +6 -1
@@ -83,7 +83,7 @@ EXPORT_SYMBOL(get_option);* get_options - Parse a string into a list of integers * @str: String to be parsed * @nints: size of integer array -* @ints: integer array +* @ints: integer array (must have a room for at least one element) * * This function parses a string containing a comma-separated * list of integers, a hyphen-separated range of _positive_ integers,
@@ -91,6 +91,11 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* Returns: +* +* The first element is filled by the amount of the collected numbers +* in the range. The rest is what was parsed from the @str. +* * Return value is the character in the string which caused * the parse to end (typically a null terminator, if @str is * completely parseable).
Signature · 1 line
2.29.2
Re: [PATCH v2 2/5] lib/cmdline: Update documentation to reflect behaviour
Quoted 35 lines · Andy Shevchenko
get_options()API has some tricks to optimize that may be not so obvious to the caller. Update documentation to reflect current behaviour.Signed-off-by Andy Shevchenko <[email protected]>--- lib/cmdline.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)lib/cmdline.c +6
-2@@ -83,7 +83,7 @@ EXPORT_SYMBOL(get_option);* get_options - Parse a string into a list of integers * @str: String to be parsed * @nints: size of integer array -* @ints: integer array +* @ints: integer array (must have a room for at least one element) * * This function parses a string containing a comma-separated * list of integers, a hyphen-separated range of _positive_ integers, @@ -91,6 +91,11 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* Returns: +* +* The first element is filled by the amount of the collected numbers +* in the range. The rest is what was parsed from the @str. +* * Return value is the character in the string which caused * the parse to end (typically a null terminator, if @str is * completely parseable). --
Quoted 2 lines
2.29.2
PATCH v2 3/5 lib/cmdline: Allow get_options() to take 0 to validate the input
In reply to Andy ShevchenkoAllow get_options() to take 0 as a number of integers parameter to validate the input.
--- lib/cmdline.c | 14 +++++++++++--- lib/cmdline_kunit.c | 10 +++++++++- 2 files changed, 20 insertions(+), 4 deletions(-)
lib/cmdline.c +11 -3
@@ -91,6 +91,9 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* When @nints is 0, the function just validates the given @str and +* returns amount of parseable integers as described below. +* * Returns: * * The first element is filled by the amount of the collected numbers
@@ -103,15 +106,20 @@ EXPORT_SYMBOL(get_option);char *get_options (const char *str,int nints,int *ints) { +bool validate = nints ==0 ;int res, i =1 ; -while (i < nints) { - res =get_option ((char **)&str, ints + i); +while (i < nints || validate) { +int *pint = validate ? ints : ints + i; + + res =get_option ((char **)&str, pint);if (res ==0 )break ;if (res ==3 ) { +int n = validate ?0 : nints - i;int range_nums; - range_nums =get_range ((char **)&str, ints + i, nints - i); + + range_nums =get_range ((char **)&str, pint, n);if (range_nums <0 )break ;/*
lib/cmdline_kunit.c +9 -1
@@ -109,15 +109,23 @@ static void cmdline_do_one_range_test(struct kunit *test, const char *in, {unsigned int i;int r[16 ]; +int *p;#define FMT "in test %u"#define FMT2 "expected %d numbers, got %d"#define FMT3 "at %d"memset (r,0 ,sizeof (r));get_options (in,ARRAY_SIZE (r), r); -KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " " FMT2 , n, e[0 ], r[0 ]); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " (parsed) " FMT2 , n, e[0 ], r[0 ]);for (i =1 ; i <ARRAY_SIZE (r); i++)KUNIT_EXPECT_EQ_MSG (test, r[i], e[i],FMT " " FMT3 , n, i); + +memset (r,0 ,sizeof (r)); +get_options (in,0 , r); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " (validated) " FMT2 , n, e[0 ], r[0 ]); + + p =memchr_inv (&r[1 ],0 ,sizeof (r) -sizeof (r[0 ])); +KUNIT_EXPECT_PTR_EQ_MSG (test, p, (int *)0 ,FMT " out of bound " FMT3 , n, p - r);#undef FMT3#undef FMT2#undef FMT
Signature · 1 line
2.29.2
Re: [PATCH v2 3/5] lib/cmdline: Allow get_options() to take 0 to validate the input
Quoted 15 lines · Andy Shevchenko
Allow
get_options()to take 0 as a number of integers parameter to validate the input.Signed-off-by Andy Shevchenko <[email protected]>--- lib/cmdline.c | 14 +++++++++++--- lib/cmdline_kunit.c | 10 +++++++++- 2 files changed, 20 insertions(+), 4 deletions(-)lib/cmdline.c +0
-0
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 95
Quoted 5 lines
lib/cmdline.c +2
-0@@ -91,6 +91,9 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* When @nints is 0, the function just validates the given @str and +* returns amount of parseable integers as described below.
I'm not a native English speaker but it sounds like this should be "returns the amount".
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 109
Quoted 9 lines
lib/cmdline.c +2
-0+* * Returns: * * The first element is filled by the amount of the collected numbers @@ -103,15 +106,20 @@ EXPORT_SYMBOL(get_option);char *get_options (const char *str,int nints,int *ints) { +bool validate = nints ==0 ;
bool validate = (nints == 0) would be clearer IMO.
Quoted 38 lines
lib/cmdline.c +7
-3int res, i =1 ; -while (i < nints) { - res =get_option ((char **)&str, ints + i); +while (i < nints || validate) { +int *pint = validate ? ints : ints + i; + + res =get_option ((char **)&str, pint);if (res ==0 )break ;if (res ==3 ) { +int n = validate ?0 : nints - i;int range_nums; - range_nums =get_range ((char **)&str, ints + i, nints - i); + + range_nums =get_range ((char **)&str, pint, n);if (range_nums <0 )break ;/* lib/cmdline_kunit.c +2
-1@@ -109,15 +109,23 @@ static void cmdline_do_one_range_test(struct kunit *test, const char *in, {unsigned int i;int r[16 ]; +int *p;#define FMT "in test %u"#define FMT2 "expected %d numbers, got %d"#define FMT3 "at %d"memset (r,0 ,sizeof (r));get_options (in,ARRAY_SIZE (r), r); -KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " " FMT2 , n, e[0 ], r[0 ]); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " (parsed) " FMT2 , n, e[0 ], r[0 ]);for (i =1 ; i <ARRAY_SIZE (r); i++)KUNIT_EXPECT_EQ_MSG (test, r[i], e[i],FMT " " FMT3 , n, i);
lib/cmdline_kunit.c static void cmdline_do_one_range_test(struct kunit *test, const char *in, · line 131
Quoted 10 lines
lib/cmdline_kunit.c +7
-0+ +memset (r,0 ,sizeof (r)); +get_options (in,0 , r); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " (validated) " FMT2 , n, e[0 ], r[0 ]); + + p =memchr_inv (&r[1 ],0 ,sizeof (r) -sizeof (r[0 ])); +KUNIT_EXPECT_PTR_EQ_MSG (test, p, (int *)0 ,FMT " out of bound " FMT3 , n, p - r);#undef FMT3#undef FMT2#undef FMT
Same as the other patch - just put the formatting strings into the messages.
Bart
Quoted 3 lines
--
2.29.2
Re: [PATCH v2 3/5] lib/cmdline: Allow get_options() to take 0 to validate the input
Quoted 2 lines · Bartosz Golaszewski
On Wed, Jan 20, 2021 at 10:45 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 5 lines
Quoted 2 lines
+ * When @nints is 0, the function just validates the given @str and
+ * returns amount of parseable integers as described below.I'm not a native English speaker but it sounds like this should be "returns the amount".
Sounds reasonable. Fixed for v3.
...
Quoted 3 lines
Quoted 1 line
+ bool validate = nints == 0;
bool validate = (nints == 0) would be clearer IMO.
I don't see the benefit, but I have changed.
...
Quoted 1 line
Same as the other patch - just put the formatting strings into the messages.
Okay, I changed.
Thanks for review!
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 4/5 gpio: aggregator: Replace isrange() by using get_options()
In reply to Andy ShevchenkoWe already have a nice helper called get_options() which can be used to validate the input format. Replace isrange() by using it.
--- drivers/gpio/gpio-aggregator.c | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-)
drivers/gpio/gpio-aggregator.c +3 -30
@@ -62,34 +62,6 @@ static char *get_arg(char **args)return start; } -static bool isrange (const char *s) -{ -size_t n; - -if (IS_ERR_OR_NULL (s)) -return false; - -while (1 ) { - n =strspn (s,"0123456789" ); -if (!n) -return false; - - s += n; - -switch (*s++) { -case '\0' : -return true; - -case '-' : -case ',' : -break ; - -default : -return false; - } - } -} -static int aggr_add_gpio (struct gpio_aggregator *aggr,const char *key,int hwnum,unsigned int *n) {
@@ -112,10 +84,10 @@ static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,static int aggr_parse (struct gpio_aggregator *aggr) { +char *name, *offsets, *p;char *args = aggr->args;unsigned long *bitmap;unsigned int i, n =0 ; -char *name, *offsets;int error =0 ; bitmap =bitmap_alloc (ARCH_NR_GPIOS ,GFP_KERNEL );
@@ -130,7 +102,8 @@ static int aggr_parse(struct gpio_aggregator *aggr) goto free_bitmap; } -if (!isrange (offsets)) { + p =get_options (offsets,0 , &error); +if (error ==0 || *p) {/* Named GPIO line */ error =aggr_add_gpio (aggr, name,U16_MAX , &n);if (error)
Signature · 1 line
2.29.2
Re: [PATCH v2 4/5] gpio: aggregator: Replace isrange() by using get_options()
Quoted 4 lines · Andy Shevchenko
We already have a nice helper called
get_options()which can be used to validate the input format. Replaceisrange()by using it.Signed-off-by Andy Shevchenko <[email protected]>
This is really neat.
Yours,
Linus Walleij
Re: [PATCH v2 4/5] gpio: aggregator: Replace isrange() by using get_options()
In reply to Andy ShevchenkoQuoted 6 lines · Andy Shevchenko
We already have a nice helper called
get_options()which can be used to validate the input format. Replaceisrange()by using it.Signed-off-by Andy Shevchenko <[email protected]>---
PATCH v2 5/5 gpio: aggregator: Remove trailing comma in terminator entries
In reply to Andy ShevchenkoRemove trailing comma in terminator entries to avoid potential expanding an array behind it.
--- drivers/gpio/gpio-aggregator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpio-aggregator.c +2 -2
@@ -244,7 +244,7 @@ static DRIVER_ATTR_WO(delete_device);static struct attribute *gpio_aggregator_attrs[] = { &driver_attr_new_device.attr, &driver_attr_delete_device.attr, -NULL , +NULL };ATTRIBUTE_GROUPS (gpio_aggregator);
@@ -518,7 +518,7 @@ static const struct of_device_id gpio_aggregator_dt_ids[] = {* Add GPIO-operated devices controlled from userspace below, * or use "driver_override" in sysfs */ - {}, + {} };MODULE_DEVICE_TABLE (of, gpio_aggregator_dt_ids);#endif
Signature · 1 line
2.29.2
Re: [PATCH v2 5/5] gpio: aggregator: Remove trailing comma in terminator entries
Quoted 4 lines · Andy Shevchenko
Remove trailing comma in terminator entries to avoid potential expanding an array behind it.
Signed-off-by Andy Shevchenko <[email protected]>
Yours,
Linus Walleij
Re: [PATCH v2 5/5] gpio: aggregator: Remove trailing comma in terminator entries
Quoted 9 lines · Linus Walleij
Quoted 4 lines · Andy Shevchenko
Remove trailing comma in terminator entries to avoid potential expanding an array behind it.
Signed-off-by Andy Shevchenko <[email protected]>Reviewed-by Linus Walleij <[email protected]>
Thanks!
Bart, I would like to send this series as a part of my Intel GPIO PR or tell me if you wish another approach.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 5/5] gpio: aggregator: Remove trailing comma in terminator entries
Quoted 12 lines · Andy Shevchenko
Quoted 9 lines · Linus Walleij
Quoted 4 lines · Andy Shevchenko
Remove trailing comma in terminator entries to avoid potential expanding an array behind it.
Signed-off-by Andy Shevchenko <[email protected]>Reviewed-by Linus Walleij <[email protected]>
Quoted 10 lines
Thanks!
Bart, I would like to send this series as a part of my Intel GPIO PR or tell me if you wish another approach.
--
With Best Regards,
Andy Shevchenko
No that's fine.
Bart
Re: [PATCH v2 5/5] gpio: aggregator: Remove trailing comma in terminator entries
In reply to Andy ShevchenkoQuoted 32 lines · Andy Shevchenko
Remove trailing comma in terminator entries to avoid potential expanding an array behind it.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/gpio/gpio-aggregator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/gpio/gpio-aggregator.c +2
-3@@ -244,7 +244,7 @@ static DRIVER_ATTR_WO(delete_device);static struct attribute *gpio_aggregator_attrs[] = { &driver_attr_new_device.attr, &driver_attr_delete_device.attr, -NULL , +NULL };ATTRIBUTE_GROUPS (gpio_aggregator);@@ -518,7 +518,7 @@ static const struct of_device_id gpio_aggregator_dt_ids[] = {* Add GPIO-operated devices controlled from userspace below, * or use "driver_override" in sysfs */ - {}, + {} };MODULE_DEVICE_TABLE (of, gpio_aggregator_dt_ids);#endif --
Quoted 2 lines
2.29.2
Re: [PATCH v2 5/5] gpio: aggregator: Remove trailing comma in terminator entries
In reply to Andy ShevchenkoQuoted 4 lines · Andy Shevchenko
Remove trailing comma in terminator entries to avoid potential expanding an array behind it.
Signed-off-by Andy Shevchenko <[email protected]>
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 50 lines · Andy Shevchenko
Add a test case for
get_options()which is provided bycmdline.c.Signed-off-by Andy Shevchenko <[email protected]>--- lib/cmdline_kunit.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)lib/cmdline_kunit.c +27
-0@@ -18,6 +18,26 @@ static const int cmdline_test_values[] = {1 ,3 ,2 ,1 ,1 ,1 ,3 ,1 , }; +static_assert (ARRAY_SIZE (cmdline_test_strings) ==ARRAY_SIZE (cmdline_test_values)); + +static const char *cmdline_test_range_strings[] = { +"-7" ,"--7" ,"-1-2" ,"7--9" , +"7-" ,"-7--9" ,"7-9," ,"9-7" , +"5-a" ,"a-5" ,"5-8" ,",8-5" , +"+,1" ,"-,4" ,"-3,0-1,6" ,"4,-" , +" +2" ," -9" ,"0-1,-3,6" ,"- 9" , +}; + +static const int cmdline_test_range_values[][16 ] = { + {1 ,-7 , }, {0 ,-0 , }, {4 ,-1 ,0 ,+1 ,2 , }, {0 ,7 , }, + {0 ,+7 , }, {0 ,-7 , }, {3 ,+7 ,8 ,+9 ,0 , }, {0 ,9 , }, + {0 ,+5 , }, {0 ,-0 , }, {4 ,+5 ,6 ,+7 ,8 , }, {0 ,0 , }, + {0 ,+0 , }, {0 ,-0 , }, {4 ,-3 ,0 ,+1 ,6 , }, {1 ,4 , }, + {0 ,+0 , }, {0 ,-0 , }, {4 ,+0 ,1 ,-3 ,6 , }, {0 ,0 , }, +}; + +static_assert (ARRAY_SIZE (cmdline_test_range_strings) ==ARRAY_SIZE (cmdline_test_range_values)); +static void cmdline_do_one_test (struct kunit *test,const char *in,int rc,int offset) {const char *fmt ="Pattern: %s" ;@@ -84,10 +104,42 @@ static void cmdline_test_tail_int(struct kunit *test) }while (++i <ARRAY_SIZE (cmdline_test_strings)); } +static void cmdline_do_one_range_test (struct kunit *test,const char *in, +unsigned int n,const int *e) +{ +unsigned int i; +int r[16 ]; + +#define FMT "in test %u"
lib/cmdline_kunit.c static void cmdline_test_tail_int(struct kunit *test) · line 123
Quoted 10 lines
lib/cmdline_kunit.c +10
-0+#define FMT2 "expected %d numbers, got %d" +#define FMT3 "at %d" +memset (r,0 ,sizeof (r)); +get_options (in,ARRAY_SIZE (r), r); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],FMT " " FMT2 , n, e[0 ], r[0 ]); +for (i =1 ; i <ARRAY_SIZE (r); i++) +KUNIT_EXPECT_EQ_MSG (test, r[i], e[i],FMT " " FMT3 , n, i); +#undef FMT3 +#undef FMT2 +#undef FMT
IMO these FMT macros are super confusing, please just use hardcoded strings because you're not saving any code anyway.
Bart
Quoted 25 lines
lib/cmdline_kunit.c +15
-1+} + +static void cmdline_test_range (struct kunit *test) +{ +unsigned int i =0 ; + +do { +const char *str = cmdline_test_range_strings[i]; +const int *e = cmdline_test_range_values[i]; + +cmdline_do_one_range_test (test, str, i, e); + }while (++i <ARRAY_SIZE (cmdline_test_range_strings)); +} +static struct kunit_case cmdline_test_cases[] = {KUNIT_CASE (cmdline_test_noint),KUNIT_CASE (cmdline_test_lead_int),KUNIT_CASE (cmdline_test_tail_int), +KUNIT_CASE (cmdline_test_range), {} }; --2.29.2