tst_test: Add option parsing helpers.
PATCH 1/1 tst_test: Add option parsing helpers.
Add two helpers for parsing integers and floags, these are intended to be used in the test setup to parse options from struct tst_option.
static char *str_threads;
static int threads;
...
static struct tst_options[] = {
{"t:", &str_threads, "Number of threads"},
...
{NULL, NULL, NULL}
};static void setup(void)
{
if (tst_parse_int(str_threads, &threads, 1, INT_MAX))
tst_brk(TBROK, "Invalid number of threads '%s'", str_threads);
...
}--- doc/test-writing-guidelines.txt | 51 +++++++++++++++++++++++++++++++++++++++++ include/tst_test.h | 10 ++++++++ lib/tst_test.c | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+)
doc/test-writing-guidelines.txt +51 -0
@@ -512,6 +512,57 @@ non-'NULL' value if option was present. The 'help' is a short help string. NOTE: The test parameters must not collide with common test parameters defined in the library the currently used ones are +-i+, +-I+, +-C+, and +-h+. +[source,c] +------------------------------------------------------------------------------- +int tst_parse_int(const char *str, int *val, int min, int max); +int tst_parse_float(const char *str, float *val, float min, float max); +------------------------------------------------------------------------------- + +Helpers for parsing the the strings returned in the 'struct tst_options'. + +Both return zero on success and 'errno', mostly 'EINVAL' or 'ERANGE', on +failure. + +Both functions are no-op if 'str' is 'NULL'. + +The valid range for result includes both 'min' and 'max'. + +.Example Usage +[source,c] +------------------------------------------------------------------------------- +#include <limits.h> +#include "tst_test.h" + +static char *str_threads; +static int threads = 10; + +static struct tst_options[] = { + {"t:", &str_threads, "Number of threads (default 10)"}, + ... + {NULL, NULL, NULL} +}; + +static void setup(void) +{ + if (tst_parse_int(str_threads, &threads, 1, INT_MAX)) + tst_brk(TBROK, "Invalid number of threads '%s'", str_threads); + + ... +} + +static void test_threads(void) +{ + ... + + for (i = 0; i < threads; i++) { + ... + } + + ... +} +------------------------------------------------------------------------------- + + 2.2.6 Runtime kernel version detection ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
include/tst_test.h +10 -0
@@ -70,6 +70,16 @@ struct tst_option {char *help; }; +/* +* Options parsing helpers. +* +* If str is NULL these are No-op. +* +* On failure non-zero (errno) is returned. +*/ +int tst_parse_int (const char *str,int *val,int min,int max); +int tst_parse_float (const char *str,float *val,float min,float max); +struct tst_test {/* test id usually the same as test filename without file suffix */ const char *tid;
lib/tst_test.c +47 -0
53 lines not on this page · raw message
Signature · 6 lines
2.7.3 -- Cyril Hrubis [email protected]
PATCH 1/1 tst_test: Add option parsing helpers.
----- Original Message -----
Quoted 20 lines
From: "Cyril Hrubis" <[email protected]>
To: [email protected]
Cc: "Jan Stancek" <[email protected]>
Sent: Wednesday, 3 August, 2016 4:59:25 PM
Subject: [PATCH]tst_test: Add option parsing helpers.Add two helpers for parsing integers and floags, these are intended to be used in the test setup to parse options from struct
tst_option.static char
*str_threads;
static int threads;
...static struct tst_options[] = { {"t:", &str_threads, "Number of threads"}, ... {NULL, NULL, NULL} };static void setup(void)
Quoted 8 lines
{ if (tst_parse_int(str_threads, &threads, 1, INT_MAX)) tst_brk(TBROK, "Invalid number of threads '%s'", str_threads); ... }Signed-off-by Cyril Hrubis <[email protected]>
Looks good to me.
Regards,
Jan
PATCH 1/1 tst_test: Add option parsing helpers.
Hi!
Quoted 1 line
Reviewed-by Jan Stancek <[email protected]>
I've fixed a few typos in the commit message and documentation and pushed.
Signature · 2 lines
Cyril Hrubis [email protected]