digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
[gnulib.git] / tests / test-argv-iter.c
1 /* Test argv iterator
2    Copyright (C) 2008-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 Jim Meyering.  */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
25 #define STREQ(s1, s2) (strcmp (s1, s2) == 0)
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           fflush (stderr);                                                   \
33           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
37
38 #include "argv-iter.h"
39
40 static FILE *
41 write_nul_delimited_argv (char **argv)
42 {
43   FILE *fp = tmpfile ();
44   ASSERT (fp);
45   while (*argv)
46     {
47       size_t len = strlen (*argv) + 1;
48       ASSERT (fwrite (*argv, len, 1, fp) == 1);
49       argv++;
50     }
51   ASSERT (fflush (fp) == 0);
52   rewind (fp);
53   return fp;
54 }
55
56 int
57 main (void)
58 {
59   /* set_program_name (argv[0]); placate overzealous "syntax-check" test.  */
60   static char one[] = "1";
61   static char two[] = "2";
62   static char three[] = "3";
63   static char *av[][4] = {
64     {NULL},
65     {one, NULL},
66     {one, two, NULL},
67     {one, two, three, NULL}
68   };
69
70   int use_stream;
71   for (use_stream = 0; use_stream < 2; use_stream++)
72     {
73       size_t i;
74       for (i = 0; i < ARRAY_CARDINALITY (av); i++)
75         {
76           FILE *fp;
77           struct argv_iterator *ai;
78           size_t n_found = 0;
79           if (use_stream)
80             {
81               /* Generate an identical list to be read via FP.  */
82               ASSERT ((fp = write_nul_delimited_argv (av[i])) != NULL);
83               ai = argv_iter_init_stream (fp);
84             }
85           else
86             {
87               fp = NULL;
88               ai = argv_iter_init_argv (av[i]);
89             }
90           ASSERT (ai);
91
92           while (1)
93             {
94               enum argv_iter_err ai_err;
95               char *s = argv_iter (ai, &ai_err);
96               ASSERT ((i == n_found) == (ai_err == AI_ERR_EOF));
97               ASSERT ((s == NULL) ^ (ai_err == AI_ERR_OK));
98               ASSERT (ai_err == AI_ERR_OK || ai_err == AI_ERR_EOF);
99               if (ai_err == AI_ERR_OK)
100                 ++n_found;
101               if (ai_err == AI_ERR_EOF)
102                 break;
103               /* In stream mode, the strings are equal, but
104                  in argv mode the actual pointers are equal.  */
105               ASSERT (use_stream
106                       ? STREQ (s, av[i][n_found - 1])
107                       : s == av[i][n_found - 1]);
108             }
109           ASSERT (argv_iter_n_args (ai) == i);
110           argv_iter_free (ai);
111           if (fp)
112             ASSERT (fclose (fp) == 0);
113         }
114     }
115
116   return 0;
117 }