Module: xenomai-forge
Branch: next
Commit: fda7a5479140d47418a7ddc5e287f5ffbf640055
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=fda7a5479140d47418a7ddc5e287f5ffbf640055

Author: Philippe Gerum <r...@xenomai.org>
Date:   Thu Jul  3 20:28:10 2014 +0200

smokey: refer to arguments by name

Rather than position in the arglist declaration order, which is
error-prone.

---

 include/smokey/smokey.h                    |   29 +++++++++++++++-------------
 lib/smokey/helpers.c                       |   17 ++++++++++++++++
 lib/smokey/init.c                          |   23 +++++++++++-----------
 testsuite/smokey/sched-quota/sched-quota.c |    8 ++++----
 4 files changed, 48 insertions(+), 29 deletions(-)

diff --git a/include/smokey/smokey.h b/include/smokey/smokey.h
index 0ebb192..81578b7 100644
--- a/include/smokey/smokey.h
+++ b/include/smokey/smokey.h
@@ -75,27 +75,27 @@ struct smokey_test {
 #define __smokey_arg_count(__args)     \
        (sizeof(__args) / sizeof(__args[0]))
 
-#define smokey_test_plugin(__name, __args, __desc)                     \
-       static int run_ ## __name(struct smokey_test *t,                \
-                                 int argc, char *const argv[]);        \
-       static struct smokey_test __name = {                            \
-               .name = #__name,                                        \
+#define smokey_test_plugin(__plugin, __args, __desc)                   \
+       static int run_ ## __plugin(struct smokey_test *t,              \
+                                   int argc, char *const argv[]);      \
+       static struct smokey_test __plugin = {                          \
+               .name = #__plugin,                                      \
                .args = (__args),                                       \
                .nargs = __smokey_arg_count(__args),                    \
                .description = (__desc),                                \
-               .run = run_ ## __name,                                  \
+               .run = run_ ## __plugin,                                \
        };                                                              \
-       void smokey_plugin_ ## __name(void);                            \
+       void smokey_plugin_ ## __plugin(void);                          \
        __attribute__((constructor(__SMOKEYPLUG_CTOR_PRIO)))            \
-       void smokey_plugin_ ## __name(void)                             \
+       void smokey_plugin_ ## __plugin(void)                           \
        {                                                               \
-               smokey_register_plugin(&(__name));                      \
+               smokey_register_plugin(&(__plugin));                    \
        }
 
-#define SMOKEY_ARG(__name, __pos)        ((__name).args + __pos)
-#define SMOKEY_ARG_ISSET(__name, __pos)          (SMOKEY_ARG(__name, 
__pos)->matched)
-#define SMOKEY_ARG_INT(__name, __pos)    (SMOKEY_ARG(__name, __pos)->u.n_val)
-#define SMOKEY_ARG_STRING(__name, __pos)  (SMOKEY_ARG(__name, __pos)->u.s_val)
+#define SMOKEY_ARG(__plugin, __arg)       (smokey_lookup_arg(&(__plugin), # 
__arg))
+#define SMOKEY_ARG_ISSET(__plugin, __arg)  (SMOKEY_ARG(__plugin, 
__arg)->matched)
+#define SMOKEY_ARG_INT(__plugin, __arg)           (SMOKEY_ARG(__plugin, 
__arg)->u.n_val)
+#define SMOKEY_ARG_STRING(__plugin, __arg) (SMOKEY_ARG(__plugin, 
__arg)->u.s_val)
 
 #ifdef __cplusplus
 extern "C" {
@@ -109,6 +109,9 @@ int smokey_bool(const char *s, struct smokey_arg *arg);
 
 int smokey_string(const char *s, struct smokey_arg *arg);
 
+struct smokey_arg *smokey_lookup_arg(struct smokey_test *t,
+                                    const char *arg);
+
 int smokey_parse_args(struct smokey_test *t,
                      int argc, char *const argv[]);
 
diff --git a/lib/smokey/helpers.c b/lib/smokey/helpers.c
index e5c9b09..9dccea9 100644
--- a/lib/smokey/helpers.c
+++ b/lib/smokey/helpers.c
@@ -100,3 +100,20 @@ int smokey_parse_args(struct smokey_test *t,
 
        return matched;
 }
+
+struct smokey_arg *smokey_lookup_arg(struct smokey_test *t,
+                                    const char *name)
+{
+       struct smokey_arg *arg = NULL;
+       int ac;
+
+       for (arg = t->args, ac = 0;
+            arg->name && ac < t->nargs; arg++, ac++) {
+               if (strcmp(arg->name, name) == 0)
+                       return arg;
+       }
+
+       /* Assume this is fatal. */
+       panic("test %s has no argument \"%s\"",
+             t->name, name);
+}
diff --git a/lib/smokey/init.c b/lib/smokey/init.c
index 6a55c08..a137225 100644
--- a/lib/smokey/init.c
+++ b/lib/smokey/init.c
@@ -98,12 +98,12 @@
  *      if (ret)
  *          return ret;
  *
- *     if (SMOKEY_ARG_ISSET(foo, 0))
- *             i_arg = SMOKEY_ARG_INT(foo, 0);
- *     if (SMOKEY_ARG_ISSET(foo, 1))
- *             s_arg = SMOKEY_ARG_STRING(foo, 1);
- *     if (SMOKEY_ARG_ISSET(foo, 2))
- *             b_arg = SMOKEY_ARG_INT(foo, 2);
+ *     if (SMOKEY_ARG_ISSET(foo, some_integer))
+ *             i_arg = SMOKEY_ARG_INT(foo, some_integer);
+ *     if (SMOKEY_ARG_ISSET(foo, some_string))
+ *             s_arg = SMOKEY_ARG_STRING(foo, some_string);
+ *     if (SMOKEY_ARG_ISSET(foo, some_boolean))
+ *             b_arg = SMOKEY_ARG_INT(foo, some_boolean);
  *
  *      return run_some_hypothetical_smoke_test_code(i_arg, s_arg, b_arg);
  * }
@@ -127,12 +127,11 @@
  *
  * Once smokey_parse_args() has returned, each argument can be checked
  * individually for presence. If a valid argument was matched on the
- * command line, SMOKEY_ARG_ISSET(name, position) returns non-zero,
- * with @a position defined as the 0-based argument position in the
- * declarator list. In the latter case, its value can be retrieved by
- * a corresponding call to SMOKEY_ARG_INT(name, position),
- * SMOKEY_ARG_STRING(name, position) or SMOKEY_ARG_BOOL(name,
- * position).
+ * command line, SMOKEY_ARG_ISSET(test_name, arg_name) returns
+ * non-zero. In the latter case, its value can be retrieved by a
+ * similar call to SMOKEY_ARG_INT(test_name, arg_name),
+ * SMOKEY_ARG_STRING(test_name, arg_name) or
+ * SMOKEY_ARG_BOOL(test_name, arg_name).
  *
  * In the above example, passing "some_integer=3" on the command line of
  * any program implementing such Smokey-based test would cause the
diff --git a/testsuite/smokey/sched-quota/sched-quota.c 
b/testsuite/smokey/sched-quota/sched-quota.c
index c06b993..1bacafb 100644
--- a/testsuite/smokey/sched-quota/sched-quota.c
+++ b/testsuite/smokey/sched-quota/sched-quota.c
@@ -286,14 +286,14 @@ static int run_sched_quota(struct smokey_test *t, int 
argc, char *const argv[])
                return -ret;
        }
 
-       if (SMOKEY_ARG_ISSET(sched_quota, 0))
-               quota = SMOKEY_ARG_INT(sched_quota, 0);
+       if (SMOKEY_ARG_ISSET(sched_quota, quota))
+               quota = SMOKEY_ARG_INT(sched_quota, quota);
 
        if (quota <= 0)
                quota = 10;
 
-       if (SMOKEY_ARG_ISSET(sched_quota, 1))
-               nrthreads = SMOKEY_ARG_INT(sched_quota, 1);
+       if (SMOKEY_ARG_ISSET(sched_quota, threads))
+               nrthreads = SMOKEY_ARG_INT(sched_quota, threads);
 
        if (nrthreads <= 0)
                nrthreads = 3;


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to