change default padding to 8K, or 64K if input audio stream is >= 20 minutes long
[flac.git] / src / test_libFLAC / bitbuffer.c
1 /* test_libFLAC - Unit tester for libFLAC
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "FLAC/assert.h"
24 #include "private/bitbuffer.h" /* from the libFLAC private include area */
25 #include <stdio.h>
26 #include <string.h> /* for memcmp() */
27
28 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
29 #ifdef _MSC_VER
30 #define FLAC__U64L(x) x
31 #else
32 #define FLAC__U64L(x) x##LLU
33 #endif
34
35 /*
36  * WATCHOUT!  Since FLAC__BitBuffer 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/bitbuffer.c
39  */
40 struct FLAC__BitBuffer {
41         FLAC__blurb *buffer;
42         unsigned capacity; /* in blurbs */
43         unsigned blurbs, bits;
44         unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
45         unsigned consumed_blurbs, consumed_bits;
46         unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
47         FLAC__uint16 read_crc16;
48 #if FLAC__BITS_PER_BLURB == 32
49         unsigned crc16_align;
50 #endif
51         FLAC__blurb save_head, save_tail;
52 };
53
54
55 static FLAC__bool dummy_read_callback(FLAC__byte buffer[], unsigned *bytes, void *client_data)
56 {
57         (void)buffer, (void)bytes, (void)client_data;
58         return true;
59 }
60
61 FLAC__bool test_bitbuffer()
62 {
63         FLAC__BitBuffer *bb, *bb_zero, *bb_one, *bbcopy;
64         FLAC__bool ok;
65         unsigned i, j;
66         static FLAC__byte test_pattern1[19] = { 0xaa, 0xf0, 0xaa, 0xbe, 0xaa, 0xaa, 0xaa, 0xa8, 0x30, 0x0a, 0xaa, 0xaa, 0xaa, 0xad, 0xea, 0xdb, 0xee, 0xfa, 0xce };
67
68         FLAC__ASSERT(FLAC__BITS_PER_BLURB == 8);
69
70         printf("\n+++ libFLAC unit test: bitbuffer\n\n");
71
72         /*
73          * test new -> delete
74          */
75         printf("testing new... ");
76         bb = FLAC__bitbuffer_new();
77         if(0 == bb) {
78                 printf("FAILED, returned NULL\n");
79                 return false;
80         }
81         printf("OK\n");
82
83         printf("testing delete... ");
84         FLAC__bitbuffer_delete(bb);
85         printf("OK\n");
86
87         /*
88          * test new -> init -> delete
89          */
90         printf("testing new... ");
91         bb = FLAC__bitbuffer_new();
92         if(0 == bb) {
93                 printf("FAILED, returned NULL\n");
94                 return false;
95         }
96         printf("OK\n");
97
98         printf("testing init... ");
99         FLAC__bitbuffer_init(bb);
100         if(0 == bb) {
101                 printf("FAILED, returned NULL\n");
102                 return false;
103         }
104         printf("OK\n");
105
106         printf("testing delete... ");
107         FLAC__bitbuffer_delete(bb);
108         printf("OK\n");
109
110         /*
111          * test new -> init -> clear -> delete
112          */
113         printf("testing new... ");
114         bb = FLAC__bitbuffer_new();
115         if(0 == bb) {
116                 printf("FAILED, returned NULL\n");
117                 return false;
118         }
119         printf("OK\n");
120
121         printf("testing init... ");
122         FLAC__bitbuffer_init(bb);
123         if(0 == bb) {
124                 printf("FAILED, returned NULL\n");
125                 return false;
126         }
127         printf("OK\n");
128
129         printf("testing clear... ");
130         FLAC__bitbuffer_clear(bb);
131         if(0 == bb) {
132                 printf("FAILED, returned NULL\n");
133                 return false;
134         }
135         printf("OK\n");
136
137         printf("testing delete... ");
138         FLAC__bitbuffer_delete(bb);
139         printf("OK\n");
140
141         /*
142          * test normal usage
143          */
144         printf("testing new... ");
145         bb = FLAC__bitbuffer_new();
146         bb_zero = FLAC__bitbuffer_new();
147         bb_one = FLAC__bitbuffer_new();
148         bbcopy = FLAC__bitbuffer_new();
149         if(0 == bb || 0 == bb_zero || 0 == bb_one || 0 == bbcopy) {
150                 printf("FAILED, returned NULL\n");
151                 return false;
152         }
153         printf("OK\n");
154
155         printf("testing init... ");
156         ok = FLAC__bitbuffer_init(bb) && FLAC__bitbuffer_init(bb_zero) && FLAC__bitbuffer_init(bb_one) && FLAC__bitbuffer_init(bbcopy);
157         printf("%s\n", ok?"OK":"FAILED");
158         if(!ok)
159                 return false;
160
161         printf("testing clear... ");
162         ok = FLAC__bitbuffer_clear(bb) && FLAC__bitbuffer_clear(bb_zero) && FLAC__bitbuffer_clear(bb_one) && FLAC__bitbuffer_clear(bbcopy);
163         printf("%s\n", ok?"OK":"FAILED");
164         if(!ok)
165                 return false;
166
167         printf("setting up bb_one... ");
168         ok = FLAC__bitbuffer_write_raw_uint32(bb_one, 1, 7) && FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 6, dummy_read_callback, 0);
169         printf("%s\n", ok?"OK":"FAILED");
170         if(!ok)
171                 return false;
172         FLAC__bitbuffer_dump(bb_one, stdout);
173
174         printf("capacity = %u\n", bb->capacity);
175
176         printf("testing zeroes, raw_uint32*... ");
177         ok =
178                 FLAC__bitbuffer_write_raw_uint32(bb, 0x1, 1) &&
179                 FLAC__bitbuffer_write_raw_uint32(bb, 0x1, 2) &&
180                 FLAC__bitbuffer_write_raw_uint32(bb, 0xa, 5) &&
181                 FLAC__bitbuffer_write_raw_uint32(bb, 0xf0, 8) &&
182                 FLAC__bitbuffer_write_raw_uint32(bb, 0x2aa, 10) &&
183                 FLAC__bitbuffer_write_raw_uint32(bb, 0xf, 4) &&
184                 FLAC__bitbuffer_write_raw_uint32(bb, 0xaaaaaaaa, 32) &&
185                 FLAC__bitbuffer_write_zeroes(bb, 4) &&
186                 FLAC__bitbuffer_write_raw_uint32(bb, 0x3, 2) &&
187                 FLAC__bitbuffer_write_zeroes(bb, 8) &&
188                 FLAC__bitbuffer_write_raw_uint64(bb, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
189                 FLAC__bitbuffer_write_raw_uint32(bb, 0xace, 12)
190         ;
191         if(!ok) {
192                 printf("FAILED\n");
193                 FLAC__bitbuffer_dump(bb, stdout);
194                 return false;
195         }
196         if(bb->blurbs != sizeof(test_pattern1)) {
197                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
198                 FLAC__bitbuffer_dump(bb, stdout);
199                 return false;
200         }
201         if(bb->bits != 0) {
202                 printf("FAILED bit count %u != 0\n", bb->bits);
203                 FLAC__bitbuffer_dump(bb, stdout);
204                 return false;
205         }
206         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
207                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
208                 FLAC__bitbuffer_dump(bb, stdout);
209                 return false;
210         }
211         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0) {
212                 printf("FAILED pattern match\n");
213                 FLAC__bitbuffer_dump(bb, stdout);
214                 return false;
215         }
216         printf("OK\n");
217         FLAC__bitbuffer_dump(bb, stdout);
218
219         printf("testing raw_uint32 some more... ");
220         ok = FLAC__bitbuffer_write_raw_uint32(bb, 0x3d, 6);
221         if(!ok) {
222                 printf("FAILED\n");
223                 FLAC__bitbuffer_dump(bb, stdout);
224                 return false;
225         }
226         if(bb->blurbs != sizeof(test_pattern1)) {
227                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
228                 FLAC__bitbuffer_dump(bb, stdout);
229                 return false;
230         }
231         if(bb->bits != 6) {
232                 printf("FAILED bit count %u != 6\n", bb->bits);
233                 FLAC__bitbuffer_dump(bb, stdout);
234                 return false;
235         }
236         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
237                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
238                 FLAC__bitbuffer_dump(bb, stdout);
239                 return false;
240         }
241         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x3d) {
242                 printf("FAILED pattern match\n");
243                 FLAC__bitbuffer_dump(bb, stdout);
244                 return false;
245         }
246         printf("OK\n");
247         FLAC__bitbuffer_dump(bb, stdout);
248
249         printf("testing concatenate_aligned (bb_zero)... ");
250         ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_zero);
251         if(!ok) {
252                 printf("FAILED\n");
253                 FLAC__bitbuffer_dump(bb, stdout);
254                 return false;
255         }
256         if(bb->blurbs != sizeof(test_pattern1)) {
257                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
258                 FLAC__bitbuffer_dump(bb, stdout);
259                 return false;
260         }
261         if(bb->bits != 6) {
262                 printf("FAILED bit count %u != 6\n", bb->bits);
263                 FLAC__bitbuffer_dump(bb, stdout);
264                 return false;
265         }
266         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
267                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
268                 FLAC__bitbuffer_dump(bb, stdout);
269                 return false;
270         }
271         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x3d) {
272                 printf("FAILED pattern match\n");
273                 FLAC__bitbuffer_dump(bb, stdout);
274                 return false;
275         }
276         printf("OK\n");
277         FLAC__bitbuffer_dump(bb, stdout);
278
279         printf("testing concatenate_aligned (bb_one)... ");
280         ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
281         if(!ok) {
282                 printf("FAILED\n");
283                 FLAC__bitbuffer_dump(bb, stdout);
284                 return false;
285         }
286         if(bb->blurbs != sizeof(test_pattern1)) {
287                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
288                 FLAC__bitbuffer_dump(bb, stdout);
289                 return false;
290         }
291         if(bb->bits != 7) {
292                 printf("FAILED bit count %u != 7\n", bb->bits);
293                 FLAC__bitbuffer_dump(bb, stdout);
294                 return false;
295         }
296         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
297                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
298                 FLAC__bitbuffer_dump(bb, stdout);
299                 return false;
300         }
301         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x7b) {
302                 printf("FAILED pattern match\n");
303                 FLAC__bitbuffer_dump(bb, stdout);
304                 return false;
305         }
306         printf("OK\n");
307         FLAC__bitbuffer_dump(bb, stdout);
308
309         printf("testing concatenate_aligned (bb_one again)... ");
310         (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 1, 1);
311         (void)FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 1, dummy_read_callback, 0);
312         ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
313         if(!ok) {
314                 printf("FAILED\n");
315                 FLAC__bitbuffer_dump(bb, stdout);
316                 return false;
317         }
318         if(bb->blurbs != sizeof(test_pattern1)+1) {
319                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+1);
320                 FLAC__bitbuffer_dump(bb, stdout);
321                 return false;
322         }
323         if(bb->bits != 0) {
324                 printf("FAILED bit count %u != 0\n", bb->bits);
325                 FLAC__bitbuffer_dump(bb, stdout);
326                 return false;
327         }
328         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
329                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
330                 FLAC__bitbuffer_dump(bb, stdout);
331                 return false;
332         }
333         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-1] != 0xf7) {
334                 printf("FAILED pattern match\n");
335                 FLAC__bitbuffer_dump(bb, stdout);
336                 return false;
337         }
338         printf("OK\n");
339         FLAC__bitbuffer_dump(bb, stdout);
340
341         printf("testing concatenate_aligned (bb_four)... ");
342         (void)FLAC__bitbuffer_clear(bb_one);
343         (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 8, 4);
344         ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
345         if(!ok) {
346                 printf("FAILED\n");
347                 FLAC__bitbuffer_dump(bb, stdout);
348                 return false;
349         }
350         if(bb->blurbs != sizeof(test_pattern1)+1) {
351                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+1);
352                 FLAC__bitbuffer_dump(bb, stdout);
353                 return false;
354         }
355         if(bb->bits != 4) {
356                 printf("FAILED bit count %u != 4\n", bb->bits);
357                 FLAC__bitbuffer_dump(bb, stdout);
358                 return false;
359         }
360         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
361                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
362                 FLAC__bitbuffer_dump(bb, stdout);
363                 return false;
364         }
365         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x08) {
366                 printf("FAILED pattern match\n");
367                 FLAC__bitbuffer_dump(bb, stdout);
368                 return false;
369         }
370         printf("OK\n");
371         FLAC__bitbuffer_dump(bb, stdout);
372
373         printf("testing concatenate_aligned (bb_eight)... ");
374         (void)FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 4, dummy_read_callback, 0);
375         (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 0xaa, 8);
376         ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
377         if(!ok) {
378                 printf("FAILED\n");
379                 FLAC__bitbuffer_dump(bb, stdout);
380                 return false;
381         }
382         if(bb->blurbs != sizeof(test_pattern1)+2) {
383                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+2);
384                 FLAC__bitbuffer_dump(bb, stdout);
385                 return false;
386         }
387         if(bb->bits != 4) {
388                 printf("FAILED bit count %u != 4\n", bb->bits);
389                 FLAC__bitbuffer_dump(bb, stdout);
390                 return false;
391         }
392         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
393                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
394                 FLAC__bitbuffer_dump(bb, stdout);
395                 return false;
396         }
397         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-1] != 0x8a || bb->buffer[bb->blurbs] != 0x0a) {
398                 printf("FAILED pattern match\n");
399                 FLAC__bitbuffer_dump(bb, stdout);
400                 return false;
401         }
402         printf("OK\n");
403         FLAC__bitbuffer_dump(bb, stdout);
404
405         printf("testing concatenate_aligned (bb_seventeen)... ");
406         (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 0x155, 9);
407         ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
408         if(!ok) {
409                 printf("FAILED\n");
410                 FLAC__bitbuffer_dump(bb, stdout);
411                 return false;
412         }
413         if(bb->blurbs != sizeof(test_pattern1)+4) {
414                 printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+4);
415                 FLAC__bitbuffer_dump(bb, stdout);
416                 return false;
417         }
418         if(bb->bits != 5) {
419                 printf("FAILED bit count %u != 5\n", bb->bits);
420                 FLAC__bitbuffer_dump(bb, stdout);
421                 return false;
422         }
423         if(bb->total_bits != 8*bb->blurbs+bb->bits) {
424                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
425                 FLAC__bitbuffer_dump(bb, stdout);
426                 return false;
427         }
428         if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-3] != 0x8a || bb->buffer[bb->blurbs-2] != 0xaa || bb->buffer[bb->blurbs-1] != 0xaa || bb->buffer[bb->blurbs] != 0x15) {
429                 printf("FAILED pattern match\n");
430                 FLAC__bitbuffer_dump(bb, stdout);
431                 return false;
432         }
433         printf("OK\n");
434         FLAC__bitbuffer_dump(bb, stdout);
435
436         printf("testing utf8_uint32(0x00000000)... ");
437         FLAC__bitbuffer_clear(bb);
438         FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000000);
439         ok = bb->total_bits == 8 && bb->buffer[0] == 0;
440         printf("%s\n", ok?"OK":"FAILED");
441         if(!ok) {
442                 FLAC__bitbuffer_dump(bb, stdout);
443                 return false;
444         }
445
446         printf("testing utf8_uint32(0x0000007F)... ");
447         FLAC__bitbuffer_clear(bb);
448         FLAC__bitbuffer_write_utf8_uint32(bb, 0x0000007F);
449         ok = bb->total_bits == 8 && bb->buffer[0] == 0x7F;
450         printf("%s\n", ok?"OK":"FAILED");
451         if(!ok) {
452                 FLAC__bitbuffer_dump(bb, stdout);
453                 return false;
454         }
455
456         printf("testing utf8_uint32(0x00000080)... ");
457         FLAC__bitbuffer_clear(bb);
458         FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000080);
459         ok = bb->total_bits == 16 && bb->buffer[0] == 0xC2 && bb->buffer[1] == 0x80;
460         printf("%s\n", ok?"OK":"FAILED");
461         if(!ok) {
462                 FLAC__bitbuffer_dump(bb, stdout);
463                 return false;
464         }
465
466         printf("testing utf8_uint32(0x000007FF)... ");
467         FLAC__bitbuffer_clear(bb);
468         FLAC__bitbuffer_write_utf8_uint32(bb, 0x000007FF);
469         ok = bb->total_bits == 16 && bb->buffer[0] == 0xDF && bb->buffer[1] == 0xBF;
470         printf("%s\n", ok?"OK":"FAILED");
471         if(!ok) {
472                 FLAC__bitbuffer_dump(bb, stdout);
473                 return false;
474         }
475
476         printf("testing utf8_uint32(0x00000800)... ");
477         FLAC__bitbuffer_clear(bb);
478         FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000800);
479         ok = bb->total_bits == 24 && bb->buffer[0] == 0xE0 && bb->buffer[1] == 0xA0 && bb->buffer[2] == 0x80;
480         printf("%s\n", ok?"OK":"FAILED");
481         if(!ok) {
482                 FLAC__bitbuffer_dump(bb, stdout);
483                 return false;
484         }
485
486         printf("testing utf8_uint32(0x0000FFFF)... ");
487         FLAC__bitbuffer_clear(bb);
488         FLAC__bitbuffer_write_utf8_uint32(bb, 0x0000FFFF);
489         ok = bb->total_bits == 24 && bb->buffer[0] == 0xEF && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF;
490         printf("%s\n", ok?"OK":"FAILED");
491         if(!ok) {
492                 FLAC__bitbuffer_dump(bb, stdout);
493                 return false;
494         }
495
496         printf("testing utf8_uint32(0x00010000)... ");
497         FLAC__bitbuffer_clear(bb);
498         FLAC__bitbuffer_write_utf8_uint32(bb, 0x00010000);
499         ok = bb->total_bits == 32 && bb->buffer[0] == 0xF0 && bb->buffer[1] == 0x90 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80;
500         printf("%s\n", ok?"OK":"FAILED");
501         if(!ok) {
502                 FLAC__bitbuffer_dump(bb, stdout);
503                 return false;
504         }
505
506         printf("testing utf8_uint32(0x001FFFFF)... ");
507         FLAC__bitbuffer_clear(bb);
508         FLAC__bitbuffer_write_utf8_uint32(bb, 0x001FFFFF);
509         ok = bb->total_bits == 32 && bb->buffer[0] == 0xF7 && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF;
510         printf("%s\n", ok?"OK":"FAILED");
511         if(!ok) {
512                 FLAC__bitbuffer_dump(bb, stdout);
513                 return false;
514         }
515
516         printf("testing utf8_uint32(0x00200000)... ");
517         FLAC__bitbuffer_clear(bb);
518         FLAC__bitbuffer_write_utf8_uint32(bb, 0x00200000);
519         ok = bb->total_bits == 40 && bb->buffer[0] == 0xF8 && bb->buffer[1] == 0x88 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80;
520         printf("%s\n", ok?"OK":"FAILED");
521         if(!ok) {
522                 FLAC__bitbuffer_dump(bb, stdout);
523                 return false;
524         }
525
526         printf("testing utf8_uint32(0x03FFFFFF)... ");
527         FLAC__bitbuffer_clear(bb);
528         FLAC__bitbuffer_write_utf8_uint32(bb, 0x03FFFFFF);
529         ok = bb->total_bits == 40 && bb->buffer[0] == 0xFB && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF;
530         printf("%s\n", ok?"OK":"FAILED");
531         if(!ok) {
532                 FLAC__bitbuffer_dump(bb, stdout);
533                 return false;
534         }
535
536         printf("testing utf8_uint32(0x04000000)... ");
537         FLAC__bitbuffer_clear(bb);
538         FLAC__bitbuffer_write_utf8_uint32(bb, 0x04000000);
539         ok = bb->total_bits == 48 && bb->buffer[0] == 0xFC && bb->buffer[1] == 0x84 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80;
540         printf("%s\n", ok?"OK":"FAILED");
541         if(!ok) {
542                 FLAC__bitbuffer_dump(bb, stdout);
543                 return false;
544         }
545
546         printf("testing utf8_uint32(0x7FFFFFFF)... ");
547         FLAC__bitbuffer_clear(bb);
548         FLAC__bitbuffer_write_utf8_uint32(bb, 0x7FFFFFFF);
549         ok = bb->total_bits == 48 && bb->buffer[0] == 0xFD && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF;
550         printf("%s\n", ok?"OK":"FAILED");
551         if(!ok) {
552                 FLAC__bitbuffer_dump(bb, stdout);
553                 return false;
554         }
555
556         printf("testing utf8_uint64(0x0000000000000000)... ");
557         FLAC__bitbuffer_clear(bb);
558         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000000);
559         ok = bb->total_bits == 8 && bb->buffer[0] == 0;
560         printf("%s\n", ok?"OK":"FAILED");
561         if(!ok) {
562                 FLAC__bitbuffer_dump(bb, stdout);
563                 return false;
564         }
565
566         printf("testing utf8_uint64(0x000000000000007F)... ");
567         FLAC__bitbuffer_clear(bb);
568         FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000000000007F);
569         ok = bb->total_bits == 8 && bb->buffer[0] == 0x7F;
570         printf("%s\n", ok?"OK":"FAILED");
571         if(!ok) {
572                 FLAC__bitbuffer_dump(bb, stdout);
573                 return false;
574         }
575
576         printf("testing utf8_uint64(0x0000000000000080)... ");
577         FLAC__bitbuffer_clear(bb);
578         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000080);
579         ok = bb->total_bits == 16 && bb->buffer[0] == 0xC2 && bb->buffer[1] == 0x80;
580         printf("%s\n", ok?"OK":"FAILED");
581         if(!ok) {
582                 FLAC__bitbuffer_dump(bb, stdout);
583                 return false;
584         }
585
586         printf("testing utf8_uint64(0x00000000000007FF)... ");
587         FLAC__bitbuffer_clear(bb);
588         FLAC__bitbuffer_write_utf8_uint64(bb, 0x00000000000007FF);
589         ok = bb->total_bits == 16 && bb->buffer[0] == 0xDF && bb->buffer[1] == 0xBF;
590         printf("%s\n", ok?"OK":"FAILED");
591         if(!ok) {
592                 FLAC__bitbuffer_dump(bb, stdout);
593                 return false;
594         }
595
596         printf("testing utf8_uint64(0x0000000000000800)... ");
597         FLAC__bitbuffer_clear(bb);
598         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000800);
599         ok = bb->total_bits == 24 && bb->buffer[0] == 0xE0 && bb->buffer[1] == 0xA0 && bb->buffer[2] == 0x80;
600         printf("%s\n", ok?"OK":"FAILED");
601         if(!ok) {
602                 FLAC__bitbuffer_dump(bb, stdout);
603                 return false;
604         }
605
606         printf("testing utf8_uint64(0x000000000000FFFF)... ");
607         FLAC__bitbuffer_clear(bb);
608         FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000000000FFFF);
609         ok = bb->total_bits == 24 && bb->buffer[0] == 0xEF && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF;
610         printf("%s\n", ok?"OK":"FAILED");
611         if(!ok) {
612                 FLAC__bitbuffer_dump(bb, stdout);
613                 return false;
614         }
615
616         printf("testing utf8_uint64(0x0000000000010000)... ");
617         FLAC__bitbuffer_clear(bb);
618         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000010000);
619         ok = bb->total_bits == 32 && bb->buffer[0] == 0xF0 && bb->buffer[1] == 0x90 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80;
620         printf("%s\n", ok?"OK":"FAILED");
621         if(!ok) {
622                 FLAC__bitbuffer_dump(bb, stdout);
623                 return false;
624         }
625
626         printf("testing utf8_uint64(0x00000000001FFFFF)... ");
627         FLAC__bitbuffer_clear(bb);
628         FLAC__bitbuffer_write_utf8_uint64(bb, 0x00000000001FFFFF);
629         ok = bb->total_bits == 32 && bb->buffer[0] == 0xF7 && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF;
630         printf("%s\n", ok?"OK":"FAILED");
631         if(!ok) {
632                 FLAC__bitbuffer_dump(bb, stdout);
633                 return false;
634         }
635
636         printf("testing utf8_uint64(0x0000000000200000)... ");
637         FLAC__bitbuffer_clear(bb);
638         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000200000);
639         ok = bb->total_bits == 40 && bb->buffer[0] == 0xF8 && bb->buffer[1] == 0x88 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80;
640         printf("%s\n", ok?"OK":"FAILED");
641         if(!ok) {
642                 FLAC__bitbuffer_dump(bb, stdout);
643                 return false;
644         }
645
646         printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
647         FLAC__bitbuffer_clear(bb);
648         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000003FFFFFF);
649         ok = bb->total_bits == 40 && bb->buffer[0] == 0xFB && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF;
650         printf("%s\n", ok?"OK":"FAILED");
651         if(!ok) {
652                 FLAC__bitbuffer_dump(bb, stdout);
653                 return false;
654         }
655
656         printf("testing utf8_uint64(0x0000000004000000)... ");
657         FLAC__bitbuffer_clear(bb);
658         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000004000000);
659         ok = bb->total_bits == 48 && bb->buffer[0] == 0xFC && bb->buffer[1] == 0x84 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80;
660         printf("%s\n", ok?"OK":"FAILED");
661         if(!ok) {
662                 FLAC__bitbuffer_dump(bb, stdout);
663                 return false;
664         }
665
666         printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
667         FLAC__bitbuffer_clear(bb);
668         FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000007FFFFFFF);
669         ok = bb->total_bits == 48 && bb->buffer[0] == 0xFD && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF;
670         printf("%s\n", ok?"OK":"FAILED");
671         if(!ok) {
672                 FLAC__bitbuffer_dump(bb, stdout);
673                 return false;
674         }
675
676         printf("testing utf8_uint64(0x0000000080000000)... ");
677         FLAC__bitbuffer_clear(bb);
678         FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000080000000);
679         ok = bb->total_bits == 56 && bb->buffer[0] == 0xFE && bb->buffer[1] == 0x82 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80 && bb->buffer[6] == 0x80;
680         printf("%s\n", ok?"OK":"FAILED");
681         if(!ok) {
682                 FLAC__bitbuffer_dump(bb, stdout);
683                 return false;
684         }
685
686         printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
687         FLAC__bitbuffer_clear(bb);
688         FLAC__bitbuffer_write_utf8_uint64(bb, FLAC__U64L(0x0000000FFFFFFFFF));
689         ok = bb->total_bits == 56 && bb->buffer[0] == 0xFE && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF && bb->buffer[6] == 0xBF;
690         printf("%s\n", ok?"OK":"FAILED");
691         if(!ok) {
692                 FLAC__bitbuffer_dump(bb, stdout);
693                 return false;
694         }
695
696         printf("testing grow... ");
697         FLAC__bitbuffer_clear(bb);
698         FLAC__bitbuffer_write_raw_uint32(bb, 0xa, 4);
699         j = bb->capacity;
700         for(i = 0; i < j; i++)
701                 FLAC__bitbuffer_write_raw_uint32(bb, 0xaa, 8);
702         ok = bb->total_bits = i*8+4 && bb->buffer[0] == 0xaa && bb->buffer[i] == 0xa;
703         printf("%s\n", ok?"OK":"FAILED");
704         if(!ok) {
705                 FLAC__bitbuffer_dump(bb, stdout);
706                 return false;
707         }
708         printf("capacity = %u\n", bb->capacity);
709
710         printf("testing clone... ");
711         ok = FLAC__bitbuffer_clone(bbcopy, bb);
712         if(!ok) {
713                 printf("FAILED\n");
714                 FLAC__bitbuffer_dump(bb, stdout);
715                 FLAC__bitbuffer_dump(bbcopy, stdout);
716                 return false;
717         }
718         if(bb->blurbs != bbcopy->blurbs) {
719                 printf("FAILED byte count %u != %u\n", bb->blurbs, bbcopy->blurbs);
720                 FLAC__bitbuffer_dump(bb, stdout);
721                 FLAC__bitbuffer_dump(bbcopy, stdout);
722                 return false;
723         }
724         if(bb->bits != bbcopy->bits) {
725                 printf("FAILED bit count %u != %u\n", bb->bits, bbcopy->bits);
726                 FLAC__bitbuffer_dump(bb, stdout);
727                 FLAC__bitbuffer_dump(bbcopy, stdout);
728                 return false;
729         }
730         if(bb->total_bits != bbcopy->total_bits) {
731                 printf("FAILED total_bits count %u != %u\n", bb->total_bits, bbcopy->total_bits);
732                 FLAC__bitbuffer_dump(bb, stdout);
733                 FLAC__bitbuffer_dump(bbcopy, stdout);
734                 return false;
735         }
736         if(memcmp(bb->buffer, bbcopy->buffer, sizeof(FLAC__byte)*bb->capacity) != 0) {
737                 printf("FAILED pattern match\n");
738                 FLAC__bitbuffer_dump(bb, stdout);
739                 FLAC__bitbuffer_dump(bbcopy, stdout);
740                 return false;
741         }
742         printf("OK\n");
743
744         printf("testing free... ");
745         FLAC__bitbuffer_free(bb);
746         FLAC__bitbuffer_free(bbcopy);
747         printf("OK\n");
748
749         printf("testing delete... ");
750         FLAC__bitbuffer_delete(bb);
751         FLAC__bitbuffer_delete(bb_zero);
752         FLAC__bitbuffer_delete(bb_one);
753         FLAC__bitbuffer_delete(bbcopy);
754         printf("OK\n");
755
756         printf("\nPASSED!\n");
757         return true;
758 }