Debian Codegraph @

tests/float_short_test.c

raw
1 /*
2 ** Copyright (c) 2003-2016, Erik de Castro Lopo <[email protected]>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <samplerate.h>
18
19 #include "util.h"
20
21 #define BUFFER_LEN 10000
22
23 static void float_to_short_test (void) ;
24 static void short_to_float_test (void) ;
25
26 static void float_to_int_test (void) ;
27 static void int_to_float_test (void) ;
28
29 int
30 main (void)
31 {
32 puts ("") ;
33
34 float_to_short_test () ;
35 short_to_float_test () ;
36
37 float_to_int_test () ;
38 int_to_float_test () ;
39
40 puts ("") ;
41
42 return 0 ;
43 } /* main */
44
45 /*=====================================================================================
46 */
47
48 static void
49 float_to_short_test (void)
50 {
51 static float fpos [] =
52 { 0.95f, 0.99f, 1.0f, 1.01f, 1.1f, 2.0f, 11.1f, 111.1f, 2222.2f, 33333.3f,
53 // Some "almost 1" as corner cases
54 (float) (32767.0 / 32768.0), (float) ((32767.0 + 0.4) / 32768.0), (float) ((32767. + 0.5) / 32768.0),
55 (float) ((32767.0 + 0.6) / 32768.0), (float) ((32767.0 + 0.9) / 32768.0),
56 } ;
57 static float fneg [] =
58 { -0.95f, -0.99f, -1.0f, -1.01f, -1.1f, -2.0f, -11.1f, -111.1f, -2222.2f, -33333.3f,
59 // Some "almost 1" as corner cases
60 (float) (-32767.0 / 32768.0), (float) (-(32767.0 + 0.4) / 32768.0), (float) (-(32767.0 + 0.5) / 32768.0),
61 (float) (-(32767.0 + 0.6) / 32768.0), (float) (-(32767.0 + 0.9) / 32768.0),
62 } ;
63
64 static short out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ;
65
66 int k ;
67
68 printf ("\tfloat_to_short_test ............................. ") ;
69
70 src_float_to_short_array (fpos, out, ARRAY_LEN (fpos)) ;
71
72 for (k = 0 ; k < ARRAY_LEN (fpos) ; k++)
73 if (out [k] < 30000)
74 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ;
75 exit (1) ;
76 } ;
77
78 src_float_to_short_array (fneg, out, ARRAY_LEN (fneg)) ;
79
80 for (k = 0 ; k < ARRAY_LEN (fneg) ; k++)
81 if (out [k] > -30000)
82 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ;
83 exit (1) ;
84 } ;
85
86 puts ("ok") ;
87
88 return ;
89 } /* float_to_short_test */
90
91 /*-------------------------------------------------------------------------------------
92 */
93
94 static void
95 short_to_float_test (void)
96 {
97 static short input [BUFFER_LEN] ;
98 static short output [BUFFER_LEN] ;
99 static float temp [BUFFER_LEN] ;
100
101 int k ;
102
103 printf ("\tshort_to_float_test ............................. ") ;
104
105 for (k = 0 ; k < ARRAY_LEN (input) ; k++)
106 input [k] = (k * 0x8000) / ARRAY_LEN (input) ;
107
108 src_short_to_float_array (input, temp, ARRAY_LEN (temp)) ;
109 src_float_to_short_array (temp, output, ARRAY_LEN (output)) ;
110
111 for (k = 0 ; k < ARRAY_LEN (input) ; k++)
112 if (ABS (input [k] - output [k]) > 0)
113 { printf ("\n\n\tLine %d : index %d %d -> %d\n", __LINE__, k, input [k], output [k]) ;
114 exit (1) ;
115 } ;
116
117 puts ("ok") ;
118
119 return ;
120 } /* short_to_float_test */
121
122 /*=====================================================================================
123 */
124
125 static void
126 float_to_int_test (void)
127 {
128 static float fpos [] =
129 { 0.95f, 0.99f, 1.0f, 1.01f, 1.1f, 2.0f, 11.1f, 111.1f, 2222.2f, 33333.3f
130 } ;
131 static float fneg [] =
132 { -0.95f, -0.99f, -1.0f, -1.01f, -1.1f, -2.0f, -11.1f, -111.1f, -2222.2f, -33333.3f
133 } ;
134
135 static int out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ;
136
137 int k ;
138
139 printf ("\tfloat_to_int_test ............................... ") ;
140
141 src_float_to_int_array (fpos, out, ARRAY_LEN (fpos)) ;
142
143 for (k = 0 ; k < ARRAY_LEN (fpos) ; k++)
144 if (out [k] < 30000 * 0x10000)
145 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ;
146 exit (1) ;
147 } ;
148
149 src_float_to_int_array (fneg, out, ARRAY_LEN (fneg)) ;
150
151 for (k = 0 ; k < ARRAY_LEN (fneg) ; k++)
152 if (out [k] > -30000 * 0x1000)
153 { printf ("\n\n\tLine %d : out [%d] == %d\n", __LINE__, k, out [k]) ;
154 exit (1) ;
155 } ;
156
157 puts ("ok") ;
158
159 return ;
160 } /* float_to_int_test */
161
162 /*-------------------------------------------------------------------------------------
163 */
164
165 static void
166 int_to_float_test (void)
167 {
168 static int input [BUFFER_LEN] ;
169 static int output [BUFFER_LEN] ;
170 static float temp [BUFFER_LEN] ;
171
172 int k ;
173
174 printf ("\tint_to_float_test ............................... ") ;
175
176 for (k = 0 ; k < ARRAY_LEN (input) ; k++)
177 input [k] = (k * 0x80000000) / ARRAY_LEN (input) ;
178
179 src_int_to_float_array (input, temp, ARRAY_LEN (temp)) ;
180 src_float_to_int_array (temp, output, ARRAY_LEN (output)) ;
181
182 for (k = 0 ; k < ARRAY_LEN (input) ; k++)
183 if (ABS (input [k] - output [k]) > 0)
184 { printf ("\n\n\tLine %d : index %d %d -> %d\n", __LINE__, k, input [k], output [k]) ;
185 exit (1) ;
186 } ;
187
188 puts ("ok") ;
189
190 return ;
191 } /* int_to_float_test */
192