OSDN Git Service

import test from glibc
authorMike Frysinger <vapier@gentoo.org>
Tue, 15 Nov 2005 00:16:27 +0000 (00:16 -0000)
committerMike Frysinger <vapier@gentoo.org>
Tue, 15 Nov 2005 00:16:27 +0000 (00:16 -0000)
test/unistd/Makefile
test/unistd/tstgetopt.c [new file with mode: 0644]

index 4db5017..3640d7c 100644 (file)
@@ -1,9 +1,10 @@
 # uClibc unistd tests
 # Licensed under the GNU Library General Public License, see COPYING.LIB
 
-TESTS = clone errno fork getcwd getopt getopt_long preadwrite vfork
+TESTS = clone errno fork getcwd getopt getopt_long preadwrite tstgetopt vfork
 
 include ../Test.mak
 
 export OPTS_getopt = -abcXXX -9
 export OPTS_getopt_long = --add XXX --delete YYY --verbose
+export OPTS_tstgetopt = -a -b -cfoobar --required foobar --optional=bazbug --none random --col --color --colour
diff --git a/test/unistd/tstgetopt.c b/test/unistd/tstgetopt.c
new file mode 100644 (file)
index 0000000..97ae2ef
--- /dev/null
@@ -0,0 +1,76 @@
+#include <getopt.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main (int argc, char **argv)
+{
+  static const struct option options[] =
+    {
+      {"required", required_argument, NULL, 'r'},
+      {"optional", optional_argument, NULL, 'o'},
+      {"none",     no_argument,       NULL, 'n'},
+      {"color",    no_argument,       NULL, 'C'},
+      {"colour",   no_argument,       NULL, 'C'},
+      {NULL,       0,                 NULL, 0 }
+    };
+
+  int aflag = 0;
+  int bflag = 0;
+  char *cvalue = NULL;
+  int Cflag = 0;
+  int nflag = 0;
+  int index;
+  int c;
+  int result = 0;
+
+  while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
+    switch (c)
+      {
+      case 'a':
+       aflag = 1;
+       break;
+      case 'b':
+       bflag = 1;
+       break;
+      case 'c':
+       cvalue = optarg;
+       break;
+      case 'C':
+       ++Cflag;
+       break;
+      case '?':
+       fputs ("Unknown option.\n", stderr);
+       return 1;
+      default:
+       fprintf (stderr, "This should never happen!\n");
+       return 1;
+
+      case 'r':
+       printf ("--required %s\n", optarg);
+       result |= strcmp (optarg, "foobar") != 0;
+       break;
+      case 'o':
+       printf ("--optional %s\n", optarg);
+       result |= optarg == NULL || strcmp (optarg, "bazbug") != 0;
+       break;
+      case 'n':
+       puts ("--none");
+       nflag = 1;
+       break;
+      }
+
+  printf ("aflag = %d, bflag = %d, cvalue = %s, Cflags = %d, nflag = %d\n",
+         aflag, bflag, cvalue, Cflag, nflag);
+
+  result |= (aflag != 1 || bflag != 1 || cvalue == NULL
+            || strcmp (cvalue, "foobar") != 0 || Cflag != 3 || nflag != 1);
+
+  for (index = optind; index < argc; index++)
+    printf ("Non-option argument %s\n", argv[index]);
+
+  result |= optind + 1 != argc || strcmp (argv[optind], "random") != 0;
+
+  return result;
+}