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