cpp_test.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. ** Copyright (C) 2006-2012 Erik de Castro Lopo <[email protected]>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "sfconfig.h"
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cstring>
  22. #include <sndfile.hh>
  23. #include "utils.h"
  24. static short sbuffer [100] ;
  25. static int ibuffer [100] ;
  26. static float fbuffer [100] ;
  27. static double dbuffer [100] ;
  28. static void
  29. ceeplusplus_wchar_test (void)
  30. {
  31. #if 0
  32. LPCWSTR filename = L"wchar_test.wav" ;
  33. print_test_name (__func__, "ceeplusplus_wchar_test.wav") ;
  34. /* Use this scope to make sure the created file is closed. */
  35. {
  36. SndfileHandle file (filename, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 2, 44100) ;
  37. if (file.refCount () != 1)
  38. { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
  39. exit (1) ;
  40. } ;
  41. /* This should check that the file did in fact get created with a
  42. ** wchar_t * filename.
  43. */
  44. exit_if_true (
  45. GetFileAttributesW (filename) == INVALID_FILE_ATTRIBUTES,
  46. "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__
  47. ) ;
  48. }
  49. /* Use this because the file was created with CreateFileW. */
  50. DeleteFileW (filename) ;
  51. puts ("ok") ;
  52. #endif
  53. } /* ceeplusplus_wchar_test */
  54. static void
  55. create_file (const char * filename, int format)
  56. { SndfileHandle file ;
  57. if (file.refCount () != 0)
  58. { printf ("\n\n%s %d : Error : Reference count (%d) should be zero.\n\n", __func__, __LINE__, file.refCount ()) ;
  59. exit (1) ;
  60. } ;
  61. file = SndfileHandle (filename, SFM_WRITE, format, 2, 48000) ;
  62. if (file.refCount () != 1)
  63. { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
  64. exit (1) ;
  65. } ;
  66. file.setString (SF_STR_TITLE, filename) ;
  67. /* Item write. */
  68. file.write (sbuffer, ARRAY_LEN (sbuffer)) ;
  69. file.write (ibuffer, ARRAY_LEN (ibuffer)) ;
  70. file.write (fbuffer, ARRAY_LEN (fbuffer)) ;
  71. file.write (dbuffer, ARRAY_LEN (dbuffer)) ;
  72. /* Frame write. */
  73. file.writef (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
  74. file.writef (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
  75. file.writef (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
  76. file.writef (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
  77. /* RAII takes care of the SndfileHandle. */
  78. } /* create_file */
  79. static void
  80. check_title (const SndfileHandle & file, const char * filename)
  81. { const char *title = NULL ;
  82. title = file.getString (SF_STR_TITLE) ;
  83. if (title == NULL)
  84. { printf ("\n\n%s %d : Error : No title for %s.\n\n", __func__, __LINE__, filename) ;
  85. exit (1) ;
  86. } ;
  87. if (strcmp (filename, title) != 0)
  88. { printf ("\n\n%s %d : Error : title '%s' should be '%s'\n\n", __func__, __LINE__, title, filename) ;
  89. exit (1) ;
  90. } ;
  91. return ;
  92. } /* check_title */
  93. static void
  94. read_file (const char * filename, int format)
  95. { SndfileHandle file ;
  96. sf_count_t count ;
  97. if (file)
  98. { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
  99. exit (1) ;
  100. } ;
  101. file = SndfileHandle (filename) ;
  102. if (1)
  103. { SndfileHandle file2 = file ;
  104. if (file.refCount () != 2 || file2.refCount () != 2)
  105. { printf ("\n\n%s %d : Error : Reference count (%d) should be two.\n\n", __func__, __LINE__, file.refCount ()) ;
  106. exit (1) ;
  107. } ;
  108. } ;
  109. if (file.refCount () != 1)
  110. { printf ("\n\n%s %d : Error : Reference count (%d) should be one.\n\n", __func__, __LINE__, file.refCount ()) ;
  111. exit (1) ;
  112. } ;
  113. if (! file)
  114. { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
  115. exit (1) ;
  116. } ;
  117. if (file.format () != format)
  118. { printf ("\n\n%s %d : Error : format 0x%08x should be 0x%08x.\n\n", __func__, __LINE__, file.format (), format) ;
  119. exit (1) ;
  120. } ;
  121. if (file.channels () != 2)
  122. { printf ("\n\n%s %d : Error : channels %d should be 2.\n\n", __func__, __LINE__, file.channels ()) ;
  123. exit (1) ;
  124. } ;
  125. if (file.frames () != ARRAY_LEN (sbuffer) * 4)
  126. { printf ("\n\n%s %d : Error : frames %ld should be %lu.\n\n", __func__, __LINE__,
  127. (long) file.frames (), (long) ARRAY_LEN (sbuffer) * 4 / 2) ;
  128. exit (1) ;
  129. } ;
  130. switch (format & SF_FORMAT_TYPEMASK)
  131. { case SF_FORMAT_AU :
  132. break ;
  133. default :
  134. check_title (file, filename) ;
  135. break ;
  136. } ;
  137. /* Item read. */
  138. file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
  139. file.read (ibuffer, ARRAY_LEN (ibuffer)) ;
  140. file.read (fbuffer, ARRAY_LEN (fbuffer)) ;
  141. file.read (dbuffer, ARRAY_LEN (dbuffer)) ;
  142. /* Frame read. */
  143. file.readf (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
  144. file.readf (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
  145. file.readf (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
  146. file.readf (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
  147. count = file.seek (file.frames () - 10, SEEK_SET) ;
  148. if (count != file.frames () - 10)
  149. { printf ("\n\n%s %d : Error : offset (%ld) should be %ld\n\n", __func__, __LINE__,
  150. (long) count, (long) (file.frames () - 10)) ;
  151. exit (1) ;
  152. } ;
  153. count = file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
  154. if (count != 10 * file.channels ())
  155. { printf ("\n\n%s %d : Error : count (%ld) should be %ld\n\n", __func__, __LINE__,
  156. (long) count, (long) (10 * file.channels ())) ;
  157. exit (1) ;
  158. } ;
  159. /* RAII takes care of the SndfileHandle. */
  160. } /* read_file */
  161. static void
  162. ceeplusplus_test (const char *filename, int format)
  163. {
  164. print_test_name ("ceeplusplus_test", filename) ;
  165. create_file (filename, format) ;
  166. read_file (filename, format) ;
  167. remove (filename) ;
  168. puts ("ok") ;
  169. } /* ceeplusplus_test */
  170. static void
  171. ceeplusplus_extra_test (void)
  172. { SndfileHandle file ;
  173. const char * filename = "bad_file_name.wav" ;
  174. int error ;
  175. print_test_name ("ceeplusplus_extra_test", filename) ;
  176. file = SndfileHandle (filename) ;
  177. error = file.error () ;
  178. if (error == 0)
  179. { printf ("\n\n%s %d : error should not be zero.\n\n", __func__, __LINE__) ;
  180. exit (1) ;
  181. } ;
  182. if (file.strError () == NULL)
  183. { printf ("\n\n%s %d : strError should not return NULL.\n\n", __func__, __LINE__) ;
  184. exit (1) ;
  185. } ;
  186. if (file.seek (0, SEEK_SET) != 0)
  187. { printf ("\n\n%s %d : bad seek ().\n\n", __func__, __LINE__) ;
  188. exit (1) ;
  189. } ;
  190. puts ("ok") ;
  191. } /* ceeplusplus_extra_test */
  192. static void
  193. ceeplusplus_rawhandle_test (const char *filename)
  194. {
  195. SNDFILE* handle ;
  196. {
  197. SndfileHandle file (filename) ;
  198. handle = file.rawHandle () ;
  199. sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) ;
  200. }
  201. } /* ceeplusplus_rawhandle_test */
  202. static void
  203. ceeplusplus_takeOwnership_test (const char *filename)
  204. {
  205. SNDFILE* handle ;
  206. {
  207. SndfileHandle file (filename) ;
  208. handle = file.takeOwnership () ;
  209. }
  210. if (sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) <= 0)
  211. { printf ("\n\n%s %d : error when taking ownership of handle.\n\n", __func__, __LINE__) ;
  212. exit (1) ;
  213. }
  214. if (sf_close (handle) != 0)
  215. { printf ("\n\n%s %d : cannot close file.\n\n", __func__, __LINE__) ;
  216. exit (1) ;
  217. }
  218. SndfileHandle file (filename) ;
  219. SndfileHandle file2 (file) ;
  220. if (file2.takeOwnership ())
  221. { printf ("\n\n%s %d : taking ownership of shared handle is not allowed.\n\n", __func__, __LINE__) ;
  222. exit (1) ;
  223. }
  224. } /* ceeplusplus_takeOwnership_test */
  225. static void
  226. ceeplusplus_handle_test (const char *filename, int format)
  227. {
  228. print_test_name ("ceeplusplus_handle_test", filename) ;
  229. create_file (filename, format) ;
  230. if (0) ceeplusplus_rawhandle_test (filename) ;
  231. ceeplusplus_takeOwnership_test (filename) ;
  232. remove (filename) ;
  233. puts ("ok") ;
  234. } /* ceeplusplus_test */
  235. int
  236. main (void)
  237. {
  238. ceeplusplus_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  239. ceeplusplus_test ("cpp_test.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
  240. ceeplusplus_test ("cpp_test.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
  241. ceeplusplus_extra_test () ;
  242. ceeplusplus_handle_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  243. ceeplusplus_wchar_test () ;
  244. return 0 ;
  245. } /* main */