From c2673dafb1677a731604096934abe69d7495d6ea Mon Sep 17 00:00:00 2001 From: Robert Kausch Date: Sun, 20 May 2018 10:56:15 +0200 Subject: Add unit tests for word-wise CRC16 functions Update FLAC__BitReader structure in unit test. --- src/test_libFLAC/bitreader.c | 1 + src/test_libFLAC/crc.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) (limited to 'src') diff --git a/src/test_libFLAC/bitreader.c b/src/test_libFLAC/bitreader.c index f9c420e0..63eae692 100644 --- a/src/test_libFLAC/bitreader.c +++ b/src/test_libFLAC/bitreader.c @@ -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; diff --git a/src/test_libFLAC/crc.c b/src/test_libFLAC/crc.c index c7434066..800f3fa4 100644 --- a/src/test_libFLAC/crc.c +++ b/src/test_libFLAC/crc.c @@ -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; +} -- cgit v1.2.1