Mercurial > hg > octave-shane > gnulib-hg
annotate tests/test-argv-iter.c @ 17632:86af85d364e1 default tip
unistd: port readlink to Mac OS X 10.3.9
* lib/unistd.in.h (_GL_INCLUDING_UNISTD_H): New macro, to work
around self-include problem in Mac OS X 10.3.9 when combined with
readlink module. Problem reported by Klaus Zietler in
<http://bugs.gnu.org/16825>.
author | Paul Eggert <eggert@penguin.cs.ucla.edu> |
---|---|
date | Tue, 25 Feb 2014 11:16:27 -0800 |
parents | 344018b6e5d7 |
children |
rev | line source |
---|---|
11708 | 1 /* Test argv iterator |
17587 | 2 Copyright (C) 2008-2014 Free Software Foundation, Inc. |
11708 | 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 Jim Meyering. */ | |
18 | |
19 #include <config.h> | |
12487
8ba9831d54bf
Include header file being tested immediately after config.h.
Bruno Haible <bruno@clisp.org>
parents:
12197
diff
changeset
|
20 |
8ba9831d54bf
Include header file being tested immediately after config.h.
Bruno Haible <bruno@clisp.org>
parents:
12197
diff
changeset
|
21 #include "argv-iter.h" |
8ba9831d54bf
Include header file being tested immediately after config.h.
Bruno Haible <bruno@clisp.org>
parents:
12197
diff
changeset
|
22 |
11708 | 23 #include <stdio.h> |
24 #include <string.h> | |
25 | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12487
diff
changeset
|
26 #include "macros.h" |
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12487
diff
changeset
|
27 |
11708 | 28 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array)) |
12772
2ea3e3fab7ee
define STREQ(a,b) consistently, removing useless parentheses
Jim Meyering <meyering@redhat.com>
parents:
12559
diff
changeset
|
29 #define STREQ(a, b) (strcmp (a, b) == 0) |
11708 | 30 |
31 static FILE * | |
32 write_nul_delimited_argv (char **argv) | |
33 { | |
34 FILE *fp = tmpfile (); | |
35 ASSERT (fp); | |
36 while (*argv) | |
37 { | |
38 size_t len = strlen (*argv) + 1; | |
39 ASSERT (fwrite (*argv, len, 1, fp) == 1); | |
40 argv++; | |
41 } | |
42 ASSERT (fflush (fp) == 0); | |
43 rewind (fp); | |
44 return fp; | |
45 } | |
46 | |
47 int | |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
48 main (void) |
11708 | 49 { |
50 /* set_program_name (argv[0]); placate overzealous "syntax-check" test. */ | |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
51 static char one[] = "1"; |
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
52 static char two[] = "2"; |
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
53 static char three[] = "3"; |
11708 | 54 static char *av[][4] = { |
55 {NULL}, | |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
56 {one, NULL}, |
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
57 {one, two, NULL}, |
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11709
diff
changeset
|
58 {one, two, three, NULL} |
11708 | 59 }; |
60 | |
61 int use_stream; | |
62 for (use_stream = 0; use_stream < 2; use_stream++) | |
63 { | |
64 size_t i; | |
65 for (i = 0; i < ARRAY_CARDINALITY (av); i++) | |
66 { | |
67 FILE *fp; | |
68 struct argv_iterator *ai; | |
69 size_t n_found = 0; | |
70 if (use_stream) | |
71 { | |
72 /* Generate an identical list to be read via FP. */ | |
73 ASSERT ((fp = write_nul_delimited_argv (av[i])) != NULL); | |
74 ai = argv_iter_init_stream (fp); | |
75 } | |
76 else | |
77 { | |
78 fp = NULL; | |
79 ai = argv_iter_init_argv (av[i]); | |
80 } | |
81 ASSERT (ai); | |
82 | |
83 while (1) | |
84 { | |
85 enum argv_iter_err ai_err; | |
86 char *s = argv_iter (ai, &ai_err); | |
87 ASSERT ((i == n_found) == (ai_err == AI_ERR_EOF)); | |
88 ASSERT ((s == NULL) ^ (ai_err == AI_ERR_OK)); | |
89 ASSERT (ai_err == AI_ERR_OK || ai_err == AI_ERR_EOF); | |
90 if (ai_err == AI_ERR_OK) | |
91 ++n_found; | |
92 if (ai_err == AI_ERR_EOF) | |
93 break; | |
94 /* In stream mode, the strings are equal, but | |
95 in argv mode the actual pointers are equal. */ | |
96 ASSERT (use_stream | |
97 ? STREQ (s, av[i][n_found - 1]) | |
98 : s == av[i][n_found - 1]); | |
99 } | |
100 ASSERT (argv_iter_n_args (ai) == i); | |
101 argv_iter_free (ai); | |
102 if (fp) | |
103 ASSERT (fclose (fp) == 0); | |
104 } | |
105 } | |
106 | |
107 return 0; | |
108 } |