test-getopt: enhance test
[gnulib.git] / tests / test-getopt.c
1 /* Test of command line argument processing.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009.  */
18
19 #include <config.h>
20
21 #if GNULIB_GETOPT_GNU
22 # include <getopt.h>
23 #endif
24 #include <unistd.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 #include "test-getopt.h"
43 #if GNULIB_GETOPT_GNU
44 # include "test-getopt_long.h"
45 #endif
46
47 int
48 main (void)
49 {
50   /* These default values are required by POSIX.  */
51   ASSERT (optind == 1);
52   ASSERT (opterr != 0);
53
54   setenv ("POSIXLY_CORRECT", "1", 1);
55   test_getopt ();
56
57 #if GNULIB_GETOPT_GNU
58   test_getopt_long_posix ();
59 #endif
60
61   unsetenv ("POSIXLY_CORRECT");
62   test_getopt ();
63
64 #if GNULIB_GETOPT_GNU
65   test_getopt_long ();
66   test_getopt_long_only ();
67 #endif
68
69   return 0;
70 }