1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "FLAC/assert.h"
24 #include "private/bitwriter.h" /* from the libFLAC private include area */
26 #include <string.h> /* for memcmp() */
28 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
30 #define FLAC__U64L(x) x
32 #define FLAC__U64L(x) x##LLU
36 * WATCHOUT! Since FLAC__BitWriter is a private structure, we use a copy of
37 * the definition here to get at the internals. Make sure this is kept up
38 * to date with what is in ../libFLAC/bitwriter.c
40 typedef FLAC__uint32 bwword;
42 struct FLAC__BitWriter {
44 bwword accum; /* accumulator; when full, accum is appended to buffer */
45 unsigned capacity; /* of buffer in words */
46 unsigned words; /* # of complete words in buffer */
47 unsigned bits; /* # of used bits in accum */
50 #define TOTAL_BITS(bw) ((bw)->words*sizeof(bwword)*8 + (bw)->bits)
53 FLAC__bool test_bitwriter()
59 static bwword test_pattern1[5] = { 0xaaf0aabe, 0xaaaaaaa8, 0x300aaaaa, 0xaaadeadb, 0x00eeface };
61 static bwword test_pattern1[5] = { 0xbeaaf0aa, 0xa8aaaaaa, 0xaaaa0a30, 0xdbeaadaa, 0x00eeface };
63 unsigned words, bits; /* what we think bw->words and bw->bits should be */
65 printf("\n+++ libFLAC unit test: bitwriter\n\n");
70 printf("testing new... ");
71 bw = FLAC__bitwriter_new();
73 printf("FAILED, returned NULL\n");
78 printf("testing delete... ");
79 FLAC__bitwriter_delete(bw);
83 * test new -> init -> delete
85 printf("testing new... ");
86 bw = FLAC__bitwriter_new();
88 printf("FAILED, returned NULL\n");
93 printf("testing init... ");
94 FLAC__bitwriter_init(bw);
96 printf("FAILED, returned NULL\n");
101 printf("testing delete... ");
102 FLAC__bitwriter_delete(bw);
106 * test new -> init -> clear -> delete
108 printf("testing new... ");
109 bw = FLAC__bitwriter_new();
111 printf("FAILED, returned NULL\n");
116 printf("testing init... ");
117 FLAC__bitwriter_init(bw);
119 printf("FAILED, returned NULL\n");
124 printf("testing clear... ");
125 FLAC__bitwriter_clear(bw);
127 printf("FAILED, returned NULL\n");
132 printf("testing delete... ");
133 FLAC__bitwriter_delete(bw);
139 printf("testing new... ");
140 bw = FLAC__bitwriter_new();
142 printf("FAILED, returned NULL\n");
147 printf("testing init... ");
148 ok = FLAC__bitwriter_init(bw);
149 printf("%s\n", ok?"OK":"FAILED");
153 printf("testing clear... ");
154 FLAC__bitwriter_clear(bw);
159 printf("capacity = %u\n", bw->capacity);
161 printf("testing zeroes, raw_uint32*... ");
163 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 1) &&
164 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 2) &&
165 FLAC__bitwriter_write_raw_uint32(bw, 0xa, 5) &&
166 FLAC__bitwriter_write_raw_uint32(bw, 0xf0, 8) &&
167 FLAC__bitwriter_write_raw_uint32(bw, 0x2aa, 10) &&
168 FLAC__bitwriter_write_raw_uint32(bw, 0xf, 4) &&
169 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32) &&
170 FLAC__bitwriter_write_zeroes(bw, 4) &&
171 FLAC__bitwriter_write_raw_uint32(bw, 0x3, 2) &&
172 FLAC__bitwriter_write_zeroes(bw, 8) &&
173 FLAC__bitwriter_write_raw_uint64(bw, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
174 FLAC__bitwriter_write_raw_uint32(bw, 0xace, 12)
178 FLAC__bitwriter_dump(bw, stdout);
183 if(bw->words != words) {
184 printf("FAILED byte count %u != %u\n", bw->words, words);
185 FLAC__bitwriter_dump(bw, stdout);
188 if(bw->bits != bits) {
189 printf("FAILED bit count %u != %u\n", bw->bits, bits);
190 FLAC__bitwriter_dump(bw, stdout);
193 if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
194 printf("FAILED pattern match (buffer)\n");
195 FLAC__bitwriter_dump(bw, stdout);
198 if((bw->accum & 0x00ffffff) != test_pattern1[words]) {
199 printf("FAILED pattern match (bw->accum=%08X != %08X)\n", bw->accum&0x00ffffff, test_pattern1[words]);
200 FLAC__bitwriter_dump(bw, stdout);
204 FLAC__bitwriter_dump(bw, stdout);
206 printf("testing raw_uint32 some more... ");
207 ok = FLAC__bitwriter_write_raw_uint32(bw, 0x3d, 6);
210 FLAC__bitwriter_dump(bw, stdout);
214 test_pattern1[words] <<= 6;
215 test_pattern1[words] |= 0x3d;
216 if(bw->words != words) {
217 printf("FAILED byte count %u != %u\n", bw->words, words);
218 FLAC__bitwriter_dump(bw, stdout);
221 if(bw->bits != bits) {
222 printf("FAILED bit count %u != %u\n", bw->bits, bits);
223 FLAC__bitwriter_dump(bw, stdout);
226 if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
227 printf("FAILED pattern match (buffer)\n");
228 FLAC__bitwriter_dump(bw, stdout);
231 if((bw->accum & 0x3fffffff) != test_pattern1[words]) {
232 printf("FAILED pattern match (bw->accum=%08X != %08X)\n", bw->accum&0x3fffffff, test_pattern1[words]);
233 FLAC__bitwriter_dump(bw, stdout);
237 FLAC__bitwriter_dump(bw, stdout);
239 printf("testing utf8_uint32(0x00000000)... ");
240 FLAC__bitwriter_clear(bw);
241 FLAC__bitwriter_write_utf8_uint32(bw, 0x00000000);
242 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
243 printf("%s\n", ok?"OK":"FAILED");
245 FLAC__bitwriter_dump(bw, stdout);
249 printf("testing utf8_uint32(0x0000007F)... ");
250 FLAC__bitwriter_clear(bw);
251 FLAC__bitwriter_write_utf8_uint32(bw, 0x0000007F);
252 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
253 printf("%s\n", ok?"OK":"FAILED");
255 FLAC__bitwriter_dump(bw, stdout);
259 printf("testing utf8_uint32(0x00000080)... ");
260 FLAC__bitwriter_clear(bw);
261 FLAC__bitwriter_write_utf8_uint32(bw, 0x00000080);
262 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
263 printf("%s\n", ok?"OK":"FAILED");
265 FLAC__bitwriter_dump(bw, stdout);
269 printf("testing utf8_uint32(0x000007FF)... ");
270 FLAC__bitwriter_clear(bw);
271 FLAC__bitwriter_write_utf8_uint32(bw, 0x000007FF);
272 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
273 printf("%s\n", ok?"OK":"FAILED");
275 FLAC__bitwriter_dump(bw, stdout);
279 printf("testing utf8_uint32(0x00000800)... ");
280 FLAC__bitwriter_clear(bw);
281 FLAC__bitwriter_write_utf8_uint32(bw, 0x00000800);
282 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
283 printf("%s\n", ok?"OK":"FAILED");
285 FLAC__bitwriter_dump(bw, stdout);
289 printf("testing utf8_uint32(0x0000FFFF)... ");
290 FLAC__bitwriter_clear(bw);
291 FLAC__bitwriter_write_utf8_uint32(bw, 0x0000FFFF);
292 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
293 printf("%s\n", ok?"OK":"FAILED");
295 FLAC__bitwriter_dump(bw, stdout);
299 printf("testing utf8_uint32(0x00010000)... ");
300 FLAC__bitwriter_clear(bw);
301 FLAC__bitwriter_write_utf8_uint32(bw, 0x00010000);
303 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
305 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
307 printf("%s\n", ok?"OK":"FAILED");
309 FLAC__bitwriter_dump(bw, stdout);
313 printf("testing utf8_uint32(0x001FFFFF)... ");
314 FLAC__bitwriter_clear(bw);
315 FLAC__bitwriter_write_utf8_uint32(bw, 0x001FFFFF);
317 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
319 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
321 printf("%s\n", ok?"OK":"FAILED");
323 FLAC__bitwriter_dump(bw, stdout);
327 printf("testing utf8_uint32(0x00200000)... ");
328 FLAC__bitwriter_clear(bw);
329 FLAC__bitwriter_write_utf8_uint32(bw, 0x00200000);
331 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
333 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
335 printf("%s\n", ok?"OK":"FAILED");
337 FLAC__bitwriter_dump(bw, stdout);
341 printf("testing utf8_uint32(0x03FFFFFF)... ");
342 FLAC__bitwriter_clear(bw);
343 FLAC__bitwriter_write_utf8_uint32(bw, 0x03FFFFFF);
345 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
347 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
349 printf("%s\n", ok?"OK":"FAILED");
351 FLAC__bitwriter_dump(bw, stdout);
355 printf("testing utf8_uint32(0x04000000)... ");
356 FLAC__bitwriter_clear(bw);
357 FLAC__bitwriter_write_utf8_uint32(bw, 0x04000000);
359 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
361 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
363 printf("%s\n", ok?"OK":"FAILED");
365 FLAC__bitwriter_dump(bw, stdout);
369 printf("testing utf8_uint32(0x7FFFFFFF)... ");
370 FLAC__bitwriter_clear(bw);
371 FLAC__bitwriter_write_utf8_uint32(bw, 0x7FFFFFFF);
373 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
375 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
377 printf("%s\n", ok?"OK":"FAILED");
379 FLAC__bitwriter_dump(bw, stdout);
383 printf("testing utf8_uint64(0x0000000000000000)... ");
384 FLAC__bitwriter_clear(bw);
385 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000000);
386 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
387 printf("%s\n", ok?"OK":"FAILED");
389 FLAC__bitwriter_dump(bw, stdout);
393 printf("testing utf8_uint64(0x000000000000007F)... ");
394 FLAC__bitwriter_clear(bw);
395 FLAC__bitwriter_write_utf8_uint64(bw, 0x000000000000007F);
396 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
397 printf("%s\n", ok?"OK":"FAILED");
399 FLAC__bitwriter_dump(bw, stdout);
403 printf("testing utf8_uint64(0x0000000000000080)... ");
404 FLAC__bitwriter_clear(bw);
405 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000080);
406 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
407 printf("%s\n", ok?"OK":"FAILED");
409 FLAC__bitwriter_dump(bw, stdout);
413 printf("testing utf8_uint64(0x00000000000007FF)... ");
414 FLAC__bitwriter_clear(bw);
415 FLAC__bitwriter_write_utf8_uint64(bw, 0x00000000000007FF);
416 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
417 printf("%s\n", ok?"OK":"FAILED");
419 FLAC__bitwriter_dump(bw, stdout);
423 printf("testing utf8_uint64(0x0000000000000800)... ");
424 FLAC__bitwriter_clear(bw);
425 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000800);
426 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
427 printf("%s\n", ok?"OK":"FAILED");
429 FLAC__bitwriter_dump(bw, stdout);
433 printf("testing utf8_uint64(0x000000000000FFFF)... ");
434 FLAC__bitwriter_clear(bw);
435 FLAC__bitwriter_write_utf8_uint64(bw, 0x000000000000FFFF);
436 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
437 printf("%s\n", ok?"OK":"FAILED");
439 FLAC__bitwriter_dump(bw, stdout);
443 printf("testing utf8_uint64(0x0000000000010000)... ");
444 FLAC__bitwriter_clear(bw);
445 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000010000);
447 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
449 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
451 printf("%s\n", ok?"OK":"FAILED");
453 FLAC__bitwriter_dump(bw, stdout);
457 printf("testing utf8_uint64(0x00000000001FFFFF)... ");
458 FLAC__bitwriter_clear(bw);
459 FLAC__bitwriter_write_utf8_uint64(bw, 0x00000000001FFFFF);
461 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
463 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
465 printf("%s\n", ok?"OK":"FAILED");
467 FLAC__bitwriter_dump(bw, stdout);
471 printf("testing utf8_uint64(0x0000000000200000)... ");
472 FLAC__bitwriter_clear(bw);
473 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000200000);
475 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
477 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
479 printf("%s\n", ok?"OK":"FAILED");
481 FLAC__bitwriter_dump(bw, stdout);
485 printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
486 FLAC__bitwriter_clear(bw);
487 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000003FFFFFF);
489 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
491 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
493 printf("%s\n", ok?"OK":"FAILED");
495 FLAC__bitwriter_dump(bw, stdout);
499 printf("testing utf8_uint64(0x0000000004000000)... ");
500 FLAC__bitwriter_clear(bw);
501 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000004000000);
503 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
505 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
507 printf("%s\n", ok?"OK":"FAILED");
509 FLAC__bitwriter_dump(bw, stdout);
513 printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
514 FLAC__bitwriter_clear(bw);
515 FLAC__bitwriter_write_utf8_uint64(bw, 0x000000007FFFFFFF);
517 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
519 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
521 printf("%s\n", ok?"OK":"FAILED");
523 FLAC__bitwriter_dump(bw, stdout);
527 printf("testing utf8_uint64(0x0000000080000000)... ");
528 FLAC__bitwriter_clear(bw);
529 FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000080000000);
531 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFE828080 && (bw->accum & 0xffffff) == 0x808080;
533 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0x808082FE && (bw->accum & 0xffffff) == 0x808080;
535 printf("%s\n", ok?"OK":"FAILED");
537 FLAC__bitwriter_dump(bw, stdout);
541 printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
542 FLAC__bitwriter_clear(bw);
543 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000FFFFFFFFF));
545 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFEBFBFBF && (bw->accum & 0xffffff) == 0xBFBFBF;
547 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xBFBFBFFE && (bw->accum & 0xffffff) == 0xBFBFBF;
549 printf("%s\n", ok?"OK":"FAILED");
551 FLAC__bitwriter_dump(bw, stdout);
555 printf("testing grow... ");
556 FLAC__bitwriter_clear(bw);
557 FLAC__bitwriter_write_raw_uint32(bw, 0x5, 4);
559 for(i = 0; i < j; i++)
560 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32);
562 ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0x5aaaaaaa && (bw->accum & 0xf) == 0xa;
564 ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0xaaaaaa5a && (bw->accum & 0xf) == 0xa;
566 printf("%s\n", ok?"OK":"FAILED");
568 FLAC__bitwriter_dump(bw, stdout);
571 printf("capacity = %u\n", bw->capacity);
573 printf("testing free... ");
574 FLAC__bitwriter_free(bw);
577 printf("testing delete... ");
578 FLAC__bitwriter_delete(bw);
581 printf("\nPASSED!\n");