You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/04/04 19:14:27 UTC
[incubator-nuttx-apps] branch master updated: apps/testing/ostest:
Add test for required argument format
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new dd7c3bf apps/testing/ostest: Add test for required argument format
dd7c3bf is described below
commit dd7c3bfa5367b0f7a3c34aebd0a4641cc20124a2
Author: Gregory Nutt <gn...@nuttx.org>
AuthorDate: Sun Apr 4 11:51:27 2021 -0600
apps/testing/ostest: Add test for required argument format
The Linux man page requires that the getopt_long() and getopt_long_only() functions accept arguments to options in a form like:
--option=argument
This PR adds a test that missing functionality that was recently added to NuttX.
This change also fixes an error in string comparison that was working before only because of the way that strings are stored by in linker ELF. The address of the strings were being compared, not the value of the string.
This change effects only the getopt() tests of the OS test.
Tested on a simulator NSH configuration and used to verify the NuttX change.
---
testing/ostest/getopt.c | 39 ++++++++++++++++++++++++++++++++++++---
1 file changed, 36 insertions(+), 3 deletions(-)
diff --git a/testing/ostest/getopt.c b/testing/ostest/getopt.c
index 1c76799..baf215b 100644
--- a/testing/ostest/getopt.c
+++ b/testing/ostest/getopt.c
@@ -213,7 +213,10 @@ static int getopt_short_test(int noptions, int argc, FAR char **argv,
ndx + 1, ret, expected[ndx].ret);
}
- if (expected[ndx].arg != optarg)
+ if ((expected[ndx].arg == NULL &&
+ optarg != NULL) ||
+ (expected[ndx].arg != NULL &&
+ strcmp(expected[ndx].arg, optarg) != 0))
{
printf("ERROR: arg %d: optarg=%s (expected %s)\n",
ndx + 1, optarg == NULL ? "null" : optarg,
@@ -281,7 +284,10 @@ static int getopt_long_test(int noptions, int argc, FAR char **argv,
ndx + 1, expected[ndx].flag, g_flag);
}
- if (expected[ndx].arg != optarg)
+ if ((expected[ndx].arg == NULL &&
+ optarg != NULL) ||
+ (expected[ndx].arg != NULL &&
+ strcmp(expected[ndx].arg, optarg) != 0))
{
printf("ERROR: arg %d: optarg=%s (expected %s)\n",
ndx + 1, optarg == NULL ? "null" : optarg,
@@ -350,7 +356,10 @@ static int getopt_longonly_test(int noptions, int argc, FAR char **argv,
ndx + 1, expected[ndx].flag, g_flag);
}
- if (expected[ndx].arg != optarg)
+ if ((expected[ndx].arg == NULL &&
+ optarg != NULL) ||
+ (expected[ndx].arg != NULL &&
+ strcmp(expected[ndx].arg, optarg) != 0))
{
printf("ERROR: arg %d: optarg=%s (expected %s)\n",
ndx + 1, optarg == NULL ? "null" : optarg,
@@ -477,6 +486,30 @@ int getopt_test(void)
getopt_long_test(4, 8, argv, NULL, long_options, NULL,
results);
+ printf("getopt_long(): Argument for --option=argument\n");
+
+ argv[0] = NULL;
+ argv[1] = "--OptionA";
+ argv[2] = "--OptionB";
+ argv[3] = "--OptionC=Arg1";
+ argv[4] = "--OptionD=Arg2";
+ argv[5] = "NoOption";
+ argv[6] = NULL;
+
+ LONG_OPTION_A(0);
+ LONG_OPTION_B(1);
+ LONG_OPTION_C(2);
+ LONG_OPTION_D(3);
+ LONG_OPTION_END(4)
+
+ LONG_RESULT_A(0);
+ LONG_RESULT_B(1);
+ LONG_RESULT_C(2);
+ LONG_RESULT_D1(3);
+
+ getopt_long_test(4, 6, argv, g_optstring, long_options, NULL,
+ results);
+
printf("getopt_long(): Invalid long option\n");
argv[0] = NULL;