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