From: Olivier Matz Date: Sat, 10 Mar 2018 20:52:09 +0000 (+0100) Subject: check for double test registration X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=9dd9c86a48eae89712ee46197c2817484ac9e6f2;p=protos%2Flibecoli.git check for double test registration --- diff --git a/lib/ecoli_node.h b/lib/ecoli_node.h index f0b0281..b026990 100644 --- a/lib/ecoli_node.h +++ b/lib/ecoli_node.h @@ -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); diff --git a/lib/ecoli_test.c b/lib/ecoli_test.c index 4a915f1..4306792 100644 --- a/lib/ecoli_test.c +++ b/lib/ecoli_test.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -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, ...) diff --git a/lib/ecoli_test.h b/lib/ecoli_test.h index 201f497..f28dfdb 100644 --- a/lib/ecoli_test.h +++ b/lib/ecoli_test.h @@ -35,13 +35,14 @@ 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);