1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2023 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 uint32_t capacity; /* capacity of buffer in words */
56 uint32_t words; /* # of complete words in buffer */
57 uint32_t 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
FLAC__bitwriter_dump(const FLAC__BitWriter * bw,FILE * out)63 static void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
64 {
65 uint32_t i, j;
66 if(bw == 0) {
67 fprintf(out, "bitwriter is NULL\n");
68 }
69 else {
70 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, TOTAL_BITS(bw));
71
72 for(i = 0; i < bw->words; i++) {
73 fprintf(out, "%08X: ", i);
74 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
75 fprintf(out, "%01d", bw->buffer[i] & ((bwword)1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
76 fprintf(out, "\n");
77 }
78 if(bw->bits > 0) {
79 fprintf(out, "%08X: ", i);
80 for(j = 0; j < bw->bits; j++)
81 fprintf(out, "%01d", bw->accum & ((bwword)1 << (bw->bits-j-1)) ? 1:0);
82 fprintf(out, "\n");
83 }
84 }
85 }
86
test_bitwriter(void)87 FLAC__bool test_bitwriter(void)
88 {
89 FLAC__BitWriter *bw;
90 FLAC__bool ok;
91 uint32_t i, j;
92 #if FLAC__BYTES_PER_WORD == 4
93 #if WORDS_BIGENDIAN
94 static bwword test_pattern1[5] = { 0xaaf0aabe, 0xaaaaaaa8, 0x300aaaaa, 0xaaadeadb, 0x00eeface };
95 #else
96 static bwword test_pattern1[5] = { 0xbeaaf0aa, 0xa8aaaaaa, 0xaaaa0a30, 0xdbeaadaa, 0x00eeface };
97 #endif
98 #elif FLAC__BYTES_PER_WORD == 8
99 #if WORDS_BIGENDIAN
100 static bwword test_pattern1[3] = { FLAC__U64L(0xaaf0aabeaaaaaaa8), FLAC__U64L(0x300aaaaaaaadeadb), FLAC__U64L(0x0000000000eeface) };
101 #else
102 static bwword test_pattern1[3] = { FLAC__U64L(0xa8aaaaaabeaaf0aa), FLAC__U64L(0xdbeaadaaaaaa0a30), FLAC__U64L(0x0000000000eeface) };
103 #endif
104 #else
105 #error FLAC__BYTES_PER_WORD is neither 4 nor 8 -- not implemented
106 #endif
107 uint32_t words, bits; /* what we think bw->words and bw->bits should be */
108
109 printf("\n+++ libFLAC unit test: bitwriter\n\n");
110
111 /*
112 * test new -> delete
113 */
114 printf("testing new... ");
115 bw = FLAC__bitwriter_new();
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 -> 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 if(!FLAC__bitwriter_init(bw)) {
139 printf("FAILED, returned false\n");
140 return false;
141 }
142 printf("OK\n");
143
144 printf("testing delete... ");
145 FLAC__bitwriter_delete(bw);
146 printf("OK\n");
147
148 /*
149 * test new -> init -> clear -> delete
150 */
151 printf("testing new... ");
152 bw = FLAC__bitwriter_new();
153 if(0 == bw) {
154 printf("FAILED, returned NULL\n");
155 return false;
156 }
157 printf("OK\n");
158
159 printf("testing init... ");
160 if(!FLAC__bitwriter_init(bw)) {
161 printf("FAILED, returned false\n");
162 return false;
163 }
164 printf("OK\n");
165
166 printf("testing clear... ");
167 FLAC__bitwriter_clear(bw);
168 printf("OK\n");
169
170 printf("testing delete... ");
171 FLAC__bitwriter_delete(bw);
172 printf("OK\n");
173
174 /*
175 * test normal usage
176 */
177 printf("testing new... ");
178 bw = FLAC__bitwriter_new();
179 if(0 == bw) {
180 printf("FAILED, returned NULL\n");
181 return false;
182 }
183 printf("OK\n");
184
185 printf("testing init... ");
186 if(!FLAC__bitwriter_init(bw)) {
187 printf("FAILED, returned false\n");
188 return false;
189 }
190 printf("OK\n");
191
192 printf("testing clear... ");
193 FLAC__bitwriter_clear(bw);
194 printf("OK\n");
195
196 words = bits = 0;
197
198 printf("capacity = %u\n", bw->capacity);
199
200 printf("testing zeroes, raw_uint32*... ");
201 ok =
202 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 1) &&
203 FLAC__bitwriter_write_raw_uint32(bw, 0x1, 2) &&
204 FLAC__bitwriter_write_raw_uint32(bw, 0xa, 5) &&
205 FLAC__bitwriter_write_raw_uint32(bw, 0xf0, 8) &&
206 FLAC__bitwriter_write_raw_uint32(bw, 0x2aa, 10) &&
207 FLAC__bitwriter_write_raw_uint32(bw, 0xf, 4) &&
208 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32) &&
209 FLAC__bitwriter_write_zeroes(bw, 4) &&
210 FLAC__bitwriter_write_raw_uint32(bw, 0x3, 2) &&
211 FLAC__bitwriter_write_zeroes(bw, 8) &&
212 FLAC__bitwriter_write_raw_uint64(bw, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
213 FLAC__bitwriter_write_raw_uint32(bw, 0xace, 12)
214 ;
215 if(!ok) {
216 printf("FAILED\n");
217 FLAC__bitwriter_dump(bw, stdout);
218 return false;
219 }
220 /* we wrote 152 bits (=19 bytes) to the bitwriter */
221 words = 152 / FLAC__BITS_PER_WORD;
222 bits = 152 - words*FLAC__BITS_PER_WORD;
223
224 if(bw->words != words) {
225 printf("FAILED word count %u != %u\n", bw->words, words);
226 FLAC__bitwriter_dump(bw, stdout);
227 return false;
228 }
229 if(bw->bits != bits) {
230 printf("FAILED bit count %u != %u\n", bw->bits, bits);
231 FLAC__bitwriter_dump(bw, stdout);
232 return false;
233 }
234 if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
235 printf("FAILED pattern match (buffer)\n");
236 FLAC__bitwriter_dump(bw, stdout);
237 return false;
238 }
239 if((bw->accum & 0x00ffffff) != test_pattern1[words]) {
240 printf("FAILED pattern match (bw->accum=%" PRI_BWWORD " != %" PRI_BWWORD ")\n", bw->accum&0x00ffffff, test_pattern1[words]);
241 FLAC__bitwriter_dump(bw, stdout);
242 return false;
243 }
244 printf("OK\n");
245 FLAC__bitwriter_dump(bw, stdout);
246
247 printf("testing raw_uint32 some more... ");
248 ok = FLAC__bitwriter_write_raw_uint32(bw, 0x3d, 6);
249 if(!ok) {
250 printf("FAILED\n");
251 FLAC__bitwriter_dump(bw, stdout);
252 return false;
253 }
254 bits += 6;
255 test_pattern1[words] <<= 6;
256 test_pattern1[words] |= 0x3d;
257 if(bw->words != words) {
258 printf("FAILED word count %u != %u\n", bw->words, words);
259 FLAC__bitwriter_dump(bw, stdout);
260 return false;
261 }
262 if(bw->bits != bits) {
263 printf("FAILED bit count %u != %u\n", bw->bits, bits);
264 FLAC__bitwriter_dump(bw, stdout);
265 return false;
266 }
267 if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
268 printf("FAILED pattern match (buffer)\n");
269 FLAC__bitwriter_dump(bw, stdout);
270 return false;
271 }
272 if((bw->accum & 0x3fffffff) != test_pattern1[words]) {
273 printf("FAILED pattern match (bw->accum=%" PRI_BWWORD " != %" PRI_BWWORD ")\n", bw->accum&0x3fffffff, test_pattern1[words]);
274 FLAC__bitwriter_dump(bw, stdout);
275 return false;
276 }
277 printf("OK\n");
278 FLAC__bitwriter_dump(bw, stdout);
279
280 printf("testing utf8_uint32(0x00000000)... ");
281 FLAC__bitwriter_clear(bw);
282 FLAC__bitwriter_write_utf8_uint32(bw, 0x00000000);
283 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
284 printf("%s\n", ok?"OK":"FAILED");
285 if(!ok) {
286 FLAC__bitwriter_dump(bw, stdout);
287 return false;
288 }
289
290 printf("testing utf8_uint32(0x0000007F)... ");
291 FLAC__bitwriter_clear(bw);
292 FLAC__bitwriter_write_utf8_uint32(bw, 0x0000007F);
293 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
294 printf("%s\n", ok?"OK":"FAILED");
295 if(!ok) {
296 FLAC__bitwriter_dump(bw, stdout);
297 return false;
298 }
299
300 printf("testing utf8_uint32(0x00000080)... ");
301 FLAC__bitwriter_clear(bw);
302 FLAC__bitwriter_write_utf8_uint32(bw, 0x00000080);
303 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
304 printf("%s\n", ok?"OK":"FAILED");
305 if(!ok) {
306 FLAC__bitwriter_dump(bw, stdout);
307 return false;
308 }
309
310 printf("testing utf8_uint32(0x000007FF)... ");
311 FLAC__bitwriter_clear(bw);
312 FLAC__bitwriter_write_utf8_uint32(bw, 0x000007FF);
313 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
314 printf("%s\n", ok?"OK":"FAILED");
315 if(!ok) {
316 FLAC__bitwriter_dump(bw, stdout);
317 return false;
318 }
319
320 printf("testing utf8_uint32(0x00000800)... ");
321 FLAC__bitwriter_clear(bw);
322 FLAC__bitwriter_write_utf8_uint32(bw, 0x00000800);
323 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
324 printf("%s\n", ok?"OK":"FAILED");
325 if(!ok) {
326 FLAC__bitwriter_dump(bw, stdout);
327 return false;
328 }
329
330 printf("testing utf8_uint32(0x0000FFFF)... ");
331 FLAC__bitwriter_clear(bw);
332 FLAC__bitwriter_write_utf8_uint32(bw, 0x0000FFFF);
333 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
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(0x00010000)... ");
341 FLAC__bitwriter_clear(bw);
342 FLAC__bitwriter_write_utf8_uint32(bw, 0x00010000);
343 #if FLAC__BYTES_PER_WORD == 4
344 #if WORDS_BIGENDIAN
345 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
346 #else
347 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
348 #endif
349 #elif FLAC__BYTES_PER_WORD == 8
350 ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF0908080;
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(0x001FFFFF)... ");
359 FLAC__bitwriter_clear(bw);
360 FLAC__bitwriter_write_utf8_uint32(bw, 0x001FFFFF);
361 #if FLAC__BYTES_PER_WORD == 4
362 #if WORDS_BIGENDIAN
363 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
364 #else
365 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
366 #endif
367 #elif FLAC__BYTES_PER_WORD == 8
368 ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF7BFBFBF;
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(0x00200000)... ");
377 FLAC__bitwriter_clear(bw);
378 FLAC__bitwriter_write_utf8_uint32(bw, 0x00200000);
379 #if FLAC__BYTES_PER_WORD == 4
380 #if WORDS_BIGENDIAN
381 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
382 #else
383 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
384 #endif
385 #elif FLAC__BYTES_PER_WORD == 8
386 ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xF888808080);
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(0x03FFFFFF)... ");
395 FLAC__bitwriter_clear(bw);
396 FLAC__bitwriter_write_utf8_uint32(bw, 0x03FFFFFF);
397 #if FLAC__BYTES_PER_WORD == 4
398 #if WORDS_BIGENDIAN
399 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
400 #else
401 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
402 #endif
403 #elif FLAC__BYTES_PER_WORD == 8
404 ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xFBBFBFBFBF);
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(0x04000000)... ");
413 FLAC__bitwriter_clear(bw);
414 FLAC__bitwriter_write_utf8_uint32(bw, 0x04000000);
415 #if FLAC__BYTES_PER_WORD == 4
416 #if WORDS_BIGENDIAN
417 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
418 #else
419 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
420 #endif
421 #elif FLAC__BYTES_PER_WORD == 8
422 ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFC8480808080);
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_uint32(0x7FFFFFFF)... ");
431 FLAC__bitwriter_clear(bw);
432 FLAC__bitwriter_write_utf8_uint32(bw, 0x7FFFFFFF);
433 #if FLAC__BYTES_PER_WORD == 4
434 #if WORDS_BIGENDIAN
435 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
436 #else
437 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
438 #endif
439 #elif FLAC__BYTES_PER_WORD == 8
440 ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFDBFBFBFBFBF);
441 #endif
442 printf("%s\n", ok?"OK":"FAILED");
443 if(!ok) {
444 FLAC__bitwriter_dump(bw, stdout);
445 return false;
446 }
447
448 printf("testing utf8_uint64(0x0000000000000000)... ");
449 FLAC__bitwriter_clear(bw);
450 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000000));
451 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
452 printf("%s\n", ok?"OK":"FAILED");
453 if(!ok) {
454 FLAC__bitwriter_dump(bw, stdout);
455 return false;
456 }
457
458 printf("testing utf8_uint64(0x000000000000007F)... ");
459 FLAC__bitwriter_clear(bw);
460 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000007F));
461 ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
462 printf("%s\n", ok?"OK":"FAILED");
463 if(!ok) {
464 FLAC__bitwriter_dump(bw, stdout);
465 return false;
466 }
467
468 printf("testing utf8_uint64(0x0000000000000080)... ");
469 FLAC__bitwriter_clear(bw);
470 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000080));
471 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
472 printf("%s\n", ok?"OK":"FAILED");
473 if(!ok) {
474 FLAC__bitwriter_dump(bw, stdout);
475 return false;
476 }
477
478 printf("testing utf8_uint64(0x00000000000007FF)... ");
479 FLAC__bitwriter_clear(bw);
480 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000000007FF));
481 ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
482 printf("%s\n", ok?"OK":"FAILED");
483 if(!ok) {
484 FLAC__bitwriter_dump(bw, stdout);
485 return false;
486 }
487
488 printf("testing utf8_uint64(0x0000000000000800)... ");
489 FLAC__bitwriter_clear(bw);
490 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000800));
491 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
492 printf("%s\n", ok?"OK":"FAILED");
493 if(!ok) {
494 FLAC__bitwriter_dump(bw, stdout);
495 return false;
496 }
497
498 printf("testing utf8_uint64(0x000000000000FFFF)... ");
499 FLAC__bitwriter_clear(bw);
500 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000FFFF));
501 ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
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(0x0000000000010000)... ");
509 FLAC__bitwriter_clear(bw);
510 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000010000));
511 #if FLAC__BYTES_PER_WORD == 4
512 #if WORDS_BIGENDIAN
513 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
514 #else
515 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
516 #endif
517 #elif FLAC__BYTES_PER_WORD == 8
518 ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF0908080;
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(0x00000000001FFFFF)... ");
527 FLAC__bitwriter_clear(bw);
528 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000001FFFFF));
529 #if FLAC__BYTES_PER_WORD == 4
530 #if WORDS_BIGENDIAN
531 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
532 #else
533 ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
534 #endif
535 #elif FLAC__BYTES_PER_WORD == 8
536 ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF7BFBFBF;
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(0x0000000000200000)... ");
545 FLAC__bitwriter_clear(bw);
546 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000200000));
547 #if FLAC__BYTES_PER_WORD == 4
548 #if WORDS_BIGENDIAN
549 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
550 #else
551 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
552 #endif
553 #elif FLAC__BYTES_PER_WORD == 8
554 ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xF888808080);
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(0x0000000003FFFFFF)... ");
563 FLAC__bitwriter_clear(bw);
564 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000003FFFFFF));
565 #if FLAC__BYTES_PER_WORD == 4
566 #if WORDS_BIGENDIAN
567 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
568 #else
569 ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
570 #endif
571 #elif FLAC__BYTES_PER_WORD == 8
572 ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xFBBFBFBFBF);
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(0x0000000004000000)... ");
581 FLAC__bitwriter_clear(bw);
582 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000004000000));
583 #if FLAC__BYTES_PER_WORD == 4
584 #if WORDS_BIGENDIAN
585 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
586 #else
587 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
588 #endif
589 #elif FLAC__BYTES_PER_WORD == 8
590 ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFC8480808080);
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(0x000000007FFFFFFF)... ");
599 FLAC__bitwriter_clear(bw);
600 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000007FFFFFFF));
601 #if FLAC__BYTES_PER_WORD == 4
602 #if WORDS_BIGENDIAN
603 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
604 #else
605 ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
606 #endif
607 #elif FLAC__BYTES_PER_WORD == 8
608 ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFDBFBFBFBFBF);
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(0x0000000080000000)... ");
617 FLAC__bitwriter_clear(bw);
618 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000080000000));
619 #if FLAC__BYTES_PER_WORD == 4
620 #if WORDS_BIGENDIAN
621 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFE828080 && (bw->accum & 0xffffff) == 0x808080;
622 #else
623 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0x808082FE && (bw->accum & 0xffffff) == 0x808080;
624 #endif
625 #elif FLAC__BYTES_PER_WORD == 8
626 ok = TOTAL_BITS(bw) == 56 && (bw->accum & FLAC__U64L(0xffffffffffffff)) == FLAC__U64L(0xFE828080808080);
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 utf8_uint64(0x0000000FFFFFFFFF)... ");
635 FLAC__bitwriter_clear(bw);
636 FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000FFFFFFFFF));
637 #if FLAC__BYTES_PER_WORD == 4
638 #if WORDS_BIGENDIAN
639 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFEBFBFBF && (bw->accum & 0xffffff) == 0xBFBFBF;
640 #else
641 ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xBFBFBFFE && (bw->accum & 0xffffff) == 0xBFBFBF;
642 #endif
643 #elif FLAC__BYTES_PER_WORD == 8
644 ok = TOTAL_BITS(bw) == 56 && (bw->accum & FLAC__U64L(0xffffffffffffff)) == FLAC__U64L(0xFEBFBFBFBFBFBF);
645 #endif
646 printf("%s\n", ok?"OK":"FAILED");
647 if(!ok) {
648 FLAC__bitwriter_dump(bw, stdout);
649 return false;
650 }
651
652 printf("testing grow... ");
653 FLAC__bitwriter_clear(bw);
654 FLAC__bitwriter_write_raw_uint32(bw, 0x5, 4);
655 j = bw->capacity;
656 for(i = 0; i < j; i++)
657 FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32);
658 #if FLAC__BYTES_PER_WORD == 4
659 #if WORDS_BIGENDIAN
660 ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0x5aaaaaaa && (bw->accum & 0xf) == 0xa;
661 #else
662 ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0xaaaaaa5a && (bw->accum & 0xf) == 0xa;
663 #endif
664 #elif FLAC__BYTES_PER_WORD == 8
665 #if WORDS_BIGENDIAN
666 ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == FLAC__U64L(0x5aaaaaaaaaaaaaaa) && (bw->accum & 0xf) == 0xa;
667 #else
668 ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == FLAC__U64L(0xaaaaaaaaaaaaaa5a) && (bw->accum & 0xf) == 0xa;
669 #endif
670 #endif
671 printf("%s\n", ok?"OK":"FAILED");
672 if(!ok) {
673 FLAC__bitwriter_dump(bw, stdout);
674 return false;
675 }
676 printf("capacity = %u\n", bw->capacity);
677
678 printf("testing free... ");
679 FLAC__bitwriter_free(bw);
680 printf("OK\n");
681
682 printf("testing delete... ");
683 FLAC__bitwriter_delete(bw);
684 printf("OK\n");
685
686 printf("\nPASSED!\n");
687 return true;
688 }
689