Checks for no PP.
[platform/upstream/libaec.git] / tests / check_code_options.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "check_aec.h"
5
6 #define BUF_SIZE 1024 * 3
7
8 int check_block_sizes(struct test_state *state, int id, int id_len)
9 {
10     int bs, status, rsi, max_rsi;
11
12     for (bs = 8; bs <= 64; bs *= 2) {
13         state->strm->block_size = bs;
14
15         max_rsi = (int)(state->buf_len / (bs * state->bytes_per_sample));
16         if (max_rsi > 4096)
17             max_rsi = 4096;
18
19         for (rsi = 1; rsi <= max_rsi; rsi++) {
20             state->strm->rsi = rsi;
21             status = state->codec(state);
22             if (status)
23                 return status;
24
25             if ((state->cbuf[0] >> (8 - id_len)) != id) {
26                 printf(
27                     "%s: block of size %i created with ID:%x, expected %x.\n",
28                     CHECK_FAIL, bs, state->cbuf[0] >> (8 - id_len), id
29                     );
30                 return 99;
31             }
32         }
33     }
34     return 0;
35 }
36
37 int check_zero(struct test_state *state)
38 {
39     int status;
40
41     if (state->strm->flags & AEC_DATA_PREPROCESS)
42         memset(state->ubuf, 0x55, state->buf_len);
43     else
44         memset(state->ubuf, 0, state->buf_len);
45
46     printf("Checking zero blocks ... ");
47     status = check_block_sizes(state, 0, state->id_len + 1);
48     if (status)
49         return status;
50
51     printf ("%s\n", CHECK_PASS);
52     return 0;
53 }
54
55 int check_splitting(struct test_state *state, int k)
56 {
57     int status, size;
58     unsigned char *tmp;
59
60     size = state->bytes_per_sample;
61
62     if (state->strm->flags & AEC_DATA_PREPROCESS) {
63         for (tmp = state->ubuf;
64              tmp < state->ubuf + state->buf_len;
65              tmp += 4 * size) {
66             state->out(tmp, state->xmin + (1ULL << (k - 1)) - 1, size);
67             state->out(tmp + size, state->xmin, size);
68             state->out(tmp + 2 * size, state->xmin
69                        + (1ULL << (k + 1)) - 1, size);
70             state->out(tmp + 3 * size, state->xmin, size);
71         }
72     } else {
73         for (tmp = state->ubuf;
74              tmp < state->ubuf + state->buf_len;
75              tmp += 4 * size) {
76             state->out(tmp, 0, size);
77             state->out(tmp + size, (1ULL << k) - 1, size);
78             state->out(tmp + 2 * size, 0, size);
79             state->out(tmp + 3 * size, (1ULL << (k + 2)) - 1, size);
80         }
81     }
82
83     printf("Checking splitting with k=%i ... ", k);
84     status = check_block_sizes(state, k + 1, state->id_len);
85     if (status)
86         return status;
87
88     printf ("%s\n", CHECK_PASS);
89     return 0;
90 }
91
92 int check_uncompressed(struct test_state *state)
93 {
94     int status, size;
95     unsigned char *tmp;
96
97     size = state->bytes_per_sample;
98
99     for (tmp = state->ubuf;
100          tmp < state->ubuf + state->buf_len;
101          tmp += 2 * size) {
102         state->out(tmp, state->xmax, size);
103         state->out(tmp + size, state->xmin, size);
104     }
105
106     printf("Checking uncompressed ... ");
107     status = check_block_sizes(state,
108                                (1ULL << state->id_len) - 1,
109                                state->id_len);
110     if (status)
111         return status;
112
113     printf ("%s\n", CHECK_PASS);
114     return 0;
115 }
116
117 int check_fs(struct test_state *state)
118 {
119     int status, size;
120     unsigned char *tmp;
121
122     size = state->bytes_per_sample;
123
124     if (state->strm->flags & AEC_DATA_PREPROCESS) {
125         for (tmp = state->ubuf;
126              tmp < state->ubuf + state->buf_len;
127              tmp += 4 * size) {
128             state->out(tmp, state->xmin + 2, size);
129             state->out(tmp + size, state->xmin, size);
130             state->out(tmp + 2 * size, state->xmin, size);
131             state->out(tmp + 3 * size, state->xmin, size);
132         }
133     } else {
134         for (tmp = state->ubuf;
135              tmp < state->ubuf + state->buf_len;
136              tmp += 4 * size) {
137             state->out(tmp, 0, size);
138             state->out(tmp + size, 0, size);
139             state->out(tmp + 2 * size, 0, size);
140             state->out(tmp + 3 * size, 4, size);
141         }
142     }
143
144     printf("Checking FS ... ");
145     status = check_block_sizes(state, 1, state->id_len);
146     if (status)
147         return status;
148
149     printf ("%s\n", CHECK_PASS);
150     return 0;
151 }
152
153 int check_se(struct test_state *state)
154 {
155     int status, size;
156     unsigned char *tmp;
157
158     size = state->bytes_per_sample;
159
160     if (state->strm->flags & AEC_DATA_PREPROCESS) {
161         for (tmp = state->ubuf;
162              tmp < state->ubuf + state->buf_len;
163              tmp += 8 * size) {
164             state->out(tmp, state->xmax - 1, size);
165             state->out(tmp + size, state->xmax - 1, size);
166             state->out(tmp + 2 * size, state->xmax - 1, size);
167             state->out(tmp + 3 * size, state->xmax - 1, size);
168             state->out(tmp + 4 * size, state->xmax, size);
169             state->out(tmp + 5 * size, state->xmax, size);
170             state->out(tmp + 6 * size, state->xmax, size);
171             state->out(tmp + 7 * size, state->xmax, size);
172         }
173     } else {
174         for (tmp = state->ubuf;
175              tmp < state->ubuf + state->buf_len;
176              tmp += 8 * size) {
177             state->out(tmp, 0, size);
178             state->out(tmp + size, 0, size);
179             state->out(tmp + 2 * size, 0, size);
180             state->out(tmp + 3 * size, 0, size);
181             state->out(tmp + 4 * size, 1, size);
182             state->out(tmp + 5 * size, 0, size);
183             state->out(tmp + 6 * size, 0, size);
184             state->out(tmp + 7 * size, 2, size);
185         }
186     }
187
188     printf("Checking Second Extension ... ");
189     status = check_block_sizes(state, 1, state->id_len + 1);
190     if (status)
191         return status;
192
193     printf ("%s\n", CHECK_PASS);
194     return 0;
195 }
196
197 int check_bps(struct test_state *state)
198 {
199     int k, status, bps;
200
201     for (bps = 8; bps <= 32; bps += 8) {
202         state->strm->bits_per_sample = bps;
203         if (bps == 24)
204             state->strm->flags |= AEC_DATA_3BYTE;
205         else
206             state->strm->flags &= ~AEC_DATA_3BYTE;
207
208         update_state(state);
209
210         status = check_zero(state);
211         if (status)
212             return status;
213
214         status = check_se(state);
215         if (status)
216             return status;
217
218         status = check_uncompressed(state);
219         if (status)
220             return status;
221
222         status = check_fs(state);
223         if (status)
224             return status;
225
226         for (k = 1; k < bps - 2; k++) {
227             status = check_splitting(state, k);
228             if (status)
229                 return status;
230         }
231         printf("All checks with %i bit per sample passed.\n", bps);
232     }
233     return 0;
234 }
235
236 int check_byte_orderings(struct test_state *state)
237 {
238     int status;
239
240     printf("-----------------------------------\n");
241     printf("Checking no PP, LSB first, unsigned\n");
242     printf("-----------------------------------\n");
243     status = check_bps(state);
244     if (status)
245         return status;
246
247     printf("-----------------------------------\n");
248     printf("Checking PP, LSB first, unsigned\n");
249     printf("-----------------------------------\n");
250     state->strm->flags |= AEC_DATA_PREPROCESS;
251     status = check_bps(state);
252     if (status)
253         return status;
254
255     printf("-----------------------------------\n");
256     printf("Checking PP, LSB first, signed\n");
257     printf("-----------------------------------\n");
258     state->strm->flags |= AEC_DATA_SIGNED;
259
260     status = check_bps(state);
261     if (status)
262         return status;
263
264     state->strm->flags &= ~AEC_DATA_SIGNED;
265     state->strm->flags |= AEC_DATA_MSB;
266
267     printf("-----------------------------------\n");
268     printf("Checking PP, MSB first, unsigned\n");
269     printf("-----------------------------------\n");
270     status = check_bps(state);
271     if (status)
272         return status;
273
274     printf("-----------------------------------\n");
275     printf("Checking PP, MSB first, signed\n");
276     printf("-----------------------------------\n");
277     state->strm->flags |= AEC_DATA_SIGNED;
278
279     status = check_bps(state);
280     if (status)
281         return status;
282     return 0;
283 }
284
285 int main (void)
286 {
287     int status;
288     struct aec_stream strm;
289     struct test_state state;
290
291     state.buf_len = state.ibuf_len = BUF_SIZE;
292     state.cbuf_len = 2 * BUF_SIZE;
293
294     state.ubuf = (unsigned char *)malloc(state.buf_len);
295     state.cbuf = (unsigned char *)malloc(state.cbuf_len);
296     state.obuf = (unsigned char *)malloc(state.buf_len);
297
298     if (!state.ubuf || !state.cbuf || !state.obuf) {
299         printf("Not enough memory.\n");
300         return 99;
301     }
302
303     strm.flags = 0;
304     state.strm = &strm;
305
306     printf("***************************\n");
307     printf("Checking with small buffers\n");
308     printf("***************************\n");
309     state.codec = encode_decode_small;
310     status = check_byte_orderings(&state);
311     if (status)
312         goto DESTRUCT;
313
314     printf("***************************\n");
315     printf("Checking with large buffers\n");
316     printf("***************************\n");
317     state.codec = encode_decode_large;
318     status = check_byte_orderings(&state);
319
320 DESTRUCT:
321     free(state.ubuf);
322     free(state.cbuf);
323     free(state.obuf);
324
325     return status;
326 }