From 096a63d36a5efed9207fbfc5c1c660e314989ec6 Mon Sep 17 00:00:00 2001 From: Robert Kausch Date: Sat, 19 May 2018 01:35:18 +0200 Subject: [PATCH] Add unit tests for CRC calculation. --- src/test_libFLAC/Makefile.am | 4 +- src/test_libFLAC/Makefile.lite | 3 +- src/test_libFLAC/crc.c | 182 ++++++++++++++++++ src/test_libFLAC/crc.h | 26 +++ src/test_libFLAC/main.c | 6 +- src/test_libFLAC/test_libFLAC.vcproj | 8 + src/test_libFLAC/test_libFLAC.vcxproj | 2 + src/test_libFLAC/test_libFLAC.vcxproj.filters | 6 + 8 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 src/test_libFLAC/crc.c create mode 100644 src/test_libFLAC/crc.h diff --git a/src/test_libFLAC/Makefile.am b/src/test_libFLAC/Makefile.am index 22e9453e..8371731a 100644 --- a/src/test_libFLAC/Makefile.am +++ b/src/test_libFLAC/Makefile.am @@ -1,6 +1,6 @@ # test_libFLAC - Unit tester for libFLAC # Copyright (C) 2000-2009 Josh Coalson -# Copyright (C) 2011-2016 Xiph.Org Foundation +# Copyright (C) 2011-2018 Xiph.Org Foundation # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -36,6 +36,7 @@ test_libFLAC_LDADD = \ test_libFLAC_SOURCES = \ bitwriter.c \ + crc.c \ decoders.c \ encoders.c \ endswap.c \ @@ -46,6 +47,7 @@ test_libFLAC_SOURCES = \ metadata_object.c \ md5.c \ bitwriter.h \ + crc.h \ decoders.h \ encoders.h \ endswap.h \ diff --git a/src/test_libFLAC/Makefile.lite b/src/test_libFLAC/Makefile.lite index 637feece..a1a24680 100644 --- a/src/test_libFLAC/Makefile.lite +++ b/src/test_libFLAC/Makefile.lite @@ -1,6 +1,6 @@ # test_libFLAC - Unit tester for libFLAC # Copyright (C) 2000-2009 Josh Coalson -# Copyright (C) 2011-2016 Xiph.Org Foundation +# Copyright (C) 2011-2018 Xiph.Org Foundation # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -37,6 +37,7 @@ endif SRCS_C = \ bitwriter.c \ + crc.c \ decoders.c \ encoders.c \ endswap.c \ diff --git a/src/test_libFLAC/crc.c b/src/test_libFLAC/crc.c new file mode 100644 index 00000000..c7434066 --- /dev/null +++ b/src/test_libFLAC/crc.c @@ -0,0 +1,182 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2018 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include "FLAC/assert.h" +#include "share/compat.h" +#include "private/crc.h" +#include "crc.h" + +static FLAC__uint8 crc8_update_ref(FLAC__byte byte, FLAC__uint8 crc); +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); + +#define DATA_SIZE 32768 + +FLAC__bool test_crc(void) +{ + uint32_t i; + FLAC__byte data[DATA_SIZE] = { 0 }; + + /* Initialize data reproducibly with pseudo-random values. */ + for (i = 1; i < DATA_SIZE; i++) + data[i] = crc8_update_ref(i % 256, data[i - 1]); + + printf("\n+++ libFLAC unit test: crc\n\n"); + + if (! test_crc8(data, DATA_SIZE)) + return false; + + if (! test_crc16(data, DATA_SIZE)) + return false; + + if (! test_crc16_update(data, DATA_SIZE)) + return false; + + printf("\nPASSED!\n"); + return true; +} + +/*----------------------------------------------------------------------------*/ + +/* Reference implementations of CRC-8 and CRC-16 to check against. */ + +#define CRC8_POLYNOMIAL 0x07 + +static FLAC__uint8 crc8_update_ref(FLAC__byte byte, FLAC__uint8 crc) +{ + uint32_t i; + + crc ^= byte; + + for (i = 0; i < 8; i++) { + crc = (crc << 1) ^ ((crc >> 7) ? CRC8_POLYNOMIAL : 0); + } + + return crc; +} + +#define CRC16_POLYNOMIAL 0x8005 + +static FLAC__uint16 crc16_update_ref(FLAC__byte byte, FLAC__uint16 crc) +{ + uint32_t i; + + crc ^= byte << 8; + + for (i = 0; i < 8; i++) { + crc = (crc << 1) ^ ((crc >> 15) ? CRC16_POLYNOMIAL : 0); + } + + return crc; +} + +/*----------------------------------------------------------------------------*/ + +static FLAC__bool test_crc8(const FLAC__byte *data, size_t size) +{ + uint32_t i; + FLAC__uint8 crc0,crc1; + + printf("testing FLAC__crc8 ... "); + + crc0 = 0; + crc1 = FLAC__crc8(data, 0); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc8 returned non-zero CRC for zero bytes of data\n"); + return false; + } + + for (i = 0; i < size; i++) { + crc0 = crc8_update_ref(data[i], crc0); + crc1 = FLAC__crc8(data, i + 1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc8 result did not match reference CRC for %i bytes of test data\n", i + 1); + return false; + } + } + + printf("OK\n"); + + return true; +} + +static FLAC__bool test_crc16(const FLAC__byte *data, size_t size) +{ + uint32_t i; + FLAC__uint16 crc0,crc1; + + printf("testing FLAC__crc16 ... "); + + crc0 = 0; + crc1 = FLAC__crc16(data, 0); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16 returned non-zero CRC for zero bytes of data\n"); + return false; + } + + for (i = 0; i < size; i++) { + crc0 = crc16_update_ref(data[i], crc0); + crc1 = FLAC__crc16(data, i + 1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16 result did not match reference CRC for %i bytes of test data\n", i + 1); + return false; + } + } + + printf("OK\n"); + + return true; +} + +static FLAC__bool test_crc16_update(const FLAC__byte *data, size_t size) +{ + uint32_t i; + FLAC__uint16 crc0,crc1; + + printf("testing FLAC__CRC16_UPDATE macro ... "); + + crc0 = 0; + crc1 = 0; + + for (i = 0; i < size; i++) { + crc0 = crc16_update_ref(data[i], crc0); + crc1 = FLAC__CRC16_UPDATE(data[i], crc1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__CRC16_UPDATE result did not match reference CRC after %i bytes of test data\n", i + 1); + return false; + } + } + + printf("OK\n"); + + return true; +} diff --git a/src/test_libFLAC/crc.h b/src/test_libFLAC/crc.h new file mode 100644 index 00000000..bac055db --- /dev/null +++ b/src/test_libFLAC/crc.h @@ -0,0 +1,26 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2018 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_CRC_H +#define FLAC__TEST_LIBFLAC_CRC_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_crc(void); + +#endif diff --git a/src/test_libFLAC/main.c b/src/test_libFLAC/main.c index 0316e0c1..a0777398 100644 --- a/src/test_libFLAC/main.c +++ b/src/test_libFLAC/main.c @@ -1,6 +1,6 @@ /* test_libFLAC - Unit tester for libFLAC * Copyright (C) 2000-2009 Josh Coalson - * Copyright (C) 2011-2016 Xiph.Org Foundation + * Copyright (C) 2011-2018 Xiph.Org Foundation * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -22,6 +22,7 @@ #endif #include "bitwriter.h" +#include "crc.h" #include "decoders.h" #include "encoders.h" #include "endswap.h" @@ -34,6 +35,9 @@ int main(void) if(!test_endswap()) return 1; + if(!test_crc()) + return 1; + if(!test_md5()) return 1; diff --git a/src/test_libFLAC/test_libFLAC.vcproj b/src/test_libFLAC/test_libFLAC.vcproj index bcca1cc3..57fcd46b 100644 --- a/src/test_libFLAC/test_libFLAC.vcproj +++ b/src/test_libFLAC/test_libFLAC.vcproj @@ -188,6 +188,10 @@ RelativePath=".\bitwriter.h" > + + @@ -222,6 +226,10 @@ RelativePath=".\bitwriter.c" > + + diff --git a/src/test_libFLAC/test_libFLAC.vcxproj b/src/test_libFLAC/test_libFLAC.vcxproj index 0cb431ba..722b0cee 100644 --- a/src/test_libFLAC/test_libFLAC.vcxproj +++ b/src/test_libFLAC/test_libFLAC.vcxproj @@ -166,6 +166,7 @@ + @@ -175,6 +176,7 @@ + diff --git a/src/test_libFLAC/test_libFLAC.vcxproj.filters b/src/test_libFLAC/test_libFLAC.vcxproj.filters index b060355a..277ec145 100644 --- a/src/test_libFLAC/test_libFLAC.vcxproj.filters +++ b/src/test_libFLAC/test_libFLAC.vcxproj.filters @@ -14,6 +14,9 @@ Header Files + + Header Files + Header Files @@ -37,6 +40,9 @@ Source Files + + Source Files + Source Files