Bundle libsamplerate
[audio-libsamplerate.git] / libsamplerate / tests / misc_test.c
1 /*
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <samplerate.h>
14
15 #include "util.h"
16
17 static void name_test (void) ;
18 static void error_test (void) ;
19 static void src_ratio_test (void) ;
20 static void zero_input_test (int converter) ;
21
22 int
23 main (void)
24 {
25 puts ("") ;
26
27 printf (" version : %s\n\n", src_get_version ()) ;
28
29 /* Current max converter is SRC_LINEAR. */
30 name_test () ;
31
32 error_test () ;
33
34 src_ratio_test () ;
35
36 zero_input_test (SRC_ZERO_ORDER_HOLD) ;
37 zero_input_test (SRC_LINEAR) ;
38 zero_input_test (SRC_SINC_FASTEST) ;
39
40 puts ("") ;
41 return 0 ;
42 } /* main */
43
44 static void
45 name_test (void)
46 { const char *name ;
47 int k = 0 ;
48
49 puts (" name_test :") ;
50
51 while (1)
52 { name = src_get_name (k) ;
53 if (name == NULL)
54 break ;
55 printf ("\tName %d : %s\n", k, name) ;
56 printf ("\tDesc %d : %s\n", k, src_get_description (k)) ;
57 k ++ ;
58 } ;
59
60 puts ("") ;
61
62 return ;
63 } /* name_test */
64
65 /*------------------------------------------------------------------------------
66 */
67
68 typedef struct
69 { double ratio ;
70 int should_pass ;
71 } RATIO_TEST ;
72
73 static RATIO_TEST ratio_test [] =
74 { { 1.0 / 256.1, 0 },
75 { 1.0 / 256.0, 1 },
76 { 1.0, 1 },
77 { 256.0, 1 },
78 { 256.1, 0 },
79 { -1.0, 0 }
80 } ;
81
82 static void
83 src_ratio_test (void)
84 { int k ;
85
86 puts (" src_ratio_test (SRC ratio must be in range [1/256, 256]):" ) ;
87
88
89 for (k = 0 ; k < ARRAY_LEN (ratio_test) ; k++)
90 { if (ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) == 0)
91 { printf ("\n\nLine %d : SRC ratio %f should have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
92 exit (1) ;
93 } ;
94 if (! ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) != 0)
95 { printf ("\n\nLine %d : SRC ratio %f should not have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
96 exit (1) ;
97 } ;
98 printf ("\t SRC ratio (%9.5f) : %s ................... ok\n", ratio_test [k].ratio,
99 (ratio_test [k].should_pass ? "pass" : "fail")) ;
100 } ;
101
102 puts ("") ;
103
104 return ;
105 } /* src_ratio_test */
106
107 static void
108 error_test (void)
109 { const char *errorstr ;
110 int k, errors = 0 ;
111
112 puts (" error_test :") ;
113
114 for (k = 0 ; 1 ; k++)
115 { errorstr = src_strerror (k) ;
116 printf ("\t%-2d : %s\n", k, errorstr) ;
117 if (errorstr == NULL)
118 { errors ++ ;
119 continue ;
120 } ;
121 if (strstr (errorstr, "Placeholder.") == errorstr)
122 break ;
123 } ;
124
125 if (errors != 0)
126 { printf ("\n\nLine %d : Missing error numbers above.\n\n", __LINE__) ;
127 exit (1) ;
128 } ;
129
130 puts ("") ;
131
132 return ;
133 } /* error_test */
134
135 static void
136 zero_input_test (int converter)
137 { SRC_DATA data ;
138 SRC_STATE *state ;
139 float out [100] ;
140 int error ;
141
142 printf (" %s (%-26s) ........ ", __func__, src_get_name (converter)) ;
143 fflush (stdout) ;
144
145 if ((state = src_new (converter, 1, &error)) == NULL)
146 { printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
147 exit (1) ;
148 } ;
149
150 data.data_in = (float *) 0xdeadbeef ;
151 data.input_frames = 0 ;
152 data.data_out = out ;
153 data.output_frames = ARRAY_LEN (out) ;
154 data.end_of_input = 0 ;
155 data.src_ratio = 1.0 ;
156
157 if ((error = src_process (state, &data)))
158 { printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
159 exit (1) ;
160 } ;
161
162 state = src_delete (state) ;
163
164 puts ("ok") ;
165 } /* zero_input_test */
This page took 0.025628 seconds and 4 git commands to generate.