annotate src/libsndfile-1.0.27/tests/pipe_test.tpl @ 127:7867fa7e1b6b

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