README.osx wasn't easily readable in Finder. Revert back to
[sox.git] / src / sox_sample_test.h
blobe4889740a872a3e6a4ef4d3ffd4fe5035acf0e29
1 /* libSoX test code copyright (c) 2006 robs@users.sourceforge.net
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the
5 * Free Software Foundation; either version 2 of the License, or (at your
6 * option) any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #ifdef NDEBUG /* Enable assert always. */
19 #undef NDEBUG /* Must undef above assert.h or other that might include it. */
20 #endif
21 #include <assert.h>
22 #include "sox.h"
24 #define TEST_UINT(bits) \
25 uint##bits = 0; \
26 sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
27 assert(sample == SOX_SAMPLE_MIN); \
28 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
29 assert(uint##bits == 0 && clips == 0); \
31 uint##bits = 1; \
32 sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
33 assert(sample > SOX_SAMPLE_MIN && sample < 0); \
34 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
35 assert(uint##bits == 1 && clips == 0); \
37 uint##bits = SOX_INT_MAX(bits); \
38 sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
39 assert(sample * SOX_INT_MAX(bits) == SOX_UNSIGNED_TO_SAMPLE(bits,1)); \
40 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
41 assert(uint##bits == SOX_INT_MAX(bits) && clips == 0); \
43 sample =SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2; \
44 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
45 assert(uint##bits == 1 && clips == 0); \
47 sample = SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2-1; \
48 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
49 assert(uint##bits == 0 && clips == 0); \
51 uint##bits = (0^SOX_INT_MIN(bits)); \
52 sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
53 assert(sample == 0); \
54 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
55 assert(uint##bits == (0^SOX_INT_MIN(bits)) && clips == 0); \
57 uint##bits = ((0^SOX_INT_MIN(bits))+1); \
58 sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
59 assert(sample > 0 && sample < SOX_SAMPLE_MAX); \
60 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
61 assert(uint##bits == ((0^SOX_INT_MIN(bits))+1) && clips == 0); \
63 uint##bits = SOX_UINT_MAX(bits); \
64 sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
65 assert(sample == SOX_INT_MAX(bits) * SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))); \
66 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
67 assert(uint##bits == SOX_UINT_MAX(bits) && clips == 0); \
69 sample =SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2-1; \
70 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
71 assert(uint##bits == SOX_UINT_MAX(bits) && clips == 0); \
73 sample = SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2; \
74 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
75 assert(uint##bits == SOX_UINT_MAX(bits) && --clips == 0); \
77 sample = SOX_SAMPLE_MAX; \
78 uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
79 assert(uint##bits == SOX_UINT_MAX(bits) && --clips == 0); \
81 #define TEST_SINT(bits) \
82 int##bits = SOX_INT_MIN(bits); \
83 sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
84 assert(sample == SOX_SAMPLE_MIN); \
85 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
86 assert(int##bits##_2 == int##bits && clips == 0); \
88 int##bits = SOX_INT_MIN(bits)+1; \
89 sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
90 assert(sample > SOX_SAMPLE_MIN && sample < 0); \
91 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
92 assert(int##bits##_2 == int##bits && clips == 0); \
94 int##bits = SOX_UINT_MAX(bits) /* i.e. -1 */; \
95 sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
96 assert(sample * SOX_INT_MAX(bits) == SOX_SIGNED_TO_SAMPLE(bits,SOX_INT_MIN(bits)+1)); \
97 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
98 assert(int##bits##_2 == int##bits && clips == 0); \
100 int##bits = SOX_INT_MIN(bits)+1; \
101 sample =SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2; \
102 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
103 assert(int##bits##_2 == int##bits && clips == 0); \
105 int##bits = SOX_INT_MIN(bits); \
106 sample = SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2-1; \
107 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
108 assert(int##bits##_2 == int##bits && clips == 0); \
110 int##bits = 0; \
111 sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
112 assert(sample == 0); \
113 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
114 assert(int##bits##_2 == int##bits && clips == 0); \
116 int##bits = 1; \
117 sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
118 assert(sample > 0 && sample < SOX_SAMPLE_MAX); \
119 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
120 assert(int##bits##_2 == int##bits && clips == 0); \
122 int##bits = SOX_INT_MAX(bits); \
123 sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
124 assert(sample == SOX_INT_MAX(bits) * SOX_SIGNED_TO_SAMPLE(bits,1)); \
125 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
126 assert(int##bits##_2 == int##bits && clips == 0); \
128 sample =SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2-1; \
129 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
130 assert(int##bits##_2 == int##bits && clips == 0); \
132 sample =SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2; \
133 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
134 assert(int##bits##_2 == int##bits && --clips == 0); \
136 sample = SOX_SAMPLE_MAX; \
137 int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
138 assert(int##bits##_2 == int##bits && --clips == 0);
140 #if defined __GNUC__
141 #pragma GCC system_header
142 #elif defined __SUNPRO_CC
143 #pragma disable_warn
144 #elif defined _MSC_VER
145 #pragma warning(push, 1)
146 #endif
148 int main()
150 int8_t int8;
151 int16_t int16;
152 int24_t int24;
154 uint8_t uint8;
155 uint16_t uint16;
156 uint24_t uint24;
158 int8_t int8_2;
159 int16_t int16_2;
160 int24_t int24_2;
162 sox_sample_t sample;
163 size_t clips = 0;
165 double d;
166 SOX_SAMPLE_LOCALS;
168 TEST_UINT(8)
169 TEST_UINT(16)
170 TEST_UINT(24)
172 TEST_SINT(8)
173 TEST_SINT(16)
174 TEST_SINT(24)
176 d = -1.000000001;
177 sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
178 assert(sample == SOX_SAMPLE_MIN && --clips == 0);
180 d = -1;
181 sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
182 assert(sample == SOX_SAMPLE_MIN && clips == 0);
183 d = SOX_SAMPLE_TO_FLOAT_64BIT(sample,clips);
184 assert(d == -1 && clips == 0);
186 d = 1;
187 sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
188 assert(sample == SOX_SAMPLE_MAX && clips == 0);
189 d = SOX_SAMPLE_TO_FLOAT_64BIT(sample,clips);
190 assert(fabs(d - 1) < 1e-9 && clips == 0);
192 d = 1.0000000001;
193 sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
194 assert(sample == SOX_SAMPLE_MAX && --clips == 0);
196 return 0;
199 #if defined __SUNPRO_CC
200 #pragma enable_warn
201 #elif defined _MSC_VER
202 #pragma warning(pop)
203 #endif