Add gzencode test.

GitOrigin-RevId: 2c4cced1680efe16b8145ba2c5614bad26c9e77e
This commit is contained in:
levlam 2020-03-15 02:32:53 +03:00
parent a0c5c5900a
commit 34973a5823

View File

@ -11,28 +11,34 @@
#include "td/utils/logging.h"
#include "td/utils/Status.h"
#include "td/utils/tests.h"
#include "td/utils/Time.h"
static void encode_decode(td::string s) {
auto r = td::gzencode(s, 2);
ASSERT_TRUE(!r.empty());
if (r.empty()) {
return;
}
auto new_s = td::gzdecode(r.as_slice());
ASSERT_TRUE(!new_s.empty());
if (new_s.empty()) {
return;
}
ASSERT_EQ(s, new_s.as_slice().str());
ASSERT_EQ(s, td::gzdecode(r.as_slice()));
}
TEST(Gzip, gzencode_gzdecode) {
auto str = td::rand_string(0, 127, 1000);
encode_decode(str);
str = td::rand_string('a', 'z', 1000000);
encode_decode(str);
str = td::string(1000000, 'a');
encode_decode(str);
encode_decode(td::rand_string(0, 255, 1000));
encode_decode(td::rand_string('a', 'z', 1000000));
encode_decode(td::string(1000000, 'a'));
}
static void test_gzencode(td::string s) {
auto begin_time = td::Time::now();
auto r = td::gzencode(s, td::max(2, static_cast<int>(100 / s.size())));
ASSERT_TRUE(!r.empty());
LOG(INFO) << "Encoded string of size " << s.size() << " in " << (td::Time::now() - begin_time)
<< " with compression ratio " << r.size() * 1.0 / s.size();
}
TEST(Gzip, gzencode) {
for (size_t len = 1; len <= 10000000; len *= 10) {
test_gzencode(td::rand_string('a', 'a', len));
test_gzencode(td::rand_string('a', 'z', len));
test_gzencode(td::rand_string(0, 255, len));
}
}
TEST(Gzip, flow) {