[4/4] test/argparse: refine testcases

Message ID 20240220131502.47510-5-fengchengwen@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series refine argparse library |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/intel-Functional success Functional PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS

Commit Message

Chengwen Feng Feb. 20, 2024, 1:15 p.m. UTC
  Refine testcases, including:
1. add testcase comment.
2. argv[0] should set obj->prog_name.
3. set val_set as NULL in test_argparse_invalid_arg_flags, let it
test to the specified code logic.
4. enable index verification in opt_callback_parse_int_of_no_val.

Fixes: 6c5c6571601c ("argparse: verify argument config")
Fixes: 31ed9f9f43bb ("argparse: parse parameters")

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
 app/test/test_argparse.c | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)
  

Patch

diff --git a/app/test/test_argparse.c b/app/test/test_argparse.c
index 708a575e16..5be9552aae 100644
--- a/app/test/test_argparse.c
+++ b/app/test/test_argparse.c
@@ -196,11 +196,13 @@  test_argparse_invalid_has_val(void)
 	uint32_t index;
 	int ret;
 
+	/* test optional arg don't config has-value. */
 	obj = test_argparse_init_obj();
 	obj->args[0].flags &= ~0x3ull;
 	ret = rte_argparse_parse(obj, default_argc, default_argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
 
+	/* test positional arg don't config required-value. */
 	for (index = 0; index < RTE_DIM(set_mask); index++) {
 		obj = test_argparse_init_obj();
 		obj->args[0].name_long = "abc";
@@ -268,21 +270,24 @@  test_argparse_invalid_arg_flags(void)
 	struct rte_argparse *obj;
 	int ret;
 
+	/* test set unused bits. */
 	obj = test_argparse_init_obj();
 	obj->args[0].flags |= ~0x7FFull;
 	ret = rte_argparse_parse(obj, default_argc, default_argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
 
+	/* test positional arg should not config multiple.  */
 	obj = test_argparse_init_obj();
 	obj->args[0].name_long = "positional";
 	obj->args[0].name_short = NULL;
 	obj->args[0].val_saver = (void *)1;
-	obj->args[0].val_set = (void *)1;
+	obj->args[0].val_set = NULL;
 	obj->args[0].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_INT |
 			     RTE_ARGPARSE_ARG_SUPPORT_MULTI;
 	ret = rte_argparse_parse(obj, default_argc, default_argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
 
+	/* test optional arg enabled multiple but prased by autosave. */
 	obj = test_argparse_init_obj();
 	obj->args[0].flags |= RTE_ARGPARSE_ARG_SUPPORT_MULTI;
 	ret = rte_argparse_parse(obj, default_argc, default_argv);
@@ -320,13 +325,13 @@  test_argparse_invalid_option(void)
 	int ret;
 
 	obj = test_argparse_init_obj();
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("--invalid");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
 
 	obj = test_argparse_init_obj();
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("invalid");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
@@ -350,7 +355,7 @@  test_argparse_opt_autosave_parse_int_of_no_val(void)
 	obj->args[0].val_set = (void *)100;
 	obj->args[0].flags = flags;
 	obj->args[1].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("--test-long");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == 0, "Argparse parse expect success!");
@@ -382,7 +387,7 @@  test_argparse_opt_autosave_parse_int_of_required_val(void)
 	obj->args[0].val_set = NULL;
 	obj->args[0].flags = flags;
 	obj->args[1].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("--test-long");
 	argv[2] = test_strdup("100");
 	ret = rte_argparse_parse(obj, 3, argv);
@@ -416,6 +421,7 @@  test_argparse_opt_autosave_parse_int_of_optional_val(void)
 	char *argv[2];
 	int ret;
 
+	/* test without value. */
 	obj = test_argparse_init_obj();
 	obj->args[0].name_long = "--test-long";
 	obj->args[0].name_short = "-t";
@@ -423,7 +429,7 @@  test_argparse_opt_autosave_parse_int_of_optional_val(void)
 	obj->args[0].val_set = (void *)100;
 	obj->args[0].flags = flags;
 	obj->args[1].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("--test-long");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == 0, "Argparse parse expect success!");
@@ -467,7 +473,8 @@  test_argparse_opt_autosave_parse_int_of_optional_val(void)
 static int
 opt_callback_parse_int_of_no_val(uint32_t index, const char *value, void *opaque)
 {
-	RTE_SET_USED(index);
+	if (index != 1)
+		return -EINVAL;
 	if (value != NULL)
 		return -EINVAL;
 	*(int *)opaque = 100;
@@ -488,10 +495,10 @@  test_argparse_opt_callback_parse_int_of_no_val(void)
 	obj->args[0].name_long = "--test-long";
 	obj->args[0].name_short = "-t";
 	obj->args[0].val_saver = NULL;
-	obj->args[0].val_set = (void *)100;
+	obj->args[0].val_set = (void *)1;
 	obj->args[0].flags = RTE_ARGPARSE_ARG_NO_VALUE;
 	obj->args[1].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("--test-long");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == 0, "Argparse parse expect success!");
@@ -542,7 +549,7 @@  test_argparse_opt_callback_parse_int_of_required_val(void)
 	obj->args[0].val_set = (void *)1;
 	obj->args[0].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE;
 	obj->args[1].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("--test-long");
 	argv[2] = test_strdup("100");
 	ret = rte_argparse_parse(obj, 3, argv);
@@ -606,7 +613,7 @@  test_argparse_opt_callback_parse_int_of_optional_val(void)
 	obj->args[0].val_set = (void *)1;
 	obj->args[0].flags = RTE_ARGPARSE_ARG_OPTIONAL_VALUE;
 	obj->args[1].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("--test-long");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == 0, "Argparse parse expect success!");
@@ -651,6 +658,7 @@  test_argparse_pos_autosave_parse_int(void)
 	char *argv[3];
 	int ret;
 
+	/* test positional autosave parse successful. */
 	obj = test_argparse_init_obj();
 	obj->args[0].name_long = "test-long";
 	obj->args[0].name_short = NULL;
@@ -658,19 +666,20 @@  test_argparse_pos_autosave_parse_int(void)
 	obj->args[0].val_set = NULL;
 	obj->args[0].flags = flags;
 	obj->args[1].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("100");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == 0, "Argparse parse expect success!");
 	TEST_ASSERT(val_saver == 100, "Argparse parse expect success!");
 
+	/* test positional autosave parse failed. */
 	obj->args[0].flags = flags;
 	val_saver = 0;
 	argv[1] = test_strdup("100a");
 	ret = rte_argparse_parse(obj, 2, argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
 
-	/* test over position parameters. */
+	/* test too much position parameters. */
 	obj->args[0].flags = flags;
 	argv[1] = test_strdup("100");
 	argv[2] = test_strdup("200");
@@ -708,6 +717,7 @@  test_argparse_pos_callback_parse_int(void)
 	char *argv[3];
 	int ret;
 
+	/* test positional callback parse successful. */
 	obj = test_argparse_init_obj();
 	obj->callback = pos_callback_parse_int;
 	obj->opaque = (void *)val_saver;
@@ -722,7 +732,7 @@  test_argparse_pos_callback_parse_int(void)
 	obj->args[1].val_set = (void *)2;
 	obj->args[1].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE;
 	obj->args[2].name_long = NULL;
-	argv[0] = test_strdup(obj->usage);
+	argv[0] = test_strdup(obj->prog_name);
 	argv[1] = test_strdup("100");
 	argv[2] = test_strdup("200");
 	ret = rte_argparse_parse(obj, 3, argv);
@@ -730,7 +740,7 @@  test_argparse_pos_callback_parse_int(void)
 	TEST_ASSERT(val_saver[1] == 100, "Argparse parse expect success!");
 	TEST_ASSERT(val_saver[2] == 200, "Argparse parse expect success!");
 
-	/* test callback return failed. */
+	/* test positional callback parse failed. */
 	obj->args[0].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE;
 	obj->args[1].flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE;
 	argv[2] = test_strdup("200a");