Add unit tests for CRC calculation.
diff --git a/src/test_libFLAC/Makefile.am b/src/test_libFLAC/Makefile.am
index 22e9453..8371731 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_SOURCES = \
bitwriter.c \
+ crc.c \
decoders.c \
encoders.c \
endswap.c \
@@ -46,6 +47,7 @@
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 637feec..a1a2468 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 @@
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 0000000..c743406
--- /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 <config.h>
+#endif
+
+#include <stdio.h>
+
+#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 0000000..bac055d
--- /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 0316e0c..a077739 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 @@
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 bcca1cc..57fcd46 100644
--- a/src/test_libFLAC/test_libFLAC.vcproj
+++ b/src/test_libFLAC/test_libFLAC.vcproj
@@ -189,6 +189,10 @@
>
</File>
<File
+ RelativePath=".\crc.h"
+ >
+ </File>
+ <File
RelativePath=".\decoders.h"
>
</File>
@@ -223,6 +227,10 @@
>
</File>
<File
+ RelativePath=".\crc.c"
+ >
+ </File>
+ <File
RelativePath=".\decoders.c"
>
</File>
diff --git a/src/test_libFLAC/test_libFLAC.vcxproj b/src/test_libFLAC/test_libFLAC.vcxproj
index 0cb431b..722b0ce 100644
--- a/src/test_libFLAC/test_libFLAC.vcxproj
+++ b/src/test_libFLAC/test_libFLAC.vcxproj
@@ -166,6 +166,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="bitwriter.h" />
+ <ClInclude Include="crc.h" />
<ClInclude Include="decoders.h" />
<ClInclude Include="encoders.h" />
<ClInclude Include="endswap.h" />
@@ -175,6 +176,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="bitwriter.c" />
+ <ClCompile Include="crc.c" />
<ClCompile Include="decoders.c" />
<ClCompile Include="encoders.c" />
<ClCompile Include="endswap.c" />
diff --git a/src/test_libFLAC/test_libFLAC.vcxproj.filters b/src/test_libFLAC/test_libFLAC.vcxproj.filters
index b060355..277ec14 100644
--- a/src/test_libFLAC/test_libFLAC.vcxproj.filters
+++ b/src/test_libFLAC/test_libFLAC.vcxproj.filters
@@ -14,6 +14,9 @@
<ClInclude Include="bitwriter.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="crc.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
<ClInclude Include="decoders.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -37,6 +40,9 @@
<ClCompile Include="bitwriter.c">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="crc.c">
+ <Filter>Source Files</Filter>
+ </ClCompile>
<ClCompile Include="decoders.c">
<Filter>Source Files</Filter>
</ClCompile>