annotate src/libsndfile-1.0.25/tests/raw_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 c7265573341e
children
rev   line source
Chris@0 1 /*
Chris@0 2 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@0 3 **
Chris@0 4 ** This program is free software; you can redistribute it and/or modify
Chris@0 5 ** it under the terms of the GNU General Public License as published by
Chris@0 6 ** the Free Software Foundation; either version 2 of the License, or
Chris@0 7 ** (at your option) any later version.
Chris@0 8 **
Chris@0 9 ** This program is distributed in the hope that it will be useful,
Chris@0 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 ** GNU General Public License for more details.
Chris@0 13 **
Chris@0 14 ** You should have received a copy of the GNU General Public License
Chris@0 15 ** along with this program; if not, write to the Free Software
Chris@0 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@0 17 */
Chris@0 18
Chris@0 19 #include "sfconfig.h"
Chris@0 20
Chris@0 21 #include <stdio.h>
Chris@0 22 #include <stdlib.h>
Chris@0 23 #include <string.h>
Chris@0 24 #include <math.h>
Chris@0 25
Chris@0 26 #if HAVE_UNISTD_H
Chris@0 27 #include <unistd.h>
Chris@0 28 #endif
Chris@0 29
Chris@0 30 #include <sndfile.h>
Chris@0 31
Chris@0 32 #include "utils.h"
Chris@0 33
Chris@0 34 #define BUFFER_LEN (1<<10)
Chris@0 35 #define LOG_BUFFER_SIZE 1024
Chris@0 36
Chris@0 37 static void raw_offset_test (const char *filename, int typeminor) ;
Chris@0 38 static void bad_raw_test (void) ;
Chris@0 39
Chris@0 40 /* Force the start of this buffer to be double aligned. Sparc-solaris will
Chris@0 41 ** choke if its not.
Chris@0 42 */
Chris@0 43 static short data [BUFFER_LEN] ;
Chris@0 44
Chris@0 45 int
Chris@0 46 main (void)
Chris@0 47 {
Chris@0 48 raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ;
Chris@0 49 bad_raw_test () ;
Chris@0 50
Chris@0 51 return 0 ;
Chris@0 52 } /* main */
Chris@0 53
Chris@0 54 /*============================================================================================
Chris@0 55 ** Here are the test functions.
Chris@0 56 */
Chris@0 57
Chris@0 58 static void
Chris@0 59 raw_offset_test (const char *filename, int typeminor)
Chris@0 60 { SNDFILE *sndfile ;
Chris@0 61 SF_INFO sfinfo ;
Chris@0 62 sf_count_t start ;
Chris@0 63 int k ;
Chris@0 64
Chris@0 65 print_test_name ("raw_offset_test", filename) ;
Chris@0 66
Chris@0 67 sfinfo.samplerate = 44100 ;
Chris@0 68 sfinfo.format = SF_FORMAT_RAW | typeminor ;
Chris@0 69 sfinfo.channels = 1 ;
Chris@0 70 sfinfo.frames = 0 ;
Chris@0 71
Chris@0 72 sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
Chris@0 73
Chris@0 74 start = 0 ;
Chris@0 75 sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ;
Chris@0 76
Chris@0 77 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@0 78 data [k] = k ;
Chris@0 79 test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
Chris@0 80
Chris@0 81 sf_close (sndfile) ;
Chris@0 82
Chris@0 83 sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@0 84 check_log_buffer_or_die (sndfile, __LINE__) ;
Chris@0 85
Chris@0 86 if (abs (BUFFER_LEN - sfinfo.frames) > 1)
Chris@0 87 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ;
Chris@0 88 dump_log_buffer (sndfile) ;
Chris@0 89 exit (1) ;
Chris@0 90 } ;
Chris@0 91
Chris@0 92 memset (data, 0 , sizeof (data)) ;
Chris@0 93 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
Chris@0 94 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@0 95 if (data [k] != k)
Chris@0 96 printf ("Error : line %d\n", __LINE__) ;
Chris@0 97
Chris@0 98 /* Set dataoffset to 2 bytes from beginning of file. */
Chris@0 99 start = 2 ;
Chris@0 100 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
Chris@0 101
Chris@0 102 /* Seek to new start */
Chris@0 103 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
Chris@0 104
Chris@0 105 memset (data, 0 , sizeof (data)) ;
Chris@0 106 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ;
Chris@0 107 for (k = 0 ; k < BUFFER_LEN - 1 ; k++)
Chris@0 108 if (data [k] != k + 1)
Chris@0 109 { printf ("Error : line %d\n", __LINE__) ;
Chris@0 110 exit (1) ;
Chris@0 111 } ;
Chris@0 112
Chris@0 113 /* Set dataoffset to 4 bytes from beginning of file. */
Chris@0 114 start = 4 ;
Chris@0 115 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
Chris@0 116
Chris@0 117 /* Seek to new start */
Chris@0 118 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
Chris@0 119
Chris@0 120 memset (data, 0 , sizeof (data)) ;
Chris@0 121 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ;
Chris@0 122 for (k = 0 ; k < BUFFER_LEN - 2 ; k++)
Chris@0 123 if (data [k] != k + 2)
Chris@0 124 { printf ("Error : line %d\n", __LINE__) ;
Chris@0 125 exit (1) ;
Chris@0 126 } ;
Chris@0 127
Chris@0 128 /* Set dataoffset back to 0 bytes from beginning of file. */
Chris@0 129 start = 0 ;
Chris@0 130 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
Chris@0 131
Chris@0 132 /* Seek to new start */
Chris@0 133 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
Chris@0 134
Chris@0 135 memset (data, 0 , sizeof (data)) ;
Chris@0 136 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
Chris@0 137 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@0 138 if (data [k] != k)
Chris@0 139 { printf ("Error : line %d\n", __LINE__) ;
Chris@0 140 exit (1) ;
Chris@0 141 } ;
Chris@0 142
Chris@0 143 sf_close (sndfile) ;
Chris@0 144 unlink (filename) ;
Chris@0 145
Chris@0 146 puts ("ok") ;
Chris@0 147 } /* raw_offset_test */
Chris@0 148
Chris@0 149 static void
Chris@0 150 bad_raw_test (void)
Chris@0 151 { FILE *textfile ;
Chris@0 152 SNDFILE *file ;
Chris@0 153 SF_INFO sfinfo ;
Chris@0 154 const char *errorstr, *filename = "bad.raw" ;
Chris@0 155
Chris@0 156 print_test_name ("bad_raw_test", filename) ;
Chris@0 157
Chris@0 158 if ((textfile = fopen (filename, "w")) == NULL)
Chris@0 159 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
Chris@0 160 exit (1) ;
Chris@0 161 } ;
Chris@0 162
Chris@0 163 fprintf (textfile, "This is not a valid file.\n") ;
Chris@0 164 fclose (textfile) ;
Chris@0 165
Chris@0 166 sfinfo.samplerate = 44100 ;
Chris@0 167 sfinfo.format = SF_FORMAT_RAW | 0xABCD ;
Chris@0 168 sfinfo.channels = 1 ;
Chris@0 169
Chris@0 170 if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL)
Chris@0 171 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
Chris@0 172 exit (1) ;
Chris@0 173 } ;
Chris@0 174
Chris@0 175 errorstr = sf_strerror (file) ;
Chris@0 176
Chris@0 177 if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL)
Chris@0 178 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
Chris@0 179 exit (1) ;
Chris@0 180 } ;
Chris@0 181
Chris@0 182 unlink (filename) ;
Chris@0 183
Chris@0 184 puts ("ok") ;
Chris@0 185 } /* bad_raw_test */
Chris@0 186