52155565c87a32657497369927c86d4ee2a9163b
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / pipe_test.tpl
1 [+ AutoGen5 template c +]
2 /*
3 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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 2 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, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*==========================================================================
21 ** This is a test program which tests reading from and writing to pipes.
22 */
23
24 #include "sfconfig.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #if (OS_IS_WIN32 || HAVE_PIPE == 0 || HAVE_WAITPID == 0)
31
32 int
33 main (void)
34 {
35 puts (" pipe_test : this test doesn't work on this OS.") ;
36 return 0 ;
37 } /* main */
38
39 #else
40
41 #if HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include <errno.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/wait.h>
49
50 #include <sndfile.h>
51
52 #include "utils.h"
53
54 typedef struct
55 { int format ;
56 const char *ext ;
57 } FILETYPE ;
58
59 static int file_exists (const char *filename) ;
60 static void useek_pipe_rw_test (int filetype, const char *ext) ;
61 static void pipe_read_test (int filetype, const char *ext) ;
62 static void pipe_write_test (const char *ext) ;
63 static void pipe_test_others (FILETYPE*, FILETYPE*) ;
64
65 static FILETYPE read_write_types [] =
66 { { SF_FORMAT_RAW , "raw" },
67 { SF_FORMAT_AU , "au" },
68 /* Lite remove start */
69 { SF_FORMAT_PAF , "paf" },
70 { SF_FORMAT_IRCAM , "ircam" },
71 { SF_FORMAT_PVF , "pvf" },
72 /* Lite remove end */
73 { 0 , NULL }
74 } ;
75
76 static FILETYPE read_only_types [] =
77 { { SF_FORMAT_RAW , "raw" },
78 { SF_FORMAT_AU , "au" },
79 { SF_FORMAT_AIFF , "aiff" },
80 { SF_FORMAT_WAV , "wav" },
81 { SF_FORMAT_W64 , "w64" },
82 /* Lite remove start */
83 { SF_FORMAT_PAF , "paf" },
84 { SF_FORMAT_NIST , "nist" },
85 { SF_FORMAT_IRCAM , "ircam" },
86 { SF_FORMAT_MAT4 , "mat4" },
87 { SF_FORMAT_MAT5 , "mat5" },
88 { SF_FORMAT_SVX , "svx" },
89 { SF_FORMAT_PVF , "pvf" },
90 /* Lite remove end */
91 { 0 , NULL }
92 } ;
93
94 int
95 main (void)
96 { int k ;
97
98 if (file_exists ("libsndfile.spec.in"))
99 exit_if_true (chdir ("tests") != 0, "\n Error : chdir ('tests') failed.\n") ;
100
101 for (k = 0 ; read_only_types [k].format ; k++)
102 pipe_read_test (read_only_types [k].format, read_only_types [k].ext) ;
103
104 for (k = 0 ; read_write_types [k].format ; k++)
105 pipe_write_test (read_write_types [k].ext) ;
106
107 for (k = 0 ; read_write_types [k].format ; k++)
108 useek_pipe_rw_test (read_write_types [k].format, read_write_types [k].ext) ;
109
110 if (0)
111 pipe_test_others (read_write_types, read_only_types) ;
112
113 return 0 ;
114 } /* main */
115
116 /*==============================================================================
117 */
118
119 static void
120 pipe_read_test (int filetype, const char *ext)
121 { static short data [PIPE_TEST_LEN] ;
122 static char buffer [256] ;
123 static char filename [256] ;
124
125 SNDFILE *outfile ;
126 SF_INFO sfinfo ;
127 int k, retval ;
128
129 snprintf (filename, sizeof (filename), "pipe_in.%s", ext) ;
130 print_test_name ("pipe_read_test", filename) ;
131
132 sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
133 sfinfo.channels = 1 ;
134 sfinfo.samplerate = 44100 ;
135
136 for (k = 0 ; k < PIPE_TEST_LEN ; k++)
137 data [k] = PIPE_INDEX (k) ;
138
139 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
140 test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
141 sf_close (outfile) ;
142
143 snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s ", filename, ext) ;
144 if ((retval = system (buffer)) != 0)
145 { retval = WEXITSTATUS (retval) ;
146 printf ("\n\n Line %d : pipe test returned error for file type \"%s\".\n\n", __LINE__, ext) ;
147 exit (retval) ;
148 } ;
149
150 unlink (filename) ;
151 puts ("ok") ;
152
153 return ;
154 } /* pipe_read_test */
155
156 static void
157 pipe_write_test (const char *ext)
158 { static char buffer [256] ;
159
160 int retval ;
161
162 print_test_name ("pipe_write_test", ext) ;
163
164 snprintf (buffer, sizeof (buffer), "./stdout_test %s | ./stdin_test %s ", ext, ext) ;
165 if ((retval = system (buffer)))
166 { retval = WEXITSTATUS (retval) ;
167 printf ("\n\n Line %d : pipe test returned error file type \"%s\".\n\n", __LINE__, ext) ;
168 exit (retval) ;
169 } ;
170
171 puts ("ok") ;
172
173 return ;
174 } /* pipe_write_test */
175
176 /*==============================================================================
177 */
178
179 [+ FOR data_type +]
180 static void
181 useek_pipe_rw_[+ (get "type_name") +] (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read)
182 { static [+ (get "type_name") +] buffer [PIPE_TEST_LEN] ;
183 static [+ (get "type_name") +] data [PIPE_TEST_LEN] ;
184 SNDFILE *outfile ;
185 SNDFILE *infile_piped ;
186
187 int k, status = 0 ;
188 int pipefd [2] ;
189 pid_t pida ;
190
191 for (k = 0 ; k < PIPE_TEST_LEN ; k++)
192 data [k] = PIPE_INDEX (k) ;
193
194 /*
195 ** Create the pipe.
196 */
197 exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ;
198
199 /*
200 ** Attach the write end of the pipe to be written to.
201 */
202 if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL)
203 { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ;
204 printf ("\t%s\n\n", sf_strerror (outfile)) ;
205 exit (1) ;
206 } ;
207
208 if (sf_error (outfile) != SF_ERR_NO_ERROR)
209 { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ;
210 exit (1) ;
211 } ;
212
213 /*
214 ** Attach the read end of the pipe to be read from.
215 */
216 if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL)
217 { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ;
218 exit (1) ;
219 } ;
220
221 if (sf_error (infile_piped) != SF_ERR_NO_ERROR)
222 { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ;
223 exit (1) ;
224 } ;
225
226 /* Fork a child process that will write directly into the pipe. */
227 if ((pida = fork ()) == 0) /* child process */
228 { test_writef_[+ (get "type_name") +]_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
229 exit (0) ;
230 } ;
231
232 /* In the parent process, read from the pipe and compare what is read
233 ** to what is written, if they match everything went as planned.
234 */
235 test_readf_[+ (get "type_name") +]_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ;
236 if (memcmp (buffer, data, sizeof (buffer)) != 0)
237 { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ;
238 exit (1) ;
239 } ;
240
241 /* Wait for the child process to return. */
242 waitpid (pida, &status, 0) ;
243 status = WEXITSTATUS (status) ;
244 sf_close (outfile) ;
245 sf_close (infile_piped) ;
246
247 if (status != 0)
248 { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
249 exit (1) ;
250 } ;
251
252 return ;
253 } /* useek_pipe_rw_[+ (get "type_name") +] */
254
255 [+ ENDFOR data_type +]
256
257
258 static void
259 useek_pipe_rw_test (int filetype, const char *ext)
260 { SF_INFO sfinfo_write ;
261 SF_INFO sfinfo_read ;
262
263 print_test_name ("useek_pipe_rw_test", ext) ;
264
265 /*
266 ** Setup the INFO structures for the filetype we will be
267 ** working with.
268 */
269 sfinfo_write.format = filetype | SF_FORMAT_PCM_16 ;
270 sfinfo_write.channels = 1 ;
271 sfinfo_write.samplerate = 44100 ;
272
273
274 sfinfo_read.format = 0 ;
275 if (filetype == SF_FORMAT_RAW)
276 { sfinfo_read.format = filetype | SF_FORMAT_PCM_16 ;
277 sfinfo_read.channels = 1 ;
278 sfinfo_read.samplerate = 44100 ;
279 } ;
280
281 useek_pipe_rw_short (ext, &sfinfo_write, &sfinfo_read) ;
282
283 sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_FLOAT ;
284 if (sf_format_check (&sfinfo_read) != 0)
285 useek_pipe_rw_float (ext, &sfinfo_write, &sfinfo_read) ;
286
287 sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_DOUBLE ;
288 if (sf_format_check (&sfinfo_read) != 0)
289 useek_pipe_rw_double (ext, &sfinfo_write, &sfinfo_read) ;
290
291 puts ("ok") ;
292 return ;
293 } /* useek_pipe_rw_test */
294
295
296
297 static void
298 pipe_test_others (FILETYPE* list1, FILETYPE* list2)
299 { SF_FORMAT_INFO info ;
300 int k, m, major_count, in_list ;
301
302 print_test_name ("pipe_test_others", "") ;
303
304 sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ;
305
306 for (k = 0 ; k < major_count ; k++)
307 { info.format = k ;
308
309 sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ;
310
311 in_list = SF_FALSE ;
312 for (m = 0 ; list1 [m].format ; m++)
313 if (info.format == list1 [m].format)
314 in_list = SF_TRUE ;
315
316 for (m = 0 ; list2 [m].format ; m++)
317 if (info.format == list2 [m].format)
318 in_list = SF_TRUE ;
319
320 if (in_list)
321 continue ;
322
323 printf ("%s %x\n", info.name, info.format) ;
324
325 if (1)
326 { static short data [PIPE_TEST_LEN] ;
327 static char buffer [256] ;
328 static const char *filename = "pipe_in.dat" ;
329
330 SNDFILE *outfile ;
331 SF_INFO sfinfo ;
332 int retval ;
333
334 sfinfo.format = info.format | SF_FORMAT_PCM_16 ;
335 sfinfo.channels = 1 ;
336 sfinfo.samplerate = 44100 ;
337
338 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
339 test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
340 sf_close (outfile) ;
341
342 snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s %d ", filename, info.extension, PIPE_TEST_LEN) ;
343 if ((retval = system (buffer)) == 0)
344 { retval = WEXITSTATUS (retval) ;
345 printf ("\n\n Line %d : pipe test should have returned error file type \"%s\" but didn't.\n\n", __LINE__, info.name) ;
346 exit (1) ;
347 } ;
348
349 unlink (filename) ;
350 } ;
351 } ;
352
353
354 puts ("ok") ;
355
356 return ;
357 } /* pipe_test_others */
358
359
360 /*==============================================================================
361 */
362
363 static int
364 file_exists (const char *filename)
365 { struct stat buf ;
366
367 if (stat (filename, &buf))
368 return 0 ;
369
370 return 1 ;
371 } /* file_exists */
372
373 #endif
374