37#include <gtest/gtest.h>
54 struct AdaptiveHuffmanWithoutMemory :
public ::testing::Test
59 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
65 std::string
message(
const size_t n)
67 std::mt19937
rng(213);
69 for (
size_t i = 0; i < n; ++i)
70 text.push_back(
static_cast<char>(
rng() % 5 != 0 ?
'a' +
rng() % 6 :
rng() % 256));
81TEST_F(AdaptiveHuffmanWithoutMemory, ALostByteLeavesTheDecoderFailed)
83 const std::string text =
message(3000);
88 const std::string
prefix =
"kept:";
90 for (
size_t k = 1; ; ++
k)
92 SCOPED_TRACE(testing::Message() <<
"growth " <<
k);
102 catch (
const std::bad_alloc &)
110 EXPECT_NO_THROW(decoder.
finish());
118 ASSERT_LT(kept, text.size());
122 EXPECT_THROW(decoder.
finish(), std::domain_error);
123 unsigned char byte = 99;
124 EXPECT_THROW(decoder.
decode_bit(0,
byte), std::domain_error);
125 EXPECT_THROW(decoder.
decode_bit(1,
byte), std::domain_error);
136 EXPECT_NO_THROW(decoder.
finish());
138 EXPECT_GE(failures, 5u);
144TEST_F(AdaptiveHuffmanWithoutMemory, DecodeBitLetsTheCallerGoOn)
146 const std::string text =
message(500);
154 for (
size_t i = 0; i <
bits.size(); ++i)
159 if (not decoder.
decode_bit(
static_cast<unsigned int>(
bits[i]),
byte))
163 for (
bool refused_once =
false; ;)
167 Fail_At fail(grows
and not refused_once ? 1 : 0);
168 output.push_back(
static_cast<char>(
byte));
171 catch (
const std::bad_alloc &)
178 EXPECT_NO_THROW(decoder.
finish());
179 EXPECT_GE(refused, 4u);
One-pass adaptive Huffman coding of bytes using FGK updates.
TEST_F(AdaptiveHuffmanWithoutMemory, ALostByteLeavesTheDecoderFailed)
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Incremental FGK decoder accepting arbitrarily fragmented bits.
void reset() noexcept
Clear the tree, pending bits and error state in O(256) time.
std::uint64_t symbols_processed() const noexcept
Return the number of complete bytes decoded.
void finish() const
Verify that an externally framed message ended at a byte boundary.
size_t decode(const BitArray &input, std::string &output)
Decode a bit chunk and append all complete bytes to a string.
bool decode_bit(const unsigned int bit, unsigned char &symbol)
Consume one bit and emit a byte if its codeword is complete.
Stateful, one-pass FGK Huffman encoder for binary byte strings.
size_t encode(const unsigned char symbol, BitArray &output)
Append the current code for one byte, then update the tree.
Contiguous array of bits.
Main namespace for Aleph-w library functions.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
static void prefix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.