error_test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ** Copyright (C) 1999-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 "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #if OS_IS_WIN32
  26. #include <windows.h>
  27. #endif
  28. #include <sndfile.h>
  29. #include "utils.h"
  30. #define BUFFER_SIZE (1 << 15)
  31. #define SHORT_BUFFER (256)
  32. static void
  33. error_number_test (void)
  34. { const char *noerror, *errstr ;
  35. int k ;
  36. print_test_name ("error_number_test", "") ;
  37. noerror = sf_error_number (0) ;
  38. for (k = 1 ; k < 300 ; k++)
  39. { errstr = sf_error_number (k) ;
  40. /* Test for termination condition. */
  41. if (errstr == noerror)
  42. break ;
  43. /* Test for error. */
  44. if (strstr (errstr, "This is a bug in libsndfile."))
  45. { printf ("\n\nError : error number %d : %s\n\n\n", k, errstr) ;
  46. exit (1) ;
  47. } ;
  48. } ;
  49. puts ("ok") ;
  50. return ;
  51. } /* error_number_test */
  52. static void
  53. error_value_test (void)
  54. { static unsigned char aiff_data [0x1b0] =
  55. { 'F' , 'O' , 'R' , 'M' ,
  56. 0x00, 0x00, 0x01, 0xA8, /* FORM length */
  57. 'A' , 'I' , 'F' , 'C' ,
  58. } ;
  59. const char *filename = "error.aiff" ;
  60. SNDFILE *file ;
  61. SF_INFO sfinfo ;
  62. int error_num ;
  63. print_test_name ("error_value_test", filename) ;
  64. dump_data_to_file (filename, aiff_data, sizeof (aiff_data)) ;
  65. file = sf_open (filename, SFM_READ, &sfinfo) ;
  66. if (file != NULL)
  67. { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__) ;
  68. exit (1) ;
  69. } ;
  70. if ((error_num = sf_error (NULL)) <= 1 || error_num > 300)
  71. { printf ("\n\nLine %d : Should not have had an error number of %d.\n\n", __LINE__, error_num) ;
  72. exit (1) ;
  73. } ;
  74. remove (filename) ;
  75. puts ("ok") ;
  76. return ;
  77. } /* error_value_test */
  78. static void
  79. no_file_test (const char * filename)
  80. { SNDFILE *sndfile ;
  81. SF_INFO sfinfo ;
  82. print_test_name (__func__, filename) ;
  83. unlink (filename) ;
  84. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  85. sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
  86. exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
  87. unlink (filename) ;
  88. puts ("ok") ;
  89. } /* no_file_test */
  90. static void
  91. zero_length_test (const char *filename)
  92. { SNDFILE *sndfile ;
  93. SF_INFO sfinfo ;
  94. FILE *file ;
  95. print_test_name (__func__, filename) ;
  96. /* Create a zero length file. */
  97. file = fopen (filename, "w") ;
  98. exit_if_true (file == NULL, "\n\nLine %d : fopen ('%s') failed.\n", __LINE__, filename) ;
  99. fclose (file) ;
  100. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  101. sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
  102. exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
  103. exit_if_true (0 && sf_error (NULL) != SF_ERR_UNRECOGNISED_FORMAT,
  104. "\n\nLine %3d : Error : %s\n should be : %s\n", __LINE__,
  105. sf_strerror (NULL), sf_error_number (SF_ERR_UNRECOGNISED_FORMAT)) ;
  106. unlink (filename) ;
  107. puts ("ok") ;
  108. } /* zero_length_test */
  109. static void
  110. bad_wav_test (const char * filename)
  111. { SNDFILE *sndfile ;
  112. SF_INFO sfinfo ;
  113. FILE *file ;
  114. const char data [] = "RIFF WAVEfmt " ;
  115. print_test_name (__func__, filename) ;
  116. if ((file = fopen (filename, "w")) == NULL)
  117. { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
  118. exit (1) ;
  119. } ;
  120. exit_if_true (fwrite (data, sizeof (data), 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ;
  121. fclose (file) ;
  122. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  123. sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
  124. if (sndfile)
  125. { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
  126. exit (1) ;
  127. } ;
  128. unlink (filename) ;
  129. puts ("ok") ;
  130. } /* bad_wav_test */
  131. static void
  132. error_close_test (void)
  133. { static short buffer [SHORT_BUFFER] ;
  134. const char *filename = "error_close.wav" ;
  135. SNDFILE *sndfile ;
  136. SF_INFO sfinfo ;
  137. FILE *file ;
  138. print_test_name (__func__, filename) ;
  139. /* Open a FILE* from which we will extract a file descriptor. */
  140. if ((file = fopen (filename, "w")) == NULL)
  141. { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
  142. exit (1) ;
  143. } ;
  144. /* Set parameters for writing the file. */
  145. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  146. sfinfo.channels = 1 ;
  147. sfinfo.samplerate = 44100 ;
  148. sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  149. sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ;
  150. if (sndfile == NULL)
  151. { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ;
  152. exit (1) ;
  153. } ;
  154. test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ;
  155. /* Now close the fd associated with file before calling sf_close. */
  156. fclose (file) ;
  157. if (sf_close (sndfile) == 0)
  158. {
  159. #if OS_IS_WIN32
  160. OSVERSIONINFOEX osvi ;
  161. memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ;
  162. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ;
  163. if (GetVersionEx ((OSVERSIONINFO *) &osvi))
  164. { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
  165. printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n",
  166. (int) osvi.dwMajorVersion, (int) osvi.dwMinorVersion) ;
  167. } ;
  168. #else
  169. printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
  170. exit (1) ;
  171. #endif
  172. } ;
  173. unlink (filename) ;
  174. puts ("ok") ;
  175. } /* error_close_test */
  176. int
  177. main (void)
  178. {
  179. error_number_test () ;
  180. error_value_test () ;
  181. error_close_test () ;
  182. no_file_test ("no_file.wav") ;
  183. zero_length_test ("zero_length.wav") ;
  184. bad_wav_test ("bad_wav.wav") ;
  185. return 0 ;
  186. } /* main */