tst_test: Add option parsing helpers.
Add two helpers for parsing integers and floats, 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_option 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);
...
}
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
Reviewed-by: Jan Stancek <jstancek@redhat.com>
diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
index 4990903..68b4602 100644
--- a/doc/test-writing-guidelines.txt
+++ b/doc/test-writing-guidelines.txt
@@ -523,6 +523,63 @@
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_option'.
+
+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_option 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++) {
+ ...
+ }
+
+ ...
+}
+
+static struct tst_test test = {
+ ...
+ .options = options,
+ ...
+};
+-------------------------------------------------------------------------------
+
+
2.2.6 Runtime kernel version detection
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/include/tst_test.h b/include/tst_test.h
index c839178..b02168a 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -70,6 +70,16 @@
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;
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 9eb1393..6c93152 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -443,6 +443,53 @@
}
}
+int tst_parse_int(const char *str, int *val, int min, int max)
+{
+ long rval;
+ char *end;
+
+ if (!str)
+ return 0;
+
+ errno = 0;
+ rval = strtol(str, &end, 10);
+
+ if (str == end || *end != '\0')
+ return EINVAL;
+
+ if (errno)
+ return errno;
+
+ if (rval > (long)max || rval < (long)min)
+ return ERANGE;
+
+ *val = (int)rval;
+ return 0;
+}
+
+int tst_parse_float(const char *str, float *val, float min, float max)
+{
+ double rval;
+ char *end;
+
+ if (!str)
+ return 0;
+
+ errno = 0;
+ rval = strtod(str, &end);
+
+ if (str == end || *end != '\0')
+ return EINVAL;
+
+ if (errno)
+ return errno;
+
+ if (rval > (double)max || rval < (double)min)
+ return ERANGE;
+
+ *val = (float)rval;
+ return 0;
+}
static void do_exit(int ret)
{