annotate src/libsndfile-1.0.25/tests/pipe_test.tpl @ 0:c7265573341e

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