Skip to content

Commit

Permalink
TESTS: Add unit tests for our our Huffman decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Jan 23, 2017
1 parent 4eb7a82 commit 1122d3c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
122 changes: 122 additions & 0 deletions tests/common/huffman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos 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 3
* of the License, or (at your option) any later version.
*
* xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Unit tests for our Huffman decoder.
*/

#include "gtest/gtest.h"

#include "src/common/huffman.h"
#include "src/common/error.h"
#include "src/common/util.h"
#include "src/common/memreadstream.h"
#include "src/common/bitstream.h"

static const uint32 kCodes [] = { 0, 4, 5, 6, 7 };
static const uint8 kLengths[] = { 1, 3, 3, 3, 3 };
static const uint32 kSymbols[] = { 'A', 'B', 'C', 'D', 'E' };
static const uint8 kMaxLength = 3;

static const byte kHuffmanData[] = { 0x45, 0x67 };

static const uint32 kDeHuffmanDataSymbols[] = { 'A', 'B', 'A', 'C', 'A', 'D', 'A', 'E' };
static const uint32 kDeHuffmanDataCodes [] = { 0, 1, 0, 2, 0, 3, 0, 4 };

GTEST_TEST(Huffman, getSymbolSymbols) {
Common::MemoryReadStream byteStream(kHuffmanData);
Common::BitStream8MSB bitStream (byteStream);

Common::Huffman huffman(kMaxLength, ARRAYSIZE(kCodes), kCodes, kLengths, kSymbols);

for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataSymbols); i++)
EXPECT_EQ(huffman.getSymbol(bitStream), kDeHuffmanDataSymbols[i]) << "At index " << i;
}

GTEST_TEST(Huffman, getSymbolCodes) {
Common::MemoryReadStream byteStream(kHuffmanData);
Common::BitStream8MSB bitStream (byteStream);

Common::Huffman huffman(kMaxLength, ARRAYSIZE(kCodes), kCodes, kLengths, 0);

for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataCodes); i++)
EXPECT_EQ(huffman.getSymbol(bitStream), kDeHuffmanDataCodes[i]) << "At index " << i;
}

GTEST_TEST(Huffman, eos) {
Common::MemoryReadStream byteStream(kHuffmanData);
Common::BitStream8MSB bitStream (byteStream);

Common::Huffman huffman(kMaxLength, ARRAYSIZE(kCodes), kCodes, kLengths, kSymbols);

for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataSymbols); i++)
huffman.getSymbol(bitStream);

EXPECT_THROW(huffman.getSymbol(bitStream), Common::Exception);
}

GTEST_TEST(Huffman, searchMaxLength) {
Common::MemoryReadStream byteStream(kHuffmanData);
Common::BitStream8MSB bitStream (byteStream);

Common::Huffman huffman(0, ARRAYSIZE(kCodes), kCodes, kLengths, kSymbols);

for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataSymbols); i++)
EXPECT_EQ(huffman.getSymbol(bitStream), kDeHuffmanDataSymbols[i]) << "At index " << i;
}

GTEST_TEST(Huffman, setSymbols) {
Common::MemoryReadStream byteStream(kHuffmanData);
Common::BitStream8MSB bitStream (byteStream);

Common::Huffman huffman(kMaxLength, ARRAYSIZE(kCodes), kCodes, kLengths, kSymbols);

for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataSymbols); i++)
EXPECT_EQ(huffman.getSymbol(bitStream), kDeHuffmanDataSymbols[i]) << "At index " << i;

huffman.setSymbols(0);
bitStream.rewind();

for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataCodes); i++)
EXPECT_EQ(huffman.getSymbol(bitStream), kDeHuffmanDataCodes[i]) << "At index " << i;

huffman.setSymbols(kSymbols);
bitStream.rewind();

for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataSymbols); i++)
EXPECT_EQ(huffman.getSymbol(bitStream), kDeHuffmanDataSymbols[i]) << "At index " << i;
}

GTEST_TEST(Huffman, invalidSymbol) {
static const uint32 kInvalidCodes [] = { 0, 4, 5, 6 };
static const uint8 kInvalidLengths[] = { 1, 3, 3, 3 };
static const uint8 kInvalidMaxLength = 3;

static const byte kInvalidHuffmanData[] = { 0x77 };

Common::MemoryReadStream byteStream(kInvalidHuffmanData);
Common::BitStream8MSB bitStream (byteStream);

Common::Huffman huffman(kInvalidMaxLength, ARRAYSIZE(kInvalidCodes), kInvalidCodes, kInvalidLengths, 0);

EXPECT_EQ(huffman.getSymbol(bitStream), 0);

EXPECT_THROW(huffman.getSymbol(bitStream), Common::Exception);
}
5 changes: 5 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,8 @@ check_PROGRAMS += tests/common/test_bitstream
tests_common_test_bitstream_SOURCES = tests/common/bitstream.cpp
tests_common_test_bitstream_LDADD = $(common_LIBS)
tests_common_test_bitstream_CXXFLAGS = $(test_CXXFLAGS)

check_PROGRAMS += tests/common/test_huffman
tests_common_test_huffman_SOURCES = tests/common/huffman.cpp
tests_common_test_huffman_LDADD = $(common_LIBS)
tests_common_test_huffman_CXXFLAGS = $(test_CXXFLAGS)

0 comments on commit 1122d3c

Please sign in to comment.