annotate src/libsndfile-1.0.27/tests/pipe_test.tpl @ 84:08ae793730bd

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