Add unit tests for word-wise CRC16 functions

Update FLAC__BitReader structure in unit test.
This commit is contained in:
Robert Kausch 2018-05-20 10:56:15 +02:00 committed by Erik de Castro Lopo
parent 65c2796402
commit c2673dafb1
2 changed files with 93 additions and 0 deletions

View File

@ -57,6 +57,7 @@ struct FLAC__BitReader {
uint32_t consumed_words; /* #words ... */
uint32_t consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
uint32_t read_crc16; /* the running frame CRC */
uint32_t crc16_offset; /* the number of words in the current buffer that should not be CRC'd */
uint32_t crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
FLAC__BitReaderReadCallback read_callback;
void *client_data;

View File

@ -33,6 +33,8 @@ static FLAC__uint16 crc16_update_ref(FLAC__byte byte, FLAC__uint16 crc);
static FLAC__bool test_crc8(const FLAC__byte *data, size_t size);
static FLAC__bool test_crc16(const FLAC__byte *data, size_t size);
static FLAC__bool test_crc16_update(const FLAC__byte *data, size_t size);
static FLAC__bool test_crc16_32bit_words(const FLAC__uint32 *words, size_t size);
static FLAC__bool test_crc16_64bit_words(const FLAC__uint64 *words, size_t size);
#define DATA_SIZE 32768
@ -56,6 +58,12 @@ FLAC__bool test_crc(void)
if (! test_crc16_update(data, DATA_SIZE))
return false;
if (! test_crc16_32bit_words((FLAC__uint32 *)data, DATA_SIZE / 4))
return false;
if (! test_crc16_64bit_words((FLAC__uint64 *)data, DATA_SIZE / 8))
return false;
printf("\nPASSED!\n");
return true;
}
@ -180,3 +188,87 @@ static FLAC__bool test_crc16_update(const FLAC__byte *data, size_t size)
return true;
}
static FLAC__bool test_crc16_32bit_words(const FLAC__uint32 *words, size_t size)
{
uint32_t n,i,k;
FLAC__uint16 crc0,crc1;
for (n = 1; n <= 16; n++) {
printf("testing FLAC__crc16_update_words32 (length=%i) ... ", n);
crc0 = 0;
crc1 = 0;
for (i = 0; i <= size - n; i += n) {
for (k = 0; k < n; k++) {
crc0 = crc16_update_ref( words[i + k] >> 24, crc0);
crc0 = crc16_update_ref((words[i + k] >> 16) & 0xFF, crc0);
crc0 = crc16_update_ref((words[i + k] >> 8) & 0xFF, crc0);
crc0 = crc16_update_ref( words[i + k] & 0xFF, crc0);
}
crc1 = FLAC__crc16_update_words32(words + i, n, crc1);
if (crc1 != crc0) {
printf("FAILED, FLAC__crc16_update_words32 result did not match reference CRC after %i words of test data\n", i + n);
return false;
}
}
crc1 = FLAC__crc16_update_words32(words, 0, crc1);
if (crc1 != crc0) {
printf("FAILED, FLAC__crc16_update_words32 called with zero bytes changed CRC value\n");
return false;
}
printf("OK\n");
}
return true;
}
static FLAC__bool test_crc16_64bit_words(const FLAC__uint64 *words, size_t size)
{
uint32_t n,i,k;
FLAC__uint16 crc0,crc1;
for (n = 1; n <= 16; n++) {
printf("testing FLAC__crc16_update_words64 (length=%i) ... ", n);
crc0 = 0;
crc1 = 0;
for (i = 0; i <= size - n; i += n) {
for (k = 0; k < n; k++) {
crc0 = crc16_update_ref( words[i + k] >> 56, crc0);
crc0 = crc16_update_ref((words[i + k] >> 48) & 0xFF, crc0);
crc0 = crc16_update_ref((words[i + k] >> 40) & 0xFF, crc0);
crc0 = crc16_update_ref((words[i + k] >> 32) & 0xFF, crc0);
crc0 = crc16_update_ref((words[i + k] >> 24) & 0xFF, crc0);
crc0 = crc16_update_ref((words[i + k] >> 16) & 0xFF, crc0);
crc0 = crc16_update_ref((words[i + k] >> 8) & 0xFF, crc0);
crc0 = crc16_update_ref( words[i + k] & 0xFF, crc0);
}
crc1 = FLAC__crc16_update_words64(words + i, n, crc1);
if (crc1 != crc0) {
printf("FAILED, FLAC__crc16_update_words64 result did not match reference CRC after %i words of test data\n", i + n);
return false;
}
}
crc1 = FLAC__crc16_update_words64(words, 0, crc1);
if (crc1 != crc0) {
printf("FAILED, FLAC__crc16_update_words64 called with zero bytes changed CRC value\n");
return false;
}
printf("OK\n");
}
return true;
}