summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Kausch <robert.kausch@freac.org>2018-05-19 02:26:43 +0200
committerErik de Castro Lopo <erikd@mega-nerd.com>2018-05-20 10:29:48 +1000
commit1aff2348cd3f3644b7db95c200a9a3059bdacfa7 (patch)
tree10f974297b07e9ab7247a736055d2d23dd9712ab
parentf01a3aa77a611f6a3c2e2bc9ee3b80f36b2638d4 (diff)
downloadflac-1aff2348cd3f3644b7db95c200a9a3059bdacfa7.tar.gz
Add bitreader unit test.
-rw-r--r--src/test_libFLAC/Makefile.am2
-rw-r--r--src/test_libFLAC/Makefile.lite1
-rw-r--r--src/test_libFLAC/bitreader.c321
-rw-r--r--src/test_libFLAC/bitreader.h27
-rw-r--r--src/test_libFLAC/main.c4
-rw-r--r--src/test_libFLAC/test_libFLAC.vcproj8
-rw-r--r--src/test_libFLAC/test_libFLAC.vcxproj2
-rw-r--r--src/test_libFLAC/test_libFLAC.vcxproj.filters6
8 files changed, 371 insertions, 0 deletions
diff --git a/src/test_libFLAC/Makefile.am b/src/test_libFLAC/Makefile.am
index 8371731a..3e37f200 100644
--- a/src/test_libFLAC/Makefile.am
+++ b/src/test_libFLAC/Makefile.am
@@ -35,6 +35,7 @@ test_libFLAC_LDADD = \
-lm
test_libFLAC_SOURCES = \
+ bitreader.c \
bitwriter.c \
crc.c \
decoders.c \
@@ -46,6 +47,7 @@ test_libFLAC_SOURCES = \
metadata_manip.c \
metadata_object.c \
md5.c \
+ bitreader.h \
bitwriter.h \
crc.h \
decoders.h \
diff --git a/src/test_libFLAC/Makefile.lite b/src/test_libFLAC/Makefile.lite
index a1a24680..47aff235 100644
--- a/src/test_libFLAC/Makefile.lite
+++ b/src/test_libFLAC/Makefile.lite
@@ -36,6 +36,7 @@ else
endif
SRCS_C = \
+ bitreader.c \
bitwriter.c \
crc.c \
decoders.c \
diff --git a/src/test_libFLAC/bitreader.c b/src/test_libFLAC/bitreader.c
new file mode 100644
index 00000000..f9c420e0
--- /dev/null
+++ b/src/test_libFLAC/bitreader.c
@@ -0,0 +1,321 @@
+/* test_libFLAC - Unit tester for libFLAC
+ * Copyright (C) 2000-2009 Josh Coalson
+ * 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
+ * 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 "FLAC/assert.h"
+#include "share/compat.h"
+#include "private/bitreader.h" /* from the libFLAC private include area */
+#include "bitreader.h"
+#include <stdio.h>
+#include <string.h> /* for memcpy() */
+
+/*
+ * WATCHOUT! Since FLAC__BitReader is a private structure, we use a copy of
+ * the definition here to get at the internals. Make sure this is kept up
+ * to date with what is in ../libFLAC/bitreader.c
+ */
+#if (ENABLE_64_BIT_WORDS == 0)
+
+typedef FLAC__uint32 brword;
+#define FLAC__BYTES_PER_WORD 4
+#define FLAC__BITS_PER_WORD 32
+
+#else
+
+typedef FLAC__uint64 brword;
+#define FLAC__BYTES_PER_WORD 8
+#define FLAC__BITS_PER_WORD 64
+
+#endif
+
+struct FLAC__BitReader {
+ /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
+ /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
+ brword *buffer;
+ uint32_t capacity; /* in words */
+ uint32_t words; /* # of completed words in buffer */
+ uint32_t bytes; /* # of bytes in incomplete word at buffer[words] */
+ 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_align; /* the number of bits in the current consumed word that should not be CRC'd */
+ FLAC__BitReaderReadCallback read_callback;
+ void *client_data;
+};
+
+static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data);
+
+FLAC__bool test_bitreader(void)
+{
+ FLAC__BitReader *br;
+ FLAC__bool ok;
+ uint32_t i;
+ uint32_t words, bits; /* what we think br->consumed_words and br->consumed_bits should be */
+
+ FLAC__uint16 crc,expected_crcs[4] = { 0x5e4c, 0x7f6b, 0x2272, 0x42bf };
+ FLAC__byte data[32];
+
+ FLAC__uint32 val_uint32;
+ FLAC__uint64 val_uint64;
+
+ for (i = 0; i < 32; i++)
+ data[i] = i * 8 + 7;
+
+ printf("\n+++ libFLAC unit test: bitreader\n\n");
+
+ /*
+ * test new -> delete
+ */
+ printf("testing new... ");
+ br = FLAC__bitreader_new();
+ if(0 == br) {
+ printf("FAILED, returned NULL\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing delete... ");
+ FLAC__bitreader_delete(br);
+ printf("OK\n");
+
+ /*
+ * test new -> init -> delete
+ */
+ printf("testing new... ");
+ br = FLAC__bitreader_new();
+ if(0 == br) {
+ printf("FAILED, returned NULL\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing init... ");
+ if(!FLAC__bitreader_init(br, read_callback, data)) {
+ printf("FAILED, returned false\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing delete... ");
+ FLAC__bitreader_delete(br);
+ printf("OK\n");
+
+ /*
+ * test new -> init -> clear -> delete
+ */
+ printf("testing new... ");
+ br = FLAC__bitreader_new();
+ if(0 == br) {
+ printf("FAILED, returned NULL\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing init... ");
+ if(!FLAC__bitreader_init(br, read_callback, data)) {
+ printf("FAILED, returned false\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing clear... ");
+ if(!FLAC__bitreader_clear(br)) {
+ printf("FAILED, returned false\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing delete... ");
+ FLAC__bitreader_delete(br);
+ printf("OK\n");
+
+ /*
+ * test normal usage
+ */
+ printf("testing new... ");
+ br = FLAC__bitreader_new();
+ if(0 == br) {
+ printf("FAILED, returned NULL\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing init... ");
+ if(!FLAC__bitreader_init(br, read_callback, data)) {
+ printf("FAILED, returned false\n");
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing clear... ");
+ if(!FLAC__bitreader_clear(br)) {
+ printf("FAILED, returned false\n");
+ return false;
+ }
+ printf("OK\n");
+
+ words = bits = 0;
+
+ printf("capacity = %u\n", br->capacity);
+
+ printf("testing raw reads... ");
+ ok =
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 10) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
+ FLAC__bitreader_read_raw_uint64(br, &val_uint64, 64) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 12)
+ ;
+ if(!ok) {
+ printf("FAILED\n");
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ /* we read 152 bits (=19 bytes) from the bitreader */
+ words = 152 / FLAC__BITS_PER_WORD;
+ bits = 152 - words*FLAC__BITS_PER_WORD;
+
+ if(br->consumed_words != words) {
+ printf("FAILED word count %u != %u\n", br->consumed_words, words);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ if(br->consumed_bits != bits) {
+ printf("FAILED bit count %u != %u\n", br->consumed_bits, bits);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ crc = FLAC__bitreader_get_read_crc16(br);
+ if(crc != expected_crcs[0]) {
+ printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[0]);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ printf("OK\n");
+ FLAC__bitreader_dump(br, stdout);
+
+ printf("testing CRC reset... ");
+ FLAC__bitreader_clear(br);
+ FLAC__bitreader_reset_read_crc16(br, 0xFFFF);
+ crc = FLAC__bitreader_get_read_crc16(br);
+ if(crc != 0xFFFF) {
+ printf("FAILED reported CRC 0x%04x does not match expected 0xFFFF\n", crc);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ FLAC__bitreader_reset_read_crc16(br, 0);
+ crc = FLAC__bitreader_get_read_crc16(br);
+ if(crc != 0) {
+ printf("FAILED reported CRC 0x%04x does not match expected 0x0000\n", crc);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 16);
+ FLAC__bitreader_reset_read_crc16(br, 0);
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32);
+ crc = FLAC__bitreader_get_read_crc16(br);
+ if(crc != expected_crcs[1]) {
+ printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[1]);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ printf("OK\n");
+
+ printf("testing unaligned < 32 bit reads... ");
+ FLAC__bitreader_clear(br);
+ FLAC__bitreader_skip_bits_no_crc(br, 8);
+ FLAC__bitreader_reset_read_crc16(br, 0);
+ ok =
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8)
+ ;
+ if(!ok) {
+ printf("FAILED\n");
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ crc = FLAC__bitreader_get_read_crc16(br);
+ if(crc != expected_crcs[2]) {
+ printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[2]);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ printf("OK\n");
+ FLAC__bitreader_dump(br, stdout);
+
+ printf("testing unaligned < 64 bit reads... ");
+ FLAC__bitreader_clear(br);
+ FLAC__bitreader_skip_bits_no_crc(br, 8);
+ FLAC__bitreader_reset_read_crc16(br, 0);
+ ok =
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
+ FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32)
+ ;
+ if(!ok) {
+ printf("FAILED\n");
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ crc = FLAC__bitreader_get_read_crc16(br);
+ if(crc != expected_crcs[3]) {
+ printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[3]);
+ FLAC__bitreader_dump(br, stdout);
+ return false;
+ }
+ printf("OK\n");
+ FLAC__bitreader_dump(br, stdout);
+
+ printf("testing free... ");
+ FLAC__bitreader_free(br);
+ printf("OK\n");
+
+ printf("testing delete... ");
+ FLAC__bitreader_delete(br);
+ printf("OK\n");
+
+ printf("\nPASSED!\n");
+ return true;
+}
+
+/*----------------------------------------------------------------------------*/
+
+static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data)
+{
+ if (*bytes > 32)
+ *bytes = 32;
+
+ memcpy(buffer, data, *bytes);
+
+ return true;
+}
diff --git a/src/test_libFLAC/bitreader.h b/src/test_libFLAC/bitreader.h
new file mode 100644
index 00000000..b4e02c0b
--- /dev/null
+++ b/src/test_libFLAC/bitreader.h
@@ -0,0 +1,27 @@
+/* test_libFLAC - Unit tester for libFLAC
+ * Copyright (C) 2000-2009 Josh Coalson
+ * 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
+ * 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_BITREADER_H
+#define FLAC__TEST_LIBFLAC_BITREADER_H
+
+#include "FLAC/ordinals.h"
+
+FLAC__bool test_bitreader(void);
+
+#endif
diff --git a/src/test_libFLAC/main.c b/src/test_libFLAC/main.c
index a0777398..e627b86a 100644
--- a/src/test_libFLAC/main.c
+++ b/src/test_libFLAC/main.c
@@ -21,6 +21,7 @@
# include <config.h>
#endif
+#include "bitreader.h"
#include "bitwriter.h"
#include "crc.h"
#include "decoders.h"
@@ -41,6 +42,9 @@ int main(void)
if(!test_md5())
return 1;
+ if(!test_bitreader())
+ return 1;
+
if(!test_bitwriter())
return 1;
diff --git a/src/test_libFLAC/test_libFLAC.vcproj b/src/test_libFLAC/test_libFLAC.vcproj
index 57fcd46b..8689eebb 100644
--- a/src/test_libFLAC/test_libFLAC.vcproj
+++ b/src/test_libFLAC/test_libFLAC.vcproj
@@ -185,6 +185,10 @@
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
+ RelativePath=".\bitreader.h"
+ >
+ </File>
+ <File
RelativePath=".\bitwriter.h"
>
</File>
@@ -223,6 +227,10 @@
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
+ RelativePath=".\bitreader.c"
+ >
+ </File>
+ <File
RelativePath=".\bitwriter.c"
>
</File>
diff --git a/src/test_libFLAC/test_libFLAC.vcxproj b/src/test_libFLAC/test_libFLAC.vcxproj
index 722b0cee..062b2ae2 100644
--- a/src/test_libFLAC/test_libFLAC.vcxproj
+++ b/src/test_libFLAC/test_libFLAC.vcxproj
@@ -165,6 +165,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
+ <ClInclude Include="bitreader.h" />
<ClInclude Include="bitwriter.h" />
<ClInclude Include="crc.h" />
<ClInclude Include="decoders.h" />
@@ -175,6 +176,7 @@
<ClInclude Include="metadata.h" />
</ItemGroup>
<ItemGroup>
+ <ClCompile Include="bitreader.c" />
<ClCompile Include="bitwriter.c" />
<ClCompile Include="crc.c" />
<ClCompile Include="decoders.c" />
diff --git a/src/test_libFLAC/test_libFLAC.vcxproj.filters b/src/test_libFLAC/test_libFLAC.vcxproj.filters
index 277ec145..862efb6f 100644
--- a/src/test_libFLAC/test_libFLAC.vcxproj.filters
+++ b/src/test_libFLAC/test_libFLAC.vcxproj.filters
@@ -11,6 +11,9 @@
</Filter>
</ItemGroup>
<ItemGroup>
+ <ClInclude Include="bitreader.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
<ClInclude Include="bitwriter.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -37,6 +40,9 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
+ <ClCompile Include="bitreader.c">
+ <Filter>Source Files</Filter>
+ </ClCompile>
<ClCompile Include="bitwriter.c">
<Filter>Source Files</Filter>
</ClCompile>