1 [+ AutoGen5 template c +]
3 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
20 /*==========================================================================
21 ** This is a test program which tests reading from and writing to pipes.
30 #if (OS_IS_WIN32 || HAVE_PIPE == 0 || HAVE_WAITPID == 0)
35 puts (" pipe_test : this test doesn't work on this OS.") ;
46 #include <sys/types.h>
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*) ;
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" },
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" },
98 if (file_exists ("libsndfile.spec.in"))
99 exit_if_true (chdir ("tests") != 0, "\n Error : chdir ('tests') failed.\n") ;
101 for (k = 0 ; read_only_types [k].format ; k++)
102 pipe_read_test (read_only_types [k].format, read_only_types [k].ext) ;
104 for (k = 0 ; read_write_types [k].format ; k++)
105 pipe_write_test (read_write_types [k].ext) ;
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) ;
111 pipe_test_others (read_write_types, read_only_types) ;
116 /*==============================================================================
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] ;
129 snprintf (filename, sizeof (filename), "pipe_in.%s", ext) ;
130 print_test_name ("pipe_read_test", filename) ;
132 sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
133 sfinfo.channels = 1 ;
134 sfinfo.samplerate = 44100 ;
136 for (k = 0 ; k < PIPE_TEST_LEN ; k++)
137 data [k] = PIPE_INDEX (k) ;
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__) ;
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) ;
154 } /* pipe_read_test */
157 pipe_write_test (const char *ext)
158 { static char buffer [256] ;
162 print_test_name ("pipe_write_test", ext) ;
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) ;
174 } /* pipe_write_test */
176 /*==============================================================================
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] ;
185 SNDFILE *infile_piped ;
191 for (k = 0 ; k < PIPE_TEST_LEN ; k++)
192 data [k] = PIPE_INDEX (k) ;
197 exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ;
200 ** Attach the write end of the pipe to be written to.
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)) ;
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) ;
214 ** Attach the read end of the pipe to be read from.
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) ;
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) ;
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__) ;
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.
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) ;
241 /* Wait for the child process to return. */
242 waitpid (pida, &status, 0) ;
243 status = WEXITSTATUS (status) ;
245 sf_close (infile_piped) ;
248 { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
253 } /* useek_pipe_rw_[+ (get "type_name") +] */
255 [+ ENDFOR data_type +]
259 useek_pipe_rw_test (int filetype, const char *ext)
260 { SF_INFO sfinfo_write ;
261 SF_INFO sfinfo_read ;
263 print_test_name ("useek_pipe_rw_test", ext) ;
266 ** Setup the INFO structures for the filetype we will be
269 sfinfo_write.format = filetype | SF_FORMAT_PCM_16 ;
270 sfinfo_write.channels = 1 ;
271 sfinfo_write.samplerate = 44100 ;
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 ;
281 useek_pipe_rw_short (ext, &sfinfo_write, &sfinfo_read) ;
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) ;
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) ;
293 } /* useek_pipe_rw_test */
298 pipe_test_others (FILETYPE* list1, FILETYPE* list2)
299 { SF_FORMAT_INFO info ;
300 int k, m, major_count, in_list ;
302 print_test_name ("pipe_test_others", "") ;
304 sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ;
306 for (k = 0 ; k < major_count ; k++)
309 sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ;
312 for (m = 0 ; list1 [m].format ; m++)
313 if (info.format == list1 [m].format)
316 for (m = 0 ; list2 [m].format ; m++)
317 if (info.format == list2 [m].format)
323 printf ("%s %x\n", info.name, info.format) ;
326 { static short data [PIPE_TEST_LEN] ;
327 static char buffer [256] ;
328 static const char *filename = "pipe_in.dat" ;
334 sfinfo.format = info.format | SF_FORMAT_PCM_16 ;
335 sfinfo.channels = 1 ;
336 sfinfo.samplerate = 44100 ;
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__) ;
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) ;
357 } /* pipe_test_others */
360 /*==============================================================================
364 file_exists (const char *filename)
367 if (stat (filename, &buf))