cannam@125: /* cannam@125: ** Copyright (C) 2003-2016 Erik de Castro Lopo cannam@125: ** cannam@125: ** This program is free software; you can redistribute it and/or modify cannam@125: ** it under the terms of the GNU General Public License as published by cannam@125: ** the Free Software Foundation; either version 2 of the License, or cannam@125: ** (at your option) any later version. cannam@125: ** cannam@125: ** This program is distributed in the hope that it will be useful, cannam@125: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@125: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@125: ** GNU General Public License for more details. cannam@125: ** cannam@125: ** You should have received a copy of the GNU General Public License cannam@125: ** along with this program; if not, write to the Free Software cannam@125: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@125: */ cannam@125: cannam@125: #include "sfconfig.h" cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #if HAVE_UNISTD_H cannam@125: #include cannam@125: #endif cannam@125: cannam@125: #include cannam@125: cannam@125: #include "utils.h" cannam@125: cannam@125: #define BUFFER_LEN (1 << 10) cannam@125: #define LOG_BUFFER_SIZE 1024 cannam@125: cannam@125: static void chunk_test (const char *filename, int format) ; cannam@125: cannam@125: static void cannam@125: chunk_test_helper (const char *filename, int format, const char * testdata) ; cannam@125: cannam@125: int cannam@125: main (int argc, char *argv []) cannam@125: { int do_all = 0 ; cannam@125: int test_count = 0 ; cannam@125: cannam@125: if (argc != 2) cannam@125: { printf ("Usage : %s \n", argv [0]) ; cannam@125: printf (" Where is one of the following:\n") ; cannam@125: printf (" wav - test adding chunks to WAV files\n") ; cannam@125: printf (" aiff - test adding chunks to AIFF files\n") ; cannam@125: printf (" caf - test adding chunks to CAF files\n") ; cannam@125: printf (" rf64 - test adding chunks to RF64 files\n") ; cannam@125: printf (" all - perform all tests\n") ; cannam@125: exit (1) ; cannam@125: } ; cannam@125: cannam@125: do_all = ! strcmp (argv [1], "all") ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "wav")) cannam@125: { chunk_test ("chunks_pcm16.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; cannam@125: chunk_test ("chunks_pcm16.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; cannam@125: chunk_test ("chunks_pcm16.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "aiff")) cannam@125: { chunk_test ("chunks_pcm16.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "caf")) cannam@125: { chunk_test ("chunks_pcm16.caf", SF_FORMAT_CAF | SF_FORMAT_PCM_16) ; cannam@125: chunk_test ("chunks_alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_16) ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (do_all || ! strcmp (argv [1], "rf64")) cannam@125: { chunk_test ("chunks_pcm16.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ; cannam@125: test_count++ ; cannam@125: } ; cannam@125: cannam@125: if (test_count == 0) cannam@125: { printf ("Mono : ************************************\n") ; cannam@125: printf ("Mono : * No '%s' test defined.\n", argv [1]) ; cannam@125: printf ("Mono : ************************************\n") ; cannam@125: return 1 ; cannam@125: } ; cannam@125: cannam@125: return 0 ; cannam@125: } /* main */ cannam@125: cannam@125: cannam@125: /*============================================================================================ cannam@125: ** Here are the test functions. cannam@125: */ cannam@125: cannam@125: static void cannam@125: chunk_test_helper (const char *filename, int format, const char * testdata) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: SF_CHUNK_INFO chunk_info ; cannam@125: SF_CHUNK_ITERATOR * iterator ; cannam@125: uint32_t length_before ; cannam@125: int err, allow_fd ; cannam@125: cannam@125: switch (format & SF_FORMAT_SUBMASK) cannam@125: { case SF_FORMAT_ALAC_16 : cannam@125: allow_fd = SF_FALSE ; cannam@125: break ; cannam@125: default : cannam@125: allow_fd = SF_TRUE ; cannam@125: break ; cannam@125: } ; cannam@125: cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.frames = 0 ; cannam@125: sfinfo.format = format ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ; cannam@125: cannam@125: /* Set up the chunk to write. */ cannam@125: memset (&chunk_info, 0, sizeof (chunk_info)) ; cannam@125: snprintf (chunk_info.id, sizeof (chunk_info.id), "Test") ; cannam@125: chunk_info.id_size = 4 ; cannam@125: chunk_info.data = strdup (testdata) ; cannam@125: chunk_info.datalen = strlen (chunk_info.data) ; cannam@125: cannam@125: length_before = chunk_info.datalen ; cannam@125: cannam@125: err = sf_set_chunk (file, &chunk_info) ; cannam@125: exit_if_true ( cannam@125: err != SF_ERR_NO_ERROR, cannam@125: "\n\nLine %d : sf_set_chunk returned for testdata '%s' : %s\n\n", __LINE__, testdata, sf_error_number (err) cannam@125: ) ; cannam@125: cannam@125: memset (chunk_info.data, 0, chunk_info.datalen) ; cannam@125: free (chunk_info.data) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ; cannam@125: cannam@125: memset (&chunk_info, 0, sizeof (chunk_info)) ; cannam@125: snprintf (chunk_info.id, sizeof (chunk_info.id), "Test") ; cannam@125: chunk_info.id_size = 4 ; cannam@125: cannam@125: iterator = sf_get_chunk_iterator (file, &chunk_info) ; cannam@125: err = sf_get_chunk_size (iterator, &chunk_info) ; cannam@125: exit_if_true ( cannam@125: err != SF_ERR_NO_ERROR, cannam@125: "\n\nLine %d : sf_get_chunk_size returned for testdata '%s' : %s\n\n", __LINE__, testdata, sf_error_number (err) cannam@125: ) ; cannam@125: cannam@125: exit_if_true ( cannam@125: length_before > chunk_info.datalen || chunk_info.datalen - length_before > 4, cannam@125: "\n\nLine %d : testdata '%s' : Bad chunk length %u (previous length %u)\n\n", __LINE__, testdata, chunk_info.datalen, length_before cannam@125: ) ; cannam@125: cannam@125: chunk_info.data = malloc (chunk_info.datalen) ; cannam@125: err = sf_get_chunk_data (iterator, &chunk_info) ; cannam@125: exit_if_true ( cannam@125: err != SF_ERR_NO_ERROR, cannam@125: "\n\nLine %d : sf_get_chunk_size returned for testdata '%s' : %s\n\n", __LINE__, testdata, sf_error_number (err) cannam@125: ) ; cannam@125: cannam@125: exit_if_true ( cannam@125: memcmp (testdata, chunk_info.data, length_before), cannam@125: "\n\nLine %d : Data compare failed.\n %s\n %s\n\n", __LINE__, testdata, (char*) chunk_info.data cannam@125: ) ; cannam@125: cannam@125: free (chunk_info.data) ; cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: } /* chunk_test_helper */ cannam@125: cannam@125: static void cannam@125: multichunk_test_helper (const char *filename, int format, const char * testdata [], size_t testdata_len) cannam@125: { SNDFILE *file ; cannam@125: SF_INFO sfinfo ; cannam@125: SF_CHUNK_INFO chunk_info ; cannam@125: SF_CHUNK_ITERATOR * iterator ; cannam@125: uint32_t length_before [testdata_len] ; cannam@125: int err, allow_fd ; cannam@125: size_t i ; cannam@125: cannam@125: sfinfo.samplerate = 44100 ; cannam@125: sfinfo.channels = 1 ; cannam@125: sfinfo.frames = 0 ; cannam@125: sfinfo.format = format ; cannam@125: cannam@125: switch (format & SF_FORMAT_SUBMASK) cannam@125: { case SF_FORMAT_ALAC_16 : cannam@125: allow_fd = SF_FALSE ; cannam@125: break ; cannam@125: default : cannam@125: allow_fd = SF_TRUE ; cannam@125: break ; cannam@125: } ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ; cannam@125: cannam@125: /* Set up the chunk to write. */ cannam@125: for (i = 0 ; i < testdata_len ; i++) cannam@125: { memset (&chunk_info, 0, sizeof (chunk_info)) ; cannam@125: snprintf (chunk_info.id, sizeof (chunk_info.id), "Test") ; cannam@125: chunk_info.id_size = 4 ; cannam@125: cannam@125: chunk_info.data = strdup (testdata [i]) ; cannam@125: chunk_info.datalen = strlen (chunk_info.data) ; cannam@125: cannam@125: length_before [i] = chunk_info.datalen ; cannam@125: cannam@125: err = sf_set_chunk (file, &chunk_info) ; cannam@125: exit_if_true ( cannam@125: err != SF_ERR_NO_ERROR, cannam@125: "\n\nLine %d : sf_set_chunk returned for testdata[%d] '%s' : %s\n\n", __LINE__, (int) i, testdata [i], sf_error_number (err) cannam@125: ) ; cannam@125: cannam@125: memset (chunk_info.data, 0, chunk_info.datalen) ; cannam@125: free (chunk_info.data) ; cannam@125: } cannam@125: cannam@125: sf_close (file) ; cannam@125: cannam@125: file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ; cannam@125: cannam@125: memset (&chunk_info, 0, sizeof (chunk_info)) ; cannam@125: snprintf (chunk_info.id, sizeof (chunk_info.id), "Test") ; cannam@125: chunk_info.id_size = 4 ; cannam@125: cannam@125: iterator = sf_get_chunk_iterator (file, &chunk_info) ; cannam@125: cannam@125: i = 0 ; cannam@125: while (iterator) cannam@125: { memset (&chunk_info, 0, sizeof (chunk_info)) ; cannam@125: err = sf_get_chunk_size (iterator, &chunk_info) ; cannam@125: exit_if_true ( cannam@125: i > testdata_len, cannam@125: "\n\nLine %d : iterated to chunk #%d, but only %d chunks have been written\n\n", __LINE__, (int) i, (int) testdata_len cannam@125: ) ; cannam@125: cannam@125: exit_if_true ( cannam@125: err != SF_ERR_NO_ERROR, cannam@125: "\n\nLine %d : sf_get_chunk_size returned for testdata[%d] '%s' : %s\n\n", __LINE__, (int) i, testdata [i], sf_error_number (err) cannam@125: ) ; cannam@125: cannam@125: exit_if_true ( cannam@125: length_before [i] > chunk_info.datalen || chunk_info.datalen - length_before [i] > 4, cannam@125: "\n\nLine %d : testdata[%d] '%s' : Bad chunk length %u (previous length %u)\n\n", __LINE__, (int) i, testdata [i], chunk_info.datalen, length_before [i] cannam@125: ) ; cannam@125: cannam@125: chunk_info.data = malloc (chunk_info.datalen) ; cannam@125: err = sf_get_chunk_data (iterator, &chunk_info) ; cannam@125: exit_if_true ( cannam@125: err != SF_ERR_NO_ERROR, cannam@125: "\n\nLine %d : sf_get_chunk_size returned for testdata[%d] '%s' : %s\n\n", __LINE__, (int) i, testdata [i], sf_error_number (err) cannam@125: ) ; cannam@125: cannam@125: exit_if_true ( cannam@125: 4 != chunk_info.id_size, cannam@125: "\n\nLine %d : testdata[%d] : Bad ID length %u (previous length %u)\n\n", __LINE__, (int) i, chunk_info.id_size, 4 cannam@125: ) ; cannam@125: exit_if_true ( cannam@125: memcmp ("Test", chunk_info.id, 4), cannam@125: "\n\nLine %d : ID compare failed at %d.\n %s\n %s\n\n", __LINE__, (int) i, "Test", (char*) chunk_info.id cannam@125: ) ; cannam@125: cannam@125: exit_if_true ( cannam@125: memcmp (testdata [i], chunk_info.data, length_before [i]), cannam@125: "\n\nLine %d : Data compare failed at %d.\n %s\n %s\n\n", __LINE__, (int) i, testdata [i], (char*) chunk_info.data cannam@125: ) ; cannam@125: cannam@125: free (chunk_info.data) ; cannam@125: iterator = sf_next_chunk_iterator (iterator) ; cannam@125: i++ ; cannam@125: } cannam@125: cannam@125: sf_close (file) ; cannam@125: unlink (filename) ; cannam@125: } /* multichunk_test_helper */ cannam@125: cannam@125: cannam@125: static void cannam@125: chunk_test (const char *filename, int format) cannam@125: { const char* testdata [] = cannam@125: { "There can be only one.", "", "A", "AB", "ABC", "ABCD", "ABCDE" } ; cannam@125: uint32_t k ; cannam@125: cannam@125: print_test_name (__func__, filename) ; cannam@125: cannam@125: for (k = 0 ; k < ARRAY_LEN (testdata) ; k++) cannam@125: chunk_test_helper (filename, format, testdata [k]) ; cannam@125: cannam@125: multichunk_test_helper (filename, format, testdata, ARRAY_LEN (testdata)) ; cannam@125: cannam@125: puts ("ok") ; cannam@125: } /* chunk_test */