From 1d8aacc31a1422468f421b778eedc343bd22c9f6 Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Wed, 14 Mar 2018 22:25:29 +0100 Subject: [PATCH] increase test coverage: 90% lines, 95% funcs --- lib/ecoli_node.c | 21 +++++--- lib/ecoli_parse.c | 126 +++++++++++++++++++++++++++++++++++++++++---- lib/ecoli_strvec.c | 42 +++++++++++++-- 3 files changed, 169 insertions(+), 20 deletions(-) diff --git a/lib/ecoli_node.c b/lib/ecoli_node.c index ba3d98f..20f90b3 100644 --- a/lib/ecoli_node.c +++ b/lib/ecoli_node.c @@ -240,8 +240,6 @@ struct ec_keyval *ec_node_attrs(const struct ec_node *node) const char *ec_node_id(const struct ec_node *node) { - if (node->id == NULL) - return "None"; return node->id; } @@ -306,16 +304,18 @@ static int ec_node_testcase(void) int testres = 0; int ret; - f = open_memstream(&buf, &buflen); - if (f == NULL) - goto fail; - node = EC_NODE_SEQ(EC_NO_ID, ec_node_str("id_x", "x"), ec_node_str("id_y", "y")); if (node == NULL) goto fail; + ec_node_clone(node); + ec_node_free(node); + + f = open_memstream(&buf, &buflen); + if (f == NULL) + goto fail; ec_node_dump(f, node); ec_node_type_dump(f); ec_node_dump(f, NULL); @@ -331,6 +331,12 @@ static int ec_node_testcase(void) "bad dump\n"); free(buf); + testres |= EC_TEST_CHECK( + !strcmp(ec_node_type(node)->name, "seq") && + !strcmp(ec_node_id(node), EC_NO_ID) && + !strcmp(ec_node_desc(node), ""), + "bad child 0"); + testres |= EC_TEST_CHECK( ec_node_get_children_count(node) == 2, "bad children count\n"); @@ -351,7 +357,8 @@ static int ec_node_testcase(void) child = ec_node_find(node, "id_x"); testres |= EC_TEST_CHECK(child != NULL && !strcmp(ec_node_type(child)->name, "str") && - !strcmp(ec_node_id(child), "id_x"), + !strcmp(ec_node_id(child), "id_x") && + !strcmp(ec_node_desc(child), "x"), "bad child id_x"); child = ec_node_find(node, "id_dezdex"); testres |= EC_TEST_CHECK(child == NULL, diff --git a/lib/ecoli_parse.c b/lib/ecoli_parse.c index 87dda7a..7a94ef8 100644 --- a/lib/ecoli_parse.c +++ b/lib/ecoli_parse.c @@ -14,9 +14,15 @@ #include #include #include +#include #include +#include +#include +#include #include +EC_LOG_TYPE_REGISTER(parse); + TAILQ_HEAD(ec_parse_list, ec_parse); struct ec_parse { @@ -366,20 +372,16 @@ struct ec_parse *ec_parse_iter_next(struct ec_parse *parse) struct ec_parse *ec_parse_find_first(struct ec_parse *parse, const char *id) { - struct ec_parse *child, *ret; + struct ec_parse *iter; if (parse == NULL) return NULL; - if (parse->node != NULL && - parse->node->id != NULL && - !strcmp(parse->node->id, id)) - return parse; - - TAILQ_FOREACH(child, &parse->children, next) { - ret = ec_parse_find_first(child, id); - if (ret != NULL) - return ret; + for (iter = parse; iter != NULL; iter = ec_parse_iter_next(iter)) { + if (iter->node != NULL && + iter->node->id != NULL && + !strcmp(iter->node->id, id)) + return iter; } return NULL; @@ -421,3 +423,107 @@ size_t ec_parse_matches(const struct ec_parse *parse) return 1; } + +/* LCOV_EXCL_START */ +static int ec_parse_testcase(void) +{ + struct ec_node *node = NULL; + struct ec_parse *p = NULL, *p2 = NULL; + const struct ec_parse *pc; + FILE *f = NULL; + char *buf = NULL; + size_t buflen = 0; + int testres = 0; + int ret; + + node = ec_node_sh_lex(EC_NO_ID, + EC_NODE_SEQ(EC_NO_ID, + ec_node_str("id_x", "x"), + ec_node_str("id_y", "y"))); + if (node == NULL) + goto fail; + + p = ec_node_parse(node, "xcdscds"); + testres |= EC_TEST_CHECK( + p != NULL && !ec_parse_matches(p), + "parse should not match\n"); + + f = open_memstream(&buf, &buflen); + if (f == NULL) + goto fail; + ec_parse_dump(f, p); + fclose(f); + f = NULL; + + testres |= EC_TEST_CHECK( + strstr(buf, "no match"), "bad dump\n"); + free(buf); + ec_parse_free(p); + + p = ec_node_parse(node, "x y"); + testres |= EC_TEST_CHECK( + p != NULL && ec_parse_matches(p), + "parse should match\n"); + testres |= EC_TEST_CHECK( + ec_parse_len(p) == 1, "bad parse len\n"); + + ret = ec_keyval_set(ec_parse_get_attrs(p), "key", "val", NULL); + testres |= EC_TEST_CHECK(ret == 0, + "cannot set parse attribute\n"); + + p2 = ec_parse_dup(p); + testres |= EC_TEST_CHECK( + p2 != NULL && ec_parse_matches(p2), + "parse should match\n"); + ec_parse_free(p2); + p2 = NULL; + + pc = ec_parse_find_first(p, "id_x"); + testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_x"); + testres |= EC_TEST_CHECK(pc != NULL && + ec_parse_get_parent(pc) != NULL && + ec_parse_get_parent(ec_parse_get_parent(pc)) == p, + "invalid parent\n"); + + pc = ec_parse_find_first(p, "id_y"); + testres |= EC_TEST_CHECK(pc != NULL, "cannot find id_y"); + pc = ec_parse_find_first(p, "id_dezdezdez"); + testres |= EC_TEST_CHECK(pc == NULL, "should not find bad id"); + + + f = open_memstream(&buf, &buflen); + if (f == NULL) + goto fail; + ec_parse_dump(f, p); + fclose(f); + f = NULL; + + testres |= EC_TEST_CHECK( + strstr(buf, "type=sh_lex id=no-id") && + strstr(buf, "type=seq id=no-id") && + strstr(buf, "type=str id=id_x") && + strstr(buf, "type=str id=id_x"), + "bad dump\n"); + free(buf); + + ec_parse_free(p); + ec_node_free(node); + return testres; + +fail: + ec_parse_free(p2); + ec_parse_free(p); + ec_node_free(node); + if (f != NULL) + fclose(f); + + return -1; +} +/* LCOV_EXCL_STOP */ + +static struct ec_test ec_parse_test = { + .name = "parse", + .test = ec_parse_testcase, +}; + +EC_TEST_REGISTER(ec_parse_test); diff --git a/lib/ecoli_strvec.c b/lib/ecoli_strvec.c index 5a2bd23..bd23d98 100644 --- a/lib/ecoli_strvec.c +++ b/lib/ecoli_strvec.c @@ -215,6 +215,10 @@ static int ec_strvec_testcase(void) { struct ec_strvec *strvec = NULL; struct ec_strvec *strvec2 = NULL; + FILE *f = NULL; + char *buf = NULL; + size_t buflen = 0; + int testres = 0; strvec = ec_strvec(); if (strvec == NULL) { @@ -318,16 +322,48 @@ static int ec_strvec_testcase(void) } ec_strvec_free(strvec2); - ec_strvec_dump(stdout, strvec); - ec_strvec_dump(stdout, NULL); + strvec2 = EC_STRVEC("0", "1"); + if (strvec2 == NULL) { + EC_TEST_ERR("cannot create strvec from array\n"); + goto fail; + } + testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0, + "strvec and strvec2 should be equal\n"); + ec_strvec_free(strvec2); + + f = open_memstream(&buf, &buflen); + if (f == NULL) + goto fail; + ec_strvec_dump(f, strvec); + fclose(f); + f = NULL; + testres |= EC_TEST_CHECK( + strstr(buf, "strvec (len=2) [0, 1]"), "bad dump\n"); + free(buf); + buf = NULL; + + f = open_memstream(&buf, &buflen); + if (f == NULL) + goto fail; + ec_strvec_dump(f, NULL); + fclose(f); + f = NULL; + testres |= EC_TEST_CHECK( + strstr(buf, "none"), "bad dump\n"); + free(buf); + buf = NULL; ec_strvec_free(strvec); - return 0; + return testres; fail: + if (f != NULL) + fclose(f); ec_strvec_free(strvec); ec_strvec_free(strvec2); + free(buf); + return -1; } /* LCOV_EXCL_STOP */ -- 2.20.1