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 "sox.h"
23 
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); \
30  \
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); \
36  \
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); \
42  \
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); \
46  \
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); \
50  \
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); \
56  \
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); \
62  \
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); \
68  \
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); \
72  \
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); \
76  \
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); \
80 
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); \
87  \
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); \
93  \
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); \
99  \
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); \
104  \
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); \
109  \
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); \
115  \
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); \
121  \
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); \
127  \
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); \
131  \
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); \
135  \
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);
139 
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
147 
main()148 int main()
149 {
150   sox_int8_t int8;
151   sox_int16_t int16;
152   sox_int24_t int24;
153 
154   sox_uint8_t uint8;
155   sox_uint16_t uint16;
156   sox_uint24_t uint24;
157 
158   sox_int8_t int8_2;
159   sox_int16_t int16_2;
160   sox_int24_t int24_2;
161 
162   sox_sample_t sample;
163   size_t clips = 0;
164 
165   double d;
166   SOX_SAMPLE_LOCALS;
167 
168   TEST_UINT(8)
169   TEST_UINT(16)
170   TEST_UINT(24)
171 
172   TEST_SINT(8)
173   TEST_SINT(16)
174   TEST_SINT(24)
175 
176   d = -1.000000001;
177   sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
178   assert(sample == SOX_SAMPLE_MIN && --clips == 0);
179 
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);
185 
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);
191 
192   d = 1.0000000001;
193   sample = SOX_FLOAT_64BIT_TO_SAMPLE(d, clips);
194   assert(sample == SOX_SAMPLE_MAX && --clips == 0);
195 
196   return 0;
197 }
198 
199 #if defined __SUNPRO_CC
200   #pragma enable_warn
201 #elif defined _MSC_VER
202   #pragma warning(pop)
203 #endif
204