From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 7A8AF45B9C; Tue, 22 Oct 2024 08:14:39 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0B6EE40A6E; Tue, 22 Oct 2024 08:13:58 +0200 (CEST) Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by mails.dpdk.org (Postfix) with ESMTP id 021C7402A3 for ; Tue, 22 Oct 2024 08:13:46 +0200 (CEST) Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4XXhdr2h3Bz1T8xR; Tue, 22 Oct 2024 14:11:44 +0800 (CST) Received: from dggpeml500024.china.huawei.com (unknown [7.185.36.10]) by mail.maildlp.com (Postfix) with ESMTPS id 7AC8B14035F; Tue, 22 Oct 2024 14:13:44 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Tue, 22 Oct 2024 14:13:44 +0800 From: Chengwen Feng To: , CC: , Subject: [PATCH v3 7/8] app/test: add process opt testcase for kvargs Date: Tue, 22 Oct 2024 06:14:30 +0000 Message-ID: <20241022061431.21925-8-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20241022061431.21925-1-fengchengwen@huawei.com> References: <20231103095325.47843-1-fengchengwen@huawei.com> <20241022061431.21925-1-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This commit adds rte_kvargs_process_opt() API's testcase. Signed-off-by: Chengwen Feng --- app/test/test_kvargs.c | 106 ++++++++++++++++++++++++++++++++++------- 1 file changed, 90 insertions(+), 16 deletions(-) diff --git a/app/test/test_kvargs.c b/app/test/test_kvargs.c index d1e668ac4a..43bb7a0243 100644 --- a/app/test/test_kvargs.c +++ b/app/test/test_kvargs.c @@ -11,14 +11,20 @@ #include "test.h" +typedef int (*f_kvargs_process)(const struct rte_kvargs *kvlist, + const char *key_match, arg_handler_t handler, + void *opaque_arg); + +static bool use_kvargs_process_opt[] = { false, true }; + /* incremented in handler, to check it is properly called once per * key/value association */ static unsigned count; /* this handler increment the "count" variable at each call and check * that the key is "check" and the value is "value%d" */ -static int check_handler(const char *key, const char *value, - __rte_unused void *opaque) +static int +check_handler(const char *key, const char *value, __rte_unused void *opaque) { char buf[16]; @@ -35,6 +41,18 @@ static int check_handler(const char *key, const char *value, return 0; } +static int +check_only_handler(const char *key, const char *value, __rte_unused void *opaque) +{ + if (strcmp(key, "check")) + return -1; + + if (value != NULL) + return -1; + + return 0; +} + static int test_basic_token_count(void) { @@ -80,8 +98,11 @@ test_basic_token_count(void) } static int -test_parse_without_valid_keys(void) +test_parse_without_valid_keys(const void *params) { + const bool use_opt = *(const bool *)params; + f_kvargs_process proc_func = use_opt ? rte_kvargs_process_opt : rte_kvargs_process; + const char *proc_name = use_opt ? "rte_kvargs_process_opt" : "rte_kvargs_process"; const char *args = "foo=1234,check=value0,check=value1"; struct rte_kvargs *kvlist; @@ -93,28 +114,28 @@ test_parse_without_valid_keys(void) /* call check_handler() for all entries with key="check" */ count = 0; - if (rte_kvargs_process(kvlist, "check", check_handler, NULL) < 0) { - printf("rte_kvargs_process(check) error\n"); + if (proc_func(kvlist, "check", check_handler, NULL) < 0) { + printf("%s(check) error\n", proc_name); rte_kvargs_free(kvlist); return -1; } if (count != 2) { - printf("invalid count value %u after rte_kvargs_process(check)\n", - count); + printf("invalid count value %u after %s(check)\n", + count, proc_name); rte_kvargs_free(kvlist); return -1; } /* call check_handler() for all entries with key="nonexistent_key" */ count = 0; - if (rte_kvargs_process(kvlist, "nonexistent_key", check_handler, NULL) < 0) { - printf("rte_kvargs_process(nonexistent_key) error\n"); + if (proc_func(kvlist, "nonexistent_key", check_handler, NULL) < 0) { + printf("%s(nonexistent_key) error\n", proc_name); rte_kvargs_free(kvlist); return -1; } if (count != 0) { - printf("invalid count value %d after rte_kvargs_process(nonexistent_key)\n", - count); + printf("invalid count value %d after %s(nonexistent_key)\n", + count, proc_name); rte_kvargs_free(kvlist); return -1; } @@ -142,8 +163,11 @@ test_parse_without_valid_keys(void) } static int -test_parse_with_valid_keys(void) +test_parse_with_valid_keys(const void *params) { + const bool use_opt = *(const bool *)params; + f_kvargs_process proc_func = use_opt ? rte_kvargs_process_opt : rte_kvargs_process; + const char *proc_name = use_opt ? "rte_kvargs_process_opt" : "rte_kvargs_process"; const char *args = "foo=droids,check=value0,check=value1,check=wrong_value"; const char *valid_keys[] = { "foo", "check", NULL }; struct rte_kvargs *kvlist; @@ -158,8 +182,8 @@ test_parse_with_valid_keys(void) * should fail as the value is not recognized by the handler */ count = 0; - if (rte_kvargs_process(kvlist, "check", check_handler, NULL) == 0 || count != 2) { - printf("rte_kvargs_process(check) is success but should not\n"); + if (proc_func(kvlist, "check", check_handler, NULL) == 0 || count != 2) { + printf("%s(check) is success but should not\n", proc_name); rte_kvargs_free(kvlist); return -1; } @@ -218,6 +242,13 @@ test_parse_empty_elements(void) return -1; } + count = kvlist->count; + if (count != 2) { + printf("invalid count value %u\n", count); + rte_kvargs_free(kvlist); + return -1; + } + if (rte_kvargs_count(kvlist, "foo") != 1) { printf("invalid count value for 'foo'\n"); rte_kvargs_free(kvlist); @@ -234,6 +265,34 @@ test_parse_empty_elements(void) return 0; } +static int +test_parse_with_only_key(void) +{ + const char *args = "foo,check"; + struct rte_kvargs *kvlist; + + kvlist = rte_kvargs_parse(args, NULL); + if (kvlist == NULL) { + printf("rte_kvargs_parse() error\n"); + return -1; + } + + if (rte_kvargs_process(kvlist, "check", check_only_handler, NULL) == 0) { + printf("rte_kvargs_process(check) error\n"); + rte_kvargs_free(kvlist); + return -1; + } + + if (rte_kvargs_process_opt(kvlist, "check", check_only_handler, NULL) != 0) { + printf("rte_kvargs_process_opt(check) error\n"); + rte_kvargs_free(kvlist); + return -1; + } + + rte_kvargs_free(kvlist); + return 0; +} + /* test several error cases */ static int test_invalid_kvargs(void) { @@ -275,10 +334,25 @@ static struct unit_test_suite kvargs_test_suite = { .teardown = NULL, .unit_test_cases = { TEST_CASE(test_basic_token_count), - TEST_CASE(test_parse_without_valid_keys), - TEST_CASE(test_parse_with_valid_keys), + TEST_CASE_NAMED_WITH_DATA("test_parse_without_valid_keys_no_opt", + NULL, NULL, + test_parse_without_valid_keys, + &use_kvargs_process_opt[0]), + TEST_CASE_NAMED_WITH_DATA("test_parse_without_valid_keys_with_opt", + NULL, NULL, + test_parse_without_valid_keys, + &use_kvargs_process_opt[1]), + TEST_CASE_NAMED_WITH_DATA("test_parse_with_valid_keys_no_opt", + NULL, NULL, + test_parse_with_valid_keys, + &use_kvargs_process_opt[0]), + TEST_CASE_NAMED_WITH_DATA("test_parse_with_valid_keys_with_opt", + NULL, NULL, + test_parse_with_valid_keys, + &use_kvargs_process_opt[1]), TEST_CASE(test_parse_list_value), TEST_CASE(test_parse_empty_elements), + TEST_CASE(test_parse_with_only_key), TEST_CASE(test_invalid_kvargs), TEST_CASES_END() /**< NULL terminate unit test array */ } -- 2.17.1