-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdc_test.c
361 lines (307 loc) · 7.76 KB
/
mdc_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*-
* functiontest.c
* Functional test for mdc_decode and mdc_encode
*
* 24 May 2011 - Initial implementation
*
* Author: Matthew Kaufman ([email protected])
*
* Copyright (c) 2011 Matthew Kaufman All rights reserved.
*
* This file is part of Matthew Kaufman's MDC Encoder/Decoder Library
*
* The MDC Encoder/Decoder Library is free software; you can
* redistribute it and/or modify it under the terms of version 2 of
* the GNU General Public License as published by the Free Software
* Foundation.
*
* If you cannot comply with the terms of this license, contact
* the author for alternative license arrangements or do not use
* or redistribute this software.
*
* The MDC Encoder/Decoder Library is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA.
*
* or see http://www.gnu.org/copyleft/gpl.html
*
-*/
#include <stdlib.h>
#include <stdio.h>
#include "mdc_encode.h"
#include "mdc_decode.h"
void run(mdc_encoder_t *encoder, mdc_decoder_t *decoder, int expect);
void testCallback(int numFrames, unsigned char op, unsigned char arg, unsigned short unitID, unsigned char extra0, unsigned char extra1, unsigned char extra2, unsigned char extra3, void *context);
int main()
{
mdc_encoder_t *encoder;
mdc_decoder_t *decoder;
int rv;
encoder = mdc_encoder_new(16000);
decoder = mdc_decoder_new(16000);
if(!decoder)
{
fprintf(stderr,"mdc_decoder_new() failed\n");
exit(-1);
}
if(!encoder)
{
fprintf(stderr,"mdc_encoder_new() failed\n");
exit(-1);
}
rv = mdc_encoder_set_packet(encoder, 0x12, 0x34, 0x5678);
if(rv)
{
fprintf(stderr,"mdc_encoder_set_packet() failed\n");
exit(-1);
}
run(encoder, decoder, 1);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
if(rv)
{
fprintf(stderr,"mdc_encoder_set_packet() failed\n");
exit(-1);
}
printf("1: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("2: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("3: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("4: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("5: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("6: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("7: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("8: ");
run(encoder, decoder, 2);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
printf("9: ");
run(encoder, decoder, 2);
/* additional tests for callback mode */
rv = mdc_decoder_set_callback(decoder, testCallback, (void*) 0x555);
rv = mdc_encoder_set_packet(encoder, 0x12, 0x34, 0x5678);
if(rv)
{
fprintf(stderr,"mdc_encoder_set_packet() failed\n");
exit(-1);
}
run(encoder, decoder, -1);
rv = mdc_encoder_set_double_packet(encoder, 0x55, 0x34, 0x5678, 0x0a, 0x0b, 0x0c, 0x0d);
if(rv)
{
fprintf(stderr,"mdc_encoder_set_packet() failed\n");
exit(-1);
}
run(encoder, decoder, -2);
fprintf(stderr,"mdc functional test overall success\n");
exit(0);
}
int callbackFound;
#define NUMSAMPLES 1024
void run(mdc_encoder_t *encoder, mdc_decoder_t *decoder, int expect)
{
int rv, rv2, rv3;
int cont = 10;
int msgFound = 0;
callbackFound = 0;
while(cont)
{
mdc_sample_t buffer[NUMSAMPLES];
rv = mdc_encoder_get_samples(encoder, buffer, NUMSAMPLES);
if(rv < 0)
{
fprintf(stderr,"mdc_encoder_get_samples() failed\n");
exit(-1);
}
else if(rv == 0)
{
int i;
--cont;
for(i = 0; i<NUMSAMPLES; i++)
buffer[i] = 0;
rv = NUMSAMPLES;
}
rv2 = mdc_decoder_process_samples(decoder, buffer, rv);
if(rv2 < 0)
{
fprintf(stderr,"mdc_decoder_process_samples() failed\n");
exit(-1);
}
else if(rv2 == 0)
{
if(callbackFound != 0)
{
if(callbackFound == expect)
{
return;
}
else
{
fprintf(stderr,"callback found %d but expected %d\n", callbackFound, expect);
exit(-1);
}
}
// else continue
}
else if(rv2 == 1 || rv2 == 2)
{
unsigned char op;
unsigned char arg;
unsigned short unitID;
unsigned char extra0, extra1, extra2, extra3;
msgFound = 1;
if(rv2 != expect)
{
fprintf(stderr,"process samples returned %d but expected %d\n",rv2,expect);
exit(-1);
}
if(rv2 == 1)
rv3 = mdc_decoder_get_packet(decoder, &op, &arg, &unitID);
else
rv3 = mdc_decoder_get_double_packet(decoder, &op, &arg, &unitID, &extra0, &extra1, &extra2, &extra3);
if(rv3 < 0)
{
fprintf(stderr,"%s failed\n", rv2 == 1 ? "mdc_decoder_get_packet()" : "mdc_decoder_get_double_packet()");
exit(-1);
}
if(op != (expect == 1 ? 0x12 : 0x55))
{
fprintf(stderr,"op doesn't match\n");
exit(-1);
}
if(arg != 0x34)
{
fprintf(stderr,"arg doesn't match\n");
exit(-1);
}
if(unitID != 0x5678)
{
fprintf(stderr,"unitID doesn't match\n");
exit(-1);
}
if(rv2 == 1)
printf("single decode success\n");
else
{
if(extra0 != 0x0a)
{
fprintf(stderr,"extra0 doesn't match\n");
exit(-1);
}
if(extra1 != 0x0b)
{
fprintf(stderr,"extra1 doesn't match\n");
exit(-1);
}
if(extra2 != 0x0c)
{
fprintf(stderr,"extra2 doesn't match\n");
exit(-1);
}
if(extra3 != 0x0d)
{
fprintf(stderr,"extra3 doesn't match\n");
exit(-1);
}
printf("double decode success\n");
}
}
else
{
fprintf(stderr,"mdc_decoder_process_samples() invalid return value\n");
exit(-1);
}
} // while
if(!msgFound && !callbackFound)
{
fprintf(stderr,"warning: no packet found\n");
}
}
void testCallback(int numFrames, unsigned char op, unsigned char arg, unsigned short unitID, unsigned char extra0, unsigned char extra1, unsigned char extra2, unsigned char extra3, void *context)
{
if(context != (void *)0x555)
{
fprintf(stderr,"context invalid\n");
exit(-1);
}
if(numFrames == 1)
{
callbackFound = -1;
}
else if(numFrames == 2)
{
callbackFound = -2;
}
else
{
fprintf(stderr,"numFrames invalid (callback)\n");
exit(-1);
}
if(op == 0x12 && callbackFound == -1)
{
// good
}
else if(op == 0x55 && callbackFound == -2)
{
// good
}
else
{
fprintf(stderr,"op doesn't match expected (callback)\n");
exit(-1);
}
if(arg != 0x34)
{
fprintf(stderr,"arg doesn't match (callback)\n");
exit(-1);
}
if(unitID != 0x5678)
{
fprintf(stderr,"unitID doesn't match (callback)\n");
exit(-1);
}
if(callbackFound == -1)
printf("single decode success (callback)\n");
else if(callbackFound == -2)
{
if(extra0 != 0x0a)
{
fprintf(stderr,"extra0 doesn't match (callback)\n");
exit(-1);
}
if(extra1 != 0x0b)
{
fprintf(stderr,"extra1 doesn't match (callback)\n");
exit(-1);
}
if(extra2 != 0x0c)
{
fprintf(stderr,"extra2 doesn't match (callback)\n");
exit(-1);
}
if(extra3 != 0x0d)
{
fprintf(stderr,"extra3 doesn't match (callback)\n");
exit(-1);
}
printf("double decode success (callback)\n");
}
// no else required, would have already failed above
}