annotate src/libsndfile-1.0.27/tests/headerless_test.c @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** This program is free software; you can redistribute it and/or modify
Chris@40 5 ** it under the terms of the GNU General Public License as published by
Chris@40 6 ** the Free Software Foundation; either version 2 of the License, or
Chris@40 7 ** (at your option) any later version.
Chris@40 8 **
Chris@40 9 ** This program is distributed in the hope that it will be useful,
Chris@40 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@40 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@40 12 ** GNU General Public License for more details.
Chris@40 13 **
Chris@40 14 ** You should have received a copy of the GNU General Public License
Chris@40 15 ** along with this program; if not, write to the Free Software
Chris@40 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@40 17 */
Chris@40 18
Chris@40 19 #include "sfconfig.h"
Chris@40 20
Chris@40 21 #include <stdio.h>
Chris@40 22 #include <stdlib.h>
Chris@40 23 #include <string.h>
Chris@40 24 #include <math.h>
Chris@40 25 #include <inttypes.h>
Chris@40 26
Chris@40 27 #if HAVE_UNISTD_H
Chris@40 28 #include <unistd.h>
Chris@40 29 #endif
Chris@40 30
Chris@40 31 #include <sndfile.h>
Chris@40 32
Chris@40 33 #include "utils.h"
Chris@40 34
Chris@40 35 #define BUFFER_SIZE (2000)
Chris@40 36
Chris@40 37 static void old_test (void) ;
Chris@40 38 static void headerless_test (const char * filename, int format, int expected) ;
Chris@40 39
Chris@40 40 int
Chris@40 41 main (void)
Chris@40 42 {
Chris@40 43 old_test () ;
Chris@40 44
Chris@40 45 headerless_test ("raw.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ;
Chris@40 46 headerless_test ("raw.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ;
Chris@40 47
Chris@40 48 headerless_test ("raw.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
Chris@40 49 headerless_test ("raw.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
Chris@40 50
Chris@40 51 return 0 ;
Chris@40 52 } /* main */
Chris@40 53
Chris@40 54 static void
Chris@40 55 headerless_test (const char * filename, int format, int expected)
Chris@40 56 { static short buffer [BUFFER_SIZE] ;
Chris@40 57 SNDFILE *file ;
Chris@40 58 SF_INFO sfinfo ;
Chris@40 59 int k ;
Chris@40 60
Chris@40 61 format &= SF_FORMAT_SUBMASK ;
Chris@40 62
Chris@40 63 print_test_name (__func__, filename) ;
Chris@40 64
Chris@40 65 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 66 buffer [k] = k ;
Chris@40 67
Chris@40 68 sfinfo.samplerate = 8000 ;
Chris@40 69 sfinfo.frames = 0 ;
Chris@40 70 sfinfo.channels = 1 ;
Chris@40 71 sfinfo.format = SF_FORMAT_RAW | format ;
Chris@40 72
Chris@40 73 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 74
Chris@40 75 if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
Chris@40 76 { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
Chris@40 77 fflush (stdout) ;
Chris@40 78 puts (sf_strerror (file)) ;
Chris@40 79 exit (1) ;
Chris@40 80 } ;
Chris@40 81
Chris@40 82 sf_close (file) ;
Chris@40 83
Chris@40 84 memset (buffer, 0, sizeof (buffer)) ;
Chris@40 85
Chris@40 86 /* We should be able to detect these so clear sfinfo. */
Chris@40 87 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 88
Chris@40 89 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 90
Chris@40 91 if (sfinfo.format != expected)
Chris@40 92 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ;
Chris@40 93 exit (1) ;
Chris@40 94 } ;
Chris@40 95
Chris@40 96 if (sfinfo.frames < BUFFER_SIZE)
Chris@40 97 { printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 98 exit (1) ;
Chris@40 99 } ;
Chris@40 100
Chris@40 101 if (sfinfo.channels != 1)
Chris@40 102 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
Chris@40 103 exit (1) ;
Chris@40 104 } ;
Chris@40 105
Chris@40 106 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 107
Chris@40 108 sf_close (file) ;
Chris@40 109
Chris@40 110 printf ("ok\n") ;
Chris@40 111 unlink (filename) ;
Chris@40 112 } /* headerless_test */
Chris@40 113
Chris@40 114 static void
Chris@40 115 old_test (void)
Chris@40 116 { static short buffer [BUFFER_SIZE] ;
Chris@40 117 SNDFILE *file ;
Chris@40 118 SF_INFO sfinfo ;
Chris@40 119 int k, filetype ;
Chris@40 120 const char *filename = "headerless.wav" ;
Chris@40 121
Chris@40 122 print_test_name (__func__, "") ;
Chris@40 123
Chris@40 124 for (k = 0 ; k < BUFFER_SIZE ; k++)
Chris@40 125 buffer [k] = k ;
Chris@40 126
Chris@40 127 filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
Chris@40 128
Chris@40 129 sfinfo.samplerate = 32000 ;
Chris@40 130 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
Chris@40 131 sfinfo.channels = 1 ;
Chris@40 132 sfinfo.format = filetype ;
Chris@40 133
Chris@40 134 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 135
Chris@40 136 if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
Chris@40 137 { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
Chris@40 138 fflush (stdout) ;
Chris@40 139 puts (sf_strerror (file)) ;
Chris@40 140 exit (1) ;
Chris@40 141 } ;
Chris@40 142
Chris@40 143 sf_close (file) ;
Chris@40 144
Chris@40 145 memset (buffer, 0, sizeof (buffer)) ;
Chris@40 146
Chris@40 147 /* Read as RAW but get the bit width and endian-ness correct. */
Chris@40 148 sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
Chris@40 149
Chris@40 150 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 151
Chris@40 152 if (sfinfo.format != filetype)
Chris@40 153 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
Chris@40 154 exit (1) ;
Chris@40 155 } ;
Chris@40 156
Chris@40 157 if (sfinfo.frames < BUFFER_SIZE)
Chris@40 158 { printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
Chris@40 159 exit (1) ;
Chris@40 160 } ;
Chris@40 161
Chris@40 162 if (sfinfo.channels != 1)
Chris@40 163 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
Chris@40 164 exit (1) ;
Chris@40 165 } ;
Chris@40 166
Chris@40 167 check_log_buffer_or_die (file, __LINE__) ;
Chris@40 168
Chris@40 169 if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
Chris@40 170 { printf ("Line %d: short read (%d).\n", __LINE__, k) ;
Chris@40 171 exit (1) ;
Chris@40 172 } ;
Chris@40 173
Chris@40 174 for (k = 0 ; k < BUFFER_SIZE - 22 ; k++)
Chris@40 175 if (buffer [k + 22] != k)
Chris@40 176 { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ;
Chris@40 177 exit (1) ;
Chris@40 178 } ;
Chris@40 179
Chris@40 180 sf_close (file) ;
Chris@40 181
Chris@40 182 printf ("ok\n") ;
Chris@40 183 unlink (filename) ;
Chris@40 184 } /* old_test */
Chris@40 185