update copyright date to include 2002
[platform/upstream/flac.git] / src / test_unit / bitbuffer.c
1 /* test_unit - Simple FLAC unit tester
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include "private/bitbuffer.h" /* from the libFLAC private include area */
20 #include <string.h> /* for memcmp() */
21
22 static FLAC__bool dummy_read_callback(FLAC__byte buffer[], unsigned *bytes, void *client_data)
23 {
24         (void)buffer, (void)bytes, (void)client_data;
25         return true;
26 }
27
28 int test_bitbuffer()
29 {
30         FLAC__BitBuffer bb, bb_zero, bb_one, bbcopy;
31         FLAC__bool ok;
32         unsigned i, j;
33         static FLAC__byte test_pattern1[19] = { 0xaa, 0xf0, 0xaa, 0xbe, 0xaa, 0xaa, 0xaa, 0xa8, 0x30, 0x0a, 0xaa, 0xaa, 0xaa, 0xad, 0xea, 0xdb, 0xee, 0xfa, 0xce };
34
35         printf("testing init... OK\n");
36         FLAC__bitbuffer_init(&bb);
37         FLAC__bitbuffer_init(&bb_zero);
38         FLAC__bitbuffer_init(&bb_one);
39         FLAC__bitbuffer_init(&bbcopy);
40
41         printf("testing clear... ");
42         ok = FLAC__bitbuffer_clear(&bb) && FLAC__bitbuffer_clear(&bb_zero) && FLAC__bitbuffer_clear(&bb_one) && FLAC__bitbuffer_clear(&bbcopy);
43         printf("%s\n", ok?"OK":"FAILED");
44         if(!ok)
45                 return 1;
46
47         printf("setting up bb_one... ");
48         ok = FLAC__bitbuffer_write_raw_uint32(&bb_one, 1, 7) && FLAC__bitbuffer_read_raw_uint32(&bb_one, &i, 6, dummy_read_callback, 0);
49         printf("%s\n", ok?"OK":"FAILED");
50         if(!ok)
51                 return 1;
52         FLAC__bitbuffer_dump(&bb_one, stdout);
53
54         printf("capacity = %u\n", bb.capacity);
55
56         printf("testing zeroes, raw_uint32*... ");
57         ok =
58                 FLAC__bitbuffer_write_raw_uint32(&bb, 0x1, 1) &&
59                 FLAC__bitbuffer_write_raw_uint32(&bb, 0x1, 2) &&
60                 FLAC__bitbuffer_write_raw_uint32(&bb, 0xa, 5) &&
61                 FLAC__bitbuffer_write_raw_uint32(&bb, 0xf0, 8) &&
62                 FLAC__bitbuffer_write_raw_uint32(&bb, 0x2aa, 10) &&
63                 FLAC__bitbuffer_write_raw_uint32(&bb, 0xf, 4) &&
64                 FLAC__bitbuffer_write_raw_uint32(&bb, 0xaaaaaaaa, 32) &&
65                 FLAC__bitbuffer_write_zeroes(&bb, 4) &&
66                 FLAC__bitbuffer_write_raw_uint32(&bb, 0x3, 2) &&
67                 FLAC__bitbuffer_write_zeroes(&bb, 8) &&
68                 FLAC__bitbuffer_write_raw_uint64(&bb, 0xaaaaaaaadeadbeef, 64) &&
69                 FLAC__bitbuffer_write_raw_uint32(&bb, 0xace, 12)
70         ;
71         if(!ok) {
72                 printf("FAILED\n");
73                 FLAC__bitbuffer_dump(&bb, stdout);
74                 return 1;
75         }
76         if(bb.bytes != sizeof(test_pattern1)) {
77                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1));
78                 FLAC__bitbuffer_dump(&bb, stdout);
79                 return 1;
80         }
81         if(bb.bits != 0) {
82                 printf("FAILED bit count %u != 0\n", bb.bits);
83                 FLAC__bitbuffer_dump(&bb, stdout);
84                 return 1;
85         }
86         if(bb.total_bits != 8*bb.bytes+bb.bits) {
87                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
88                 FLAC__bitbuffer_dump(&bb, stdout);
89                 return 1;
90         }
91         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0) {
92                 printf("FAILED pattern match\n");
93                 FLAC__bitbuffer_dump(&bb, stdout);
94                 return 1;
95         }
96         printf("OK\n");
97         FLAC__bitbuffer_dump(&bb, stdout);
98
99         printf("testing raw_uint32 some more... ");
100         ok = FLAC__bitbuffer_write_raw_uint32(&bb, 0x3d, 6);
101         if(!ok) {
102                 printf("FAILED\n");
103                 FLAC__bitbuffer_dump(&bb, stdout);
104                 return 1;
105         }
106         if(bb.bytes != sizeof(test_pattern1)) {
107                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1));
108                 FLAC__bitbuffer_dump(&bb, stdout);
109                 return 1;
110         }
111         if(bb.bits != 6) {
112                 printf("FAILED bit count %u != 6\n", bb.bits);
113                 FLAC__bitbuffer_dump(&bb, stdout);
114                 return 1;
115         }
116         if(bb.total_bits != 8*bb.bytes+bb.bits) {
117                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
118                 FLAC__bitbuffer_dump(&bb, stdout);
119                 return 1;
120         }
121         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb.buffer[bb.bytes] != 0x3d) {
122                 printf("FAILED pattern match\n");
123                 FLAC__bitbuffer_dump(&bb, stdout);
124                 return 1;
125         }
126         printf("OK\n");
127         FLAC__bitbuffer_dump(&bb, stdout);
128
129         printf("testing concatenate_aligned (bb_zero)... ");
130         ok = FLAC__bitbuffer_concatenate_aligned(&bb, &bb_zero);
131         if(!ok) {
132                 printf("FAILED\n");
133                 FLAC__bitbuffer_dump(&bb, stdout);
134                 return 1;
135         }
136         if(bb.bytes != sizeof(test_pattern1)) {
137                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1));
138                 FLAC__bitbuffer_dump(&bb, stdout);
139                 return 1;
140         }
141         if(bb.bits != 6) {
142                 printf("FAILED bit count %u != 6\n", bb.bits);
143                 FLAC__bitbuffer_dump(&bb, stdout);
144                 return 1;
145         }
146         if(bb.total_bits != 8*bb.bytes+bb.bits) {
147                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
148                 FLAC__bitbuffer_dump(&bb, stdout);
149                 return 1;
150         }
151         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb.buffer[bb.bytes] != 0x3d) {
152                 printf("FAILED pattern match\n");
153                 FLAC__bitbuffer_dump(&bb, stdout);
154                 return 1;
155         }
156         printf("OK\n");
157         FLAC__bitbuffer_dump(&bb, stdout);
158
159         printf("testing concatenate_aligned (bb_one)... ");
160         ok = FLAC__bitbuffer_concatenate_aligned(&bb, &bb_one);
161         if(!ok) {
162                 printf("FAILED\n");
163                 FLAC__bitbuffer_dump(&bb, stdout);
164                 return 1;
165         }
166         if(bb.bytes != sizeof(test_pattern1)) {
167                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1));
168                 FLAC__bitbuffer_dump(&bb, stdout);
169                 return 1;
170         }
171         if(bb.bits != 7) {
172                 printf("FAILED bit count %u != 7\n", bb.bits);
173                 FLAC__bitbuffer_dump(&bb, stdout);
174                 return 1;
175         }
176         if(bb.total_bits != 8*bb.bytes+bb.bits) {
177                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
178                 FLAC__bitbuffer_dump(&bb, stdout);
179                 return 1;
180         }
181         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb.buffer[bb.bytes] != 0x7b) {
182                 printf("FAILED pattern match\n");
183                 FLAC__bitbuffer_dump(&bb, stdout);
184                 return 1;
185         }
186         printf("OK\n");
187         FLAC__bitbuffer_dump(&bb, stdout);
188
189         printf("testing concatenate_aligned (bb_one again)... ");
190         (void)FLAC__bitbuffer_write_raw_uint32(&bb_one, 1, 1);
191         (void)FLAC__bitbuffer_read_raw_uint32(&bb_one, &i, 1, dummy_read_callback, 0);
192         ok = FLAC__bitbuffer_concatenate_aligned(&bb, &bb_one);
193         if(!ok) {
194                 printf("FAILED\n");
195                 FLAC__bitbuffer_dump(&bb, stdout);
196                 return 1;
197         }
198         if(bb.bytes != sizeof(test_pattern1)+1) {
199                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1)+1);
200                 FLAC__bitbuffer_dump(&bb, stdout);
201                 return 1;
202         }
203         if(bb.bits != 0) {
204                 printf("FAILED bit count %u != 0\n", bb.bits);
205                 FLAC__bitbuffer_dump(&bb, stdout);
206                 return 1;
207         }
208         if(bb.total_bits != 8*bb.bytes+bb.bits) {
209                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
210                 FLAC__bitbuffer_dump(&bb, stdout);
211                 return 1;
212         }
213         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb.buffer[bb.bytes-1] != 0xf7) {
214                 printf("FAILED pattern match\n");
215                 FLAC__bitbuffer_dump(&bb, stdout);
216                 return 1;
217         }
218         printf("OK\n");
219         FLAC__bitbuffer_dump(&bb, stdout);
220
221         printf("testing concatenate_aligned (bb_four)... ");
222         (void)FLAC__bitbuffer_clear(&bb_one);
223         (void)FLAC__bitbuffer_write_raw_uint32(&bb_one, 8, 4);
224         ok = FLAC__bitbuffer_concatenate_aligned(&bb, &bb_one);
225         if(!ok) {
226                 printf("FAILED\n");
227                 FLAC__bitbuffer_dump(&bb, stdout);
228                 return 1;
229         }
230         if(bb.bytes != sizeof(test_pattern1)+1) {
231                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1)+1);
232                 FLAC__bitbuffer_dump(&bb, stdout);
233                 return 1;
234         }
235         if(bb.bits != 4) {
236                 printf("FAILED bit count %u != 4\n", bb.bits);
237                 FLAC__bitbuffer_dump(&bb, stdout);
238                 return 1;
239         }
240         if(bb.total_bits != 8*bb.bytes+bb.bits) {
241                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
242                 FLAC__bitbuffer_dump(&bb, stdout);
243                 return 1;
244         }
245         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb.buffer[bb.bytes] != 0x08) {
246                 printf("FAILED pattern match\n");
247                 FLAC__bitbuffer_dump(&bb, stdout);
248                 return 1;
249         }
250         printf("OK\n");
251         FLAC__bitbuffer_dump(&bb, stdout);
252
253         printf("testing concatenate_aligned (bb_eight)... ");
254         (void)FLAC__bitbuffer_read_raw_uint32(&bb_one, &i, 4, dummy_read_callback, 0);
255         (void)FLAC__bitbuffer_write_raw_uint32(&bb_one, 0xaa, 8);
256         ok = FLAC__bitbuffer_concatenate_aligned(&bb, &bb_one);
257         if(!ok) {
258                 printf("FAILED\n");
259                 FLAC__bitbuffer_dump(&bb, stdout);
260                 return 1;
261         }
262         if(bb.bytes != sizeof(test_pattern1)+2) {
263                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1)+2);
264                 FLAC__bitbuffer_dump(&bb, stdout);
265                 return 1;
266         }
267         if(bb.bits != 4) {
268                 printf("FAILED bit count %u != 4\n", bb.bits);
269                 FLAC__bitbuffer_dump(&bb, stdout);
270                 return 1;
271         }
272         if(bb.total_bits != 8*bb.bytes+bb.bits) {
273                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
274                 FLAC__bitbuffer_dump(&bb, stdout);
275                 return 1;
276         }
277         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb.buffer[bb.bytes-1] != 0x8a || bb.buffer[bb.bytes] != 0x0a) {
278                 printf("FAILED pattern match\n");
279                 FLAC__bitbuffer_dump(&bb, stdout);
280                 return 1;
281         }
282         printf("OK\n");
283         FLAC__bitbuffer_dump(&bb, stdout);
284
285         printf("testing concatenate_aligned (bb_seventeen)... ");
286         (void)FLAC__bitbuffer_write_raw_uint32(&bb_one, 0x155, 9);
287         ok = FLAC__bitbuffer_concatenate_aligned(&bb, &bb_one);
288         if(!ok) {
289                 printf("FAILED\n");
290                 FLAC__bitbuffer_dump(&bb, stdout);
291                 return 1;
292         }
293         if(bb.bytes != sizeof(test_pattern1)+4) {
294                 printf("FAILED byte count %u != %u\n", bb.bytes, sizeof(test_pattern1)+4);
295                 FLAC__bitbuffer_dump(&bb, stdout);
296                 return 1;
297         }
298         if(bb.bits != 5) {
299                 printf("FAILED bit count %u != 5\n", bb.bits);
300                 FLAC__bitbuffer_dump(&bb, stdout);
301                 return 1;
302         }
303         if(bb.total_bits != 8*bb.bytes+bb.bits) {
304                 printf("FAILED total_bits count %u != %u (%u:%u)\n", bb.total_bits, 8*bb.bytes+bb.bits, bb.bytes, bb.bits);
305                 FLAC__bitbuffer_dump(&bb, stdout);
306                 return 1;
307         }
308         if(memcmp(bb.buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb.buffer[bb.bytes-3] != 0x8a || bb.buffer[bb.bytes-2] != 0xaa || bb.buffer[bb.bytes-1] != 0xaa || bb.buffer[bb.bytes] != 0x15) {
309                 printf("FAILED pattern match\n");
310                 FLAC__bitbuffer_dump(&bb, stdout);
311                 return 1;
312         }
313         printf("OK\n");
314         FLAC__bitbuffer_dump(&bb, stdout);
315
316         printf("testing utf8_uint32(0x00000000)... ");
317         FLAC__bitbuffer_clear(&bb);
318         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x00000000);
319         ok = bb.total_bits == 8 && bb.buffer[0] == 0;
320         printf("%s\n", ok?"OK":"FAILED");
321         if(!ok) {
322                 FLAC__bitbuffer_dump(&bb, stdout);
323                 return 1;
324         }
325
326         printf("testing utf8_uint32(0x0000007F)... ");
327         FLAC__bitbuffer_clear(&bb);
328         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x0000007F);
329         ok = bb.total_bits == 8 && bb.buffer[0] == 0x7F;
330         printf("%s\n", ok?"OK":"FAILED");
331         if(!ok) {
332                 FLAC__bitbuffer_dump(&bb, stdout);
333                 return 1;
334         }
335
336         printf("testing utf8_uint32(0x00000080)... ");
337         FLAC__bitbuffer_clear(&bb);
338         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x00000080);
339         ok = bb.total_bits == 16 && bb.buffer[0] == 0xC2 && bb.buffer[1] == 0x80;
340         printf("%s\n", ok?"OK":"FAILED");
341         if(!ok) {
342                 FLAC__bitbuffer_dump(&bb, stdout);
343                 return 1;
344         }
345
346         printf("testing utf8_uint32(0x000007FF)... ");
347         FLAC__bitbuffer_clear(&bb);
348         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x000007FF);
349         ok = bb.total_bits == 16 && bb.buffer[0] == 0xDF && bb.buffer[1] == 0xBF;
350         printf("%s\n", ok?"OK":"FAILED");
351         if(!ok) {
352                 FLAC__bitbuffer_dump(&bb, stdout);
353                 return 1;
354         }
355
356         printf("testing utf8_uint32(0x00000800)... ");
357         FLAC__bitbuffer_clear(&bb);
358         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x00000800);
359         ok = bb.total_bits == 24 && bb.buffer[0] == 0xE0 && bb.buffer[1] == 0xA0 && bb.buffer[2] == 0x80;
360         printf("%s\n", ok?"OK":"FAILED");
361         if(!ok) {
362                 FLAC__bitbuffer_dump(&bb, stdout);
363                 return 1;
364         }
365
366         printf("testing utf8_uint32(0x0000FFFF)... ");
367         FLAC__bitbuffer_clear(&bb);
368         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x0000FFFF);
369         ok = bb.total_bits == 24 && bb.buffer[0] == 0xEF && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF;
370         printf("%s\n", ok?"OK":"FAILED");
371         if(!ok) {
372                 FLAC__bitbuffer_dump(&bb, stdout);
373                 return 1;
374         }
375
376         printf("testing utf8_uint32(0x00010000)... ");
377         FLAC__bitbuffer_clear(&bb);
378         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x00010000);
379         ok = bb.total_bits == 32 && bb.buffer[0] == 0xF0 && bb.buffer[1] == 0x90 && bb.buffer[2] == 0x80 && bb.buffer[3] == 0x80;
380         printf("%s\n", ok?"OK":"FAILED");
381         if(!ok) {
382                 FLAC__bitbuffer_dump(&bb, stdout);
383                 return 1;
384         }
385
386         printf("testing utf8_uint32(0x001FFFFF)... ");
387         FLAC__bitbuffer_clear(&bb);
388         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x001FFFFF);
389         ok = bb.total_bits == 32 && bb.buffer[0] == 0xF7 && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF && bb.buffer[3] == 0xBF;
390         printf("%s\n", ok?"OK":"FAILED");
391         if(!ok) {
392                 FLAC__bitbuffer_dump(&bb, stdout);
393                 return 1;
394         }
395
396         printf("testing utf8_uint32(0x00200000)... ");
397         FLAC__bitbuffer_clear(&bb);
398         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x00200000);
399         ok = bb.total_bits == 40 && bb.buffer[0] == 0xF8 && bb.buffer[1] == 0x88 && bb.buffer[2] == 0x80 && bb.buffer[3] == 0x80 && bb.buffer[4] == 0x80;
400         printf("%s\n", ok?"OK":"FAILED");
401         if(!ok) {
402                 FLAC__bitbuffer_dump(&bb, stdout);
403                 return 1;
404         }
405
406         printf("testing utf8_uint32(0x03FFFFFF)... ");
407         FLAC__bitbuffer_clear(&bb);
408         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x03FFFFFF);
409         ok = bb.total_bits == 40 && bb.buffer[0] == 0xFB && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF && bb.buffer[3] == 0xBF && bb.buffer[4] == 0xBF;
410         printf("%s\n", ok?"OK":"FAILED");
411         if(!ok) {
412                 FLAC__bitbuffer_dump(&bb, stdout);
413                 return 1;
414         }
415
416         printf("testing utf8_uint32(0x04000000)... ");
417         FLAC__bitbuffer_clear(&bb);
418         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x04000000);
419         ok = bb.total_bits == 48 && bb.buffer[0] == 0xFC && bb.buffer[1] == 0x84 && bb.buffer[2] == 0x80 && bb.buffer[3] == 0x80 && bb.buffer[4] == 0x80 && bb.buffer[5] == 0x80;
420         printf("%s\n", ok?"OK":"FAILED");
421         if(!ok) {
422                 FLAC__bitbuffer_dump(&bb, stdout);
423                 return 1;
424         }
425
426         printf("testing utf8_uint32(0x7FFFFFFF)... ");
427         FLAC__bitbuffer_clear(&bb);
428         FLAC__bitbuffer_write_utf8_uint32(&bb, 0x7FFFFFFF);
429         ok = bb.total_bits == 48 && bb.buffer[0] == 0xFD && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF && bb.buffer[3] == 0xBF && bb.buffer[4] == 0xBF && bb.buffer[5] == 0xBF;
430         printf("%s\n", ok?"OK":"FAILED");
431         if(!ok) {
432                 FLAC__bitbuffer_dump(&bb, stdout);
433                 return 1;
434         }
435
436         printf("testing utf8_uint64(0x0000000000000000)... ");
437         FLAC__bitbuffer_clear(&bb);
438         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000000000000);
439         ok = bb.total_bits == 8 && bb.buffer[0] == 0;
440         printf("%s\n", ok?"OK":"FAILED");
441         if(!ok) {
442                 FLAC__bitbuffer_dump(&bb, stdout);
443                 return 1;
444         }
445
446         printf("testing utf8_uint64(0x000000000000007F)... ");
447         FLAC__bitbuffer_clear(&bb);
448         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x000000000000007F);
449         ok = bb.total_bits == 8 && bb.buffer[0] == 0x7F;
450         printf("%s\n", ok?"OK":"FAILED");
451         if(!ok) {
452                 FLAC__bitbuffer_dump(&bb, stdout);
453                 return 1;
454         }
455
456         printf("testing utf8_uint64(0x0000000000000080)... ");
457         FLAC__bitbuffer_clear(&bb);
458         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000000000080);
459         ok = bb.total_bits == 16 && bb.buffer[0] == 0xC2 && bb.buffer[1] == 0x80;
460         printf("%s\n", ok?"OK":"FAILED");
461         if(!ok) {
462                 FLAC__bitbuffer_dump(&bb, stdout);
463                 return 1;
464         }
465
466         printf("testing utf8_uint64(0x00000000000007FF)... ");
467         FLAC__bitbuffer_clear(&bb);
468         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x00000000000007FF);
469         ok = bb.total_bits == 16 && bb.buffer[0] == 0xDF && bb.buffer[1] == 0xBF;
470         printf("%s\n", ok?"OK":"FAILED");
471         if(!ok) {
472                 FLAC__bitbuffer_dump(&bb, stdout);
473                 return 1;
474         }
475
476         printf("testing utf8_uint64(0x0000000000000800)... ");
477         FLAC__bitbuffer_clear(&bb);
478         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000000000800);
479         ok = bb.total_bits == 24 && bb.buffer[0] == 0xE0 && bb.buffer[1] == 0xA0 && bb.buffer[2] == 0x80;
480         printf("%s\n", ok?"OK":"FAILED");
481         if(!ok) {
482                 FLAC__bitbuffer_dump(&bb, stdout);
483                 return 1;
484         }
485
486         printf("testing utf8_uint64(0x000000000000FFFF)... ");
487         FLAC__bitbuffer_clear(&bb);
488         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x000000000000FFFF);
489         ok = bb.total_bits == 24 && bb.buffer[0] == 0xEF && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF;
490         printf("%s\n", ok?"OK":"FAILED");
491         if(!ok) {
492                 FLAC__bitbuffer_dump(&bb, stdout);
493                 return 1;
494         }
495
496         printf("testing utf8_uint64(0x0000000000010000)... ");
497         FLAC__bitbuffer_clear(&bb);
498         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000000010000);
499         ok = bb.total_bits == 32 && bb.buffer[0] == 0xF0 && bb.buffer[1] == 0x90 && bb.buffer[2] == 0x80 && bb.buffer[3] == 0x80;
500         printf("%s\n", ok?"OK":"FAILED");
501         if(!ok) {
502                 FLAC__bitbuffer_dump(&bb, stdout);
503                 return 1;
504         }
505
506         printf("testing utf8_uint64(0x00000000001FFFFF)... ");
507         FLAC__bitbuffer_clear(&bb);
508         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x00000000001FFFFF);
509         ok = bb.total_bits == 32 && bb.buffer[0] == 0xF7 && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF && bb.buffer[3] == 0xBF;
510         printf("%s\n", ok?"OK":"FAILED");
511         if(!ok) {
512                 FLAC__bitbuffer_dump(&bb, stdout);
513                 return 1;
514         }
515
516         printf("testing utf8_uint64(0x0000000000200000)... ");
517         FLAC__bitbuffer_clear(&bb);
518         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000000200000);
519         ok = bb.total_bits == 40 && bb.buffer[0] == 0xF8 && bb.buffer[1] == 0x88 && bb.buffer[2] == 0x80 && bb.buffer[3] == 0x80 && bb.buffer[4] == 0x80;
520         printf("%s\n", ok?"OK":"FAILED");
521         if(!ok) {
522                 FLAC__bitbuffer_dump(&bb, stdout);
523                 return 1;
524         }
525
526         printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
527         FLAC__bitbuffer_clear(&bb);
528         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000003FFFFFF);
529         ok = bb.total_bits == 40 && bb.buffer[0] == 0xFB && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF && bb.buffer[3] == 0xBF && bb.buffer[4] == 0xBF;
530         printf("%s\n", ok?"OK":"FAILED");
531         if(!ok) {
532                 FLAC__bitbuffer_dump(&bb, stdout);
533                 return 1;
534         }
535
536         printf("testing utf8_uint64(0x0000000004000000)... ");
537         FLAC__bitbuffer_clear(&bb);
538         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000004000000);
539         ok = bb.total_bits == 48 && bb.buffer[0] == 0xFC && bb.buffer[1] == 0x84 && bb.buffer[2] == 0x80 && bb.buffer[3] == 0x80 && bb.buffer[4] == 0x80 && bb.buffer[5] == 0x80;
540         printf("%s\n", ok?"OK":"FAILED");
541         if(!ok) {
542                 FLAC__bitbuffer_dump(&bb, stdout);
543                 return 1;
544         }
545
546         printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
547         FLAC__bitbuffer_clear(&bb);
548         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x000000007FFFFFFF);
549         ok = bb.total_bits == 48 && bb.buffer[0] == 0xFD && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF && bb.buffer[3] == 0xBF && bb.buffer[4] == 0xBF && bb.buffer[5] == 0xBF;
550         printf("%s\n", ok?"OK":"FAILED");
551         if(!ok) {
552                 FLAC__bitbuffer_dump(&bb, stdout);
553                 return 1;
554         }
555
556         printf("testing utf8_uint64(0x0000000080000000)... ");
557         FLAC__bitbuffer_clear(&bb);
558         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000080000000);
559         ok = bb.total_bits == 56 && bb.buffer[0] == 0xFE && bb.buffer[1] == 0x82 && bb.buffer[2] == 0x80 && bb.buffer[3] == 0x80 && bb.buffer[4] == 0x80 && bb.buffer[5] == 0x80 && bb.buffer[6] == 0x80;
560         printf("%s\n", ok?"OK":"FAILED");
561         if(!ok) {
562                 FLAC__bitbuffer_dump(&bb, stdout);
563                 return 1;
564         }
565
566         printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
567         FLAC__bitbuffer_clear(&bb);
568         FLAC__bitbuffer_write_utf8_uint64(&bb, 0x0000000FFFFFFFFF);
569         ok = bb.total_bits == 56 && bb.buffer[0] == 0xFE && bb.buffer[1] == 0xBF && bb.buffer[2] == 0xBF && bb.buffer[3] == 0xBF && bb.buffer[4] == 0xBF && bb.buffer[5] == 0xBF && bb.buffer[6] == 0xBF;
570         printf("%s\n", ok?"OK":"FAILED");
571         if(!ok) {
572                 FLAC__bitbuffer_dump(&bb, stdout);
573                 return 1;
574         }
575
576         printf("testing grow... ");
577         FLAC__bitbuffer_clear(&bb);
578         FLAC__bitbuffer_write_raw_uint32(&bb, 0xa, 4);
579         j = bb.capacity;
580         for(i = 0; i < j; i++)
581                 FLAC__bitbuffer_write_raw_uint32(&bb, 0xaa, 8);
582         ok = bb.total_bits = i*8+4 && bb.buffer[0] == 0xaa && bb.buffer[i] == 0xa;
583         printf("%s\n", ok?"OK":"FAILED");
584         if(!ok) {
585                 FLAC__bitbuffer_dump(&bb, stdout);
586                 return 1;
587         }
588         printf("capacity = %u\n", bb.capacity);
589
590         printf("testing clone... ");
591         ok = FLAC__bitbuffer_clone(&bbcopy, &bb);
592         if(!ok) {
593                 printf("FAILED\n");
594                 FLAC__bitbuffer_dump(&bb, stdout);
595                 FLAC__bitbuffer_dump(&bbcopy, stdout);
596                 return 1;
597         }
598         if(bb.bytes != bbcopy.bytes) {
599                 printf("FAILED byte count %u != %u\n", bb.bytes, bbcopy.bytes);
600                 FLAC__bitbuffer_dump(&bb, stdout);
601                 FLAC__bitbuffer_dump(&bbcopy, stdout);
602                 return 1;
603         }
604         if(bb.bits != bbcopy.bits) {
605                 printf("FAILED bit count %u != %u\n", bb.bits, bbcopy.bits);
606                 FLAC__bitbuffer_dump(&bb, stdout);
607                 FLAC__bitbuffer_dump(&bbcopy, stdout);
608                 return 1;
609         }
610         if(bb.total_bits != bbcopy.total_bits) {
611                 printf("FAILED total_bits count %u != %u\n", bb.total_bits, bbcopy.total_bits);
612                 FLAC__bitbuffer_dump(&bb, stdout);
613                 FLAC__bitbuffer_dump(&bbcopy, stdout);
614                 return 1;
615         }
616         if(memcmp(bb.buffer, bbcopy.buffer, sizeof(FLAC__byte)*bb.capacity) != 0) {
617                 printf("FAILED pattern match\n");
618                 FLAC__bitbuffer_dump(&bb, stdout);
619                 FLAC__bitbuffer_dump(&bbcopy, stdout);
620                 return 1;
621         }
622         printf("OK\n");
623
624         printf("testing free... OK\n");
625         FLAC__bitbuffer_free(&bb);
626         FLAC__bitbuffer_free(&bbcopy);
627
628         printf("\nPASSED!\n");
629         return 0;
630 }