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