added missing dependencies to fix failing unistr/ tests
[gnulib.git] / tests / test-pipe-filter-ii1.c
1 /* Test of filtering of data through a subprocess.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 #include "pipe-filter.h"
21
22 #include "binary-io.h"
23 #include "c-ctype.h"
24 #include "read-file.h"
25 #include "progname.h"
26 #include "macros.h"
27
28
29 /* Pipe a text file through 'tr a-z A-Z', which converts ASCII characters from
30    lower case to upper case.  */
31
32 struct locals
33 {
34   const char *input;
35   size_t size;
36   size_t nwritten;
37   size_t nread;
38   char buf[19];
39 };
40
41 static const void *
42 prepare_write (size_t *num_bytes_p, void *private_data)
43 {
44   struct locals *l = (struct locals *) private_data;
45   if (l->nwritten < l->size)
46     {
47       *num_bytes_p = l->size - l->nwritten;
48       return l->input + l->nwritten;
49     }
50   else
51     return NULL;
52 }
53
54 static void
55 done_write (void *data_written, size_t num_bytes_written, void *private_data)
56 {
57   struct locals *l = (struct locals *) private_data;
58   l->nwritten += num_bytes_written;
59 }
60
61 static void *
62 prepare_read (size_t *num_bytes_p, void *private_data)
63 {
64   struct locals *l = (struct locals *) private_data;
65   *num_bytes_p = sizeof (l->buf);
66   return l->buf;
67 }
68
69 static void
70 done_read (void *data_read, size_t num_bytes_read, void *private_data)
71 {
72   struct locals *l = (struct locals *) private_data;
73   const char *p = l->input + l->nread;
74   const char *q = (const char *) data_read;
75   size_t i;
76
77   for (i = 0; i < num_bytes_read; i++, q++)
78     {
79       /* Handle conversion NL -> CRLF possibly done by the child process.  */
80       if (!(O_BINARY && *q == '\r'))
81         {
82           char orig = *p;
83           char expected = c_toupper (orig);
84           ASSERT (*q == expected);
85           p++;
86         }
87     }
88   l->nread = p - l->input;
89 }
90
91 int
92 main (int argc, char *argv[])
93 {
94   const char *tr_program;
95   const char *input_filename;
96   size_t input_size;
97   char *input;
98
99   set_program_name (argv[0]);
100
101   ASSERT (argc == 3);
102
103   tr_program = argv[1];
104
105   /* Read some text from a file.  */
106   input_filename = argv[2];
107   input = read_binary_file (input_filename, &input_size);
108   ASSERT (input != NULL);
109
110   /* Convert it to uppercase, line by line.  */
111   {
112     const char *argv[4];
113     struct locals l;
114     int result;
115
116     l.input = input;
117     l.size = input_size;
118     l.nwritten = 0;
119     l.nread = 0;
120
121     argv[0] = tr_program;
122     argv[1] = "a-z";
123     argv[2] = "A-Z";
124     argv[3] = NULL;
125
126     result = pipe_filter_ii_execute ("tr", tr_program, argv, false, true,
127                                      prepare_write, done_write,
128                                      prepare_read, done_read,
129                                      &l);
130     ASSERT (result == 0);
131     ASSERT (l.nwritten == input_size);
132     ASSERT (l.nread == input_size);
133   }
134
135   return 0;
136 }