Add bitreader unit test.
authorRobert Kausch <robert.kausch@freac.org>
Sat, 19 May 2018 00:26:43 +0000 (02:26 +0200)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Sun, 20 May 2018 00:29:48 +0000 (10:29 +1000)
src/test_libFLAC/Makefile.am
src/test_libFLAC/Makefile.lite
src/test_libFLAC/bitreader.c [new file with mode: 0644]
src/test_libFLAC/bitreader.h [new file with mode: 0644]
src/test_libFLAC/main.c
src/test_libFLAC/test_libFLAC.vcproj
src/test_libFLAC/test_libFLAC.vcxproj
src/test_libFLAC/test_libFLAC.vcxproj.filters

index 8371731..3e37f20 100644 (file)
@@ -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 \
index a1a2468..47aff23 100644 (file)
@@ -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 (file)
index 0000000..f9c420e
--- /dev/null
@@ -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 (file)
index 0000000..b4e02c0
--- /dev/null
@@ -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
index a077739..e627b86 100644 (file)
@@ -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;
 
index 57fcd46..8689eeb 100644 (file)
                        UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"\r
                        >\r
                        <File\r
+                               RelativePath=".\bitreader.h"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\bitwriter.h"\r
                                >\r
                        </File>\r
                        UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"\r
                        >\r
                        <File\r
+                               RelativePath=".\bitreader.c"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\bitwriter.c"\r
                                >\r
                        </File>\r
index 722b0ce..062b2ae 100644 (file)
     </Link>\r
   </ItemDefinitionGroup>\r
   <ItemGroup>\r
+    <ClInclude Include="bitreader.h" />\r
     <ClInclude Include="bitwriter.h" />\r
     <ClInclude Include="crc.h" />\r
     <ClInclude Include="decoders.h" />\r
     <ClInclude Include="metadata.h" />\r
   </ItemGroup>\r
   <ItemGroup>\r
+    <ClCompile Include="bitreader.c" />\r
     <ClCompile Include="bitwriter.c" />\r
     <ClCompile Include="crc.c" />\r
     <ClCompile Include="decoders.c" />\r
index 277ec14..862efb6 100644 (file)
@@ -11,6 +11,9 @@
     </Filter>\r
   </ItemGroup>\r
   <ItemGroup>\r
+    <ClInclude Include="bitreader.h">\r
+      <Filter>Header Files</Filter>\r
+    </ClInclude>\r
     <ClInclude Include="bitwriter.h">\r
       <Filter>Header Files</Filter>\r
     </ClInclude>\r
@@ -37,6 +40,9 @@
     </ClInclude>\r
   </ItemGroup>\r
   <ItemGroup>\r
+    <ClCompile Include="bitreader.c">\r
+      <Filter>Source Files</Filter>\r
+    </ClCompile>\r
     <ClCompile Include="bitwriter.c">\r
       <Filter>Source Files</Filter>\r
     </ClCompile>\r