Add bitreader unit test.
[flac.git] / src / test_libFLAC / bitreader.c
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/bitreader.h" /* from the libFLAC private include area */
27 #include "bitreader.h"
28 #include <stdio.h>
29 #include <string.h> /* for memcpy() */
30
31 /*
32  * WATCHOUT!  Since FLAC__BitReader 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/bitreader.c
35  */
36 #if (ENABLE_64_BIT_WORDS == 0)
37
38 typedef FLAC__uint32 brword;
39 #define FLAC__BYTES_PER_WORD 4
40 #define FLAC__BITS_PER_WORD 32
41
42 #else
43
44 typedef FLAC__uint64 brword;
45 #define FLAC__BYTES_PER_WORD 8
46 #define FLAC__BITS_PER_WORD 64
47
48 #endif
49
50 struct FLAC__BitReader {
51         /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
52         /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
53         brword *buffer;
54         uint32_t capacity; /* in words */
55         uint32_t words; /* # of completed words in buffer */
56         uint32_t bytes; /* # of bytes in incomplete word at buffer[words] */
57         uint32_t consumed_words; /* #words ... */
58         uint32_t consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
59         uint32_t read_crc16; /* the running frame CRC */
60         uint32_t crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
61         FLAC__BitReaderReadCallback read_callback;
62         void *client_data;
63 };
64
65 static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data);
66
67 FLAC__bool test_bitreader(void)
68 {
69         FLAC__BitReader *br;
70         FLAC__bool ok;
71         uint32_t i;
72         uint32_t words, bits; /* what we think br->consumed_words and br->consumed_bits should be */
73
74         FLAC__uint16     crc,expected_crcs[4] = { 0x5e4c, 0x7f6b, 0x2272, 0x42bf };
75         FLAC__byte       data[32];
76
77         FLAC__uint32     val_uint32;
78         FLAC__uint64     val_uint64;
79
80         for (i = 0; i < 32; i++)
81                 data[i] = i * 8 + 7;
82
83         printf("\n+++ libFLAC unit test: bitreader\n\n");
84
85         /*
86          * test new -> delete
87          */
88         printf("testing new... ");
89         br = FLAC__bitreader_new();
90         if(0 == br) {
91                 printf("FAILED, returned NULL\n");
92                 return false;
93         }
94         printf("OK\n");
95
96         printf("testing delete... ");
97         FLAC__bitreader_delete(br);
98         printf("OK\n");
99
100         /*
101          * test new -> init -> delete
102          */
103         printf("testing new... ");
104         br = FLAC__bitreader_new();
105         if(0 == br) {
106                 printf("FAILED, returned NULL\n");
107                 return false;
108         }
109         printf("OK\n");
110
111         printf("testing init... ");
112         if(!FLAC__bitreader_init(br, read_callback, data)) {
113                 printf("FAILED, returned false\n");
114                 return false;
115         }
116         printf("OK\n");
117
118         printf("testing delete... ");
119         FLAC__bitreader_delete(br);
120         printf("OK\n");
121
122         /*
123          * test new -> init -> clear -> delete
124          */
125         printf("testing new... ");
126         br = FLAC__bitreader_new();
127         if(0 == br) {
128                 printf("FAILED, returned NULL\n");
129                 return false;
130         }
131         printf("OK\n");
132
133         printf("testing init... ");
134         if(!FLAC__bitreader_init(br, read_callback, data)) {
135                 printf("FAILED, returned false\n");
136                 return false;
137         }
138         printf("OK\n");
139
140         printf("testing clear... ");
141         if(!FLAC__bitreader_clear(br)) {
142                 printf("FAILED, returned false\n");
143                 return false;
144         }
145         printf("OK\n");
146
147         printf("testing delete... ");
148         FLAC__bitreader_delete(br);
149         printf("OK\n");
150
151         /*
152          * test normal usage
153          */
154         printf("testing new... ");
155         br = FLAC__bitreader_new();
156         if(0 == br) {
157                 printf("FAILED, returned NULL\n");
158                 return false;
159         }
160         printf("OK\n");
161
162         printf("testing init... ");
163         if(!FLAC__bitreader_init(br, read_callback, data)) {
164                 printf("FAILED, returned false\n");
165                 return false;
166         }
167         printf("OK\n");
168
169         printf("testing clear... ");
170         if(!FLAC__bitreader_clear(br)) {
171                 printf("FAILED, returned false\n");
172                 return false;
173         }
174         printf("OK\n");
175
176         words = bits = 0;
177
178         printf("capacity = %u\n", br->capacity);
179
180         printf("testing raw reads... ");
181         ok =
182                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
183                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
184                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
185                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
186                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 10) &&
187                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
188                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32) &&
189                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
190                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
191                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
192                 FLAC__bitreader_read_raw_uint64(br, &val_uint64, 64) &&
193                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 12)
194         ;
195         if(!ok) {
196                 printf("FAILED\n");
197                 FLAC__bitreader_dump(br, stdout);
198                 return false;
199         }
200         /* we read 152 bits (=19 bytes) from the bitreader */
201         words = 152 / FLAC__BITS_PER_WORD;
202         bits = 152 - words*FLAC__BITS_PER_WORD;
203
204         if(br->consumed_words != words) {
205                 printf("FAILED word count %u != %u\n", br->consumed_words, words);
206                 FLAC__bitreader_dump(br, stdout);
207                 return false;
208         }
209         if(br->consumed_bits != bits) {
210                 printf("FAILED bit count %u != %u\n", br->consumed_bits, bits);
211                 FLAC__bitreader_dump(br, stdout);
212                 return false;
213         }
214         crc = FLAC__bitreader_get_read_crc16(br);
215         if(crc != expected_crcs[0]) {
216                 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[0]);
217                 FLAC__bitreader_dump(br, stdout);
218                 return false;
219         }
220         printf("OK\n");
221         FLAC__bitreader_dump(br, stdout);
222
223         printf("testing CRC reset... ");
224         FLAC__bitreader_clear(br);
225         FLAC__bitreader_reset_read_crc16(br, 0xFFFF);
226         crc = FLAC__bitreader_get_read_crc16(br);
227         if(crc != 0xFFFF) {
228                 printf("FAILED reported CRC 0x%04x does not match expected 0xFFFF\n", crc);
229                 FLAC__bitreader_dump(br, stdout);
230                 return false;
231         }
232         FLAC__bitreader_reset_read_crc16(br, 0);
233         crc = FLAC__bitreader_get_read_crc16(br);
234         if(crc != 0) {
235                 printf("FAILED reported CRC 0x%04x does not match expected 0x0000\n", crc);
236                 FLAC__bitreader_dump(br, stdout);
237                 return false;
238         }
239         FLAC__bitreader_read_raw_uint32(br, &val_uint32, 16);
240         FLAC__bitreader_reset_read_crc16(br, 0);
241         FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32);
242         crc = FLAC__bitreader_get_read_crc16(br);
243         if(crc != expected_crcs[1]) {
244                 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[1]);
245                 FLAC__bitreader_dump(br, stdout);
246                 return false;
247         }
248         printf("OK\n");
249
250         printf("testing unaligned < 32 bit reads... ");
251         FLAC__bitreader_clear(br);
252         FLAC__bitreader_skip_bits_no_crc(br, 8);
253         FLAC__bitreader_reset_read_crc16(br, 0);
254         ok =
255                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
256                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
257                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
258                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8)
259         ;
260         if(!ok) {
261                 printf("FAILED\n");
262                 FLAC__bitreader_dump(br, stdout);
263                 return false;
264         }
265         crc = FLAC__bitreader_get_read_crc16(br);
266         if(crc != expected_crcs[2]) {
267                 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[2]);
268                 FLAC__bitreader_dump(br, stdout);
269                 return false;
270         }
271         printf("OK\n");
272         FLAC__bitreader_dump(br, stdout);
273
274         printf("testing unaligned < 64 bit reads... ");
275         FLAC__bitreader_clear(br);
276         FLAC__bitreader_skip_bits_no_crc(br, 8);
277         FLAC__bitreader_reset_read_crc16(br, 0);
278         ok =
279                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
280                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
281                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
282                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
283                 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32)
284         ;
285         if(!ok) {
286                 printf("FAILED\n");
287                 FLAC__bitreader_dump(br, stdout);
288                 return false;
289         }
290         crc = FLAC__bitreader_get_read_crc16(br);
291         if(crc != expected_crcs[3]) {
292                 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[3]);
293                 FLAC__bitreader_dump(br, stdout);
294                 return false;
295         }
296         printf("OK\n");
297         FLAC__bitreader_dump(br, stdout);
298
299         printf("testing free... ");
300         FLAC__bitreader_free(br);
301         printf("OK\n");
302
303         printf("testing delete... ");
304         FLAC__bitreader_delete(br);
305         printf("OK\n");
306
307         printf("\nPASSED!\n");
308         return true;
309 }
310
311 /*----------------------------------------------------------------------------*/
312
313 static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data)
314 {
315         if (*bytes > 32)
316                 *bytes = 32;
317
318         memcpy(buffer, data, *bytes);
319
320         return true;
321 }