forked from libsndfile/libsamplerate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_test.c
208 lines (167 loc) · 4.58 KB
/
misc_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
** Copyright (c) 2002-2016, Erik de Castro Lopo <[email protected]>
** All rights reserved.
**
** This code is released under 2-clause BSD license. Please see the
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <samplerate.h>
#include "util.h"
static void name_test (void) ;
static void error_test (void) ;
static void src_ratio_test (void) ;
static void zero_input_test (int converter) ;
static void get_channels_test (int converter);
int
main (void)
{
puts ("") ;
printf (" version : %s\n\n", src_get_version ()) ;
/* Current max converter is SRC_LINEAR. */
name_test () ;
error_test () ;
src_ratio_test () ;
zero_input_test (SRC_ZERO_ORDER_HOLD) ;
zero_input_test (SRC_LINEAR) ;
#ifdef ENABLE_SINC_FAST_CONVERTER
zero_input_test (SRC_SINC_FASTEST) ;
#endif
get_channels_test (SRC_ZERO_ORDER_HOLD) ;
get_channels_test (SRC_LINEAR) ;
#ifdef ENABLE_SINC_FAST_CONVERTER
get_channels_test (SRC_SINC_FASTEST) ;
#endif
puts ("") ;
return 0 ;
} /* main */
static void
name_test (void)
{ const char *name ;
int k = 0 ;
puts (" name_test :") ;
while (1)
{ name = src_get_name (k) ;
if (name == NULL)
break ;
printf ("\tName %d : %s\n", k, name) ;
printf ("\tDesc %d : %s\n", k, src_get_description (k)) ;
k ++ ;
} ;
puts ("") ;
return ;
} /* name_test */
/*------------------------------------------------------------------------------
*/
typedef struct
{ double ratio ;
int should_pass ;
} RATIO_TEST ;
static RATIO_TEST ratio_test [] =
{ { 1.0 / 256.1, 0 },
{ 1.0 / 256.0, 1 },
{ 1.0, 1 },
{ 256.0, 1 },
{ 256.1, 0 },
{ -1.0, 0 }
} ;
static void
src_ratio_test (void)
{ int k ;
puts (" src_ratio_test (SRC ratio must be in range [1/256, 256]):" ) ;
for (k = 0 ; k < ARRAY_LEN (ratio_test) ; k++)
{ if (ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) == 0)
{ printf ("\n\nLine %d : SRC ratio %f should have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
exit (1) ;
} ;
if (! ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) != 0)
{ printf ("\n\nLine %d : SRC ratio %f should not have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
exit (1) ;
} ;
printf ("\t SRC ratio (%9.5f) : %s ................... ok\n", ratio_test [k].ratio,
(ratio_test [k].should_pass ? "pass" : "fail")) ;
} ;
puts ("") ;
return ;
} /* src_ratio_test */
static void
error_test (void)
{ const char *errorstr ;
int k, errors = 0 ;
puts (" error_test :") ;
for (k = 0 ; 1 ; k++)
{ errorstr = src_strerror (k) ;
printf ("\t%-2d : %s\n", k, errorstr) ;
if (errorstr == NULL)
{ errors ++ ;
continue ;
} ;
if (strstr (errorstr, "Placeholder.") == errorstr)
break ;
} ;
if (errors != 0)
{ printf ("\n\nLine %d : Missing error numbers above.\n\n", __LINE__) ;
exit (1) ;
} ;
puts ("") ;
return ;
} /* error_test */
static void
zero_input_test (int converter)
{ SRC_DATA data ;
SRC_STATE *state ;
float out [100] ;
int error ;
printf (" %s (%-26s) ........ ", __func__, src_get_name (converter)) ;
fflush (stdout) ;
if ((state = src_new (converter, 1, &error)) == NULL)
{ printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
exit (1) ;
} ;
data.data_in = (float *) (size_t) 0xdeadbeef ;
data.input_frames = 0 ;
data.data_out = out ;
data.output_frames = ARRAY_LEN (out) ;
data.end_of_input = 0 ;
data.src_ratio = 1.0 ;
if ((error = src_process (state, &data)))
{ printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
exit (1) ;
} ;
state = src_delete (state) ;
puts ("ok") ;
} /* zero_input_test */
static void get_channels_test(int converter)
{
SRC_STATE *state;
int channels_in, channels_out;
const char *errorstr;
state = NULL;
if ((channels_out = src_get_channels(state)) >= 0)
{
printf ("\n\nLine %d : Return value should be negative, was : %d.\n\n", __LINE__, channels_out) ;
exit (1) ;
}
errorstr = src_strerror(-channels_out);
if (!strstr(errorstr, "NULL"))
{
printf ("\n\nLine %d : Inverted output should be valid error code mentioning a NULL pointer, was : %s.\n\n", __LINE__, errorstr) ;
exit (1) ;
}
for (channels_in = 1; channels_in <= 8; channels_in++)
{
int error;
state = src_new(converter, channels_in, &error);
if ((channels_out = src_get_channels(state)) != channels_in)
{
printf ("\n\nLine %d : Return value should be %d, was : %d.\n\n", __LINE__, channels_in, channels_out) ;
exit (1) ;
}
state = src_delete(state);
}
}