2
0

pipe_test.tpl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. [+ AutoGen5 template c +]
  2. /*
  3. ** Copyright (C) 2001-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  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 int file_exists (const char *filename) ;
  48. static void useek_pipe_rw_test (int filetype, const char *ext) ;
  49. static void pipe_read_test (int filetype, const char *ext) ;
  50. static void pipe_write_test (const char *ext) ;
  51. static void pipe_test_others (FILETYPE*, FILETYPE*) ;
  52. static FILETYPE read_write_types [] =
  53. { { SF_FORMAT_RAW , "raw" },
  54. { SF_FORMAT_AU , "au" },
  55. /* Lite remove start */
  56. { SF_FORMAT_PAF , "paf" },
  57. { SF_FORMAT_IRCAM , "ircam" },
  58. { SF_FORMAT_PVF , "pvf" },
  59. /* Lite remove end */
  60. { 0 , NULL }
  61. } ;
  62. static FILETYPE read_only_types [] =
  63. { { SF_FORMAT_RAW , "raw" },
  64. { SF_FORMAT_AU , "au" },
  65. { SF_FORMAT_AIFF , "aiff" },
  66. { SF_FORMAT_WAV , "wav" },
  67. { SF_FORMAT_W64 , "w64" },
  68. /* Lite remove start */
  69. { SF_FORMAT_PAF , "paf" },
  70. { SF_FORMAT_NIST , "nist" },
  71. { SF_FORMAT_IRCAM , "ircam" },
  72. { SF_FORMAT_MAT4 , "mat4" },
  73. { SF_FORMAT_MAT5 , "mat5" },
  74. { SF_FORMAT_SVX , "svx" },
  75. { SF_FORMAT_PVF , "pvf" },
  76. /* Lite remove end */
  77. { 0 , NULL }
  78. } ;
  79. int
  80. main (void)
  81. { int k ;
  82. if (file_exists ("libsndfile.spec.in"))
  83. exit_if_true (chdir ("tests") != 0, "\n Error : chdir ('tests') failed.\n") ;
  84. for (k = 0 ; read_only_types [k].format ; k++)
  85. pipe_read_test (read_only_types [k].format, read_only_types [k].ext) ;
  86. for (k = 0 ; read_write_types [k].format ; k++)
  87. pipe_write_test (read_write_types [k].ext) ;
  88. for (k = 0 ; read_write_types [k].format ; k++)
  89. useek_pipe_rw_test (read_write_types [k].format, read_write_types [k].ext) ;
  90. if (0)
  91. pipe_test_others (read_write_types, read_only_types) ;
  92. return 0 ;
  93. } /* main */
  94. /*==============================================================================
  95. */
  96. static void
  97. pipe_read_test (int filetype, const char *ext)
  98. { static short data [PIPE_TEST_LEN] ;
  99. static char buffer [256] ;
  100. static char filename [256] ;
  101. SNDFILE *outfile ;
  102. SF_INFO sfinfo ;
  103. int k, retval ;
  104. snprintf (filename, sizeof (filename), "pipe_in.%s", ext) ;
  105. print_test_name ("pipe_read_test", filename) ;
  106. sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
  107. sfinfo.channels = 1 ;
  108. sfinfo.samplerate = 44100 ;
  109. for (k = 0 ; k < PIPE_TEST_LEN ; k++)
  110. data [k] = PIPE_INDEX (k) ;
  111. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  112. test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
  113. sf_close (outfile) ;
  114. snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s ", filename, ext) ;
  115. if ((retval = system (buffer)) != 0)
  116. { retval = WEXITSTATUS (retval) ;
  117. printf ("\n\n Line %d : pipe test returned error for file type \"%s\".\n\n", __LINE__, ext) ;
  118. exit (retval) ;
  119. } ;
  120. unlink (filename) ;
  121. puts ("ok") ;
  122. return ;
  123. } /* pipe_read_test */
  124. static void
  125. pipe_write_test (const char *ext)
  126. { static char buffer [256] ;
  127. int retval ;
  128. print_test_name ("pipe_write_test", ext) ;
  129. snprintf (buffer, sizeof (buffer), "./stdout_test %s | ./stdin_test %s ", ext, ext) ;
  130. if ((retval = system (buffer)))
  131. { retval = WEXITSTATUS (retval) ;
  132. printf ("\n\n Line %d : pipe test returned error file type \"%s\".\n\n", __LINE__, ext) ;
  133. exit (retval) ;
  134. } ;
  135. puts ("ok") ;
  136. return ;
  137. } /* pipe_write_test */
  138. /*==============================================================================
  139. */
  140. [+ FOR data_type +]
  141. static void
  142. useek_pipe_rw_[+ (get "type_name") +] (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read)
  143. { static [+ (get "type_name") +] buffer [PIPE_TEST_LEN] ;
  144. static [+ (get "type_name") +] data [PIPE_TEST_LEN] ;
  145. SNDFILE *outfile ;
  146. SNDFILE *infile_piped ;
  147. int k, status = 0 ;
  148. int pipefd [2] ;
  149. pid_t pida ;
  150. for (k = 0 ; k < PIPE_TEST_LEN ; k++)
  151. data [k] = PIPE_INDEX (k) ;
  152. /*
  153. ** Create the pipe.
  154. */
  155. exit_if_true (pipe (pipefd) != 0, "\n\n%s %d : pipe failed : %s\n", __func__, __LINE__, strerror (errno)) ;
  156. /*
  157. ** Attach the write end of the pipe to be written to.
  158. */
  159. if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL)
  160. { printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ;
  161. printf ("\t%s\n\n", sf_strerror (outfile)) ;
  162. exit (1) ;
  163. } ;
  164. if (sf_error (outfile) != SF_ERR_NO_ERROR)
  165. { printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ;
  166. exit (1) ;
  167. } ;
  168. /*
  169. ** Attach the read end of the pipe to be read from.
  170. */
  171. if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL)
  172. { printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ;
  173. exit (1) ;
  174. } ;
  175. if (sf_error (infile_piped) != SF_ERR_NO_ERROR)
  176. { printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ;
  177. exit (1) ;
  178. } ;
  179. /* Fork a child process that will write directly into the pipe. */
  180. if ((pida = fork ()) == 0) /* child process */
  181. { test_writef_[+ (get "type_name") +]_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
  182. exit (0) ;
  183. } ;
  184. /* In the parent process, read from the pipe and compare what is read
  185. ** to what is written, if they match everything went as planned.
  186. */
  187. test_readf_[+ (get "type_name") +]_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ;
  188. if (memcmp (buffer, data, sizeof (buffer)) != 0)
  189. { printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ;
  190. exit (1) ;
  191. } ;
  192. /* Wait for the child process to return. */
  193. waitpid (pida, &status, 0) ;
  194. status = WEXITSTATUS (status) ;
  195. sf_close (outfile) ;
  196. sf_close (infile_piped) ;
  197. if (status != 0)
  198. { printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
  199. exit (1) ;
  200. } ;
  201. return ;
  202. } /* useek_pipe_rw_[+ (get "type_name") +] */
  203. [+ ENDFOR data_type +]
  204. static void
  205. useek_pipe_rw_test (int filetype, const char *ext)
  206. { SF_INFO sfinfo_write ;
  207. SF_INFO sfinfo_read ;
  208. print_test_name ("useek_pipe_rw_test", ext) ;
  209. /*
  210. ** Setup the INFO structures for the filetype we will be
  211. ** working with.
  212. */
  213. sfinfo_write.format = filetype | SF_FORMAT_PCM_16 ;
  214. sfinfo_write.channels = 1 ;
  215. sfinfo_write.samplerate = 44100 ;
  216. sfinfo_read.format = 0 ;
  217. if (filetype == SF_FORMAT_RAW)
  218. { sfinfo_read.format = filetype | SF_FORMAT_PCM_16 ;
  219. sfinfo_read.channels = 1 ;
  220. sfinfo_read.samplerate = 44100 ;
  221. } ;
  222. useek_pipe_rw_short (ext, &sfinfo_write, &sfinfo_read) ;
  223. sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_FLOAT ;
  224. if (sf_format_check (&sfinfo_read) != 0)
  225. useek_pipe_rw_float (ext, &sfinfo_write, &sfinfo_read) ;
  226. sfinfo_read.format = sfinfo_write.format = filetype | SF_FORMAT_DOUBLE ;
  227. if (sf_format_check (&sfinfo_read) != 0)
  228. useek_pipe_rw_double (ext, &sfinfo_write, &sfinfo_read) ;
  229. puts ("ok") ;
  230. return ;
  231. } /* useek_pipe_rw_test */
  232. static void
  233. pipe_test_others (FILETYPE* list1, FILETYPE* list2)
  234. { SF_FORMAT_INFO info ;
  235. int k, m, major_count, in_list ;
  236. print_test_name ("pipe_test_others", "") ;
  237. sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ;
  238. for (k = 0 ; k < major_count ; k++)
  239. { info.format = k ;
  240. sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ;
  241. in_list = SF_FALSE ;
  242. for (m = 0 ; list1 [m].format ; m++)
  243. if (info.format == list1 [m].format)
  244. in_list = SF_TRUE ;
  245. for (m = 0 ; list2 [m].format ; m++)
  246. if (info.format == list2 [m].format)
  247. in_list = SF_TRUE ;
  248. if (in_list)
  249. continue ;
  250. printf ("%s %x\n", info.name, info.format) ;
  251. if (1)
  252. { static short data [PIPE_TEST_LEN] ;
  253. static char buffer [256] ;
  254. static const char *filename = "pipe_in.dat" ;
  255. SNDFILE *outfile ;
  256. SF_INFO sfinfo ;
  257. int retval ;
  258. sfinfo.format = info.format | SF_FORMAT_PCM_16 ;
  259. sfinfo.channels = 1 ;
  260. sfinfo.samplerate = 44100 ;
  261. outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  262. test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
  263. sf_close (outfile) ;
  264. snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s %d ", filename, info.extension, PIPE_TEST_LEN) ;
  265. if ((retval = system (buffer)) == 0)
  266. { retval = WEXITSTATUS (retval) ;
  267. printf ("\n\n Line %d : pipe test should have returned error file type \"%s\" but didn't.\n\n", __LINE__, info.name) ;
  268. exit (1) ;
  269. } ;
  270. unlink (filename) ;
  271. } ;
  272. } ;
  273. puts ("ok") ;
  274. return ;
  275. } /* pipe_test_others */
  276. /*==============================================================================
  277. */
  278. static int
  279. file_exists (const char *filename)
  280. { struct stat buf ;
  281. if (stat (filename, &buf))
  282. return 0 ;
  283. return 1 ;
  284. } /* file_exists */
  285. #endif