raw_test.c 5.1 KB

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