annotate src/libsndfile-1.0.25/tests/pipe_test.tpl @ 94:d278df1123f9

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