long_read_write_test.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. ** Copyright (C) 2015-2016 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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <inttypes.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #else
  27. #include "sf_unistd.h"
  28. #endif
  29. #include <sndfile.h>
  30. #include "dft_cmp.h"
  31. #include "utils.h"
  32. #define BUFFER_LENGTH 10000
  33. #define SAMPLE_RATE 44010
  34. static void short_lrw_test (const char *filename, int filetype, const short * output, int out_len) ;
  35. static void int_lrw_test (const char *filename, int filetype, const int * output, int out_len) ;
  36. static void float_lrw_test (const char *filename, int filetype, const float * output, int out_len) ;
  37. static void double_lrw_test (const char *filename, int filetype, const double * output, int out_len) ;
  38. static short short_data [BUFFER_LENGTH] ;
  39. static int int_data [BUFFER_LENGTH] ;
  40. static float float_data [BUFFER_LENGTH] ;
  41. static double double_data [BUFFER_LENGTH] ;
  42. int
  43. main (int argc, char *argv [])
  44. { int do_all ;
  45. size_t k ;
  46. if (argc != 2)
  47. { printf ("Usage : %s <test>\n", argv [0]) ;
  48. printf (" Where <test> is one of the following:\n") ;
  49. printf (" alac - test CAF/ALAC file functions\n") ;
  50. printf (" all - perform all tests\n") ;
  51. exit (1) ;
  52. } ;
  53. for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
  54. { int value = k / 32 ;
  55. int_data [k] = (value & 1 ? -1 : 1) * value ;
  56. short_data [k] = int_data [k] ;
  57. float_data [k] = int_data [k] / 32000.0f ;
  58. double_data [k] = int_data [k] / 32000.0 ;
  59. }
  60. do_all = ! strcmp (argv [1], "all") ;
  61. if (do_all || strcmp (argv [1], "alac") == 0)
  62. { short_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_16, short_data, ARRAY_LEN (short_data)) ;
  63. int_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, int_data, ARRAY_LEN (int_data)) ;
  64. float_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, float_data, ARRAY_LEN (float_data)) ;
  65. double_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, double_data, ARRAY_LEN (double_data)) ;
  66. } ;
  67. return 0 ;
  68. } /* main */
  69. /*============================================================================================
  70. * Here are the test functions.
  71. */
  72. static void
  73. short_lrw_test (const char *filename, int filetype, const short * output, int out_len)
  74. { SNDFILE *file ;
  75. SF_INFO sfinfo ;
  76. int k ;
  77. short input [BUFFER_LENGTH] ;
  78. print_test_name ("short_lrw_test", filename) ;
  79. exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
  80. sfinfo.samplerate = SAMPLE_RATE ;
  81. sfinfo.frames = out_len ;
  82. sfinfo.channels = 1 ;
  83. sfinfo.format = filetype ;
  84. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  85. test_write_short_or_die (file, 0, output, out_len, __LINE__) ;
  86. sf_close (file) ;
  87. memset (input, 0, sizeof (input)) ;
  88. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  89. exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  90. exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
  91. exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  92. check_log_buffer_or_die (file, __LINE__) ;
  93. test_read_short_or_die (file, 0, input, out_len, __LINE__) ;
  94. sf_close (file) ;
  95. for (k = 0 ; k < out_len ; k++)
  96. exit_if_true (input [k] != output [k],
  97. "\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
  98. puts ("ok") ;
  99. unlink (filename) ;
  100. return ;
  101. } /* short_lrw_test */
  102. static void
  103. int_lrw_test (const char *filename, int filetype, const int * output, int out_len)
  104. { SNDFILE *file ;
  105. SF_INFO sfinfo ;
  106. int k ;
  107. int input [BUFFER_LENGTH] ;
  108. print_test_name ("int_lrw_test", filename) ;
  109. exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
  110. sfinfo.samplerate = SAMPLE_RATE ;
  111. sfinfo.frames = out_len ;
  112. sfinfo.channels = 1 ;
  113. sfinfo.format = filetype ;
  114. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  115. test_write_int_or_die (file, 0, output, out_len, __LINE__) ;
  116. sf_close (file) ;
  117. memset (input, 0, sizeof (input)) ;
  118. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  119. exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  120. exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too int). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
  121. exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  122. check_log_buffer_or_die (file, __LINE__) ;
  123. test_read_int_or_die (file, 0, input, out_len, __LINE__) ;
  124. sf_close (file) ;
  125. for (k = 0 ; k < out_len ; k++)
  126. exit_if_true (input [k] != output [k],
  127. "\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
  128. puts ("ok") ;
  129. unlink (filename) ;
  130. return ;
  131. } /* int_lrw_test */
  132. static void
  133. float_lrw_test (const char *filename, int filetype, const float * output, int out_len)
  134. { SNDFILE *file ;
  135. SF_INFO sfinfo ;
  136. int k ;
  137. float input [BUFFER_LENGTH] ;
  138. print_test_name ("float_lrw_test", filename) ;
  139. exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
  140. sfinfo.samplerate = SAMPLE_RATE ;
  141. sfinfo.frames = out_len ;
  142. sfinfo.channels = 1 ;
  143. sfinfo.format = filetype ;
  144. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  145. test_write_float_or_die (file, 0, output, out_len, __LINE__) ;
  146. sf_close (file) ;
  147. memset (input, 0, sizeof (input)) ;
  148. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  149. exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  150. exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too float). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
  151. exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  152. check_log_buffer_or_die (file, __LINE__) ;
  153. test_read_float_or_die (file, 0, input, out_len, __LINE__) ;
  154. sf_close (file) ;
  155. for (k = 0 ; k < out_len ; k++)
  156. exit_if_true (fabs (input [k] - output [k]) > 0.00001,
  157. "\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
  158. puts ("ok") ;
  159. unlink (filename) ;
  160. return ;
  161. } /* float_lrw_test */
  162. static void
  163. double_lrw_test (const char *filename, int filetype, const double * output, int out_len)
  164. { SNDFILE *file ;
  165. SF_INFO sfinfo ;
  166. int k ;
  167. double input [BUFFER_LENGTH] ;
  168. print_test_name ("double_lrw_test", filename) ;
  169. exit_if_true (BUFFER_LENGTH > out_len, "\n\nLine %d: Bad array length.\n", __LINE__) ;
  170. sfinfo.samplerate = SAMPLE_RATE ;
  171. sfinfo.frames = out_len ;
  172. sfinfo.channels = 1 ;
  173. sfinfo.format = filetype ;
  174. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  175. test_write_double_or_die (file, 0, output, out_len, __LINE__) ;
  176. sf_close (file) ;
  177. memset (input, 0, sizeof (input)) ;
  178. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  179. exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  180. exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too double). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
  181. exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
  182. check_log_buffer_or_die (file, __LINE__) ;
  183. test_read_double_or_die (file, 0, input, out_len, __LINE__) ;
  184. sf_close (file) ;
  185. for (k = 0 ; k < out_len ; k++)
  186. exit_if_true (fabs (input [k] - output [k]) > 0.00001,
  187. "\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
  188. puts ("ok") ;
  189. unlink (filename) ;
  190. return ;
  191. } /* double_lrw_test */