test_libFLAC/bitwriter.c: Unify with bitwriter.c
[flac.git] / src / test_libFLAC / bitwriter.c
1 /* test_libFLAC - Unit tester for libFLAC
2  * Copyright (C) 2000-2009  Josh Coalson
3  * Copyright (C) 2011-2014  Xiph.Org Foundation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "FLAC/assert.h"
25 #include "share/compat.h"
26 #include "private/bitwriter.h" /* from the libFLAC private include area */
27 #include "bitwriter.h"
28 #include <stdio.h>
29 #include <string.h> /* for memcmp() */
30
31 /*
32  * WATCHOUT!  Since FLAC__BitWriter is a private structure, we use a copy of
33  * the definition here to get at the internals.  Make sure this is kept up
34  * to date with what is in ../libFLAC/bitwriter.c
35  */
36 typedef FLAC__uint32 bwword;
37 #define FLAC__BITS_PER_WORD 32
38
39 struct FLAC__BitWriter {
40         bwword *buffer;
41         bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
42         unsigned capacity; /* capacity of buffer in words */
43         unsigned words; /* # of complete words in buffer */
44         unsigned bits; /* # of used bits in accum */
45 };
46
47 #define WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
48 #define TOTAL_BITS(bw) (WORDS_TO_BITS((bw)->words) + (bw)->bits)
49
50
51 FLAC__bool test_bitwriter(void)
52 {
53         FLAC__BitWriter *bw;
54         FLAC__bool ok;
55         unsigned i, j;
56 #if WORDS_BIGENDIAN
57         static bwword test_pattern1[5] = { 0xaaf0aabe, 0xaaaaaaa8, 0x300aaaaa, 0xaaadeadb, 0x00eeface };
58 #else
59         static bwword test_pattern1[5] = { 0xbeaaf0aa, 0xa8aaaaaa, 0xaaaa0a30, 0xdbeaadaa, 0x00eeface };
60 #endif
61         unsigned words, bits; /* what we think bw->words and bw->bits should be */
62
63         printf("\n+++ libFLAC unit test: bitwriter\n\n");
64
65         /*
66          * test new -> delete
67          */
68         printf("testing new... ");
69         bw = FLAC__bitwriter_new();
70         if(0 == bw) {
71                 printf("FAILED, returned NULL\n");
72                 return false;
73         }
74         printf("OK\n");
75
76         printf("testing delete... ");
77         FLAC__bitwriter_delete(bw);
78         printf("OK\n");
79
80         /*
81          * test new -> init -> delete
82          */
83         printf("testing new... ");
84         bw = FLAC__bitwriter_new();
85         if(0 == bw) {
86                 printf("FAILED, returned NULL\n");
87                 return false;
88         }
89         printf("OK\n");
90
91         printf("testing init... ");
92         FLAC__bitwriter_init(bw);
93         if(0 == bw) {
94                 printf("FAILED, returned NULL\n");
95                 return false;
96         }
97         printf("OK\n");
98
99         printf("testing delete... ");
100         FLAC__bitwriter_delete(bw);
101         printf("OK\n");
102
103         /*
104          * test new -> init -> clear -> delete
105          */
106         printf("testing new... ");
107         bw = FLAC__bitwriter_new();
108         if(0 == bw) {
109                 printf("FAILED, returned NULL\n");
110                 return false;
111         }
112         printf("OK\n");
113
114         printf("testing init... ");
115         FLAC__bitwriter_init(bw);
116         if(0 == bw) {
117                 printf("FAILED, returned NULL\n");
118                 return false;
119         }
120         printf("OK\n");
121
122         printf("testing clear... ");
123         FLAC__bitwriter_clear(bw);
124         if(0 == bw) {
125                 printf("FAILED, returned NULL\n");
126                 return false;
127         }
128         printf("OK\n");
129
130         printf("testing delete... ");
131         FLAC__bitwriter_delete(bw);
132         printf("OK\n");
133
134         /*
135          * test normal usage
136          */
137         printf("testing new... ");
138         bw = FLAC__bitwriter_new();
139         if(0 == bw) {
140                 printf("FAILED, returned NULL\n");
141                 return false;
142         }
143         printf("OK\n");
144
145         printf("testing init... ");
146         ok = FLAC__bitwriter_init(bw);
147         printf("%s\n", ok?"OK":"FAILED");
148         if(!ok)
149                 return false;
150
151         printf("testing clear... ");
152         FLAC__bitwriter_clear(bw);
153         printf("OK\n");
154
155         words = bits = 0;
156
157         printf("capacity = %u\n", bw->capacity);
158
159         printf("testing zeroes, raw_uint32*... ");
160         ok =
161                 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 1) &&
162                 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 2) &&
163                 FLAC__bitwriter_write_raw_uint32(bw, 0xa, 5) &&
164                 FLAC__bitwriter_write_raw_uint32(bw, 0xf0, 8) &&
165                 FLAC__bitwriter_write_raw_uint32(bw, 0x2aa, 10) &&
166                 FLAC__bitwriter_write_raw_uint32(bw, 0xf, 4) &&
167                 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32) &&
168                 FLAC__bitwriter_write_zeroes(bw, 4) &&
169                 FLAC__bitwriter_write_raw_uint32(bw, 0x3, 2) &&
170                 FLAC__bitwriter_write_zeroes(bw, 8) &&
171                 FLAC__bitwriter_write_raw_uint64(bw, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
172                 FLAC__bitwriter_write_raw_uint32(bw, 0xace, 12)
173         ;
174         if(!ok) {
175                 printf("FAILED\n");
176                 FLAC__bitwriter_dump(bw, stdout);
177                 return false;
178         }
179         /* we wrote 152 bits (=19 bytes) to the bitwriter */
180         words = 152 / FLAC__BITS_PER_WORD;
181         bits = 152 - words*FLAC__BITS_PER_WORD;
182
183         if(bw->words != words) {
184                 printf("FAILED byte count %u != %u\n", bw->words, words);
185                 FLAC__bitwriter_dump(bw, stdout);
186                 return false;
187         }
188         if(bw->bits != bits) {
189                 printf("FAILED bit count %u != %u\n", bw->bits, bits);
190                 FLAC__bitwriter_dump(bw, stdout);
191                 return false;
192         }
193         if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
194                 printf("FAILED pattern match (buffer)\n");
195                 FLAC__bitwriter_dump(bw, stdout);
196                 return false;
197         }
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);
201                 return false;
202         }
203         printf("OK\n");
204         FLAC__bitwriter_dump(bw, stdout);
205
206         printf("testing raw_uint32 some more... ");
207         ok = FLAC__bitwriter_write_raw_uint32(bw, 0x3d, 6);
208         if(!ok) {
209                 printf("FAILED\n");
210                 FLAC__bitwriter_dump(bw, stdout);
211                 return false;
212         }
213         bits += 6;
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);
219                 return false;
220         }
221         if(bw->bits != bits) {
222                 printf("FAILED bit count %u != %u\n", bw->bits, bits);
223                 FLAC__bitwriter_dump(bw, stdout);
224                 return false;
225         }
226         if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
227                 printf("FAILED pattern match (buffer)\n");
228                 FLAC__bitwriter_dump(bw, stdout);
229                 return false;
230         }
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);
234                 return false;
235         }
236         printf("OK\n");
237         FLAC__bitwriter_dump(bw, stdout);
238
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");
244         if(!ok) {
245                 FLAC__bitwriter_dump(bw, stdout);
246                 return false;
247         }
248
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");
254         if(!ok) {
255                 FLAC__bitwriter_dump(bw, stdout);
256                 return false;
257         }
258
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");
264         if(!ok) {
265                 FLAC__bitwriter_dump(bw, stdout);
266                 return false;
267         }
268
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");
274         if(!ok) {
275                 FLAC__bitwriter_dump(bw, stdout);
276                 return false;
277         }
278
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");
284         if(!ok) {
285                 FLAC__bitwriter_dump(bw, stdout);
286                 return false;
287         }
288
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");
294         if(!ok) {
295                 FLAC__bitwriter_dump(bw, stdout);
296                 return false;
297         }
298
299         printf("testing utf8_uint32(0x00010000)... ");
300         FLAC__bitwriter_clear(bw);
301         FLAC__bitwriter_write_utf8_uint32(bw, 0x00010000);
302 #if WORDS_BIGENDIAN
303         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
304 #else
305         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
306 #endif
307         printf("%s\n", ok?"OK":"FAILED");
308         if(!ok) {
309                 FLAC__bitwriter_dump(bw, stdout);
310                 return false;
311         }
312
313         printf("testing utf8_uint32(0x001FFFFF)... ");
314         FLAC__bitwriter_clear(bw);
315         FLAC__bitwriter_write_utf8_uint32(bw, 0x001FFFFF);
316 #if WORDS_BIGENDIAN
317         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
318 #else
319         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
320 #endif
321         printf("%s\n", ok?"OK":"FAILED");
322         if(!ok) {
323                 FLAC__bitwriter_dump(bw, stdout);
324                 return false;
325         }
326
327         printf("testing utf8_uint32(0x00200000)... ");
328         FLAC__bitwriter_clear(bw);
329         FLAC__bitwriter_write_utf8_uint32(bw, 0x00200000);
330 #if WORDS_BIGENDIAN
331         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
332 #else
333         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
334 #endif
335         printf("%s\n", ok?"OK":"FAILED");
336         if(!ok) {
337                 FLAC__bitwriter_dump(bw, stdout);
338                 return false;
339         }
340
341         printf("testing utf8_uint32(0x03FFFFFF)... ");
342         FLAC__bitwriter_clear(bw);
343         FLAC__bitwriter_write_utf8_uint32(bw, 0x03FFFFFF);
344 #if WORDS_BIGENDIAN
345         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
346 #else
347         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
348 #endif
349         printf("%s\n", ok?"OK":"FAILED");
350         if(!ok) {
351                 FLAC__bitwriter_dump(bw, stdout);
352                 return false;
353         }
354
355         printf("testing utf8_uint32(0x04000000)... ");
356         FLAC__bitwriter_clear(bw);
357         FLAC__bitwriter_write_utf8_uint32(bw, 0x04000000);
358 #if WORDS_BIGENDIAN
359         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
360 #else
361         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
362 #endif
363         printf("%s\n", ok?"OK":"FAILED");
364         if(!ok) {
365                 FLAC__bitwriter_dump(bw, stdout);
366                 return false;
367         }
368
369         printf("testing utf8_uint32(0x7FFFFFFF)... ");
370         FLAC__bitwriter_clear(bw);
371         FLAC__bitwriter_write_utf8_uint32(bw, 0x7FFFFFFF);
372 #if WORDS_BIGENDIAN
373         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
374 #else
375         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
376 #endif
377         printf("%s\n", ok?"OK":"FAILED");
378         if(!ok) {
379                 FLAC__bitwriter_dump(bw, stdout);
380                 return false;
381         }
382
383         printf("testing utf8_uint64(0x0000000000000000)... ");
384         FLAC__bitwriter_clear(bw);
385         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000000));
386         ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
387         printf("%s\n", ok?"OK":"FAILED");
388         if(!ok) {
389                 FLAC__bitwriter_dump(bw, stdout);
390                 return false;
391         }
392
393         printf("testing utf8_uint64(0x000000000000007F)... ");
394         FLAC__bitwriter_clear(bw);
395         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000007F));
396         ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
397         printf("%s\n", ok?"OK":"FAILED");
398         if(!ok) {
399                 FLAC__bitwriter_dump(bw, stdout);
400                 return false;
401         }
402
403         printf("testing utf8_uint64(0x0000000000000080)... ");
404         FLAC__bitwriter_clear(bw);
405         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000080));
406         ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
407         printf("%s\n", ok?"OK":"FAILED");
408         if(!ok) {
409                 FLAC__bitwriter_dump(bw, stdout);
410                 return false;
411         }
412
413         printf("testing utf8_uint64(0x00000000000007FF)... ");
414         FLAC__bitwriter_clear(bw);
415         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000000007FF));
416         ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
417         printf("%s\n", ok?"OK":"FAILED");
418         if(!ok) {
419                 FLAC__bitwriter_dump(bw, stdout);
420                 return false;
421         }
422
423         printf("testing utf8_uint64(0x0000000000000800)... ");
424         FLAC__bitwriter_clear(bw);
425         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000800));
426         ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
427         printf("%s\n", ok?"OK":"FAILED");
428         if(!ok) {
429                 FLAC__bitwriter_dump(bw, stdout);
430                 return false;
431         }
432
433         printf("testing utf8_uint64(0x000000000000FFFF)... ");
434         FLAC__bitwriter_clear(bw);
435         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000FFFF));
436         ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
437         printf("%s\n", ok?"OK":"FAILED");
438         if(!ok) {
439                 FLAC__bitwriter_dump(bw, stdout);
440                 return false;
441         }
442
443         printf("testing utf8_uint64(0x0000000000010000)... ");
444         FLAC__bitwriter_clear(bw);
445         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000010000));
446 #if WORDS_BIGENDIAN
447         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
448 #else
449         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
450 #endif
451         printf("%s\n", ok?"OK":"FAILED");
452         if(!ok) {
453                 FLAC__bitwriter_dump(bw, stdout);
454                 return false;
455         }
456
457         printf("testing utf8_uint64(0x00000000001FFFFF)... ");
458         FLAC__bitwriter_clear(bw);
459         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000001FFFFF));
460 #if WORDS_BIGENDIAN
461         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
462 #else
463         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
464 #endif
465         printf("%s\n", ok?"OK":"FAILED");
466         if(!ok) {
467                 FLAC__bitwriter_dump(bw, stdout);
468                 return false;
469         }
470
471         printf("testing utf8_uint64(0x0000000000200000)... ");
472         FLAC__bitwriter_clear(bw);
473         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000200000));
474 #if WORDS_BIGENDIAN
475         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
476 #else
477         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
478 #endif
479         printf("%s\n", ok?"OK":"FAILED");
480         if(!ok) {
481                 FLAC__bitwriter_dump(bw, stdout);
482                 return false;
483         }
484
485         printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
486         FLAC__bitwriter_clear(bw);
487         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000003FFFFFF));
488 #if WORDS_BIGENDIAN
489         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
490 #else
491         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
492 #endif
493         printf("%s\n", ok?"OK":"FAILED");
494         if(!ok) {
495                 FLAC__bitwriter_dump(bw, stdout);
496                 return false;
497         }
498
499         printf("testing utf8_uint64(0x0000000004000000)... ");
500         FLAC__bitwriter_clear(bw);
501         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000004000000));
502 #if WORDS_BIGENDIAN
503         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
504 #else
505         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
506 #endif
507         printf("%s\n", ok?"OK":"FAILED");
508         if(!ok) {
509                 FLAC__bitwriter_dump(bw, stdout);
510                 return false;
511         }
512
513         printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
514         FLAC__bitwriter_clear(bw);
515         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000007FFFFFFF));
516 #if WORDS_BIGENDIAN
517         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
518 #else
519         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
520 #endif
521         printf("%s\n", ok?"OK":"FAILED");
522         if(!ok) {
523                 FLAC__bitwriter_dump(bw, stdout);
524                 return false;
525         }
526
527         printf("testing utf8_uint64(0x0000000080000000)... ");
528         FLAC__bitwriter_clear(bw);
529         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000080000000));
530 #if WORDS_BIGENDIAN
531         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFE828080 && (bw->accum & 0xffffff) == 0x808080;
532 #else
533         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0x808082FE && (bw->accum & 0xffffff) == 0x808080;
534 #endif
535         printf("%s\n", ok?"OK":"FAILED");
536         if(!ok) {
537                 FLAC__bitwriter_dump(bw, stdout);
538                 return false;
539         }
540
541         printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
542         FLAC__bitwriter_clear(bw);
543         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000FFFFFFFFF));
544 #if WORDS_BIGENDIAN
545         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFEBFBFBF && (bw->accum & 0xffffff) == 0xBFBFBF;
546 #else
547         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xBFBFBFFE && (bw->accum & 0xffffff) == 0xBFBFBF;
548 #endif
549         printf("%s\n", ok?"OK":"FAILED");
550         if(!ok) {
551                 FLAC__bitwriter_dump(bw, stdout);
552                 return false;
553         }
554
555         printf("testing grow... ");
556         FLAC__bitwriter_clear(bw);
557         FLAC__bitwriter_write_raw_uint32(bw, 0x5, 4);
558         j = bw->capacity;
559         for(i = 0; i < j; i++)
560                 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32);
561 #if WORDS_BIGENDIAN
562         ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0x5aaaaaaa && (bw->accum & 0xf) == 0xa;
563 #else
564         ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0xaaaaaa5a && (bw->accum & 0xf) == 0xa;
565 #endif
566         printf("%s\n", ok?"OK":"FAILED");
567         if(!ok) {
568                 FLAC__bitwriter_dump(bw, stdout);
569                 return false;
570         }
571         printf("capacity = %u\n", bw->capacity);
572
573         printf("testing free... ");
574         FLAC__bitwriter_free(bw);
575         printf("OK\n");
576
577         printf("testing delete... ");
578         FLAC__bitwriter_delete(bw);
579         printf("OK\n");
580
581         printf("\nPASSED!\n");
582         return true;
583 }