45#include <gtest/gtest.h>
63 struct HuffmanWithoutMemory :
public ::testing::Test
68 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
86 void expect_two_leaves(Add add)
88 for (
size_t k = 1; ; ++
k)
90 SCOPED_TRACE(testing::Message() <<
"allocation " <<
k);
101 catch (
const std::bad_alloc &)
110 EXPECT_EQ(leaves(
root), 2u);
118 const std::string Symbol =
"a symbol longer than a short string";
121TEST_F(HuffmanWithoutMemory, SetFreqLeavesTheEncoderAsItWas)
129TEST_F(HuffmanWithoutMemory, SetEndOfStreamLeavesTheEncoderAsItWas)
138 const std::pair<std::string, size_t> Symbols[] = {
139 {
"the first symbol, longer than a short string", 5},
140 {
"the second symbol, longer than a short string", 2},
141 {
"the third symbol, longer than a short string", 2},
142 {
"the fourth symbol, longer than a short string", 9},
143 {
"the fifth symbol, longer than a short string", 1},
148 using One_More = std::pair<std::string, size_t>;
152 for (
const auto & [symbol, freq] : Symbols)
160 const One_More & one_more = {})
165 if (one_more.second != 0
and one_more.first == p->
get_key())
166 return one_more.second * depth;
167 for (
const auto & [symbol, freq] : Symbols)
168 if (symbol == p->get_key())
172 return cost(
LLINK(p), depth + 1, seen, one_more) + cost(
RLINK(p), depth + 1, seen, one_more);
178 testing::AssertionResult the_symbols_once(std::vector<std::string> seen,
const One_More & one_more = {})
181 for (
const auto & [symbol, freq] : Symbols)
183 if (one_more.second != 0)
186 std::sort(seen.begin(), seen.end());
188 return testing::AssertionSuccess();
189 auto failure = testing::AssertionFailure() << seen.size() <<
" leaves for " <<
expected.size()
191 for (
const auto & symbol : seen)
192 failure <<
" [" << symbol <<
"]";
202 const size_t sum = frequency(
LLINK(p)) + frequency(
RLINK(p));
203 EXPECT_EQ(p->
get_key().second, sum) <<
"at the node " << p->
get_key().first;
214TEST_F(HuffmanWithoutMemory, GenerateLeavesTheEncoderAsItWas)
218 for (
const auto & [symbol, freq] : Symbols)
224 std::vector<std::string> seen;
225 optimal = cost(encoder.
get_root(), 0, seen);
226 EXPECT_TRUE(the_symbols_once(seen));
230 for (
const bool with_freqs : {
false,
true})
233 for (
size_t k = 1; ; ++
k)
235 SCOPED_TRACE(testing::Message() <<
"allocation " <<
k <<
", with_freqs " << with_freqs);
246 catch (
const std::bad_alloc &)
256 for (
const auto & [symbol, freq] : Symbols)
257 EXPECT_THROW(encoder.
set_freq(symbol, freq), std::domain_error);
258 EXPECT_THROW(encoder.
get_root(), std::domain_error);
261 std::vector<std::string> seen;
262 EXPECT_EQ(cost(encoder.
get_root(), 0, seen), optimal);
263 EXPECT_TRUE(the_symbols_once(seen));
273 EXPECT_GT(failed, 2 * std::size(Symbols));
280TEST_F(HuffmanWithoutMemory, AFailedGenerationTakesMoreSymbols)
282 const One_More one_more = {
"one more symbol, longer than a short string", 1};
288 encoder.
set_freq(one_more.first, one_more.second);
290 std::vector<std::string> seen;
291 optimal = cost(encoder.
get_root(), 0, seen, one_more);
292 EXPECT_TRUE(the_symbols_once(seen, one_more));
297 for (
size_t k = 1; ; ++
k)
299 SCOPED_TRACE(testing::Message() <<
"allocation " <<
k);
309 catch (
const std::bad_alloc &)
320 encoder.
set_freq(one_more.first, one_more.second);
322 std::vector<std::string> seen;
323 EXPECT_EQ(cost(encoder.
get_root(), 0, seen, one_more), optimal);
324 EXPECT_TRUE(the_symbols_once(seen, one_more));
327 EXPECT_GT(failed, 2 * std::size(Symbols));
333TEST_F(HuffmanWithoutMemory, OneSymbolGeneratesItsLeaf)
335 for (
const bool with_freqs : {
false,
true})
338 for (
size_t k = 1; ; ++
k)
340 SCOPED_TRACE(testing::Message() <<
"allocation " <<
k <<
", with_freqs " << with_freqs);
350 catch (
const std::bad_alloc &)
361 EXPECT_EQ(leaves(
root), 1u);
362 EXPECT_EQ(
root->get_key(), Symbol);
367 EXPECT_EQ(
LLINK(freq_root),
nullptr);
368 EXPECT_EQ(
RLINK(freq_root),
nullptr);
369 EXPECT_EQ(freq_root->
get_key().first, Symbol);
370 EXPECT_EQ(freq_root->
get_key().second, 3u);
378 EXPECT_GT(failed, 0u);
386TEST_F(HuffmanWithoutMemory, AFailedGenerationKeepsNoFrequencyOfItsOwn)
388 const size_t length = std::string(
abc_input).size();
390 size_t counted_twice = 0;
391 for (
size_t k = 1; ; ++
k)
393 SCOPED_TRACE(testing::Message() <<
"allocation " <<
k);
402 catch (
const std::bad_alloc &)
418 EXPECT_EQ(total, length);
422 EXPECT_EQ(total, 2 * length);
426 EXPECT_GE(total, length);
427 EXPECT_LE(total, 2 * length);
434 EXPECT_GT(failed, 10u);
435 EXPECT_GT(counted_twice, 10u);
Huffman coding for data compression.
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Node for binary search tree.
Freq_Node *& get_freq_root()
Returns the root of the frequency tree.
BinNode< std::string > *& get_root()
Returns the root of the Huffman decoding tree.
void set_freq(const std::string &str, const size_t &freq)
Define the frequency of a symbol.
void read_input(char *input, const bool &with_freqs=false)
Read a NUL-terminated character std::string, count frequencies and build the prefix tree.
void set_end_of_stream(const std::string &str)
Defines the end-of-stream symbol.
const std::string & get_end_of_stream() const noexcept
Return the configured end-of-stream symbol.
BinNode< std::string > * generate_huffman_tree(const bool &with_freqs=false)
Generate the Huffman prefix tree.
Minimal std::expected-style result type for C++20.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
TEST_F(HuffmanWithoutMemory, SetFreqLeavesTheEncoderAsItWas)
Main namespace for Aleph-w library functions.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
and
Check uniqueness with explicit hash + equality functors.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
static void set_freq(Huffman_Node *huffman_node, const size_t &freq) noexcept
double add(double a, double b)