38#include <gtest/gtest.h>
54vector<char> to_c_buffer(
const string &s)
57 buf.reserve(s.size() + 1);
58 buf.insert(buf.end(), s.begin(), s.end());
71TEST(HuffmanCharAPI, roundtrip_with_whitespace_and_freq_tree)
73 const string input =
"hello world\nthis\tis a test\n";
74 auto buf = to_c_buffer(input);
83 EXPECT_GT(
bits.size(), 0u);
91TEST(HuffmanStreamAPI, no_eof_off_by_one_on_read_and_encode)
93 const string input =
"A";
94 istringstream in1(input);
99 istringstream in2(input);
111TEST(HuffmanPersistence, an_encoder_that_loaded_a_tree_counts_symbols_again)
116 source.set_freq(
"a", 5);
117 source.set_freq(
"b", 2);
118 source.generate_huffman_tree();
119 source.save_tree(
out);
127 istringstream in(
out.str());
138 const string left =
LLINK(
root)->get_key();
139 const string right =
RLINK(
root)->get_key();
140 EXPECT_TRUE((left ==
"x" and right ==
"w") or (left ==
"w" and right ==
"x"));
146TEST(HuffmanPersistence, save_load_tree_and_encode_roundtrip)
148 const string input =
"a b\nc\tdd\n";
149 auto buf = to_c_buffer(input);
155 encoder.
encode(buf.data(), bits1);
161 istringstream in(
out.str());
168 loaded.
encode(buf.data(), bits2);
169 EXPECT_TRUE(bits1 == bits2);
176TEST(HuffmanDecoder, stops_at_end_symbol_and_ignores_trailing_bits)
178 const string input =
"aba";
179 auto buf = to_c_buffer(input);
197TEST(HuffmanRandomized, matches_reference_roundtrip)
199 mt19937
rng(123456u);
200 uniform_int_distribution<int> len_dist(0, 200);
201 const string alphabet =
"abcde fghij\n\t";
202 uniform_int_distribution<size_t> pick(0, alphabet.size() - 1);
204 for (
int iter = 0; iter < 50; ++iter)
206 const int len = len_dist(
rng);
208 input.reserve(
static_cast<size_t>(len));
209 for (
int i = 0; i < len; ++i)
210 input.push_back(alphabet[pick(
rng)]);
212 auto buf = to_c_buffer(input);
226TEST(HuffmanErrors, encode_without_tree_throws)
231 EXPECT_THROW(encoder.
encode(input,
bits), std::domain_error);
234TEST(HuffmanErrors, set_end_of_stream_twice_throws)
241TEST(HuffmanErrors, generate_without_symbols_throws)
Huffman coding for data compression.
size_t size_t int32_t * out
Node for binary search tree.
Contiguous array of bits.
void push(const unsigned int value)
Inserts the value at the end of the array.
Freq_Node *& get_freq_root()
Returns the root of the frequency tree.
void save_tree(std::ostream &output) const
Save a Huffman tree into a stream.
size_t encode(char *input, BitArray &bit_stream)
Encode the input text.
void load_tree(std::istream &input)
Load and build a binary tree from a stream.
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.
__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
Main namespace for Aleph-w library functions.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
bool areEquivalents(Node *t1, Node *t2, Equal &op) noexcept
Return true if trees are equivalents.
and
Check uniqueness with explicit hash + equality functors.
static bool is_leaf(BinNode< std::string > *p) noexcept
Utility functions for binary tree operations.