usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-getopt-main.h
blobc1a215464bd2dd075a39e382d0ba279bbc9085c0
1 /* Test of command line argument processing.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
19 #include "signature.h"
20 SIGNATURE_CHECK (getopt, int, (int, char * const[], char const *));
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 /* This test intentionally remaps stderr. So, we arrange to have fd 10
28 (outside the range of interesting fd's during the test) set up to
29 duplicate the original stderr. */
31 #define BACKUP_STDERR_FILENO 10
32 #define ASSERT_STREAM myerr
33 #include "macros.h"
35 static FILE *myerr;
37 #include "test-getopt.h"
38 #if TEST_GETOPT_GNU
39 # include "test-getopt_long.h"
40 #endif
42 int
43 main (void)
45 /* This test validates that stderr is used correctly, so move the
46 original into fd 10. */
47 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
48 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
49 return 2;
51 ASSERT (freopen (TEST_GETOPT_TMP_NAME, "w", stderr) == stderr);
53 /* These default values are required by POSIX. */
54 ASSERT (optind == 1);
55 ASSERT (opterr != 0);
57 setenv ("POSIXLY_CORRECT", "1", 1);
58 test_getopt ();
60 #if TEST_GETOPT_GNU
61 test_getopt_long_posix ();
62 #endif
64 unsetenv ("POSIXLY_CORRECT");
65 test_getopt ();
67 #if TEST_GETOPT_GNU
68 test_getopt_long ();
69 test_getopt_long_only ();
70 #endif
72 ASSERT (fclose (stderr) == 0);
73 ASSERT (remove (TEST_GETOPT_TMP_NAME) == 0);
75 return test_exit_status;