]> git.droids-corp.org - protos/libecoli.git/commitdiff
check for double test registration
authorOlivier Matz <zer0@droids-corp.org>
Sat, 10 Mar 2018 20:52:09 +0000 (21:52 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Sat, 10 Mar 2018 20:52:09 +0000 (21:52 +0100)
lib/ecoli_node.h
lib/ecoli_test.c
lib/ecoli_test.h

index f0b02813a900baa2c73466f28fefd5fac20cb305..b0269908bac55d7f5dc04a11aafe13da79fccef2 100644 (file)
@@ -79,13 +79,15 @@ struct ec_completed;
 struct ec_strvec;
 struct ec_keyval;
 
-#define EC_NODE_TYPE_REGISTER(t)                                               \
+#define EC_NODE_TYPE_REGISTER(t)                                       \
        static void ec_node_init_##t(void);                             \
        static void __attribute__((constructor, used))                  \
        ec_node_init_##t(void)                                          \
        {                                                               \
                if (ec_node_type_register(&t) < 0)                      \
-                       fprintf(stderr, "cannot register %s\n", t.name); \
+                       fprintf(stderr,                                 \
+                               "cannot register node type %s\n",       \
+                               t.name);                                \
        }
 
 TAILQ_HEAD(ec_node_type_list, ec_node_type);
index 4a915f123bc33b80f8c2c4f0c1d6faedec448a0b..430679246eb7d2f49910412b740404738b0154dd 100644 (file)
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 
 #include <ecoli_log.h>
 #include <ecoli_malloc.h>
@@ -43,11 +44,27 @@ static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
 
 EC_LOG_TYPE_REGISTER(test);
 
-/* register a test case */
-void ec_test_register(struct ec_test *test)
+static struct ec_test *ec_test_lookup(const char *name)
 {
+       struct ec_test *test;
+
+       TAILQ_FOREACH(test, &test_list, next) {
+               if (!strcmp(name, test->name))
+                       return test;
+       }
+
+       errno = EEXIST;
+       return NULL;
+}
+
+int ec_test_register(struct ec_test *test)
+{
+       if (ec_test_lookup(test->name) != NULL)
+               return -1;
+
        TAILQ_INSERT_TAIL(&test_list, test, next);
-       // XXX check if already exist, like for type
+
+       return 0;
 }
 
 int ec_test_check_parse(struct ec_node *tk, int expected, ...)
index 201f497615735b23569d16021ac047673e1e8ca7..f28dfdb6c3ea7a2fa0f7c1fe607e1fd2ca6ce641 100644 (file)
 struct ec_node;
 enum ec_completed_type;
 
-// XXX check if already exists?
 #define EC_TEST_REGISTER(t)                                            \
        static void ec_test_init_##t(void);                             \
        static void __attribute__((constructor, used))                  \
        ec_test_init_##t(void)                                          \
        {                                                               \
-                ec_test_register(&t);                                  \
+               if (ec_test_register(&t) < 0)                           \
+                       fprintf(stderr, "cannot register test %s\n",    \
+                               t.name);                                \
        }
 
 /**
@@ -66,8 +67,10 @@ struct ec_test {
  * @param test
  *   A pointer to a ec_test structure describing the test
  *   to be registered.
+ * @return
+ *   0 on success, -1 on error (errno is set).
  */
-void ec_test_register(struct ec_test *test);
+int ec_test_register(struct ec_test *test);
 
 int ec_test_all(void);
 int ec_test_one(const char *name);