summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/tdutils/test/misc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Telegram/tdlib/td/tdutils/test/misc.cpp')
-rw-r--r--protocols/Telegram/tdlib/td/tdutils/test/misc.cpp110
1 files changed, 105 insertions, 5 deletions
diff --git a/protocols/Telegram/tdlib/td/tdutils/test/misc.cpp b/protocols/Telegram/tdlib/td/tdutils/test/misc.cpp
index e1612a9924..cabd4e6efb 100644
--- a/protocols/Telegram/tdlib/td/tdutils/test/misc.cpp
+++ b/protocols/Telegram/tdlib/td/tdutils/test/misc.cpp
@@ -1,5 +1,5 @@
//
-// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -7,6 +7,7 @@
#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/base64.h"
+#include "td/utils/benchmark.h"
#include "td/utils/BigNum.h"
#include "td/utils/bits.h"
#include "td/utils/CancellationToken.h"
@@ -82,14 +83,11 @@ TEST(Misc, update_atime_saves_mtime) {
r_file.move_as_ok().close();
auto info = td::stat(name).ok();
- td::int32 tests_ok = 0;
td::int32 tests_wa = 0;
for (int i = 0; i < 10000; i++) {
td::update_atime(name).ensure();
auto new_info = td::stat(name).ok();
- if (info.mtime_nsec_ == new_info.mtime_nsec_) {
- tests_ok++;
- } else {
+ if (info.mtime_nsec_ != new_info.mtime_nsec_) {
tests_wa++;
info.mtime_nsec_ = new_info.mtime_nsec_;
}
@@ -265,6 +263,65 @@ TEST(Misc, base64) {
ASSERT_TRUE(td::base64url_encode("ab><cd") == "YWI-PGNk");
}
+static void test_zero_encode(td::Slice str, td::Slice expected_zero = td::Slice(),
+ td::Slice expected_zero_one = td::Slice()) {
+ auto encoded = td::zero_encode(str);
+ if (!expected_zero.empty()) {
+ ASSERT_EQ(encoded, expected_zero);
+ }
+ ASSERT_EQ(td::zero_decode(encoded), str);
+
+ encoded = td::zero_one_encode(str);
+ if (!expected_zero_one.empty()) {
+ ASSERT_EQ(encoded, expected_zero_one);
+ }
+ ASSERT_EQ(td::zero_one_decode(encoded), str);
+}
+
+TEST(Misc, zero_encode) {
+ td::string str;
+ for (unsigned char i = 1; i < 255; i++) {
+ str += static_cast<char>(i);
+ }
+ test_zero_encode(str, str, str);
+
+ test_zero_encode("");
+ test_zero_encode(td::Slice("\0"), td::Slice("\0\1"), td::Slice("\0\1"));
+ test_zero_encode(td::Slice("\0\xff\0\xff\0\xff\0\xff\0\xff\0\xff\0\xff"),
+ td::Slice("\0\1\xff\0\1\xff\0\1\xff\0\1\xff\0\1\xff\0\1\xff\0\1\xff"),
+ td::Slice("\0\1\xff\1\0\1\xff\1\0\1\xff\1\0\1\xff\1\0\1\xff\1\0\1\xff\1\0\1\xff\1"));
+ test_zero_encode(td::Slice("\0\0\xff\xff\0\0\xff\xff\0\0\xff\xff\0\0\xff\xff\0\0\xff\xff\0\0\xff\xff"),
+ td::Slice("\0\2\xff\xff\0\2\xff\xff\0\2\xff\xff\0\2\xff\xff\0\2\xff\xff\0\2\xff\xff"),
+ td::Slice("\0\2\xff\2\0\2\xff\2\0\2\xff\2\0\2\xff\2\0\2\xff\2\0\2\xff\2"));
+ test_zero_encode(td::Slice("\0\0\0\0\0\xff\xff\xff\xff\xff"), td::Slice("\0\5\xff\xff\xff\xff\xff"),
+ td::Slice("\0\5\xff\5"));
+ test_zero_encode(td::Slice(
+ "\0\0\0\0\0\xff\xff\xff\xff\xff\0\0\0\0\0\xff\xff\xff\xff\xff\0\0\0\0\0\xff\xff\xff\xff\xff\0\0\0\0\0\xff\xff\xff"
+ "\xff\xff\0\0\0\0\0\xff\xff\xff\xff\xff\0\0\0\0\0\xff\xff\xff\xff\xff\0\0\0\0\0\xff\xff\xff\xff\xff"));
+ test_zero_encode(td::string(1000, '\0'));
+ test_zero_encode(str + td::string(1000, '\0') + str + td::string(1000, '\xff') + str);
+}
+
+class ZeroEncodeBenchmark final : public td::Benchmark {
+ public:
+ td::string get_description() const final {
+ return "ZeroEncodeBenchmark";
+ }
+
+ void run(int n) final {
+ for (int i = 0; i < n; i++) {
+ zero_encode(
+ td::Slice("\x02\x00\x00\x02\x01\x00\x00\x00\x19\x01\x00\x00\x7c\xc8\x64\xc1\x04\xec\x82\xb8\x20\x9e\xa0\x8d"
+ "\x1e\xbe\xb2\x79\xc4\x5a\x4c\x1e\x49\x1e\x00\x00\xa9\xa7\x31\x1b\x80\x9f\x11\x46\xfc\x97\xde\x6a"
+ "\x18\x6e\xc0\x73\x01\x00\x00\x00\x02\x00\x00\x00\x6d\x00\x00\x00\x30\x04"));
+ }
+ }
+};
+
+TEST(Misc, bench_zero_encode) {
+ td::bench(ZeroEncodeBenchmark());
+}
+
template <class T>
static void test_remove_if(td::vector<int> v, const T &func, const td::vector<int> &expected) {
td::remove_if(v, func);
@@ -349,6 +406,49 @@ TEST(Misc, remove) {
test_remove(v, 1, v);
}
+static void test_add_to_top(td::vector<int> v, size_t max_size, int new_value, const td::vector<int> &expected) {
+ auto u = v;
+ td::add_to_top(v, max_size, new_value);
+ ASSERT_EQ(expected, v);
+
+ td::add_to_top_if(u, max_size, new_value, [new_value](int value) { return value == new_value; });
+ ASSERT_EQ(expected, u);
+}
+
+static void test_add_to_top_if(td::vector<int> v, int max_size, int new_value, const td::vector<int> &expected) {
+ td::add_to_top_if(v, max_size, new_value, [new_value](int value) { return value % 10 == new_value % 10; });
+ ASSERT_EQ(expected, v);
+}
+
+TEST(Misc, add_to_top) {
+ test_add_to_top({}, 0, 1, {1});
+ test_add_to_top({}, 1, 1, {1});
+ test_add_to_top({}, 6, 1, {1});
+
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 3, 2, {2, 1, 3, 4, 5, 6});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 6, 1, {1, 2, 3, 4, 5, 6});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 7, 1, {1, 2, 3, 4, 5, 6});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 6, 2, {2, 1, 3, 4, 5, 6});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 7, 2, {2, 1, 3, 4, 5, 6});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 6, 4, {4, 1, 2, 3, 5, 6});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 7, 4, {4, 1, 2, 3, 5, 6});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 6, 6, {6, 1, 2, 3, 4, 5});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 7, 6, {6, 1, 2, 3, 4, 5});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 6, 7, {7, 1, 2, 3, 4, 5});
+ test_add_to_top({1, 2, 3, 4, 5, 6}, 7, 7, {7, 1, 2, 3, 4, 5, 6});
+
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 6, 11, {1, 2, 3, 4, 5, 6});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 7, 21, {1, 2, 3, 4, 5, 6});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 6, 32, {2, 1, 3, 4, 5, 6});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 7, 42, {2, 1, 3, 4, 5, 6});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 6, 54, {4, 1, 2, 3, 5, 6});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 7, 64, {4, 1, 2, 3, 5, 6});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 6, 76, {6, 1, 2, 3, 4, 5});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 7, 86, {6, 1, 2, 3, 4, 5});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 6, 97, {97, 1, 2, 3, 4, 5});
+ test_add_to_top_if({1, 2, 3, 4, 5, 6}, 7, 87, {87, 1, 2, 3, 4, 5, 6});
+}
+
static void test_unique(td::vector<int> v, const td::vector<int> &expected) {
auto v_str = td::transform(v, &td::to_string<int>);
auto expected_str = td::transform(expected, &td::to_string<int>);