From 34973a5823bec2ee545d1e481d036c230e951a50 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 15 Mar 2020 02:32:53 +0300 Subject: [PATCH] Add gzencode test. GitOrigin-RevId: 2c4cced1680efe16b8145ba2c5614bad26c9e77e --- tdutils/test/gzip.cpp | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/tdutils/test/gzip.cpp b/tdutils/test/gzip.cpp index f1c4be18..4c7bd683 100644 --- a/tdutils/test/gzip.cpp +++ b/tdutils/test/gzip.cpp @@ -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(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) {