1 /* libSoX test code    copyright (c) 2006 robs@users.sourceforge.net
2  *
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.
7  *
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.
12  *
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.
16  */
17 
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 <math.h>
23 #include "sox.h"
24 
25 #define TEST_UINT(bits) \
26   uint##bits = 0; \
27   sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
28   assert(sample == SOX_SAMPLE_MIN); \
29   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
30   assert(uint##bits == 0 && clips == 0); \
31  \
32   uint##bits = 1; \
33   sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
34   assert(sample > SOX_SAMPLE_MIN && sample < 0); \
35   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
36   assert(uint##bits == 1 && clips == 0); \
37  \
38   uint##bits = SOX_INT_MAX(bits); \
39   sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
40   assert(sample * SOX_INT_MAX(bits) == SOX_UNSIGNED_TO_SAMPLE(bits,1)); \
41   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
42   assert(uint##bits == SOX_INT_MAX(bits) && clips == 0); \
43  \
44   sample =SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2; \
45   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
46   assert(uint##bits == 1 && clips == 0); \
47  \
48   sample = SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2-1; \
49   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
50   assert(uint##bits == 0 && clips == 0); \
51  \
52   uint##bits = (0^SOX_INT_MIN(bits)); \
53   sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
54   assert(sample == 0); \
55   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
56   assert(uint##bits == (0^SOX_INT_MIN(bits)) && clips == 0); \
57  \
58   uint##bits = ((0^SOX_INT_MIN(bits))+1); \
59   sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
60   assert(sample > 0 && sample < SOX_SAMPLE_MAX); \
61   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
62   assert(uint##bits == ((0^SOX_INT_MIN(bits))+1) && clips == 0); \
63  \
64   uint##bits = SOX_UINT_MAX(bits); \
65   sample = SOX_UNSIGNED_TO_SAMPLE(bits,uint##bits); \
66   assert(sample == SOX_INT_MAX(bits) * SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))); \
67   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
68   assert(uint##bits == SOX_UINT_MAX(bits) && clips == 0); \
69  \
70   sample =SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2-1; \
71   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
72   assert(uint##bits == SOX_UINT_MAX(bits) && clips == 0); \
73  \
74   sample = SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2; \
75   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
76   assert(uint##bits == SOX_UINT_MAX(bits) && --clips == 0); \
77  \
78   sample = SOX_SAMPLE_MAX; \
79   uint##bits = SOX_SAMPLE_TO_UNSIGNED(bits,sample, clips); \
80   assert(uint##bits == SOX_UINT_MAX(bits) && --clips == 0); \
81 
82 #define TEST_SINT(bits) \
83   int##bits = SOX_INT_MIN(bits); \
84   sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
85   assert(sample == SOX_SAMPLE_MIN); \
86   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
87   assert(int##bits##_2 == int##bits && clips == 0); \
88  \
89   int##bits = SOX_INT_MIN(bits)+1; \
90   sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
91   assert(sample > SOX_SAMPLE_MIN && sample < 0); \
92   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
93   assert(int##bits##_2 == int##bits && clips == 0); \
94  \
95   int##bits = SOX_UINT_MAX(bits) /* i.e. -1 */; \
96   sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
97   assert(sample * SOX_INT_MAX(bits) == SOX_SIGNED_TO_SAMPLE(bits,SOX_INT_MIN(bits)+1)); \
98   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
99   assert(int##bits##_2 == int##bits && clips == 0); \
100  \
101   int##bits = SOX_INT_MIN(bits)+1; \
102   sample =SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2; \
103   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
104   assert(int##bits##_2 == int##bits && clips == 0); \
105  \
106   int##bits = SOX_INT_MIN(bits); \
107   sample = SOX_UNSIGNED_TO_SAMPLE(bits,1)+SOX_UNSIGNED_TO_SAMPLE(bits,SOX_INT_MAX(bits))/2-1; \
108   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
109   assert(int##bits##_2 == int##bits && clips == 0); \
110  \
111   int##bits = 0; \
112   sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
113   assert(sample == 0); \
114   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
115   assert(int##bits##_2 == int##bits && clips == 0); \
116  \
117   int##bits = 1; \
118   sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
119   assert(sample > 0 && sample < SOX_SAMPLE_MAX); \
120   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
121   assert(int##bits##_2 == int##bits && clips == 0); \
122  \
123   int##bits = SOX_INT_MAX(bits); \
124   sample = SOX_SIGNED_TO_SAMPLE(bits,int##bits); \
125   assert(sample == SOX_INT_MAX(bits) * SOX_SIGNED_TO_SAMPLE(bits,1)); \
126   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
127   assert(int##bits##_2 == int##bits && clips == 0); \
128  \
129   sample =SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2-1; \
130   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
131   assert(int##bits##_2 == int##bits && clips == 0); \
132  \
133   sample =SOX_UNSIGNED_TO_SAMPLE(bits,SOX_UINT_MAX(bits))+SOX_UNSIGNED_TO_SAMPLE(bits,((0^SOX_INT_MIN(bits))+1))/2; \
134   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
135   assert(int##bits##_2 == int##bits && --clips == 0); \
136  \
137   sample = SOX_SAMPLE_MAX; \
138   int##bits##_2 = SOX_SAMPLE_TO_SIGNED(bits,sample, clips); \
139   assert(int##bits##_2 == int##bits && --clips == 0);
140 
main(void)141 int main(void)
142 {
143   sox_int8_t int8;
144   sox_int16_t int16;
145   sox_int24_t int24;
146 
147   sox_uint8_t uint8;
148   sox_uint16_t uint16;
149   sox_uint24_t uint24;
150 
151   sox_int8_t int8_2;
152   sox_int16_t int16_2;
153   sox_int24_t int24_2;
154 
155   sox_sample_t sample;
156   size_t clips = 0;
157 
158   double d;
159   SOX_SAMPLE_LOCALS;
160 
161   TEST_UINT(8)
162   TEST_UINT(16)
163   TEST_UINT(24)
164 
165   TEST_SINT(8)
166   TEST_SINT(16)
167   TEST_SINT(24)
168 
169   d = -1.000000001;
170   sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
171   assert(sample == SOX_SAMPLE_MIN && --clips == 0);
172 
173   d = -1;
174   sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
175   assert(sample == SOX_SAMPLE_MIN && clips == 0);
176   d = SOX_SAMPLE_TO_FLOAT_64BIT(sample,clips);
177   assert(d == -1 && clips == 0);
178 
179   d = 1;
180   sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
181   assert(sample == SOX_SAMPLE_MAX && clips == 0);
182   d = SOX_SAMPLE_TO_FLOAT_64BIT(sample,clips);
183   assert(fabs(d - 1) < 1e-9 && clips == 0);
184 
185   d = 1.0000000001;
186   sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
187   assert(sample == SOX_SAMPLE_MAX && --clips == 0);
188 
189   return 0;
190 }
191