Skip to contentSkip to conversation

tst_test: Add option parsing helpers.

PATCH New Cyril Hrubis 2 participants 3 messages Aug 3, 2016 – Aug 4, 2016 Library code

lore
Share & tools
lore
#0 lore raw

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
index db12d18..ef8d256 100644
@@ -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
index c89e51f..eefd6f8 100644
@@ -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]
#1 lore raw

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);

	...
}

Looks good to me.

Regards,
Jan