pipe_test.tpl 9.7 KB

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