comparison src/libsndfile-1.0.27/tests/raw_test.c @ 40:1df64224f5ac

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