raw_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ** Copyright (C) 2002-2014 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 "utils.h"
  31. #define BUFFER_LEN (1 << 10)
  32. #define LOG_BUFFER_SIZE 1024
  33. static void raw_offset_test (const char *filename, int typeminor) ;
  34. static void bad_raw_test (void) ;
  35. /* Force the start of this buffer to be double aligned. Sparc-solaris will
  36. ** choke if its not.
  37. */
  38. static short data [BUFFER_LEN] ;
  39. int
  40. main (void)
  41. {
  42. raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ;
  43. bad_raw_test () ;
  44. return 0 ;
  45. } /* main */
  46. /*============================================================================================
  47. ** Here are the test functions.
  48. */
  49. static void
  50. raw_offset_test (const char *filename, int typeminor)
  51. { SNDFILE *sndfile ;
  52. SF_INFO sfinfo ;
  53. sf_count_t start ;
  54. int k ;
  55. print_test_name ("raw_offset_test", filename) ;
  56. sfinfo.samplerate = 44100 ;
  57. sfinfo.format = SF_FORMAT_RAW | typeminor ;
  58. sfinfo.channels = 1 ;
  59. sfinfo.frames = 0 ;
  60. sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  61. start = 0 ;
  62. sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ;
  63. for (k = 0 ; k < BUFFER_LEN ; k++)
  64. data [k] = k ;
  65. test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
  66. sf_close (sndfile) ;
  67. sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  68. check_log_buffer_or_die (sndfile, __LINE__) ;
  69. if (ABS (BUFFER_LEN - sfinfo.frames) > 1)
  70. { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, BUFFER_LEN) ;
  71. dump_log_buffer (sndfile) ;
  72. exit (1) ;
  73. } ;
  74. memset (data, 0 , sizeof (data)) ;
  75. test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
  76. for (k = 0 ; k < BUFFER_LEN ; k++)
  77. if (data [k] != k)
  78. printf ("Error : line %d\n", __LINE__) ;
  79. /* Set dataoffset to 2 bytes from beginning of file. */
  80. start = 2 ;
  81. sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
  82. /* Seek to new start */
  83. test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  84. memset (data, 0 , sizeof (data)) ;
  85. test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ;
  86. for (k = 0 ; k < BUFFER_LEN - 1 ; k++)
  87. if (data [k] != k + 1)
  88. { printf ("Error : line %d\n", __LINE__) ;
  89. exit (1) ;
  90. } ;
  91. /* Set dataoffset to 4 bytes from beginning of file. */
  92. start = 4 ;
  93. sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
  94. /* Seek to new start */
  95. test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  96. memset (data, 0 , sizeof (data)) ;
  97. test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ;
  98. for (k = 0 ; k < BUFFER_LEN - 2 ; k++)
  99. if (data [k] != k + 2)
  100. { printf ("Error : line %d\n", __LINE__) ;
  101. exit (1) ;
  102. } ;
  103. /* Set dataoffset back to 0 bytes from beginning of file. */
  104. start = 0 ;
  105. sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
  106. /* Seek to new start */
  107. test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
  108. memset (data, 0 , sizeof (data)) ;
  109. test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
  110. for (k = 0 ; k < BUFFER_LEN ; k++)
  111. if (data [k] != k)
  112. { printf ("Error : line %d\n", __LINE__) ;
  113. exit (1) ;
  114. } ;
  115. sf_close (sndfile) ;
  116. unlink (filename) ;
  117. puts ("ok") ;
  118. } /* raw_offset_test */
  119. static void
  120. bad_raw_test (void)
  121. { FILE *textfile ;
  122. SNDFILE *file ;
  123. SF_INFO sfinfo ;
  124. const char *errorstr, *filename = "bad.raw" ;
  125. print_test_name ("bad_raw_test", filename) ;
  126. if ((textfile = fopen (filename, "w")) == NULL)
  127. { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
  128. exit (1) ;
  129. } ;
  130. fprintf (textfile, "This is not a valid file.\n") ;
  131. fclose (textfile) ;
  132. sfinfo.samplerate = 44100 ;
  133. sfinfo.format = SF_FORMAT_RAW | 0xABCD ;
  134. sfinfo.channels = 1 ;
  135. if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL)
  136. { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
  137. exit (1) ;
  138. } ;
  139. errorstr = sf_strerror (file) ;
  140. if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL)
  141. { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
  142. exit (1) ;
  143. } ;
  144. unlink (filename) ;
  145. puts ("ok") ;
  146. } /* bad_raw_test */