Additional mathops.h tests.
[opus.git] / tests / cwrs32-test.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #define CELT_C 
9 #include "../libcelt/stack_alloc.h"
10 #include "../libcelt/rangeenc.c"
11 #include "../libcelt/rangedec.c"
12 #include "../libcelt/entenc.c"
13 #include "../libcelt/entdec.c"
14 #include "../libcelt/entcode.c"
15 #include "../libcelt/cwrs.c"
16
17 #define NMAX (14)
18 #define KMAX (32767)
19
20 static const int kmax[15]={
21   32767,32767,32767,32767, 1172,
22     238,   95,   53,   36,   27,
23      22,   18,   16,   15,   13
24 };
25
26
27 int main(int _argc,char **_argv){
28   int n;
29   ALLOC_STACK;
30   for(n=2;n<=NMAX;n++){
31     int dk;
32     int k;
33     dk=kmax[n]>7?kmax[n]/7:1;
34     k=1-dk;
35     do{
36       celt_uint32_t uu[KMAX+2U];
37       celt_uint32_t inc;
38       celt_uint32_t nc;
39       celt_uint32_t i;
40       k=kmax[n]-dk<k?kmax[n]:k+dk;
41       printf("Testing CWRS with N=%i, K=%i...\n",n,k);
42       nc=ncwrs_urow(n,k,uu);
43       inc=nc/10000;
44       if(inc<1)inc=1;
45       for(i=0;i<nc;i+=inc){
46         celt_uint32_t u[KMAX+2U];
47         int           y[NMAX];
48         int           yy[5];
49         celt_uint32_t v;
50         celt_uint32_t ii;
51         int           kk;
52         int           j;
53         memcpy(u,uu,(k+2U)*sizeof(*u));
54         cwrsi(n,k,i,y,u);
55         /*printf("%6u of %u:",i,nc);
56         for(j=0;j<n;j++)printf(" %+3i",y[j]);
57         printf(" ->");*/
58         ii=icwrs(n,k,&v,y,u);
59         if(ii!=i){
60           fprintf(stderr,"Combination-index mismatch (%lu!=%lu).\n",
61            (long)ii,(long)i);
62           return 1;
63         }
64         if(v!=nc){
65           fprintf(stderr,"Combination count mismatch (%lu!=%lu).\n",
66            (long)v,(long)nc);
67           return 2;
68         }
69         if(n==2){
70           cwrsi2(k,i,yy);
71           for(j=0;j<2;j++)if(yy[j]!=y[j]){
72             fprintf(stderr,"N=2 pulse vector mismatch ({%i,%i}!={%i,%i}).\n",
73              yy[0],yy[1],y[0],y[1]);
74             return 3;
75           }
76           ii=icwrs2(yy,&kk);
77           if(ii!=i){
78             fprintf(stderr,"N=2 combination-index mismatch (%lu!=%lu).\n",
79              (long)ii,(long)i);
80             return 4;
81           }
82           if(kk!=k){
83             fprintf(stderr,"N=2 pulse count mismatch (%i,%i).\n",kk,k);
84             return 5;
85           }
86           v=ncwrs2(k);
87           if(v!=nc){
88             fprintf(stderr,"N=2 combination count mismatch (%lu,%lu).\n",
89              (long)v,(long)nc);
90             return 6;
91           }
92         }
93         else if(n==3){
94           cwrsi3(k,i,yy);
95           for(j=0;j<3;j++)if(yy[j]!=y[j]){
96             fprintf(stderr,"N=3 pulse vector mismatch "
97              "({%i,%i,%i}!={%i,%i,%i}).\n",yy[0],yy[1],yy[2],y[0],y[1],y[2]);
98             return 7;
99           }
100           ii=icwrs3(yy,&kk);
101           if(ii!=i){
102             fprintf(stderr,"N=3 combination-index mismatch (%lu!=%lu).\n",
103              (long)ii,(long)i);
104             return 8;
105           }
106           if(kk!=k){
107             fprintf(stderr,"N=3 pulse count mismatch (%i!=%i).\n",kk,k);
108             return 9;
109           }
110           v=ncwrs3(k);
111           if(v!=nc){
112             fprintf(stderr,"N=3 combination count mismatch (%lu!=%lu).\n",
113              (long)v,(long)nc);
114             return 10;
115           }
116         }
117         else if(n==4){
118           cwrsi4(k,i,yy);
119           for(j=0;j<4;j++)if(yy[j]!=y[j]){
120             fprintf(stderr,"N=4 pulse vector mismatch "
121              "({%i,%i,%i,%i}!={%i,%i,%i,%i}.\n",
122              yy[0],yy[1],yy[2],yy[3],y[0],y[1],y[2],y[3]);
123             return 11;
124           }
125           ii=icwrs4(yy,&kk);
126           if(ii!=i){
127             fprintf(stderr,"N=4 combination-index mismatch (%lu!=%lu).\n",
128              (long)ii,(long)i);
129             return 12;
130           }
131           if(kk!=k){
132             fprintf(stderr,"N=4 pulse count mismatch (%i!=%i).\n",kk,k);
133             return 13;
134           }
135           v=ncwrs4(k);
136           if(v!=nc){
137             fprintf(stderr,"N=4 combination count mismatch (%lu!=%lu).\n",
138              (long)v,(long)nc);
139             return 14;
140           }
141         }
142         else if(n==5){
143           cwrsi5(k,i,yy);
144           for(j=0;j<5;j++)if(yy[j]!=y[j]){
145             fprintf(stderr,"N=5 pulse vector mismatch "
146              "({%i,%i,%i,%i,%i}!={%i,%i,%i,%i,%i}).\n",
147              yy[0],yy[1],yy[2],yy[3],yy[4],y[0],y[1],y[2],y[3],y[4]);
148             return 15;
149           }
150           ii=icwrs5(yy,&kk);
151           if(ii!=i){
152             fprintf(stderr,"N=5 combination-index mismatch (%lu!=%lu).\n",
153              (long)ii,(long)i);
154             return 16;
155           }
156           if(kk!=k){
157             fprintf(stderr,"N=5 pulse count mismatch (%i!=%i).\n",kk,k);
158             return 17;
159           }
160           v=ncwrs5(k);
161           if(v!=nc){
162             fprintf(stderr,"N=5 combination count mismatch (%lu!=%lu).\n",
163              (long)v,(long)nc);
164             return 18;
165           }
166         }
167         /*printf(" %6u\n",i);*/
168       }
169       /*printf("\n");*/
170     }
171     while(k<kmax[n]);
172   }
173   return 0;
174 }