Commit 096a63d3 authored by Robert Kausch's avatar Robert Kausch Committed by Erik de Castro Lopo

Add unit tests for CRC calculation.

parent fcec829b
# 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 \
......
# 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 \
......
/* 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;
}
/* 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
/* 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;
......
......@@ -188,6 +188,10 @@
RelativePath=".\bitwriter.h"
>
</File>
<File
RelativePath=".\crc.h"
>
</File>
<File
RelativePath=".\decoders.h"
>
......@@ -222,6 +226,10 @@
RelativePath=".\bitwriter.c"
>
</File>
<File
RelativePath=".\crc.c"
>
</File>
<File
RelativePath=".\decoders.c"
>
......
......@@ -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" />
......
......@@ -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>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment