Adds 3rd clause to CELT license
[opus.git] / tests / test_opus_api.c
1 /* Copyright (c) 2011 Xiph.Org Foundation
2    Written by Gregory Maxwell */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14
15    - Neither the name of Internet Society, IETF or IETF Trust, nor the
16    names of specific contributors, may be used to endorse or promote
17    products derived from this software without specific prior written
18    permission.
19
20    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* This tests the API presented by the libopus system.
34    It does not attempt to extensively exercise the codec internals.
35    The strategy here is to simply the API interface invariants:
36    That sane options are accepted, insane options are rejected,
37    and that nothing blows up. In particular we don't actually test
38    that settings are heeded by the codec (though we do check that
39    get after set returns a sane value when it should). Other
40    tests check the actual codec behavior.
41    In cases where its reasonable to do so we test exhaustively,
42    but its not reasonable to do so in all cases.
43    Although these tests are simple they found several library bugs
44    when they were initially developed. */
45
46 /* These tests are more sensitive if compiled with -DVALGRIND and
47    run inside valgrind. Malloc failure testing requires glibc. */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <stdint.h>
56 #include <string.h>
57 #include "arch.h"
58 #include "opus_multistream.h"
59 #include "opus.h"
60 #include "test_opus_common.h"
61
62 #ifdef VALGRIND
63 #include <valgrind/memcheck.h>
64 #define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
65 #define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
66 #else
67 #define VG_UNDEF(x,y)
68 #define VG_CHECK(x,y)
69 #endif
70
71 #if defined(HAVE___MALLOC_HOOK)
72 #define MALLOC_FAIL
73 #include "os_support.h"
74 #include <malloc.h>
75
76 static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
77        OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};
78
79 void *malloc_hook(__attribute__((unused)) size_t size,
80                   __attribute__((unused)) const void *caller)
81 {
82    return 0;
83 }
84 #endif
85
86 static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
87
88 opus_int32 test_dec_api(void)
89 {
90    opus_uint32 dec_final_range;
91    OpusDecoder *dec;
92    OpusDecoder *dec2;
93    opus_int32 i,j,cfgs;
94    unsigned char packet[1276];
95 #ifndef DISABLE_FLOAT_API
96    float fbuf[960*2];
97 #endif
98    short sbuf[960*2];
99    int c,err;
100    int *nullvalue;
101    nullvalue=0;
102
103    cfgs=0;
104    /*First test invalid configurations which should fail*/
105    fprintf(stdout,"\n  Decoder basic API tests\n");
106    fprintf(stdout,"  ---------------------------------------------------\n");
107    for(c=0;c<4;c++)
108    {
109       i=opus_decoder_get_size(c);
110       if(((c==1||c==2)&&(i<=2048||i>1<<16))||((c!=1&&c!=2)&&i!=0))test_failed();
111       fprintf(stdout,"    opus_decoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
112       cfgs++;
113    }
114
115    /*Test with unsupported sample rates*/
116    for(c=0;c<4;c++)
117    {
118       for(i=-7;i<=96000;i++)
119       {
120          int fs;
121          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
122          switch(i)
123          {
124            case(-5):fs=-8000;break;
125            case(-6):fs=INT32_MAX;break;
126            case(-7):fs=INT32_MIN;break;
127            default:fs=i;
128          }
129          err = OPUS_OK;
130          VG_UNDEF(&err,sizeof(err));
131          dec = opus_decoder_create(fs, c, &err);
132          if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
133          cfgs++;
134          dec=malloc(opus_decoder_get_size(2));
135          if(dec==NULL)test_failed();
136          err = opus_decoder_init(dec,fs,c);
137          if(err!=OPUS_BAD_ARG)test_failed();
138          cfgs++;
139          free(dec);
140       }
141    }
142
143    VG_UNDEF(&err,sizeof(err));
144    dec = opus_decoder_create(48000, 2, &err);
145    if(err!=OPUS_OK || dec==NULL)test_failed();
146    VG_CHECK(dec,opus_decoder_get_size(2));
147    cfgs++;
148
149    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
150    fprintf(stdout,"    opus_decoder_init() .......................... OK.\n");
151
152    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
153    err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
154    if(err!=OPUS_OK)test_failed();
155    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
156    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
157    cfgs++;
158
159    err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
160    if(err!=OPUS_UNIMPLEMENTED)test_failed();
161    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
162    cfgs++;
163
164    VG_UNDEF(&i,sizeof(i));
165    err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
166    if(err != OPUS_OK || i!=0)test_failed();
167    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
168    cfgs++;
169
170    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
171    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
172    if(err!=OPUS_BAD_ARG)test_failed();
173    cfgs++;
174    VG_UNDEF(&i,sizeof(i));
175    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
176    if(err != OPUS_OK || i>0 || i<-1)test_failed();
177    cfgs++;
178    VG_UNDEF(packet,sizeof(packet));
179    packet[0]=63<<2;packet[1]=packet[2]=0;
180    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
181    cfgs++;
182    VG_UNDEF(&i,sizeof(i));
183    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
184    if(err != OPUS_OK || i>0 || i<-1)test_failed();
185    cfgs++;
186    packet[0]=1;
187    if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
188    cfgs++;
189    VG_UNDEF(&i,sizeof(i));
190    err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
191    if(err != OPUS_OK || i>0 || i<-1)test_failed();
192    cfgs++;
193    fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
194
195    /*Reset the decoder*/
196    dec2=malloc(opus_decoder_get_size(2));
197    memcpy(dec2,dec,opus_decoder_get_size(2));
198    if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
199    if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
200    free(dec2);
201    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
202    cfgs++;
203
204    VG_UNDEF(packet,sizeof(packet));
205    packet[0]=0;
206    if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
207    cfgs++;
208    packet[0]=(63<<2)|3;
209    packet[1]=63;
210    if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
211    fprintf(stdout,"    opus_decoder_get_nb_samples() ................ OK.\n");
212    cfgs++;
213
214    if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
215    for(i=0;i<256;i++) {
216      int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
217      packet[0]=i;
218      if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
219      cfgs++;
220      for(j=0;j<256;j++) {
221        packet[1]=j;
222        if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
223        cfgs++;
224      }
225    }
226    fprintf(stdout,"    opus_packet_get_nb_frames() .................. OK.\n");
227
228    for(i=0;i<256;i++) {
229      int bw;
230      packet[0]=i;
231      bw=packet[0]>>4;
232      bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
233      if(bw!=opus_packet_get_bandwidth(packet))test_failed();
234      cfgs++;
235    }
236    fprintf(stdout,"    opus_packet_get_bandwidth() .................. OK.\n");
237
238    for(i=0;i<256;i++) {
239      int fp3s,rate;
240      packet[0]=i;
241      fp3s=packet[0]>>3;
242      fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
243      for(rate=0;rate<5;rate++) {
244        if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
245        cfgs++;
246      }
247    }
248    fprintf(stdout,"    opus_packet_get_samples_per_frame() .......... OK.\n");
249
250    packet[0]=(63<<2)+3;
251    packet[1]=49;
252    for(j=2;j<51;j++)packet[j]=0;
253    VG_UNDEF(sbuf,sizeof(sbuf));
254    if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
255    cfgs++;
256    packet[0]=(63<<2);
257    packet[1]=packet[2]=0;
258    if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
259    cfgs++;
260    if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
261    cfgs++;
262    if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
263    cfgs++;
264    if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
265    cfgs++;
266    fprintf(stdout,"    opus_decode() ................................ OK.\n");
267 #ifndef DISABLE_FLOAT_API
268    VG_UNDEF(fbuf,sizeof(fbuf));
269    if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
270    cfgs++;
271    fprintf(stdout,"    opus_decode_float() .......................... OK.\n");
272 #endif
273
274 #if 0
275    /*These tests are disabled because the library crashes with null states*/
276    if(opus_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
277    if(opus_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
278    if(opus_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
279    if(opus_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
280    if(opus_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
281    if(opus_packet_get_nb_frames(NULL,1)            !=OPUS_BAD_ARG)test_failed();
282    if(opus_packet_get_bandwidth(NULL)              !=OPUS_BAD_ARG)test_failed();
283    if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
284 #endif
285    opus_decoder_destroy(dec);
286    cfgs++;
287    fprintf(stdout,"                   All decoder interface tests passed\n");
288    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
289    return cfgs;
290 }
291
292 opus_int32 test_msdec_api(void)
293 {
294    opus_uint32 dec_final_range;
295    OpusMSDecoder *dec;
296    OpusDecoder *streamdec;
297    opus_int32 i,j,cfgs;
298    unsigned char packet[1276];
299    unsigned char mapping[256] = {0,1};
300 #ifndef DISABLE_FLOAT_API
301    float fbuf[960*2];
302 #endif
303    short sbuf[960*2];
304    int a,b,c,err;
305 #if 0
306    /*Relevant test not enabled for multistream*/
307    int *nullvalue;
308    nullvalue=0;
309 #endif
310
311    cfgs=0;
312    /*First test invalid configurations which should fail*/
313    fprintf(stdout,"\n  Multistream decoder basic API tests\n");
314    fprintf(stdout,"  ---------------------------------------------------\n");
315    for(a=-1;a<4;a++)
316    {
317       for(b=-1;b<4;b++)
318       {
319          i=opus_multistream_decoder_get_size(a,b);
320          if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
321          fprintf(stdout,"    opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
322          cfgs++;
323       }
324    }
325
326    /*Test with unsupported sample rates*/
327    for(c=1;c<3;c++)
328    {
329       for(i=-7;i<=96000;i++)
330       {
331          int fs;
332          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
333          switch(i)
334          {
335            case(-5):fs=-8000;break;
336            case(-6):fs=INT32_MAX;break;
337            case(-7):fs=INT32_MIN;break;
338            default:fs=i;
339          }
340          err = OPUS_OK;
341          VG_UNDEF(&err,sizeof(err));
342          dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
343          if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
344          cfgs++;
345          dec=malloc(opus_multistream_decoder_get_size(1,1));
346          if(dec==NULL)test_failed();
347          err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
348          if(err!=OPUS_BAD_ARG)test_failed();
349          cfgs++;
350          free(dec);
351       }
352    }
353
354    VG_UNDEF(&err,sizeof(err));
355    dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, &err);
356    if(err==OPUS_OK || dec!=NULL)test_failed();
357    cfgs++;
358
359    VG_UNDEF(&err,sizeof(err));
360    mapping[0]=mapping[1]=0;
361    dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, &err);
362    if(err!=OPUS_OK || dec==NULL)test_failed();
363    cfgs++;
364    opus_multistream_decoder_destroy(dec);
365    cfgs++;
366
367    VG_UNDEF(&err,sizeof(err));
368    mapping[0]=0;
369    mapping[1]=1;
370    dec = opus_multistream_decoder_create(48000, 2, 2, 0, mapping, &err);
371    if(err!=OPUS_OK || dec==NULL)test_failed();
372    cfgs++;
373    opus_multistream_decoder_destroy(dec);
374    cfgs++;
375
376    VG_UNDEF(&err,sizeof(err));
377    dec = opus_multistream_decoder_create(48000, 1, 4, 1, mapping, &err);
378    if(err!=OPUS_OK || dec==NULL)test_failed();
379    cfgs++;
380    opus_multistream_decoder_destroy(dec);
381    cfgs++;
382
383    VG_UNDEF(&err,sizeof(err));
384    dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
385    if(err!=OPUS_OK || dec==NULL)test_failed();
386    cfgs++;
387
388    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
389    fprintf(stdout,"    opus_multistream_decoder_init() .............. OK.\n");
390
391    VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
392    err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
393    if(err!=OPUS_OK)test_failed();
394    VG_CHECK(&dec_final_range,sizeof(dec_final_range));
395    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
396    cfgs++;
397
398    streamdec=0;
399    VG_UNDEF(&streamdec,sizeof(streamdec));
400    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(-1,&streamdec));
401    if(err!=OPUS_BAD_ARG)test_failed();
402    cfgs++;
403    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(1,&streamdec));
404    if(err!=OPUS_BAD_ARG)test_failed();
405    cfgs++;
406    err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(0,&streamdec));
407    if(err!=OPUS_OK||streamdec==NULL)test_failed();
408    VG_CHECK(streamdec,opus_decoder_get_size(2));
409    fprintf(stdout,"    OPUS_MULTISTREAM_GET_DECODER_STATE ........... OK.\n");
410    cfgs++;
411
412    err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
413    if(err!=OPUS_UNIMPLEMENTED)test_failed();
414    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
415    cfgs++;
416
417 #if 0
418    /*Currently unimplemented for multistream*/
419    VG_UNDEF(&i,sizeof(i));
420    err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
421    if(err != OPUS_OK || i!=0)test_failed();
422    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
423    cfgs++;
424    /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
425    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
426    if(err!=OPUS_BAD_ARG)test_failed();
427    cfgs++;
428    VG_UNDEF(&i,sizeof(i));
429    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
430    if(err != OPUS_OK || i>0 || i<-1)test_failed();
431    cfgs++;
432    VG_UNDEF(packet,sizeof(packet));
433    packet[0]=63<<2;packet[1]=packet[2]=0;
434    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
435    cfgs++;
436    VG_UNDEF(&i,sizeof(i));
437    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
438    if(err != OPUS_OK || i>0 || i<-1)test_failed();
439    cfgs++;
440    packet[0]=1;
441    if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
442    cfgs++;
443    VG_UNDEF(&i,sizeof(i));
444    err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
445    if(err != OPUS_OK || i>0 || i<-1)test_failed();
446    cfgs++;
447    fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
448 #endif
449
450    /*Reset the decoder*/
451    if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
452    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
453    cfgs++;
454
455    packet[0]=(63<<2)+3;
456    packet[1]=49;
457    for(j=2;j<51;j++)packet[j]=0;
458    VG_UNDEF(sbuf,sizeof(sbuf));
459    if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
460    cfgs++;
461    packet[0]=(63<<2);
462    packet[1]=packet[2]=0;
463    if(opus_multistream_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG){printf("%d\n",opus_multistream_decode(dec, packet, -1, sbuf, 960, 0));test_failed();}
464    cfgs++;
465    if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
466    cfgs++;
467    if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
468    cfgs++;
469    if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
470    cfgs++;
471    fprintf(stdout,"    opus_multistream_decode() .................... OK.\n");
472 #ifndef DISABLE_FLOAT_API
473    VG_UNDEF(fbuf,sizeof(fbuf));
474    if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
475    cfgs++;
476    fprintf(stdout,"    opus_multistream_decode_float() .............. OK.\n");
477 #endif
478
479 #if 0
480    /*These tests are disabled because the library crashes with null states*/
481    if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
482    if(opus_multistream_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
483    if(opus_multistream_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
484    if(opus_multistream_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
485    if(opus_multistream_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
486 #endif
487    opus_multistream_decoder_destroy(dec);
488    cfgs++;
489    fprintf(stdout,"       All multistream decoder interface tests passed\n");
490    fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
491    return cfgs;
492 }
493
494 #ifdef VALGRIND
495 #define UNDEFINE_FOR_PARSE  toc=-1; \
496    frames[0]=(unsigned char *)0; \
497    frames[1]=(unsigned char *)0; \
498    payload_offset=-1; \
499    VG_UNDEF(&toc,sizeof(toc)); \
500    VG_UNDEF(frames,sizeof(frames));\
501    VG_UNDEF(&payload_offset,sizeof(payload_offset));
502 #else
503 #define UNDEFINE_FOR_PARSE  toc=-1; \
504    frames[0]=(unsigned char *)0; \
505    frames[1]=(unsigned char *)0; \
506    payload_offset=-1;
507 #endif
508
509 /* This test exercises the heck out of the libopus parser.
510    It is much larger than the parser itself in part because
511    it tries to hit a lot of corner cases that could never
512    fail with the libopus code, but might be problematic for
513    other implementations. */
514 opus_int32 test_parse(void)
515 {
516    opus_int32 i,j,jj,sz;
517    unsigned char packet[1276];
518    opus_int32 cfgs,cfgs_total;
519    unsigned char toc;
520    const unsigned char *frames[48];
521    short size[48];
522    int payload_offset, ret;
523    fprintf(stdout,"\n  Packet header parsing tests\n");
524    fprintf(stdout,"  ---------------------------------------------------\n");
525    memset(packet,0,sizeof(char)*1276);
526    packet[0]=63<<2;
527    if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
528    cfgs_total=cfgs=1;
529    /*code 0*/
530    for(i=0;i<64;i++)
531    {
532       UNDEFINE_FOR_PARSE
533       ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
534       cfgs++;
535       if(ret!=1)test_failed();
536       if(size[0]!=3)test_failed();
537       if(frames[0]!=packet+1)test_failed();
538    }
539    fprintf(stdout,"    code 0 (%2d cases) ............................ OK.\n",cfgs);
540    cfgs_total+=cfgs;cfgs=0;
541
542    /*code 1, two frames of the same size*/
543    for(i=0;i<64;i++)
544    {
545       packet[0]=(i<<2)+1;
546       for(jj=0;jj<=1275*2+3;jj++)
547       {
548          UNDEFINE_FOR_PARSE
549          ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
550          cfgs++;
551          if((jj&1)==1 && jj<=2551)
552          {
553             /* Must pass if payload length even (packet length odd) and
554                size<=2551, must fail otherwise. */
555             if(ret!=2)test_failed();
556             if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
557             if(frames[0]!=packet+1)test_failed();
558             if(frames[1]!=frames[0]+size[0])test_failed();
559             if((toc>>2)!=i)test_failed();
560          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
561       }
562    }
563    fprintf(stdout,"    code 1 (%6d cases) ........................ OK.\n",cfgs);
564    cfgs_total+=cfgs;cfgs=0;
565
566    for(i=0;i<64;i++)
567    {
568       /*code 2, length code overflow*/
569       packet[0]=(i<<2)+2;
570       UNDEFINE_FOR_PARSE
571       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
572       cfgs++;
573       if(ret!=OPUS_INVALID_PACKET)test_failed();
574       packet[1]=252;
575       UNDEFINE_FOR_PARSE
576       ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
577       cfgs++;
578       if(ret!=OPUS_INVALID_PACKET)test_failed();
579       for(j=0;j<1275;j++)
580       {
581          if(j<252)packet[1]=j;
582          else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
583          /*Code 2, one too short*/
584          UNDEFINE_FOR_PARSE
585          ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
586          cfgs++;
587          if(ret!=OPUS_INVALID_PACKET)test_failed();
588          /*Code 2, one too long*/
589          UNDEFINE_FOR_PARSE
590          ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
591          cfgs++;
592          if(ret!=OPUS_INVALID_PACKET)test_failed();
593          /*Code 2, second zero*/
594          UNDEFINE_FOR_PARSE
595          ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
596          cfgs++;
597          if(ret!=2)test_failed();
598          if(size[0]!=j||size[1]!=0)test_failed();
599          if(frames[1]!=frames[0]+size[0])test_failed();
600          if((toc>>2)!=i)test_failed();
601          /*Code 2, normal*/
602          UNDEFINE_FOR_PARSE
603          ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
604          cfgs++;
605          if(ret!=2)test_failed();
606          if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
607          if(frames[1]!=frames[0]+size[0])test_failed();
608          if((toc>>2)!=i)test_failed();
609       }
610    }
611    fprintf(stdout,"    code 2 (%6d cases) ........................ OK.\n",cfgs);
612    cfgs_total+=cfgs;cfgs=0;
613
614    for(i=0;i<64;i++)
615    {
616       packet[0]=(i<<2)+3;
617       /*code 3, length code overflow*/
618       UNDEFINE_FOR_PARSE
619       ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
620       cfgs++;
621       if(ret!=OPUS_INVALID_PACKET)test_failed();
622    }
623    fprintf(stdout,"    code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
624    cfgs_total+=cfgs;cfgs=0;
625
626    for(i=0;i<64;i++)
627    {
628       /*code 3, m is zero or 49-63*/
629       packet[0]=(i<<2)+3;
630       for(jj=49;jj<=64;jj++)
631       {
632         packet[1]=0+(jj&63); /*CBR, no padding*/
633         UNDEFINE_FOR_PARSE
634         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
635         cfgs++;
636         if(ret!=OPUS_INVALID_PACKET)test_failed();
637         packet[1]=128+(jj&63); /*VBR, no padding*/
638         UNDEFINE_FOR_PARSE
639         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
640         cfgs++;
641         if(ret!=OPUS_INVALID_PACKET)test_failed();
642         packet[1]=64+(jj&63); /*CBR, padding*/
643         UNDEFINE_FOR_PARSE
644         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
645         cfgs++;
646         if(ret!=OPUS_INVALID_PACKET)test_failed();
647         packet[1]=128+64+(jj&63); /*VBR, padding*/
648         UNDEFINE_FOR_PARSE
649         ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
650         cfgs++;
651         if(ret!=OPUS_INVALID_PACKET)test_failed();
652       }
653    }
654    fprintf(stdout,"    code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
655    cfgs_total+=cfgs;cfgs=0;
656
657    for(i=0;i<64;i++)
658    {
659       packet[0]=(i<<2)+3;
660       /*code 3, m is one, cbr*/
661       packet[1]=1;
662       for(j=0;j<1276;j++)
663       {
664         UNDEFINE_FOR_PARSE
665         ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
666         cfgs++;
667         if(ret!=1)test_failed();
668         if(size[0]!=j)test_failed();
669         if((toc>>2)!=i)test_failed();
670       }
671       UNDEFINE_FOR_PARSE
672       ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
673       cfgs++;
674       if(ret!=OPUS_INVALID_PACKET)test_failed();
675    }
676    fprintf(stdout,"    code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
677    cfgs_total+=cfgs;cfgs=0;
678
679    for(i=0;i<64;i++)
680    {
681       int frame_samp;
682       /*code 3, m>1 CBR*/
683       packet[0]=(i<<2)+3;
684       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
685       for(j=2;j<49;j++)
686       {
687          packet[1]=j;
688          for(sz=2;sz<((j+2)*1275);sz++)
689          {
690             UNDEFINE_FOR_PARSE
691             ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
692             cfgs++;
693             /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
694             if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
695             {
696                if(ret!=j)test_failed();
697                for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
698                if((toc>>2)!=i)test_failed();
699             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
700          }
701       }
702       /*Super jumbo packets*/
703       packet[1]=5760/frame_samp;
704       UNDEFINE_FOR_PARSE
705       ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
706       cfgs++;
707       if(ret!=packet[1])test_failed();
708       for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
709    }
710    fprintf(stdout,"    code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
711    cfgs_total+=cfgs;cfgs=0;
712
713    for(i=0;i<64;i++)
714    {
715       int frame_samp;
716       /*Code 3 VBR, m one*/
717       packet[0]=(i<<2)+3;
718       packet[1]=128+1;
719       frame_samp=opus_packet_get_samples_per_frame(packet,48000);
720       for(jj=0;jj<1276;jj++)
721       {
722          UNDEFINE_FOR_PARSE
723          ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
724          cfgs++;
725          if(ret!=1)test_failed();
726          if(size[0]!=jj)test_failed();
727          if((toc>>2)!=i)test_failed();
728       }
729       UNDEFINE_FOR_PARSE
730       ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
731       cfgs++;
732       if(ret!=OPUS_INVALID_PACKET)test_failed();
733       for(j=2;j<49;j++)
734       {
735          packet[1]=128+j;
736          /*Length code overflow*/
737          UNDEFINE_FOR_PARSE
738          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
739          cfgs++;
740          if(ret!=OPUS_INVALID_PACKET)test_failed();
741          packet[2]=252;
742          packet[3]=0;
743          for(jj=4;jj<2+j;jj++)packet[jj]=0;
744          UNDEFINE_FOR_PARSE
745          ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
746          cfgs++;
747          if(ret!=OPUS_INVALID_PACKET)test_failed();
748          /*One byte too short*/
749          for(jj=2;jj<2+j;jj++)packet[jj]=0;
750          UNDEFINE_FOR_PARSE
751          ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
752          cfgs++;
753          if(ret!=OPUS_INVALID_PACKET)test_failed();
754          /*One byte too short thanks to length coding*/
755          packet[2]=252;
756          packet[3]=0;
757          for(jj=4;jj<2+j;jj++)packet[jj]=0;
758          UNDEFINE_FOR_PARSE
759          ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
760          cfgs++;
761          if(ret!=OPUS_INVALID_PACKET)test_failed();
762          /*Most expensive way of coding zeros*/
763          for(jj=2;jj<2+j;jj++)packet[jj]=0;
764          UNDEFINE_FOR_PARSE
765          ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
766          cfgs++;
767          if(frame_samp*j<=5760){
768             if(ret!=j)test_failed();
769             for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
770             if((toc>>2)!=i)test_failed();
771          } else if(ret!=OPUS_INVALID_PACKET)test_failed();
772          /*Quasi-CBR use of mode 3*/
773          for(sz=0;sz<8;sz++)
774          {
775             const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
776             int pos=0;
777             int as=(tsz[sz]+i-j-2)/j;
778             for(jj=0;jj<j-1;jj++)
779             {
780               if(as<252){packet[2+pos]=as;pos++;}
781               else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
782             }
783             UNDEFINE_FOR_PARSE
784             ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
785             cfgs++;
786             if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
787                if(ret!=j)test_failed();
788                for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
789                if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
790                if((toc>>2)!=i)test_failed();
791             } else if(ret!=OPUS_INVALID_PACKET)test_failed();
792          }
793       }
794    }
795    fprintf(stdout,"    code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
796    cfgs_total+=cfgs;cfgs=0;
797
798    for(i=0;i<64;i++)
799    {
800       packet[0]=(i<<2)+3;
801       /*Padding*/
802       packet[1]=128+1+64;
803       /*Overflow the length coding*/
804       for(jj=2;jj<127;jj++)packet[jj]=255;
805       UNDEFINE_FOR_PARSE
806       ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
807       cfgs++;
808       if(ret!=OPUS_INVALID_PACKET)test_failed();
809
810       for(sz=0;sz<4;sz++)
811       {
812          const int tsz[4]={0,72,512,1275};
813          for(jj=sz;jj<65025;jj+=11)
814          {
815             int pos;
816             for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
817             packet[2+pos]=jj%254;
818             pos++;
819             if(sz==0&&i==63)
820             {
821                /*Code more padding than there is room in the packet*/
822                UNDEFINE_FOR_PARSE
823                ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
824                cfgs++;
825                if(ret!=OPUS_INVALID_PACKET)test_failed();
826             }
827             UNDEFINE_FOR_PARSE
828             ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
829             cfgs++;
830             if(tsz[sz]+i<1276)
831             {
832                if(ret!=1)test_failed();
833                if(size[0]!=tsz[sz]+i)test_failed();
834                if((toc>>2)!=i)test_failed();
835             } else if (ret!=OPUS_INVALID_PACKET)test_failed();
836          }
837       }
838    }
839    fprintf(stdout,"    code 3 padding (%2d cases) ............... OK.\n",cfgs);
840    cfgs_total+=cfgs;cfgs=0;
841    fprintf(stdout,"    opus_packet_parse ............................ OK.\n");
842    fprintf(stdout,"                      All packet parsing tests passed\n");
843    fprintf(stdout,"                          (%d API invocations)\n",cfgs_total);
844    return cfgs_total;
845 }
846
847 /* This is a helper macro for the encoder tests.
848    The encoder api tests all have a pattern of set-must-fail, set-must-fail,
849    set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
850 #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
851    i=(badv);\
852    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
853    i=(badv2);\
854    if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
855    j=i=(goodv);\
856    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
857    i=-12345;\
858    VG_UNDEF(&i,sizeof(i)); \
859    err=opus_encoder_ctl(enc,getcall);\
860    if(err!=OPUS_OK || i!=j)test_failed();\
861    j=i=(goodv2);\
862    if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
863    fprintf(stdout,sok);\
864    i=-12345;\
865    VG_UNDEF(&i,sizeof(i)); \
866    err=opus_encoder_ctl(enc,getcall);\
867    if(err!=OPUS_OK || i!=j)test_failed();\
868    fprintf(stdout,gok);\
869    cfgs+=6;
870
871 opus_int32 test_enc_api(void)
872 {
873    opus_uint32 enc_final_range;
874    OpusEncoder *enc;
875    opus_int32 i,j;
876    unsigned char packet[1276];
877 #ifndef DISABLE_FLOAT_API
878    float fbuf[960*2];
879 #endif
880    short sbuf[960*2];
881    int c,err,cfgs;
882
883    cfgs=0;
884    /*First test invalid configurations which should fail*/
885    fprintf(stdout,"\n  Encoder basic API tests\n");
886    fprintf(stdout,"  ---------------------------------------------------\n");
887    for(c=0;c<4;c++)
888    {
889       i=opus_encoder_get_size(c);
890       if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
891       fprintf(stdout,"    opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
892       cfgs++;
893    }
894
895    /*Test with unsupported sample rates, channel counts*/
896    for(c=0;c<4;c++)
897    {
898       for(i=-7;i<=96000;i++)
899       {
900          int fs;
901          if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
902          switch(i)
903          {
904            case(-5):fs=-8000;break;
905            case(-6):fs=INT32_MAX;break;
906            case(-7):fs=INT32_MIN;break;
907            default:fs=i;
908          }
909          err = OPUS_OK;
910          VG_UNDEF(&err,sizeof(err));
911          enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
912          if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
913          cfgs++;
914          opus_encoder_destroy(enc);
915          enc=malloc(opus_encoder_get_size(2));
916          if(enc==NULL)test_failed();
917          err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
918          if(err!=OPUS_BAD_ARG)test_failed();
919          cfgs++;
920          free(enc);
921       }
922    }
923
924    enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
925    if(enc!=NULL)test_failed();
926    cfgs++;
927
928    VG_UNDEF(&err,sizeof(err));
929    enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
930    if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
931    cfgs++;
932
933    VG_UNDEF(&err,sizeof(err));
934    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
935    if(enc==NULL)test_failed();
936    opus_encoder_destroy(enc);
937    cfgs++;
938
939    VG_UNDEF(&err,sizeof(err));
940    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
941    if(err!=OPUS_OK || enc==NULL)test_failed();
942    cfgs++;
943    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
944    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
945    opus_encoder_destroy(enc);
946    cfgs++;
947
948    VG_UNDEF(&err,sizeof(err));
949    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
950    if(err!=OPUS_OK || enc==NULL)test_failed();
951    cfgs++;
952    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
953    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
954    opus_encoder_destroy(enc);
955    cfgs++;
956
957    VG_UNDEF(&err,sizeof(err));
958    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
959    if(err!=OPUS_OK || enc==NULL)test_failed();
960    cfgs++;
961
962    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
963    fprintf(stdout,"    opus_encoder_init() .......................... OK.\n");
964
965    i=-12345;
966    VG_UNDEF(&i,sizeof(i));
967    err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
968    if(err!=OPUS_OK || i<0 || i>32766)test_failed();
969    cfgs++;
970    fprintf(stdout,"    OPUS_GET_LOOKAHEAD ........................... OK.\n");
971
972    if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
973    fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
974    cfgs++;
975
976    CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
977      OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
978      "    OPUS_SET_APPLICATION ......................... OK.\n",
979      "    OPUS_GET_APPLICATION ......................... OK.\n")
980
981    if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
982    cfgs++;
983    VG_UNDEF(&i,sizeof(i));
984    if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
985    if(i>700000||i<256000)test_failed();
986    cfgs++;
987    CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
988      500,256000,
989      "    OPUS_SET_BITRATE ............................. OK.\n",
990      "    OPUS_GET_BITRATE ............................. OK.\n")
991
992    CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
993      1,OPUS_AUTO,
994      "    OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
995      "    OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
996
997    i=-2;
998    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
999    cfgs++;
1000    i=OPUS_BANDWIDTH_FULLBAND+1;
1001    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
1002    cfgs++;
1003    i=OPUS_BANDWIDTH_NARROWBAND;
1004    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1005    cfgs++;
1006    i=OPUS_BANDWIDTH_FULLBAND;
1007    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1008    cfgs++;
1009    i=OPUS_BANDWIDTH_WIDEBAND;
1010    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1011    cfgs++;
1012    i=OPUS_BANDWIDTH_MEDIUMBAND;
1013    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
1014    cfgs++;
1015    fprintf(stdout,"    OPUS_SET_BANDWIDTH ........................... OK.\n");
1016    /*We don't test if the bandwidth has actually changed.
1017      because the change may be delayed until the encoder is advanced.*/
1018    i=-12345;
1019    VG_UNDEF(&i,sizeof(i));
1020    err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
1021    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1022       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1023       i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
1024    cfgs++;
1025    if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
1026    cfgs++;
1027    fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
1028
1029    i=-2;
1030    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1031    cfgs++;
1032    i=OPUS_BANDWIDTH_FULLBAND+1;
1033    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
1034    cfgs++;
1035    i=OPUS_BANDWIDTH_NARROWBAND;
1036    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1037    cfgs++;
1038    i=OPUS_BANDWIDTH_FULLBAND;
1039    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1040    cfgs++;
1041    i=OPUS_BANDWIDTH_WIDEBAND;
1042    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1043    cfgs++;
1044    i=OPUS_BANDWIDTH_MEDIUMBAND;
1045    if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
1046    cfgs++;
1047    fprintf(stdout,"    OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
1048    /*We don't test if the bandwidth has actually changed.
1049      because the change may be delayed until the encoder is advanced.*/
1050    i=-12345;
1051    VG_UNDEF(&i,sizeof(i));
1052    err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
1053    if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
1054       i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
1055       i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
1056    cfgs++;
1057    fprintf(stdout,"    OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
1058
1059    CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
1060      1,0,
1061      "    OPUS_SET_DTX ................................. OK.\n",
1062      "    OPUS_GET_DTX ................................. OK.\n")
1063
1064    CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
1065      0,10,
1066      "    OPUS_SET_COMPLEXITY .......................... OK.\n",
1067      "    OPUS_GET_COMPLEXITY .......................... OK.\n")
1068
1069    CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
1070      1,0,
1071      "    OPUS_SET_INBAND_FEC .......................... OK.\n",
1072      "    OPUS_GET_INBAND_FEC .......................... OK.\n")
1073
1074    CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
1075      100,0,
1076      "    OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
1077      "    OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
1078
1079    CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
1080      1,0,
1081      "    OPUS_SET_VBR ................................. OK.\n",
1082      "    OPUS_GET_VBR ................................. OK.\n")
1083
1084    /*CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
1085      0,50,
1086      "    OPUS_SET_VOICE_RATIO ......................... OK.\n",
1087      "    OPUS_GET_VOICE_RATIO ......................... OK.\n")
1088    */
1089
1090    CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
1091      1,0,
1092      "    OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
1093      "    OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
1094
1095    CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
1096      OPUS_SIGNAL_MUSIC,OPUS_AUTO,
1097      "    OPUS_SET_SIGNAL .............................. OK.\n",
1098      "    OPUS_GET_SIGNAL .............................. OK.\n")
1099
1100    /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
1101
1102    if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
1103    cfgs++;
1104    fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
1105
1106    /*Reset the encoder*/
1107    if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
1108    cfgs++;
1109    fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
1110
1111    memset(sbuf,0,sizeof(short)*2*960);
1112    VG_UNDEF(packet,sizeof(packet));
1113    i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
1114    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1115    VG_CHECK(packet,i);
1116    cfgs++;
1117    fprintf(stdout,"    opus_encode() ................................ OK.\n");
1118 #ifndef DISABLE_FLOAT_API
1119    memset(fbuf,0,sizeof(float)*2*960);
1120    VG_UNDEF(packet,sizeof(packet));
1121    i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
1122    if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
1123    VG_CHECK(packet,i);
1124    cfgs++;
1125    fprintf(stdout,"    opus_encode_float() .......................... OK.\n");
1126 #endif
1127
1128 #if 0
1129    /*These tests are disabled because the library crashes with null states*/
1130    if(opus_encoder_ctl(0,OPUS_RESET_STATE)               !=OPUS_INVALID_STATE)test_failed();
1131    if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
1132    if(opus_encode(0,sbuf,960,packet,sizeof(packet))      !=OPUS_INVALID_STATE)test_failed();
1133    if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
1134 #endif
1135    opus_encoder_destroy(enc);
1136    cfgs++;
1137    fprintf(stdout,"                   All encoder interface tests passed\n");
1138    fprintf(stdout,"                             (%d API invocations)\n",cfgs);
1139    return cfgs;
1140 }
1141
1142 #define max_out (1276*48+48*2+2)
1143 int test_repacketizer_api(void)
1144 {
1145    int ret,cfgs,i,j,k;
1146    OpusRepacketizer *rp;
1147    unsigned char *packet;
1148    unsigned char *po;
1149    cfgs=0;
1150    fprintf(stdout,"\n  Repacketizer tests\n");
1151    fprintf(stdout,"  ---------------------------------------------------\n");
1152
1153    packet=malloc(max_out);
1154    if(packet==NULL)test_failed();
1155    po=malloc(max_out);
1156    if(po==NULL)test_failed();
1157    memset(packet,0,max_out);
1158
1159    i=opus_repacketizer_get_size();
1160    if(i<=0)test_failed();
1161    cfgs++;
1162    fprintf(stdout,"    opus_repacketizer_get_size()=%d ............. OK.\n",i);
1163
1164    rp=malloc(i);
1165    rp=opus_repacketizer_init(rp);
1166    if(rp==NULL)test_failed();
1167    cfgs++;
1168    free(rp);
1169    fprintf(stdout,"    opus_repacketizer_init ....................... OK.\n");
1170
1171    rp=opus_repacketizer_create();
1172    if(rp==NULL)test_failed();
1173    cfgs++;
1174    fprintf(stdout,"    opus_repacketizer_create ..................... OK.\n");
1175
1176    if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
1177    cfgs++;
1178    fprintf(stdout,"    opus_repacketizer_get_nb_frames .............. OK.\n");
1179
1180    /*Length overflows*/
1181    VG_UNDEF(packet,4);
1182    if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
1183    cfgs++;
1184    packet[0]=1;
1185    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
1186    cfgs++;
1187    packet[0]=2;
1188    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
1189    cfgs++;
1190    packet[0]=3;
1191    if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
1192    cfgs++;
1193    packet[0]=2;
1194    packet[1]=255;
1195    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
1196    cfgs++;
1197    packet[0]=2;
1198    packet[1]=250;
1199    if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
1200    cfgs++;
1201    packet[0]=3;
1202    packet[1]=0;
1203    if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
1204    cfgs++;
1205    packet[1]=49;
1206    if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
1207    cfgs++;
1208    packet[0]=0;
1209    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1210    cfgs++;
1211    packet[0]=1<<2;
1212    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
1213    cfgs++;
1214
1215    /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
1216    opus_repacketizer_init(rp);
1217    for(j=0;j<32;j++)
1218    {
1219       /* TOC types, test half with stereo */
1220       int maxi;
1221       packet[0]=((j<<1)+(j&1))<<2;
1222       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1223       for(i=1;i<=maxi;i++)
1224       {
1225          /* Number of CBR frames in the input packets */
1226          int maxp;
1227          packet[0]=((j<<1)+(j&1))<<2;
1228          if(i>1)packet[0]+=i==2?1:3;
1229          packet[1]=i>2?i:0;
1230          maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
1231          for(k=0;k<=(1275+75);k+=3)
1232          {
1233             /*Payload size*/
1234             opus_int32 cnt,rcnt;
1235             if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
1236             for(cnt=0;cnt<maxp+2;cnt++)
1237             {
1238                if(cnt>0)
1239                {
1240                   ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
1241                   if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
1242                   cfgs++;
1243                }
1244                rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
1245                if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
1246                cfgs++;
1247                ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
1248                if(rcnt>0)
1249                {
1250                   int len;
1251                   len=k*rcnt+((rcnt*i)>2?2:1);
1252                   if(ret!=len)test_failed();
1253                   if((rcnt*i)<2&&(po[0]&3)!=0)test_failed();                      /* Code 0 */
1254                   if((rcnt*i)==2&&(po[0]&3)!=1)test_failed();                     /* Code 1 */
1255                   if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
1256                   cfgs++;
1257                   if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1258                   cfgs++;
1259                   if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1260                   cfgs++;
1261                   if(len>1)
1262                   {
1263                      if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1264                      cfgs++;
1265                   }
1266                   if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1267                   cfgs++;
1268                } else if (ret!=OPUS_BAD_ARG)test_failed();                        /* M must not be 0 */
1269             }
1270             opus_repacketizer_init(rp);
1271          }
1272       }
1273    }
1274
1275    /*Change in input count code, CBR out*/
1276    opus_repacketizer_init(rp);
1277    packet[0]=0;
1278    if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
1279    cfgs++;
1280    packet[0]+=1;
1281    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1282    cfgs++;
1283    i=opus_repacketizer_out(rp,po,max_out);
1284    if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
1285    cfgs++;
1286    i=opus_repacketizer_out_range(rp,0,1,po,max_out);
1287    if(i!=5||(po[0]&3)!=0)test_failed();
1288    cfgs++;
1289    i=opus_repacketizer_out_range(rp,1,2,po,max_out);
1290    if(i!=5||(po[0]&3)!=0)test_failed();
1291    cfgs++;
1292
1293    /*Change in input count code, VBR out*/
1294    opus_repacketizer_init(rp);
1295    packet[0]=1;
1296    if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
1297    cfgs++;
1298    packet[0]=0;
1299    if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
1300    cfgs++;
1301    i=opus_repacketizer_out(rp,po,max_out);
1302    if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
1303    cfgs++;
1304
1305    /*VBR in, VBR out*/
1306    opus_repacketizer_init(rp);
1307    packet[0]=2;
1308    packet[1]=4;
1309    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1310    cfgs++;
1311    if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
1312    cfgs++;
1313    i=opus_repacketizer_out(rp,po,max_out);
1314    if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
1315    cfgs++;
1316
1317    /*VBR in, CBR out*/
1318    opus_repacketizer_init(rp);
1319    packet[0]=2;
1320    packet[1]=4;
1321    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1322    cfgs++;
1323    if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
1324    cfgs++;
1325    i=opus_repacketizer_out(rp,po,max_out);
1326    if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
1327    cfgs++;
1328
1329    /*Count 0 in, VBR out*/
1330    for(j=0;j<32;j++)
1331    {
1332       /* TOC types, test half with stereo */
1333       int maxi,sum,rcnt;
1334       packet[0]=((j<<1)+(j&1))<<2;
1335       maxi=960/opus_packet_get_samples_per_frame(packet,8000);
1336       sum=0;
1337       rcnt=0;
1338       opus_repacketizer_init(rp);
1339       for(i=1;i<=maxi+2;i++)
1340       {
1341          int len;
1342          ret=opus_repacketizer_cat(rp,packet,i);
1343          if(rcnt<maxi)
1344          {
1345             if(ret!=OPUS_OK)test_failed();
1346             rcnt++;
1347             sum+=i-1;
1348          } else if (ret!=OPUS_INVALID_PACKET)test_failed();
1349          cfgs++;
1350          len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
1351          if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
1352          if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
1353          if(rcnt==2&&(po[0]&3)!=2)test_failed();
1354          if(rcnt==1&&(po[0]&3)!=0)test_failed();
1355          cfgs++;
1356          if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
1357          cfgs++;
1358          if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1359          cfgs++;
1360          if(len>1)
1361          {
1362             if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1363             cfgs++;
1364          }
1365          if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
1366          cfgs++;
1367       }
1368    }
1369
1370    fprintf(stdout,"    opus_repacketizer_cat ........................ OK.\n");
1371    fprintf(stdout,"    opus_repacketizer_out ........................ OK.\n");
1372    fprintf(stdout,"    opus_repacketizer_out_range .................. OK.\n");
1373
1374    opus_repacketizer_destroy(rp);
1375    cfgs++;
1376    free(packet);
1377    free(po);
1378    fprintf(stdout,"                        All repacketizer tests passed\n");
1379    fprintf(stdout,"                            (%7d API invocations)\n",cfgs);
1380
1381    return cfgs;
1382 }
1383
1384 #ifdef MALLOC_FAIL
1385 /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
1386  * under GCC. However, this is the cleanest way to test malloc failure
1387  * handling in our codebase, and the lack of thread saftey isn't an
1388  * issue here. We therefore disable the warning for this function.
1389  */
1390 #if __GNUC_PREREQ(4,6)
1391 /* Save the current warning settings */
1392 #pragma GCC diagnostic push
1393 #endif
1394 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1395
1396 typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
1397 #endif
1398
1399 int test_malloc_fail(void)
1400 {
1401 #ifdef MALLOC_FAIL
1402    OpusDecoder *dec;
1403    OpusEncoder *enc;
1404    OpusRepacketizer *rp;
1405    unsigned char mapping[256] = {0,1};
1406    OpusMSDecoder *msdec;
1407    OpusMSEncoder *msenc;
1408    int rate,c,app,cfgs,err,useerr;
1409    int *ep;
1410    mhook orig_malloc;
1411    cfgs=0;
1412 #endif
1413    fprintf(stdout,"\n  malloc() failure tests\n");
1414    fprintf(stdout,"  ---------------------------------------------------\n");
1415 #ifdef MALLOC_FAIL
1416    orig_malloc=__malloc_hook;
1417    __malloc_hook=malloc_hook;
1418    ep=(int *)opus_alloc(sizeof(int));
1419    if(ep!=NULL)
1420    {
1421       if(ep)free(ep);
1422       __malloc_hook=orig_malloc;
1423 #endif
1424       fprintf(stdout,"    opus_decoder_create() ................... SKIPPED.\n");
1425       fprintf(stdout,"    opus_encoder_create() ................... SKIPPED.\n");
1426       fprintf(stdout,"    opus_repacketizer_create() .............. SKIPPED.\n");
1427       fprintf(stdout,"    opus_multistream_decoder_create() ....... SKIPPED.\n");
1428       fprintf(stdout,"    opus_multistream_encoder_create() ....... SKIPPED.\n");
1429       fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
1430       return 0;
1431 #ifdef MALLOC_FAIL
1432    }
1433    for(useerr=0;useerr<2;useerr++)
1434    {
1435       ep=useerr?&err:0;
1436       for(rate=0;rate<5;rate++)
1437       {
1438         for(c=1;c<3;c++)
1439         {
1440            err=1;
1441            if(useerr)
1442            {
1443               VG_UNDEF(&err,sizeof(err));
1444            }
1445            dec=opus_decoder_create(opus_rates[rate], c, ep);
1446            if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1447            {
1448               __malloc_hook=orig_malloc;
1449               test_failed();
1450            }
1451            cfgs++;
1452            msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
1453            if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1454            {
1455               __malloc_hook=orig_malloc;
1456               test_failed();
1457            }
1458            cfgs++;
1459            for(app=0;app<3;app++)
1460            {
1461               if(useerr)
1462               {
1463                  VG_UNDEF(&err,sizeof(err));
1464               }
1465               enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
1466               if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1467               {
1468                  __malloc_hook=orig_malloc;
1469                  test_failed();
1470               }
1471               cfgs++;
1472               msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
1473               if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
1474               {
1475                  __malloc_hook=orig_malloc;
1476                  test_failed();
1477               }
1478               cfgs++;
1479            }
1480         }
1481      }
1482    }
1483    rp=opus_repacketizer_create();
1484    if(rp!=NULL)
1485    {
1486       __malloc_hook=orig_malloc;
1487       test_failed();
1488    }
1489    cfgs++;
1490    __malloc_hook=orig_malloc;
1491    fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
1492    fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
1493    fprintf(stdout,"    opus_repacketizer_create() ................... OK.\n");
1494    fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
1495    fprintf(stdout,"    opus_multistream_encoder_create() ............ OK.\n");
1496    fprintf(stdout,"                      All malloc failure tests passed\n");
1497    fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
1498    return cfgs;
1499 #endif
1500 }
1501
1502 #ifdef MALLOC_FAIL
1503 #if __GNUC_PREREQ(4,6)
1504 #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
1505 #endif
1506 #endif
1507
1508 int main(int _argc, char **_argv)
1509 {
1510    opus_int32 total;
1511    const char * oversion;
1512    if(_argc>1)
1513    {
1514       fprintf(stderr,"Usage: %s\n",_argv[0]);
1515       return 1;
1516    }
1517    iseed=0;
1518
1519    oversion=opus_get_version_string();
1520    if(!oversion)test_failed();
1521    fprintf(stderr,"Testing the %s API deterministically\n", oversion);
1522    if(opus_strerror(-32768)==NULL)test_failed();
1523    if(opus_strerror(32767)==NULL)test_failed();
1524    if(strlen(opus_strerror(0))<1)test_failed();
1525    total=4;
1526
1527    total+=test_dec_api();
1528    total+=test_msdec_api();
1529    total+=test_parse();
1530    total+=test_enc_api();
1531    total+=test_repacketizer_api();
1532    total+=test_malloc_fail();
1533
1534    fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
1535
1536    return 0;
1537 }