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