11#include < algorithm>
22#include < cassert>
33#include < cstdint>
4+ #include < sstream>
45#include < string>
56#include < vector>
67
78#include " compresskit/algorithms.hpp"
9+ #include " compresskit/frequency_table.hpp"
810
911namespace {
1012
@@ -129,6 +131,65 @@ void test_decode_buffer_preserves_finish_retry_prefix() {
129131 assert (std::string (decoded.value .begin (), decoded.value .end ()) == " uvwxyz" );
130132}
131133
134+ void test_write_frequency_table_uses_little_endian_layout () {
135+ std::ostringstream out (std::ios::binary);
136+ const std::vector<uint32_t > freq = {0x78563412u , 0x01020304u };
137+
138+ const bool ok = compresskit::write_frequency_table (out, freq);
139+ assert (ok);
140+
141+ const std::string bytes = out.str ();
142+ const std::string expected (
143+ " \x02\x00\x00\x00 "
144+ " \x12\x34\x56\x78 "
145+ " \x04\x03\x02\x01 " ,
146+ 12 );
147+ assert (bytes == expected);
148+ }
149+
150+ void test_read_frequency_table_decodes_little_endian_values () {
151+ const std::string bytes (
152+ " \x02\x00\x00\x00 "
153+ " \x12\x34\x56\x78 "
154+ " \x04\x03\x02\x01 " ,
155+ 12 );
156+ std::istringstream in (bytes, std::ios::binary);
157+ std::vector<uint32_t > freq;
158+ uint32_t actual_count = 0 ;
159+
160+ const auto status = compresskit::read_frequency_table (in, freq, 2 , &actual_count);
161+
162+ assert (status == compresskit::FrequencyTableReadStatus::OK );
163+ assert (actual_count == 2 );
164+ assert ((freq == std::vector<uint32_t >{0x78563412u , 0x01020304u }));
165+ }
166+
167+ void test_read_frequency_table_reports_bad_count () {
168+ const std::string bytes (" \x02\x00\x00\x00 " , 4 );
169+ std::istringstream in (bytes, std::ios::binary);
170+ std::vector<uint32_t > freq;
171+ uint32_t actual_count = 0 ;
172+
173+ const auto status = compresskit::read_frequency_table (in, freq, 3 , &actual_count);
174+
175+ assert (status == compresskit::FrequencyTableReadStatus::BAD_COUNT );
176+ assert (actual_count == 2 );
177+ assert (freq.empty ());
178+ }
179+
180+ void test_accumulate_frequencies_reports_overflow () {
181+ std::vector<uint32_t > freq (257 , 0 );
182+ freq[0 ] = UINT32_MAX ;
183+ std::istringstream in (std::string (1 , ' \0 ' ), std::ios::binary);
184+ uint32_t overflow_symbol = UINT32_MAX ;
185+
186+ const auto status = compresskit::accumulate_frequencies (in, freq, &overflow_symbol);
187+
188+ assert (status == compresskit::FrequencyCountStatus::OVERFLOW );
189+ assert (overflow_symbol == 0 );
190+ assert (freq[0 ] == UINT32_MAX );
191+ }
192+
132193} // namespace
133194
134195int main () {
@@ -145,6 +206,10 @@ int main() {
145206
146207 test_encode_buffer_preserves_finish_retry_prefix ();
147208 test_decode_buffer_preserves_finish_retry_prefix ();
209+ test_write_frequency_table_uses_little_endian_layout ();
210+ test_read_frequency_table_decodes_little_endian_values ();
211+ test_read_frequency_table_reports_bad_count ();
212+ test_accumulate_frequencies_reports_overflow ();
148213
149214 return 0 ;
150215}
0 commit comments