blob: 4baa3343f05cee8f2d21157cc42cc6ae98772ea8 [file] [log] [blame]
Emeric Vigiereebea672012-08-06 17:36:30 -04001/*
2** Copyright (C) 2002-2011 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 <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <samplerate.h>
24
25#include "util.h"
26
27static void name_test (void) ;
28static void error_test (void) ;
29static void src_ratio_test (void) ;
30static void zero_input_test (int converter) ;
31
32int
33main (void)
34{
35 puts ("") ;
36
37 printf (" version : %s\n\n", src_get_version ()) ;
38
39 /* Current max converter is SRC_LINEAR. */
40 name_test () ;
41
42 error_test () ;
43
44 src_ratio_test () ;
45
46 zero_input_test (SRC_ZERO_ORDER_HOLD) ;
47 zero_input_test (SRC_LINEAR) ;
48 zero_input_test (SRC_SINC_FASTEST) ;
49
50 puts ("") ;
51 return 0 ;
52} /* main */
53
54static void
55name_test (void)
56{ const char *name ;
57 int k = 0 ;
58
59 puts (" name_test :") ;
60
61 while (1)
62 { name = src_get_name (k) ;
63 if (name == NULL)
64 break ;
65 printf ("\tName %d : %s\n", k, name) ;
66 printf ("\tDesc %d : %s\n", k, src_get_description (k)) ;
67 k ++ ;
68 } ;
69
70 puts ("") ;
71
72 return ;
73} /* name_test */
74
75/*------------------------------------------------------------------------------
76*/
77
78typedef struct
79{ double ratio ;
80 int should_pass ;
81} RATIO_TEST ;
82
83static RATIO_TEST ratio_test [] =
84{ { 1.0 / 256.1, 0 },
85 { 1.0 / 256.0, 1 },
86 { 1.0, 1 },
87 { 256.0, 1 },
88 { 256.1, 0 },
89 { -1.0, 0 }
90} ;
91
92static void
93src_ratio_test (void)
94{ int k ;
95
96 puts (" src_ratio_test (SRC ratio must be in range [1/256, 256]):" ) ;
97
98
99 for (k = 0 ; k < ARRAY_LEN (ratio_test) ; k++)
100 { if (ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) == 0)
101 { printf ("\n\nLine %d : SRC ratio %f should have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
102 exit (1) ;
103 } ;
104 if (! ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) != 0)
105 { printf ("\n\nLine %d : SRC ratio %f should not have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
106 exit (1) ;
107 } ;
108 printf ("\t SRC ratio (%9.5f) : %s ................... ok\n", ratio_test [k].ratio,
109 (ratio_test [k].should_pass ? "pass" : "fail")) ;
110 } ;
111
112 puts ("") ;
113
114 return ;
115} /* src_ratio_test */
116
117static void
118error_test (void)
119{ const char *errorstr ;
120 int k, errors = 0 ;
121
122 puts (" error_test :") ;
123
124 for (k = 0 ; 1 ; k++)
125 { errorstr = src_strerror (k) ;
126 printf ("\t%-2d : %s\n", k, errorstr) ;
127 if (errorstr == NULL)
128 { errors ++ ;
129 continue ;
130 } ;
131 if (strstr (errorstr, "Placeholder.") == errorstr)
132 break ;
133 } ;
134
135 if (errors != 0)
136 { printf ("\n\nLine %d : Missing error numbers above.\n\n", __LINE__) ;
137 exit (1) ;
138 } ;
139
140 puts ("") ;
141
142 return ;
143} /* error_test */
144
145static void
146zero_input_test (int converter)
147{ SRC_DATA data ;
148 SRC_STATE *state ;
149 float out [100] ;
150 int error ;
151
152 printf (" %s (%-26s) ........ ", __func__, src_get_name (converter)) ;
153 fflush (stdout) ;
154
155 if ((state = src_new (converter, 1, &error)) == NULL)
156 { printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
157 exit (1) ;
158 } ;
159
160 data.data_in = (float *) 0xdeadbeef ;
161 data.input_frames = 0 ;
162 data.data_out = out ;
163 data.output_frames = ARRAY_LEN (out) ;
164 data.end_of_input = 0 ;
165 data.src_ratio = 1.0 ;
166
167 if ((error = src_process (state, &data)))
168 { printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
169 exit (1) ;
170 } ;
171
172 state = src_delete (state) ;
173
174 puts ("ok") ;
175} /* zero_input_test */