usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-argv-iter.c
blob01c25a09713707dd107c79811b48c3c5dc18b7ca
1 /* Test argv iterator
2 Copyright (C) 2008-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 Jim Meyering. */
19 #include <config.h>
21 #include "argv-iter.h"
23 #include <stdio.h>
24 #include <string.h>
26 #include "macros.h"
28 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
29 #define STREQ(a, b) (strcmp (a, b) == 0)
31 static FILE *
32 write_nul_delimited_argv (char **argv)
34 FILE *fp = tmpfile ();
35 ASSERT (fp);
36 while (*argv)
38 size_t len = strlen (*argv) + 1;
39 ASSERT (fwrite (*argv, len, 1, fp) == 1);
40 argv++;
42 ASSERT (fflush (fp) == 0);
43 rewind (fp);
44 return fp;
47 int
48 main (void)
50 static char one[] = "1";
51 static char two[] = "2";
52 static char three[] = "3";
53 static char *av[][4] = {
54 {NULL},
55 {one, NULL},
56 {one, two, NULL},
57 {one, two, three, NULL}
60 int use_stream;
61 for (use_stream = 0; use_stream < 2; use_stream++)
63 size_t i;
64 for (i = 0; i < ARRAY_CARDINALITY (av); i++)
66 FILE *fp;
67 struct argv_iterator *ai;
68 size_t n_found = 0;
69 if (use_stream)
71 /* Generate an identical list to be read via FP. */
72 ASSERT ((fp = write_nul_delimited_argv (av[i])) != NULL);
73 ai = argv_iter_init_stream (fp);
75 else
77 fp = NULL;
78 ai = argv_iter_init_argv (av[i]);
80 ASSERT (ai);
82 while (1)
84 enum argv_iter_err ai_err;
85 char *s = argv_iter (ai, &ai_err);
86 ASSERT ((i == n_found) == (ai_err == AI_ERR_EOF));
87 ASSERT ((s == NULL) ^ (ai_err == AI_ERR_OK));
88 ASSERT (ai_err == AI_ERR_OK || ai_err == AI_ERR_EOF);
89 if (ai_err == AI_ERR_EOF)
90 break;
91 /* In stream mode, the strings are equal, but
92 in argv mode the actual pointers are equal. */
93 ASSERT (use_stream
94 ? STREQ (s, av[i][n_found])
95 : s == av[i][n_found]);
96 ++n_found;
98 ASSERT (argv_iter_n_args (ai) == i);
99 argv_iter_free (ai);
100 if (fp)
101 ASSERT (fclose (fp) == 0);
105 return test_exit_status;