cannam@125: [+ AutoGen5 template c +] cannam@125: /* cannam@125: ** Copyright (C) 2001-2012 Erik de Castro Lopo cannam@125: ** cannam@125: ** This program is free software; you can redistribute it and/or modify cannam@125: ** it under the terms of the GNU General Public License as published by cannam@125: ** the Free Software Foundation; either version 2 of the License, or cannam@125: ** (at your option) any later version. cannam@125: ** cannam@125: ** This program is distributed in the hope that it will be useful, cannam@125: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@125: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@125: ** GNU General Public License for more details. cannam@125: ** cannam@125: ** You should have received a copy of the GNU General Public License cannam@125: ** along with this program; if not, write to the Free Software cannam@125: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@125: */ cannam@125: cannam@125: /*========================================================================== cannam@125: ** This is a test program which tests reading from and writing to pipes. cannam@125: */ cannam@125: cannam@125: #include "sfconfig.h" cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #if (OS_IS_WIN32 || defined __OS2__ || HAVE_PIPE == 0 || HAVE_WAITPID == 0) cannam@125: cannam@125: int cannam@125: main (void) cannam@125: { cannam@125: puts (" pipe_test : this test doesn't work on this OS.") ; cannam@125: return 0 ; cannam@125: } /* main */ cannam@125: cannam@125: #else cannam@125: cannam@125: #if HAVE_UNISTD_H cannam@125: #include cannam@125: #endif cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #include cannam@125: cannam@125: #include "utils.h" cannam@125: cannam@125: typedef struct cannam@125: { int format ; cannam@125: const char *ext ; cannam@125: } FILETYPE ; cannam@125: cannam@125: static int file_exists (const char *filename) ; cannam@125: static void useek_pipe_rw_test (int filetype, const char *ext) ; cannam@125: static void pipe_read_test (int filetype, const char *ext) ; cannam@125: static void pipe_write_test (const char *ext) ; cannam@125: static void pipe_test_others (FILETYPE*, FILETYPE*) ; cannam@125: cannam@125: static FILETYPE read_write_types [] = cannam@125: { { SF_FORMAT_RAW , "raw" }, cannam@125: { SF_FORMAT_AU , "au" }, cannam@125: /* Lite remove start */ cannam@125: { SF_FORMAT_PAF , "paf" }, cannam@125: { SF_FORMAT_IRCAM , "ircam" }, cannam@125: { SF_FORMAT_PVF , "pvf" }, cannam@125: /* Lite remove end */ cannam@125: { 0 , NULL } cannam@125: } ; cannam@125: cannam@125: static FILETYPE read_only_types [] = cannam@125: { { SF_FORMAT_RAW , "raw" }, cannam@125: { SF_FORMAT_AU , "au" }, cannam@125: { SF_FORMAT_AIFF , "aiff" }, cannam@125: { SF_FORMAT_WAV , "wav" }, cannam@125: { SF_FORMAT_W64 , "w64" }, cannam@125: /* Lite remove start */ cannam@125: { SF_FORMAT_PAF , "paf" }, cannam@125: { SF_FORMAT_NIST , "nist" }, cannam@125: { SF_FORMAT_IRCAM , "ircam" }, cannam@125: { SF_FORMAT_MAT4 , "mat4" }, cannam@125: { SF_FORMAT_MAT5 , "mat5" }, cannam@125: { SF_FORMAT_SVX , "svx" }, cannam@125: { SF_FORMAT_PVF , "pvf" }, cannam@125: /* Lite remove end */ cannam@125: { 0 , NULL } cannam@125: } ; cannam@125: cannam@125: int cannam@125: main (void) cannam@125: { int k ; cannam@125: cannam@125: if (file_exists ("libsndfile.spec.in")) cannam@125: exit_if_true (chdir ("tests") != 0, "\n Error : chdir ('tests') failed.\n") ; cannam@125: cannam@125: for (k = 0 ; read_only_types [k].format ; k++) cannam@125: pipe_read_test (read_only_types [k].format, read_only_types [k].ext) ; cannam@125: cannam@125: for (k = 0 ; read_write_types [k].format ; k++) cannam@125: pipe_write_test (read_write_types [k].ext) ; cannam@125: cannam@125: for (k = 0 ; read_write_types [k].format ; k++) cannam@125: useek_pipe_rw_test (read_write_types [k].format, read_write_types [k].ext) ; cannam@125: cannam@125: if (0) cannam@125: pipe_test_others (read_write_types, read_only_types) ; cannam@125: cannam@125: return 0 ; cannam@125: } /* main */ cannam@125: cannam@125: /*============================================================================== cannam@125: */ cannam@125: cannam@125: static void cannam@125: pipe_read_test (int filetype, const char *ext) cannam@125: { static short data [PIPE_TEST_LEN] ; cannam@125: static char buffer [256] ; cannam@125: static char filename [256] ; cannam@125: cannam@125: SNDFILE *outfile ; cannam@125: SF_INFO sfinfo ; cannam@125: int k, retval ; cannam@125: cannam@125: snprintf (filename, sizeof (filename), "pipe_in.%s", ext) ; cannam@125: print_test_name ("pipe_read_test", filename) ; cannam@125: cannam@125: sfinfo.format = filetype | SF_FORMAT_PCM_16 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: cannam@125: for (k = 0 ; k < PIPE_TEST_LEN ; k++) cannam@125: data [k] = PIPE_INDEX (k) ; cannam@125: cannam@125: outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; cannam@125: sf_close (outfile) ; cannam@125: cannam@125: snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s ", filename, ext) ; cannam@125: if ((retval = system (buffer)) != 0) cannam@125: { retval = WEXITSTATUS (retval) ; cannam@125: printf ("\n\n Line %d : pipe test returned error for file type \"%s\".\n\n", __LINE__, ext) ; cannam@125: exit (retval) ; cannam@125: } ; cannam@125: cannam@125: unlink (filename) ; cannam@125: puts ("ok") ; cannam@125: cannam@125: return ; cannam@125: } /* pipe_read_test */ cannam@125: cannam@125: static void cannam@125: pipe_write_test (const char *ext) cannam@125: { static char buffer [256] ; cannam@125: cannam@125: int retval ; cannam@125: cannam@125: print_test_name ("pipe_write_test", ext) ; cannam@125: cannam@125: snprintf (buffer, sizeof (buffer), "./stdout_test %s | ./stdin_test %s ", ext, ext) ; cannam@125: if ((retval = system (buffer))) cannam@125: { retval = WEXITSTATUS (retval) ; cannam@125: printf ("\n\n Line %d : pipe test returned error file type \"%s\".\n\n", __LINE__, ext) ; cannam@125: exit (retval) ; cannam@125: } ; cannam@125: cannam@125: puts ("ok") ; cannam@125: cannam@125: return ; cannam@125: } /* pipe_write_test */ cannam@125: cannam@125: /*============================================================================== cannam@125: */ cannam@125: cannam@125: [+ FOR data_type +] cannam@125: static void cannam@125: useek_pipe_rw_[+ (get "type_name") +] (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read) cannam@125: { static [+ (get "type_name") +] buffer [PIPE_TEST_LEN] ; cannam@125: static [+ (get "type_name") +] data [PIPE_TEST_LEN] ; cannam@125: SNDFILE *outfile ; cannam@125: SNDFILE *infile_piped ; cannam@125: cannam@125: int k, status = 0 ; cannam@125: int pipefd [2] ; cannam@125: pid_t pida ; cannam@125: cannam@125: for (k = 0 ; k < PIPE_TEST_LEN ; k++) cannam@125: data [k] = PIPE_INDEX (k) ; cannam@125: cannam@125: /* cannam@125: ** Create the pipe. cannam@125: */ cannam@125: exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ; cannam@125: cannam@125: /* cannam@125: ** Attach the write end of the pipe to be written to. cannam@125: */ cannam@125: if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL) cannam@125: { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ; cannam@125: printf ("\t%s\n\n", sf_strerror (outfile)) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sf_error (outfile) != SF_ERR_NO_ERROR) cannam@125: { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* cannam@125: ** Attach the read end of the pipe to be read from. cannam@125: */ cannam@125: if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL) cannam@125: { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: if (sf_error (infile_piped) != SF_ERR_NO_ERROR) cannam@125: { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* Fork a child process that will write directly into the pipe. */ cannam@125: if ((pida = fork ()) == 0) /* child process */ cannam@125: { test_writef_[+ (get "type_name") +]_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; cannam@125: exit (0) ; cannam@125: } ; cannam@125: cannam@125: /* In the parent process, read from the pipe and compare what is read cannam@125: ** to what is written, if they match everything went as planned. cannam@125: */ cannam@125: test_readf_[+ (get "type_name") +]_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ; cannam@125: if (memcmp (buffer, data, sizeof (buffer)) != 0) cannam@125: { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: /* Wait for the child process to return. */ cannam@125: waitpid (pida, &status, 0) ; cannam@125: status = WEXITSTATUS (status) ; cannam@125: sf_close (outfile) ; cannam@125: sf_close (infile_piped) ; cannam@125: cannam@125: if (status != 0) cannam@125: { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: return ; cannam@125: } /* useek_pipe_rw_[+ (get "type_name") +] */ cannam@125: cannam@125: [+ ENDFOR data_type +] cannam@125: cannam@125: cannam@125: static void cannam@125: useek_pipe_rw_test (int filetype, const char *ext) cannam@125: { SF_INFO sfinfo_write ; cannam@125: SF_INFO sfinfo_read ; cannam@125: cannam@125: print_test_name ("useek_pipe_rw_test", ext) ; cannam@125: cannam@125: /* cannam@125: ** Setup the INFO structures for the filetype we will be cannam@125: ** working with. cannam@125: */ cannam@125: sfinfo_write.format = filetype | SF_FORMAT_PCM_16 ; cannam@125: sfinfo_write.channels = 1 ; cannam@125: sfinfo_write.samplerate = 44100 ; cannam@125: cannam@125: cannam@125: sfinfo_read.format = 0 ; cannam@125: if (filetype == SF_FORMAT_RAW) cannam@125: { sfinfo_read.format = filetype | SF_FORMAT_PCM_16 ; cannam@125: sfinfo_read.channels = 1 ; cannam@125: sfinfo_read.samplerate = 44100 ; cannam@125: } ; cannam@125: cannam@125: useek_pipe_rw_short (ext, &sfinfo_write, &sfinfo_read) ; cannam@125: cannam@125: sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_FLOAT ; cannam@125: if (sf_format_check (&sfinfo_read) != 0) cannam@125: useek_pipe_rw_float (ext, &sfinfo_write, &sfinfo_read) ; cannam@125: cannam@125: sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_DOUBLE ; cannam@125: if (sf_format_check (&sfinfo_read) != 0) cannam@125: useek_pipe_rw_double (ext, &sfinfo_write, &sfinfo_read) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: return ; cannam@125: } /* useek_pipe_rw_test */ cannam@125: cannam@125: cannam@125: cannam@125: static void cannam@125: pipe_test_others (FILETYPE* list1, FILETYPE* list2) cannam@125: { SF_FORMAT_INFO info ; cannam@125: int k, m, major_count, in_list ; cannam@125: cannam@125: print_test_name ("pipe_test_others", "") ; cannam@125: cannam@125: sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ; cannam@125: cannam@125: for (k = 0 ; k < major_count ; k++) cannam@125: { info.format = k ; cannam@125: cannam@125: sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ; cannam@125: cannam@125: in_list = SF_FALSE ; cannam@125: for (m = 0 ; list1 [m].format ; m++) cannam@125: if (info.format == list1 [m].format) cannam@125: in_list = SF_TRUE ; cannam@125: cannam@125: for (m = 0 ; list2 [m].format ; m++) cannam@125: if (info.format == list2 [m].format) cannam@125: in_list = SF_TRUE ; cannam@125: cannam@125: if (in_list) cannam@125: continue ; cannam@125: cannam@125: printf ("%s %x\n", info.name, info.format) ; cannam@125: cannam@125: if (1) cannam@125: { static short data [PIPE_TEST_LEN] ; cannam@125: static char buffer [256] ; cannam@125: static const char *filename = "pipe_in.dat" ; cannam@125: cannam@125: SNDFILE *outfile ; cannam@125: SF_INFO sfinfo ; cannam@125: int retval ; cannam@125: cannam@125: sfinfo.format = info.format | SF_FORMAT_PCM_16 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.samplerate = 44100 ; cannam@125: cannam@125: outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ; cannam@125: test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ; cannam@125: sf_close (outfile) ; cannam@125: cannam@125: snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s %d ", filename, info.extension, PIPE_TEST_LEN) ; cannam@125: if ((retval = system (buffer)) == 0) cannam@125: { retval = WEXITSTATUS (retval) ; cannam@125: printf ("\n\n Line %d : pipe test should have returned error file type \"%s\" but didn't.\n\n", __LINE__, info.name) ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: unlink (filename) ; cannam@125: } ; cannam@125: } ; cannam@125: cannam@125: cannam@125: puts ("ok") ; cannam@125: cannam@125: return ; cannam@125: } /* pipe_test_others */ cannam@125: cannam@125: cannam@125: /*============================================================================== cannam@125: */ cannam@125: cannam@125: static int cannam@125: file_exists (const char *filename) cannam@125: { struct stat buf ; cannam@125: cannam@125: if (stat (filename, &buf)) cannam@125: return 0 ; cannam@125: cannam@125: return 1 ; cannam@125: } /* file_exists */ cannam@125: cannam@125: #endif cannam@125: