Bulk update copyright dates
[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-2016  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 #if (ENABLE_64_BIT_WORDS == 0)
37
38 typedef FLAC__uint32 bwword;
39 #define FLAC__BYTES_PER_WORD 4
40 #define FLAC__BITS_PER_WORD 32
41 #define PRI_BWWORD "08x"
42
43 #else
44
45 typedef FLAC__uint64 bwword;
46 #define FLAC__BYTES_PER_WORD 8
47 #define FLAC__BITS_PER_WORD 64
48 #define PRI_BWWORD "016" PRIx64
49
50 #endif
51
52 struct FLAC__BitWriter {
53         bwword *buffer;
54         bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
55         unsigned capacity; /* capacity of buffer in words */
56         unsigned words; /* # of complete words in buffer */
57         unsigned bits; /* # of used bits in accum */
58 };
59
60 #define WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
61 #define TOTAL_BITS(bw) (WORDS_TO_BITS((bw)->words) + (bw)->bits)
62
63
64 FLAC__bool test_bitwriter(void)
65 {
66         FLAC__BitWriter *bw;
67         FLAC__bool ok;
68         unsigned i, j;
69 #if FLAC__BYTES_PER_WORD == 4
70 #if WORDS_BIGENDIAN
71         static bwword test_pattern1[5] = { 0xaaf0aabe, 0xaaaaaaa8, 0x300aaaaa, 0xaaadeadb, 0x00eeface };
72 #else
73         static bwword test_pattern1[5] = { 0xbeaaf0aa, 0xa8aaaaaa, 0xaaaa0a30, 0xdbeaadaa, 0x00eeface };
74 #endif
75 #elif FLAC__BYTES_PER_WORD == 8
76 #if WORDS_BIGENDIAN
77         static bwword test_pattern1[3] = { FLAC__U64L(0xaaf0aabeaaaaaaa8), FLAC__U64L(0x300aaaaaaaadeadb), FLAC__U64L(0x0000000000eeface) };
78 #else
79         static bwword test_pattern1[3] = { FLAC__U64L(0xa8aaaaaabeaaf0aa), FLAC__U64L(0xdbeaadaaaaaa0a30), FLAC__U64L(0x0000000000eeface) };
80 #endif
81 #else
82 #error FLAC__BYTES_PER_WORD is neither 4 nor 8 -- not implemented
83 #endif
84         unsigned words, bits; /* what we think bw->words and bw->bits should be */
85
86         printf("\n+++ libFLAC unit test: bitwriter\n\n");
87
88         /*
89          * test new -> delete
90          */
91         printf("testing new... ");
92         bw = FLAC__bitwriter_new();
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 -> 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 delete... ");
123         FLAC__bitwriter_delete(bw);
124         printf("OK\n");
125
126         /*
127          * test new -> init -> clear -> delete
128          */
129         printf("testing new... ");
130         bw = FLAC__bitwriter_new();
131         if(0 == bw) {
132                 printf("FAILED, returned NULL\n");
133                 return false;
134         }
135         printf("OK\n");
136
137         printf("testing init... ");
138         FLAC__bitwriter_init(bw);
139         if(0 == bw) {
140                 printf("FAILED, returned NULL\n");
141                 return false;
142         }
143         printf("OK\n");
144
145         printf("testing clear... ");
146         FLAC__bitwriter_clear(bw);
147         if(0 == bw) {
148                 printf("FAILED, returned NULL\n");
149                 return false;
150         }
151         printf("OK\n");
152
153         printf("testing delete... ");
154         FLAC__bitwriter_delete(bw);
155         printf("OK\n");
156
157         /*
158          * test normal usage
159          */
160         printf("testing new... ");
161         bw = FLAC__bitwriter_new();
162         if(0 == bw) {
163                 printf("FAILED, returned NULL\n");
164                 return false;
165         }
166         printf("OK\n");
167
168         printf("testing init... ");
169         ok = FLAC__bitwriter_init(bw);
170         printf("%s\n", ok?"OK":"FAILED");
171         if(!ok)
172                 return false;
173
174         printf("testing clear... ");
175         FLAC__bitwriter_clear(bw);
176         printf("OK\n");
177
178         words = bits = 0;
179
180         printf("capacity = %u\n", bw->capacity);
181
182         printf("testing zeroes, raw_uint32*... ");
183         ok =
184                 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 1) &&
185                 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 2) &&
186                 FLAC__bitwriter_write_raw_uint32(bw, 0xa, 5) &&
187                 FLAC__bitwriter_write_raw_uint32(bw, 0xf0, 8) &&
188                 FLAC__bitwriter_write_raw_uint32(bw, 0x2aa, 10) &&
189                 FLAC__bitwriter_write_raw_uint32(bw, 0xf, 4) &&
190                 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32) &&
191                 FLAC__bitwriter_write_zeroes(bw, 4) &&
192                 FLAC__bitwriter_write_raw_uint32(bw, 0x3, 2) &&
193                 FLAC__bitwriter_write_zeroes(bw, 8) &&
194                 FLAC__bitwriter_write_raw_uint64(bw, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
195                 FLAC__bitwriter_write_raw_uint32(bw, 0xace, 12)
196         ;
197         if(!ok) {
198                 printf("FAILED\n");
199                 FLAC__bitwriter_dump(bw, stdout);
200                 return false;
201         }
202         /* we wrote 152 bits (=19 bytes) to the bitwriter */
203         words = 152 / FLAC__BITS_PER_WORD;
204         bits = 152 - words*FLAC__BITS_PER_WORD;
205
206         if(bw->words != words) {
207                 printf("FAILED byte count %u != %u\n", bw->words, words);
208                 FLAC__bitwriter_dump(bw, stdout);
209                 return false;
210         }
211         if(bw->bits != bits) {
212                 printf("FAILED bit count %u != %u\n", bw->bits, bits);
213                 FLAC__bitwriter_dump(bw, stdout);
214                 return false;
215         }
216         if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
217                 printf("FAILED pattern match (buffer)\n");
218                 FLAC__bitwriter_dump(bw, stdout);
219                 return false;
220         }
221         if((bw->accum & 0x00ffffff) != test_pattern1[words]) {
222                 printf("FAILED pattern match (bw->accum=%" PRI_BWWORD " != %" PRI_BWWORD ")\n", bw->accum&0x00ffffff, test_pattern1[words]);
223                 FLAC__bitwriter_dump(bw, stdout);
224                 return false;
225         }
226         printf("OK\n");
227         FLAC__bitwriter_dump(bw, stdout);
228
229         printf("testing raw_uint32 some more... ");
230         ok = FLAC__bitwriter_write_raw_uint32(bw, 0x3d, 6);
231         if(!ok) {
232                 printf("FAILED\n");
233                 FLAC__bitwriter_dump(bw, stdout);
234                 return false;
235         }
236         bits += 6;
237         test_pattern1[words] <<= 6;
238         test_pattern1[words] |= 0x3d;
239         if(bw->words != words) {
240                 printf("FAILED byte count %u != %u\n", bw->words, words);
241                 FLAC__bitwriter_dump(bw, stdout);
242                 return false;
243         }
244         if(bw->bits != bits) {
245                 printf("FAILED bit count %u != %u\n", bw->bits, bits);
246                 FLAC__bitwriter_dump(bw, stdout);
247                 return false;
248         }
249         if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
250                 printf("FAILED pattern match (buffer)\n");
251                 FLAC__bitwriter_dump(bw, stdout);
252                 return false;
253         }
254         if((bw->accum & 0x3fffffff) != test_pattern1[words]) {
255                 printf("FAILED pattern match (bw->accum=%" PRI_BWWORD " != %" PRI_BWWORD ")\n", bw->accum&0x3fffffff, test_pattern1[words]);
256                 FLAC__bitwriter_dump(bw, stdout);
257                 return false;
258         }
259         printf("OK\n");
260         FLAC__bitwriter_dump(bw, stdout);
261
262         printf("testing utf8_uint32(0x00000000)... ");
263         FLAC__bitwriter_clear(bw);
264         FLAC__bitwriter_write_utf8_uint32(bw, 0x00000000);
265         ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
266         printf("%s\n", ok?"OK":"FAILED");
267         if(!ok) {
268                 FLAC__bitwriter_dump(bw, stdout);
269                 return false;
270         }
271
272         printf("testing utf8_uint32(0x0000007F)... ");
273         FLAC__bitwriter_clear(bw);
274         FLAC__bitwriter_write_utf8_uint32(bw, 0x0000007F);
275         ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
276         printf("%s\n", ok?"OK":"FAILED");
277         if(!ok) {
278                 FLAC__bitwriter_dump(bw, stdout);
279                 return false;
280         }
281
282         printf("testing utf8_uint32(0x00000080)... ");
283         FLAC__bitwriter_clear(bw);
284         FLAC__bitwriter_write_utf8_uint32(bw, 0x00000080);
285         ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
286         printf("%s\n", ok?"OK":"FAILED");
287         if(!ok) {
288                 FLAC__bitwriter_dump(bw, stdout);
289                 return false;
290         }
291
292         printf("testing utf8_uint32(0x000007FF)... ");
293         FLAC__bitwriter_clear(bw);
294         FLAC__bitwriter_write_utf8_uint32(bw, 0x000007FF);
295         ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
296         printf("%s\n", ok?"OK":"FAILED");
297         if(!ok) {
298                 FLAC__bitwriter_dump(bw, stdout);
299                 return false;
300         }
301
302         printf("testing utf8_uint32(0x00000800)... ");
303         FLAC__bitwriter_clear(bw);
304         FLAC__bitwriter_write_utf8_uint32(bw, 0x00000800);
305         ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
306         printf("%s\n", ok?"OK":"FAILED");
307         if(!ok) {
308                 FLAC__bitwriter_dump(bw, stdout);
309                 return false;
310         }
311
312         printf("testing utf8_uint32(0x0000FFFF)... ");
313         FLAC__bitwriter_clear(bw);
314         FLAC__bitwriter_write_utf8_uint32(bw, 0x0000FFFF);
315         ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
316         printf("%s\n", ok?"OK":"FAILED");
317         if(!ok) {
318                 FLAC__bitwriter_dump(bw, stdout);
319                 return false;
320         }
321
322         printf("testing utf8_uint32(0x00010000)... ");
323         FLAC__bitwriter_clear(bw);
324         FLAC__bitwriter_write_utf8_uint32(bw, 0x00010000);
325 #if FLAC__BYTES_PER_WORD == 4
326 #if WORDS_BIGENDIAN
327         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
328 #else
329         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
330 #endif
331 #elif FLAC__BYTES_PER_WORD == 8
332         ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF0908080;
333 #endif
334         printf("%s\n", ok?"OK":"FAILED");
335         if(!ok) {
336                 FLAC__bitwriter_dump(bw, stdout);
337                 return false;
338         }
339
340         printf("testing utf8_uint32(0x001FFFFF)... ");
341         FLAC__bitwriter_clear(bw);
342         FLAC__bitwriter_write_utf8_uint32(bw, 0x001FFFFF);
343 #if FLAC__BYTES_PER_WORD == 4
344 #if WORDS_BIGENDIAN
345         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
346 #else
347         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
348 #endif
349 #elif FLAC__BYTES_PER_WORD == 8
350         ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF7BFBFBF;
351 #endif
352         printf("%s\n", ok?"OK":"FAILED");
353         if(!ok) {
354                 FLAC__bitwriter_dump(bw, stdout);
355                 return false;
356         }
357
358         printf("testing utf8_uint32(0x00200000)... ");
359         FLAC__bitwriter_clear(bw);
360         FLAC__bitwriter_write_utf8_uint32(bw, 0x00200000);
361 #if FLAC__BYTES_PER_WORD == 4
362 #if WORDS_BIGENDIAN
363         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
364 #else
365         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
366 #endif
367 #elif FLAC__BYTES_PER_WORD == 8
368         ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xF888808080);
369 #endif
370         printf("%s\n", ok?"OK":"FAILED");
371         if(!ok) {
372                 FLAC__bitwriter_dump(bw, stdout);
373                 return false;
374         }
375
376         printf("testing utf8_uint32(0x03FFFFFF)... ");
377         FLAC__bitwriter_clear(bw);
378         FLAC__bitwriter_write_utf8_uint32(bw, 0x03FFFFFF);
379 #if FLAC__BYTES_PER_WORD == 4
380 #if WORDS_BIGENDIAN
381         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
382 #else
383         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
384 #endif
385 #elif FLAC__BYTES_PER_WORD == 8
386         ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xFBBFBFBFBF);
387 #endif
388         printf("%s\n", ok?"OK":"FAILED");
389         if(!ok) {
390                 FLAC__bitwriter_dump(bw, stdout);
391                 return false;
392         }
393
394         printf("testing utf8_uint32(0x04000000)... ");
395         FLAC__bitwriter_clear(bw);
396         FLAC__bitwriter_write_utf8_uint32(bw, 0x04000000);
397 #if FLAC__BYTES_PER_WORD == 4
398 #if WORDS_BIGENDIAN
399         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
400 #else
401         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
402 #endif
403 #elif FLAC__BYTES_PER_WORD == 8
404         ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFC8480808080);
405 #endif
406         printf("%s\n", ok?"OK":"FAILED");
407         if(!ok) {
408                 FLAC__bitwriter_dump(bw, stdout);
409                 return false;
410         }
411
412         printf("testing utf8_uint32(0x7FFFFFFF)... ");
413         FLAC__bitwriter_clear(bw);
414         FLAC__bitwriter_write_utf8_uint32(bw, 0x7FFFFFFF);
415 #if FLAC__BYTES_PER_WORD == 4
416 #if WORDS_BIGENDIAN
417         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
418 #else
419         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
420 #endif
421 #elif FLAC__BYTES_PER_WORD == 8
422         ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFDBFBFBFBFBF);
423 #endif
424         printf("%s\n", ok?"OK":"FAILED");
425         if(!ok) {
426                 FLAC__bitwriter_dump(bw, stdout);
427                 return false;
428         }
429
430         printf("testing utf8_uint64(0x0000000000000000)... ");
431         FLAC__bitwriter_clear(bw);
432         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000000));
433         ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
434         printf("%s\n", ok?"OK":"FAILED");
435         if(!ok) {
436                 FLAC__bitwriter_dump(bw, stdout);
437                 return false;
438         }
439
440         printf("testing utf8_uint64(0x000000000000007F)... ");
441         FLAC__bitwriter_clear(bw);
442         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000007F));
443         ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
444         printf("%s\n", ok?"OK":"FAILED");
445         if(!ok) {
446                 FLAC__bitwriter_dump(bw, stdout);
447                 return false;
448         }
449
450         printf("testing utf8_uint64(0x0000000000000080)... ");
451         FLAC__bitwriter_clear(bw);
452         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000080));
453         ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
454         printf("%s\n", ok?"OK":"FAILED");
455         if(!ok) {
456                 FLAC__bitwriter_dump(bw, stdout);
457                 return false;
458         }
459
460         printf("testing utf8_uint64(0x00000000000007FF)... ");
461         FLAC__bitwriter_clear(bw);
462         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000000007FF));
463         ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
464         printf("%s\n", ok?"OK":"FAILED");
465         if(!ok) {
466                 FLAC__bitwriter_dump(bw, stdout);
467                 return false;
468         }
469
470         printf("testing utf8_uint64(0x0000000000000800)... ");
471         FLAC__bitwriter_clear(bw);
472         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000800));
473         ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
474         printf("%s\n", ok?"OK":"FAILED");
475         if(!ok) {
476                 FLAC__bitwriter_dump(bw, stdout);
477                 return false;
478         }
479
480         printf("testing utf8_uint64(0x000000000000FFFF)... ");
481         FLAC__bitwriter_clear(bw);
482         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000FFFF));
483         ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
484         printf("%s\n", ok?"OK":"FAILED");
485         if(!ok) {
486                 FLAC__bitwriter_dump(bw, stdout);
487                 return false;
488         }
489
490         printf("testing utf8_uint64(0x0000000000010000)... ");
491         FLAC__bitwriter_clear(bw);
492         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000010000));
493 #if FLAC__BYTES_PER_WORD == 4
494 #if WORDS_BIGENDIAN
495         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
496 #else
497         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
498 #endif
499 #elif FLAC__BYTES_PER_WORD == 8
500         ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF0908080;
501 #endif
502         printf("%s\n", ok?"OK":"FAILED");
503         if(!ok) {
504                 FLAC__bitwriter_dump(bw, stdout);
505                 return false;
506         }
507
508         printf("testing utf8_uint64(0x00000000001FFFFF)... ");
509         FLAC__bitwriter_clear(bw);
510         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000001FFFFF));
511 #if FLAC__BYTES_PER_WORD == 4
512 #if WORDS_BIGENDIAN
513         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
514 #else
515         ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
516 #endif
517 #elif FLAC__BYTES_PER_WORD == 8
518         ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF7BFBFBF;
519 #endif
520         printf("%s\n", ok?"OK":"FAILED");
521         if(!ok) {
522                 FLAC__bitwriter_dump(bw, stdout);
523                 return false;
524         }
525
526         printf("testing utf8_uint64(0x0000000000200000)... ");
527         FLAC__bitwriter_clear(bw);
528         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000200000));
529 #if FLAC__BYTES_PER_WORD == 4
530 #if WORDS_BIGENDIAN
531         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
532 #else
533         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
534 #endif
535 #elif FLAC__BYTES_PER_WORD == 8
536         ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xF888808080);
537 #endif
538         printf("%s\n", ok?"OK":"FAILED");
539         if(!ok) {
540                 FLAC__bitwriter_dump(bw, stdout);
541                 return false;
542         }
543
544         printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
545         FLAC__bitwriter_clear(bw);
546         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000003FFFFFF));
547 #if FLAC__BYTES_PER_WORD == 4
548 #if WORDS_BIGENDIAN
549         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
550 #else
551         ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
552 #endif
553 #elif FLAC__BYTES_PER_WORD == 8
554         ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xFBBFBFBFBF);
555 #endif
556         printf("%s\n", ok?"OK":"FAILED");
557         if(!ok) {
558                 FLAC__bitwriter_dump(bw, stdout);
559                 return false;
560         }
561
562         printf("testing utf8_uint64(0x0000000004000000)... ");
563         FLAC__bitwriter_clear(bw);
564         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000004000000));
565 #if FLAC__BYTES_PER_WORD == 4
566 #if WORDS_BIGENDIAN
567         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
568 #else
569         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
570 #endif
571 #elif FLAC__BYTES_PER_WORD == 8
572         ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFC8480808080);
573 #endif
574         printf("%s\n", ok?"OK":"FAILED");
575         if(!ok) {
576                 FLAC__bitwriter_dump(bw, stdout);
577                 return false;
578         }
579
580         printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
581         FLAC__bitwriter_clear(bw);
582         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000007FFFFFFF));
583 #if FLAC__BYTES_PER_WORD == 4
584 #if WORDS_BIGENDIAN
585         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
586 #else
587         ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
588 #endif
589 #elif FLAC__BYTES_PER_WORD == 8
590         ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFDBFBFBFBFBF);
591 #endif
592         printf("%s\n", ok?"OK":"FAILED");
593         if(!ok) {
594                 FLAC__bitwriter_dump(bw, stdout);
595                 return false;
596         }
597
598         printf("testing utf8_uint64(0x0000000080000000)... ");
599         FLAC__bitwriter_clear(bw);
600         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000080000000));
601 #if FLAC__BYTES_PER_WORD == 4
602 #if WORDS_BIGENDIAN
603         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFE828080 && (bw->accum & 0xffffff) == 0x808080;
604 #else
605         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0x808082FE && (bw->accum & 0xffffff) == 0x808080;
606 #endif
607 #elif FLAC__BYTES_PER_WORD == 8
608         ok = TOTAL_BITS(bw) == 56 && (bw->accum & FLAC__U64L(0xffffffffffffff)) == FLAC__U64L(0xFE828080808080);
609 #endif
610         printf("%s\n", ok?"OK":"FAILED");
611         if(!ok) {
612                 FLAC__bitwriter_dump(bw, stdout);
613                 return false;
614         }
615
616         printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
617         FLAC__bitwriter_clear(bw);
618         FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000FFFFFFFFF));
619 #if FLAC__BYTES_PER_WORD == 4
620 #if WORDS_BIGENDIAN
621         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFEBFBFBF && (bw->accum & 0xffffff) == 0xBFBFBF;
622 #else
623         ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xBFBFBFFE && (bw->accum & 0xffffff) == 0xBFBFBF;
624 #endif
625 #elif FLAC__BYTES_PER_WORD == 8
626         ok = TOTAL_BITS(bw) == 56 && (bw->accum & FLAC__U64L(0xffffffffffffff)) == FLAC__U64L(0xFEBFBFBFBFBFBF);
627 #endif
628         printf("%s\n", ok?"OK":"FAILED");
629         if(!ok) {
630                 FLAC__bitwriter_dump(bw, stdout);
631                 return false;
632         }
633
634         printf("testing grow... ");
635         FLAC__bitwriter_clear(bw);
636         FLAC__bitwriter_write_raw_uint32(bw, 0x5, 4);
637         j = bw->capacity;
638         for(i = 0; i < j; i++)
639                 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32);
640 #if FLAC__BYTES_PER_WORD == 4
641 #if WORDS_BIGENDIAN
642         ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0x5aaaaaaa && (bw->accum & 0xf) == 0xa;
643 #else
644         ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0xaaaaaa5a && (bw->accum & 0xf) == 0xa;
645 #endif
646 #elif FLAC__BYTES_PER_WORD == 8
647 #if WORDS_BIGENDIAN
648         ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == FLAC__U64L(0x5aaaaaaaaaaaaaaa) && (bw->accum & 0xf) == 0xa;
649 #else
650         ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == FLAC__U64L(0xaaaaaaaaaaaaaa5a) && (bw->accum & 0xf) == 0xa;
651 #endif
652 #endif
653         printf("%s\n", ok?"OK":"FAILED");
654         if(!ok) {
655                 FLAC__bitwriter_dump(bw, stdout);
656                 return false;
657         }
658         printf("capacity = %u\n", bw->capacity);
659
660         printf("testing free... ");
661         FLAC__bitwriter_free(bw);
662         printf("OK\n");
663
664         printf("testing delete... ");
665         FLAC__bitwriter_delete(bw);
666         printf("OK\n");
667
668         printf("\nPASSED!\n");
669         return true;
670 }