2
0

format_check_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ** Copyright (C) 2011 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. #include "sndfile.h"
  26. #include "utils.h"
  27. static void format_error_test (void) ;
  28. static void format_combo_test (void) ;
  29. int
  30. main (void)
  31. {
  32. format_error_test () ;
  33. format_combo_test () ;
  34. return 0 ;
  35. } /* main */
  36. /*==============================================================================
  37. */
  38. static void
  39. format_error_test (void)
  40. { const char *filename = "format-error.wav" ;
  41. SNDFILE *file ;
  42. SF_INFO info ;
  43. print_test_name (__func__, NULL) ;
  44. memset (&info, 0, sizeof (info)) ;
  45. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  46. info.channels = 1 ;
  47. info.samplerate = 44100 ;
  48. info.format = SF_FORMAT_WAV ;
  49. file = sf_open (filename, SFM_WRITE, &info) ;
  50. exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
  51. exit_if_true (
  52. strstr (sf_strerror (NULL), "minor format") == NULL,
  53. "\n\nLine %d : Error string should reference bad 'minor format'.\n\n", __LINE__
  54. ) ;
  55. info.format = SF_FORMAT_PCM_16 ;
  56. file = sf_open (filename, SFM_WRITE, &info) ;
  57. exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
  58. exit_if_true (
  59. strstr (sf_strerror (NULL), "major format") == NULL,
  60. "\n\nLine %d : Error string should reference bad 'major format'.\n\n", __LINE__
  61. ) ;
  62. unlink (filename) ;
  63. puts ("ok") ;
  64. } /* format_error_test */
  65. static void
  66. format_combo_test (void)
  67. { int container_max, codec_max, cont, codec ;
  68. print_test_name (__func__, NULL) ;
  69. sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &container_max, sizeof (container_max)) ;
  70. sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &codec_max, sizeof (codec_max)) ;
  71. for (cont = 0 ; cont < container_max + 10 ; cont ++)
  72. { SF_FORMAT_INFO major_fmt_info ;
  73. memset (&major_fmt_info, 0, sizeof (major_fmt_info)) ;
  74. major_fmt_info.format = cont ;
  75. (void) sf_command (NULL, SFC_GET_FORMAT_MAJOR, &major_fmt_info, sizeof (major_fmt_info)) ;
  76. for (codec = 0 ; codec < codec_max + 10 ; codec ++)
  77. { SF_FORMAT_INFO subtype_fmt_info ;
  78. SNDFILE * sndfile ;
  79. SF_INFO info ;
  80. char filename [128] ;
  81. int subtype_is_valid, check_is_valid ;
  82. memset (&subtype_fmt_info, 0, sizeof (subtype_fmt_info)) ;
  83. subtype_fmt_info.format = codec ;
  84. subtype_is_valid = sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &subtype_fmt_info, sizeof (subtype_fmt_info)) == 0 ;
  85. sf_info_setup (&info, major_fmt_info.format | subtype_fmt_info.format, 22050, 1) ;
  86. check_is_valid = sf_format_check (&info) ;
  87. exit_if_true (
  88. NOT (subtype_is_valid) && check_is_valid,
  89. "\n\nLine %d : Subtype is not valid but checks ok.\n",
  90. __LINE__
  91. ) ;
  92. snprintf (filename, sizeof (filename), "format-check.%s", major_fmt_info.extension) ;
  93. sndfile = sf_open (filename, SFM_WRITE, &info) ;
  94. sf_close (sndfile) ;
  95. unlink (filename) ;
  96. if (major_fmt_info.extension != NULL && strcmp (major_fmt_info.extension, "sd2") == 0)
  97. { snprintf (filename, sizeof (filename), "._format-check.%s", major_fmt_info.extension) ;
  98. unlink (filename) ;
  99. } ;
  100. exit_if_true (
  101. sndfile && NOT (check_is_valid),
  102. "\n\nError : Format was not valid but file opened correctly.\n"
  103. " Container : %s\n"
  104. " Codec : %s\n\n",
  105. major_fmt_info.name, subtype_fmt_info.name
  106. ) ;
  107. exit_if_true (
  108. NOT (sndfile) && check_is_valid,
  109. "\n\nError : Format was valid but file failed to open.\n"
  110. " Container : %s\n"
  111. " Codec : %s\n\n",
  112. major_fmt_info.name, subtype_fmt_info.name
  113. ) ;
  114. } ;
  115. } ;
  116. puts ("ok") ;
  117. } /* format_combo_test */