cpp_test.cc 8.2 KB

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