From: Olivier Matz Date: Fri, 9 Mar 2018 23:04:21 +0000 (+0100) Subject: better test coverage X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=178493c1b062c6d6250ffbf64aed78c20aceab74;p=protos%2Flibecoli.git better test coverage --- diff --git a/lib/ecoli_node_int.c b/lib/ecoli_node_int.c index f8ba567..aedaae4 100644 --- a/lib/ecoli_node_int.c +++ b/lib/ecoli_node_int.c @@ -70,10 +70,8 @@ static int parse_llint(struct ec_node_int_uint *node, const char *str, errno = 0; *val = strtoll(str, &endptr, node->base); - if (errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) - return -1; - - if (errno != 0 && *val == 0) + if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) || + (errno != 0 && *val == 0)) return -1; if (node->check_min && *val < node->min) @@ -101,10 +99,8 @@ static int parse_ullint(struct ec_node_int_uint *node, const char *str, errno = 0; *val = strtoull(str, &endptr, node->base); - if (errno == ERANGE && *val == ULLONG_MAX) - return -1; - - if (errno != 0 && *val == 0) + if ((errno == ERANGE && *val == ULLONG_MAX) || + (errno != 0 && *val == 0)) return -1; if (node->check_min && *val < node->umin) @@ -125,7 +121,8 @@ static int ec_node_int_uint_parse(const struct ec_node *gen_node, { struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node; const char *str; - int64_t val; + uint64_t u64; + int64_t i64; (void)state; @@ -133,9 +130,13 @@ static int ec_node_int_uint_parse(const struct ec_node *gen_node, return EC_PARSED_NOMATCH; str = ec_strvec_val(strvec, 0); - if (parse_llint(node, str, &val) < 0) - return EC_PARSED_NOMATCH; - + if (node->is_signed) { + if (parse_llint(node, str, &i64) < 0) + return EC_PARSED_NOMATCH; + } else { + if (parse_ullint(node, str, &u64) < 0) + return EC_PARSED_NOMATCH; + } return 1; } @@ -357,23 +358,30 @@ static int ec_node_int_testcase(void) uint64_t u64; int64_t i64; - node = ec_node_uint(EC_NO_ID, 0, 256, 0); + node = ec_node_uint(EC_NO_ID, 1, 256, 0); if (node == NULL) { EC_LOG(EC_LOG_ERR, "cannot create node\n"); return -1; } - ret |= EC_TEST_CHECK_PARSE(node, 1, "0"); + ret |= EC_TEST_CHECK_PARSE(node, -1, ""); + ret |= EC_TEST_CHECK_PARSE(node, -1, "0"); + ret |= EC_TEST_CHECK_PARSE(node, 1, "1"); ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo"); ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100"); ret |= EC_TEST_CHECK_PARSE(node, 1, " 1"); ret |= EC_TEST_CHECK_PARSE(node, -1, "-1"); ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101"); + ret |= EC_TEST_CHECK_PARSE(node, -1, "zzz"); + ret |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000"); + ret |= EC_TEST_CHECK_PARSE(node, -1, "4r"); + ec_node_uint_disable_limits(node); + ret |= EC_TEST_CHECK_PARSE(node, 1, "0"); - p = ec_node_parse(node, "0"); + p = ec_node_parse(node, "1"); s = ec_strvec_val(ec_parsed_strvec(p), 0); EC_TEST_ASSERT(s != NULL && ec_node_uint_getval(node, s, &u64) == 0 && - u64 == 0); + u64 == 1); ec_parsed_free(p); p = ec_node_parse(node, "10"); @@ -393,7 +401,12 @@ static int ec_node_int_testcase(void) ret |= EC_TEST_CHECK_PARSE(node, 1, "-1"); ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff"); ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff"); + ret |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000"); ret |= EC_TEST_CHECK_PARSE(node, -1, "-2"); + ret |= EC_TEST_CHECK_PARSE(node, -1, "zzz"); + ret |= EC_TEST_CHECK_PARSE(node, -1, "4r"); + ec_node_int_disable_limits(node); + ret |= EC_TEST_CHECK_PARSE(node, 1, "-2"); p = ec_node_parse(node, "10"); s = ec_strvec_val(ec_parsed_strvec(p), 0); diff --git a/lib/ecoli_node_str.c b/lib/ecoli_node_str.c index 80134b0..dc5f805 100644 --- a/lib/ecoli_node_str.c +++ b/lib/ecoli_node_str.c @@ -171,6 +171,7 @@ static int ec_node_str_testcase(void) EC_LOG(EC_LOG_ERR, "cannot create node\n"); return -1; } + EC_TEST_ASSERT(!strcmp(ec_node_desc(node), "foo")); ret |= EC_TEST_CHECK_PARSE(node, 1, "foo"); ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar"); ret |= EC_TEST_CHECK_PARSE(node, -1, "foobar"); diff --git a/lib/ecoli_test.h b/lib/ecoli_test.h index 90159f1..a22e081 100644 --- a/lib/ecoli_test.h +++ b/lib/ecoli_test.h @@ -81,6 +81,7 @@ int ec_test_check_parse(struct ec_node *node, int expected, ...); /* XXX this is not an assert, it does not abort */ // XXX use it instead of ec_log to have the file:line +// XXX set test result? #define EC_TEST_ASSERT_STR(cond, fmt, ...) \ do { \ if (!(cond)) \