annotate src/libsndfile-1.0.27/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 1df64224f5ac
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 2002-2014 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_LEN (1 << 10)
Chris@40 36 #define LOG_BUFFER_SIZE 1024
Chris@40 37
Chris@40 38 static void raw_offset_test (const char *filename, int typeminor) ;
Chris@40 39 static void bad_raw_test (void) ;
Chris@40 40
Chris@40 41 /* Force the start of this buffer to be double aligned. Sparc-solaris will
Chris@40 42 ** choke if its not.
Chris@40 43 */
Chris@40 44 static short data [BUFFER_LEN] ;
Chris@40 45
Chris@40 46 int
Chris@40 47 main (void)
Chris@40 48 {
Chris@40 49 raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ;
Chris@40 50 bad_raw_test () ;
Chris@40 51
Chris@40 52 return 0 ;
Chris@40 53 } /* main */
Chris@40 54
Chris@40 55 /*============================================================================================
Chris@40 56 ** Here are the test functions.
Chris@40 57 */
Chris@40 58
Chris@40 59 static void
Chris@40 60 raw_offset_test (const char *filename, int typeminor)
Chris@40 61 { SNDFILE *sndfile ;
Chris@40 62 SF_INFO sfinfo ;
Chris@40 63 sf_count_t start ;
Chris@40 64 int k ;
Chris@40 65
Chris@40 66 print_test_name ("raw_offset_test", filename) ;
Chris@40 67
Chris@40 68 sfinfo.samplerate = 44100 ;
Chris@40 69 sfinfo.format = SF_FORMAT_RAW | typeminor ;
Chris@40 70 sfinfo.channels = 1 ;
Chris@40 71 sfinfo.frames = 0 ;
Chris@40 72
Chris@40 73 sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 74
Chris@40 75 start = 0 ;
Chris@40 76 sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ;
Chris@40 77
Chris@40 78 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@40 79 data [k] = k ;
Chris@40 80 test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
Chris@40 81
Chris@40 82 sf_close (sndfile) ;
Chris@40 83
Chris@40 84 sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
Chris@40 85 check_log_buffer_or_die (sndfile, __LINE__) ;
Chris@40 86
Chris@40 87 if (ABS (BUFFER_LEN - sfinfo.frames) > 1)
Chris@40 88 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, BUFFER_LEN) ;
Chris@40 89 dump_log_buffer (sndfile) ;
Chris@40 90 exit (1) ;
Chris@40 91 } ;
Chris@40 92
Chris@40 93 memset (data, 0 , sizeof (data)) ;
Chris@40 94 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
Chris@40 95 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@40 96 if (data [k] != k)
Chris@40 97 printf ("Error : line %d\n", __LINE__) ;
Chris@40 98
Chris@40 99 /* Set dataoffset to 2 bytes from beginning of file. */
Chris@40 100 start = 2 ;
Chris@40 101 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
Chris@40 102
Chris@40 103 /* Seek to new start */
Chris@40 104 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
Chris@40 105
Chris@40 106 memset (data, 0 , sizeof (data)) ;
Chris@40 107 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ;
Chris@40 108 for (k = 0 ; k < BUFFER_LEN - 1 ; k++)
Chris@40 109 if (data [k] != k + 1)
Chris@40 110 { printf ("Error : line %d\n", __LINE__) ;
Chris@40 111 exit (1) ;
Chris@40 112 } ;
Chris@40 113
Chris@40 114 /* Set dataoffset to 4 bytes from beginning of file. */
Chris@40 115 start = 4 ;
Chris@40 116 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
Chris@40 117
Chris@40 118 /* Seek to new start */
Chris@40 119 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
Chris@40 120
Chris@40 121 memset (data, 0 , sizeof (data)) ;
Chris@40 122 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ;
Chris@40 123 for (k = 0 ; k < BUFFER_LEN - 2 ; k++)
Chris@40 124 if (data [k] != k + 2)
Chris@40 125 { printf ("Error : line %d\n", __LINE__) ;
Chris@40 126 exit (1) ;
Chris@40 127 } ;
Chris@40 128
Chris@40 129 /* Set dataoffset back to 0 bytes from beginning of file. */
Chris@40 130 start = 0 ;
Chris@40 131 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
Chris@40 132
Chris@40 133 /* Seek to new start */
Chris@40 134 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
Chris@40 135
Chris@40 136 memset (data, 0 , sizeof (data)) ;
Chris@40 137 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
Chris@40 138 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@40 139 if (data [k] != k)
Chris@40 140 { printf ("Error : line %d\n", __LINE__) ;
Chris@40 141 exit (1) ;
Chris@40 142 } ;
Chris@40 143
Chris@40 144 sf_close (sndfile) ;
Chris@40 145 unlink (filename) ;
Chris@40 146
Chris@40 147 puts ("ok") ;
Chris@40 148 } /* raw_offset_test */
Chris@40 149
Chris@40 150 static void
Chris@40 151 bad_raw_test (void)
Chris@40 152 { FILE *textfile ;
Chris@40 153 SNDFILE *file ;
Chris@40 154 SF_INFO sfinfo ;
Chris@40 155 const char *errorstr, *filename = "bad.raw" ;
Chris@40 156
Chris@40 157 print_test_name ("bad_raw_test", filename) ;
Chris@40 158
Chris@40 159 if ((textfile = fopen (filename, "w")) == NULL)
Chris@40 160 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
Chris@40 161 exit (1) ;
Chris@40 162 } ;
Chris@40 163
Chris@40 164 fprintf (textfile, "This is not a valid file.\n") ;
Chris@40 165 fclose (textfile) ;
Chris@40 166
Chris@40 167 sfinfo.samplerate = 44100 ;
Chris@40 168 sfinfo.format = SF_FORMAT_RAW | 0xABCD ;
Chris@40 169 sfinfo.channels = 1 ;
Chris@40 170
Chris@40 171 if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL)
Chris@40 172 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
Chris@40 173 exit (1) ;
Chris@40 174 } ;
Chris@40 175
Chris@40 176 errorstr = sf_strerror (file) ;
Chris@40 177
Chris@40 178 if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL)
Chris@40 179 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
Chris@40 180 exit (1) ;
Chris@40 181 } ;
Chris@40 182
Chris@40 183 unlink (filename) ;
Chris@40 184
Chris@40 185 puts ("ok") ;
Chris@40 186 } /* bad_raw_test */
Chris@40 187