comparison src/libsndfile-1.0.27/tests/long_read_write_test.c @ 125:cd6cdf86811e

Current libsndfile source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
comparison
equal deleted inserted replaced
124:e3d5853d5918 125:cd6cdf86811e
1 /*
2 ** Copyright (C) 2015 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 "dft_cmp.h"
34 #include "utils.h"
35
36 #define BUFFER_LENGTH 10000
37 #define SAMPLE_RATE 44010
38
39 static void short_lrw_test (const char *filename, int filetype, const short * output, int out_len) ;
40 static void int_lrw_test (const char *filename, int filetype, const int * output, int out_len) ;
41 static void float_lrw_test (const char *filename, int filetype, const float * output, int out_len) ;
42 static void double_lrw_test (const char *filename, int filetype, const double * output, int out_len) ;
43
44
45 static short short_data [BUFFER_LENGTH] ;
46 static int int_data [BUFFER_LENGTH] ;
47 static float float_data [BUFFER_LENGTH] ;
48 static double double_data [BUFFER_LENGTH] ;
49
50 int
51 main (int argc, char *argv [])
52 { int do_all ;
53 size_t k ;
54
55 if (argc != 2)
56 { printf ("Usage : %s <test>\n", argv [0]) ;
57 printf (" Where <test> is one of the following:\n") ;
58 printf (" alac - test CAF/ALAC file functions\n") ;
59 printf (" all - perform all tests\n") ;
60 exit (1) ;
61 } ;
62
63 for (k = 0 ; k < ARRAY_LEN (short_data) ; k++)
64 { int value = k / 32 ;
65 int_data [k] = (value & 1 ? -1 : 1) * value ;
66 short_data [k] = int_data [k] ;
67 float_data [k] = int_data [k] / 32000.0 ;
68 double_data [k] = int_data [k] / 32000.0 ;
69 }
70
71 do_all = ! strcmp (argv [1], "all") ;
72
73 if (do_all || strcmp (argv [1], "alac") == 0)
74 { short_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_16, short_data, ARRAY_LEN (short_data)) ;
75 int_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, int_data, ARRAY_LEN (int_data)) ;
76 float_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, float_data, ARRAY_LEN (float_data)) ;
77 double_lrw_test ("alac.caf", SF_FORMAT_CAF | SF_FORMAT_ALAC_32, double_data, ARRAY_LEN (double_data)) ;
78 } ;
79
80 return 0 ;
81 } /* main */
82
83 /*============================================================================================
84 * Here are the test functions.
85 */
86
87 static void
88 short_lrw_test (const char *filename, int filetype, const short * output, int out_len)
89 { SNDFILE *file ;
90 SF_INFO sfinfo ;
91 int k ;
92 short input [out_len] ;
93
94 print_test_name ("short_lrw_test", filename) ;
95
96 sfinfo.samplerate = SAMPLE_RATE ;
97 sfinfo.frames = out_len ;
98 sfinfo.channels = 1 ;
99 sfinfo.format = filetype ;
100
101 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
102
103 test_write_short_or_die (file, 0, output, out_len, __LINE__) ;
104
105 sf_close (file) ;
106
107 memset (input, 0, sizeof (input)) ;
108
109 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
110
111 exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
112 exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
113 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
114
115 check_log_buffer_or_die (file, __LINE__) ;
116
117 test_read_short_or_die (file, 0, input, out_len, __LINE__) ;
118
119 sf_close (file) ;
120
121 for (k = 0 ; k < out_len ; k++)
122 exit_if_true (input [k] != output [k],
123 "\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
124
125 puts ("ok") ;
126 unlink (filename) ;
127
128 return ;
129 } /* short_lrw_test */
130
131 static void
132 int_lrw_test (const char *filename, int filetype, const int * output, int out_len)
133 { SNDFILE *file ;
134 SF_INFO sfinfo ;
135 int k ;
136 int input [out_len] ;
137
138 print_test_name ("int_lrw_test", filename) ;
139
140 sfinfo.samplerate = SAMPLE_RATE ;
141 sfinfo.frames = out_len ;
142 sfinfo.channels = 1 ;
143 sfinfo.format = filetype ;
144
145 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
146
147 test_write_int_or_die (file, 0, output, out_len, __LINE__) ;
148
149 sf_close (file) ;
150
151 memset (input, 0, sizeof (input)) ;
152
153 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
154
155 exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
156 exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too int). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
157 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
158
159 check_log_buffer_or_die (file, __LINE__) ;
160
161 test_read_int_or_die (file, 0, input, out_len, __LINE__) ;
162
163 sf_close (file) ;
164
165 for (k = 0 ; k < out_len ; k++)
166 exit_if_true (input [k] != output [k],
167 "\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
168
169 puts ("ok") ;
170 unlink (filename) ;
171
172 return ;
173 } /* int_lrw_test */
174
175 static void
176 float_lrw_test (const char *filename, int filetype, const float * output, int out_len)
177 { SNDFILE *file ;
178 SF_INFO sfinfo ;
179 int k ;
180 float input [out_len] ;
181
182 print_test_name ("float_lrw_test", filename) ;
183
184 sfinfo.samplerate = SAMPLE_RATE ;
185 sfinfo.frames = out_len ;
186 sfinfo.channels = 1 ;
187 sfinfo.format = filetype ;
188
189 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
190
191 test_write_float_or_die (file, 0, output, out_len, __LINE__) ;
192
193 sf_close (file) ;
194
195 memset (input, 0, sizeof (input)) ;
196
197 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
198
199 exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
200 exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too float). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
201 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
202
203 check_log_buffer_or_die (file, __LINE__) ;
204
205 test_read_float_or_die (file, 0, input, out_len, __LINE__) ;
206
207 sf_close (file) ;
208
209 for (k = 0 ; k < out_len ; k++)
210 exit_if_true (fabs (input [k] - output [k]) > 0.00001,
211 "\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
212
213 puts ("ok") ;
214 unlink (filename) ;
215
216 return ;
217 } /* float_lrw_test */
218
219 static void
220 double_lrw_test (const char *filename, int filetype, const double * output, int out_len)
221 { SNDFILE *file ;
222 SF_INFO sfinfo ;
223 int k ;
224 double input [out_len] ;
225
226 print_test_name ("double_lrw_test", filename) ;
227
228 sfinfo.samplerate = SAMPLE_RATE ;
229 sfinfo.frames = out_len ;
230 sfinfo.channels = 1 ;
231 sfinfo.format = filetype ;
232
233 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
234
235 test_write_double_or_die (file, 0, output, out_len, __LINE__) ;
236
237 sf_close (file) ;
238
239 memset (input, 0, sizeof (input)) ;
240
241 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
242
243 exit_if_true (sfinfo.format != filetype, "\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
244 exit_if_true (sfinfo.frames < out_len, "\n\nLine %d: Incorrect number of frames in file (too double). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, DFT_DATA_LENGTH) ;
245 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
246
247 check_log_buffer_or_die (file, __LINE__) ;
248
249 test_read_double_or_die (file, 0, input, out_len, __LINE__) ;
250
251 sf_close (file) ;
252
253 for (k = 0 ; k < out_len ; k++)
254 exit_if_true (fabs (input [k] - output [k]) > 0.00001,
255 "\n\nLine: %d: Error on input %d, expected %f, got %f\n", __LINE__, k, output [k], input [k]) ;
256
257 puts ("ok") ;
258 unlink (filename) ;
259
260 return ;
261 } /* double_lrw_test */
262